blob: dec7d9c3b8d25e69b5b0a6e5879ecf9d6ce7452f [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
svishnev8f133302018-08-06 23:08:39 +03002 * Copyright © 2016-2018 European Support Limited
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13 * or implied. See the License for the specific language governing
14 * permissions and limitations under the License.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
svishnev8f133302018-08-06 23:08:39 +030016
Michael Landoefa037d2017-02-19 12:57:33 +020017import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
18import Configuration from 'sdc-app/config/Configuration.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020019import { actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js';
20import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
21import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js';
talig8e9c0652017-12-20 14:30:43 +020022import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030023import i18n from 'nfvo-utils/i18n/i18n.js';
24import {
25 actionTypes as modalActionTypes,
26 modalSizes
27} from 'nfvo-components/modal/GlobalModalConstants.js';
28import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020029
AviZi280f8012017-06-09 02:39:56 +030030function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031 const restPrefix = Configuration.get('restPrefix');
32 const { id: versionId } = version;
33 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`;
Michael Landoefa037d2017-02-19 12:57:33 +020034}
35
az2497644017c2017-08-10 17:49:40 +030036function fetchEntitlementPoolsList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020037 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020038}
39
az2497644017c2017-08-10 17:49:40 +030040function postEntitlementPool(licenseModelId, entitlementPool, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020041 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
42 name: entitlementPool.name,
43 description: entitlementPool.description,
44 thresholdValue: entitlementPool.thresholdValue,
45 thresholdUnits: getValue(entitlementPool.thresholdUnits),
46 increments: entitlementPool.increments,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020047 time: entitlementPool.time,
48 startDate: entitlementPool.startDate,
svishnev8f133302018-08-06 23:08:39 +030049 expiryDate: entitlementPool.expiryDate,
50 manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020051 });
Michael Landoefa037d2017-02-19 12:57:33 +020052}
53
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020054function putEntitlementPool(
55 licenseModelId,
56 previousEntitlementPool,
57 entitlementPool,
58 version
59) {
60 return RestAPIUtil.put(
61 `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`,
62 {
63 name: entitlementPool.name,
64 description: entitlementPool.description,
65 thresholdValue: entitlementPool.thresholdValue,
66 thresholdUnits: getValue(entitlementPool.thresholdUnits),
67 increments: entitlementPool.increments,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020068 time: entitlementPool.time,
69 startDate: entitlementPool.startDate,
svishnev8f133302018-08-06 23:08:39 +030070 expiryDate: entitlementPool.expiryDate,
71 manufacturerReferenceNumber:
72 entitlementPool.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020073 }
74 );
Michael Landoefa037d2017-02-19 12:57:33 +020075}
76
AviZi280f8012017-06-09 02:39:56 +030077function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020078 return RestAPIUtil.destroy(
79 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
80 );
Michael Landoefa037d2017-02-19 12:57:33 +020081}
82
az2497644017c2017-08-10 17:49:40 +030083function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020084 return RestAPIUtil.fetch(
85 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
86 );
Avi Ziv61070c92017-07-26 17:37:57 +030087}
88
az2497644017c2017-08-10 17:49:40 +030089function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020090 return RestAPIUtil.destroy(
91 `${baseUrl(
92 licenseModelId,
93 version
94 )}/${entitlementPoolId}/limits/${limitId}`
95 );
Avi Ziv61070c92017-07-26 17:37:57 +030096}
97
az2497644017c2017-08-10 17:49:40 +030098function postLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020099 return RestAPIUtil.post(
100 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`,
101 {
102 name: limit.name,
103 type: limit.type,
104 description: limit.description,
105 metric: getStrValue(limit.metric),
106 value: limit.value,
107 unit: getStrValue(limit.unit),
108 aggregationFunction: getValue(limit.aggregationFunction),
109 time: getValue(limit.time)
110 }
111 );
Avi Ziv61070c92017-07-26 17:37:57 +0300112}
113
114function putLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200115 return RestAPIUtil.put(
116 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${
117 limit.id
118 }`,
119 {
120 name: limit.name,
121 type: limit.type,
122 description: limit.description,
123 metric: getStrValue(limit.metric),
124 value: limit.value,
125 unit: getStrValue(limit.unit),
126 aggregationFunction: getValue(limit.aggregationFunction),
127 time: getValue(limit.time)
128 }
129 );
Avi Ziv61070c92017-07-26 17:37:57 +0300130}
Michael Landoefa037d2017-02-19 12:57:33 +0200131
svishnev8f133302018-08-06 23:08:39 +0300132const EntitlementPoolsActionHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200133 fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) {
134 return fetchEntitlementPoolsList(licenseModelId, version).then(
135 response =>
136 dispatch({
137 type:
138 entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
139 response
140 })
141 );
142 },
Avi Ziv61070c92017-07-26 17:37:57 +0300143
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200144 openEntitlementPoolsEditor(
145 dispatch,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300146 { entitlementPool, licenseModelId, version, isReadOnlyMode } = {}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200147 ) {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300148 if (licenseModelId && version && entitlementPool) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200149 this.fetchLimits(dispatch, {
150 licenseModelId,
151 version,
152 entitlementPool
153 });
154 }
155 dispatch({
156 type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
157 entitlementPool
158 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300159 dispatch({
160 type: modalActionTypes.GLOBAL_MODAL_SHOW,
161 data: {
162 modalComponentName: modalContentMapper.EP_EDITOR,
163 modalComponentProps: {
164 version,
165 licenseModelId,
166 isReadOnlyMode,
167 size: modalSizes.LARGE
168 },
169 title:
170 licenseModelId && version && entitlementPool
171 ? i18n('Edit Entitlement Pool')
172 : i18n('Create New Entitlement Pool')
173 }
174 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200175 },
Michael Landoefa037d2017-02-19 12:57:33 +0200176
svishnev8f133302018-08-06 23:08:39 +0300177 async deleteEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200178 dispatch,
179 { licenseModelId, entitlementPoolId, version }
180 ) {
svishnev8f133302018-08-06 23:08:39 +0300181 await deleteEntitlementPool(licenseModelId, entitlementPoolId, version);
182
183 await ItemsHelper.checkItemStatus(dispatch, {
184 itemId: licenseModelId,
185 versionId: version.id
186 });
187
188 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200189 licenseModelId,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200190 version
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200191 });
192 },
Michael Landoefa037d2017-02-19 12:57:33 +0200193
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200194 entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
195 dispatch({
196 type:
197 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
198 deltaData
199 });
200 },
Michael Landoefa037d2017-02-19 12:57:33 +0200201
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200202 closeEntitlementPoolsEditor(dispatch) {
203 dispatch({
204 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
205 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300206 dispatch({
207 type: modalActionTypes.GLOBAL_MODAL_CLOSE
208 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200209 },
Michael Landoefa037d2017-02-19 12:57:33 +0200210
svishnev8f133302018-08-06 23:08:39 +0300211 async saveEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200212 dispatch,
213 { licenseModelId, previousEntitlementPool, entitlementPool, version }
214 ) {
215 if (previousEntitlementPool) {
svishnev8f133302018-08-06 23:08:39 +0300216 await putEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200217 licenseModelId,
218 previousEntitlementPool,
219 entitlementPool,
220 version
svishnev8f133302018-08-06 23:08:39 +0300221 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200222 } else {
svishnev8f133302018-08-06 23:08:39 +0300223 await postEntitlementPool(licenseModelId, entitlementPool, version);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200224 }
svishnev8f133302018-08-06 23:08:39 +0300225 await ItemsHelper.checkItemStatus(dispatch, {
226 itemId: licenseModelId,
227 versionId: version.id
228 });
229 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
230 licenseModelId,
231 version
232 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200233 },
Michael Landoefa037d2017-02-19 12:57:33 +0200234
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200235 hideDeleteConfirm(dispatch) {
236 dispatch({
237 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
238 entitlementPoolToDelete: false
239 });
240 },
241 openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) {
242 dispatch({
243 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
244 entitlementPoolToDelete: entitlementPool
245 });
246 },
Michael Landoefa037d2017-02-19 12:57:33 +0200247
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200248 fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) {
249 return fetchLimitsList(
250 licenseModelId,
251 entitlementPool.id,
252 version
253 ).then(response => {
254 dispatch({
255 type:
256 entitlementPoolsActionTypes.entitlementPoolsEditor
257 .LIMITS_LIST_LOADED,
258 response
259 });
260 });
261 },
Michael Landoefa037d2017-02-19 12:57:33 +0200262
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200263 submitLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
264 const propmise = limit.id
265 ? putLimit(licenseModelId, entitlementPool.id, version, limit)
266 : postLimit(licenseModelId, entitlementPool.id, version, limit);
267 return propmise.then(() => {
268 dispatch({
269 type: limitEditorActions.CLOSE
270 });
271 this.fetchLimits(dispatch, {
272 licenseModelId,
273 version,
274 entitlementPool
275 });
276 return ItemsHelper.checkItemStatus(dispatch, {
277 itemId: licenseModelId,
278 versionId: version.id
279 });
280 });
281 },
Avi Ziv61070c92017-07-26 17:37:57 +0300282
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200283 deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
284 return deleteLimit(
285 licenseModelId,
286 entitlementPool.id,
287 version,
288 limit.id
289 ).then(() => {
290 this.fetchLimits(dispatch, {
291 licenseModelId,
292 version,
293 entitlementPool
294 });
295 return ItemsHelper.checkItemStatus(dispatch, {
296 itemId: licenseModelId,
297 versionId: version.id
298 });
299 });
300 }
Michael Landoefa037d2017-02-19 12:57:33 +0200301};
svishnev8f133302018-08-06 23:08:39 +0300302
303export default EntitlementPoolsActionHelper;