blob: f1e2f2d956c05c22d1953d4ef9cba79f63c40047 [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,
Ben David, Elad (eb7504)ec7da6a2020-03-17 18:12:17 +020044 type: entitlementPool.type,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020045 thresholdValue: entitlementPool.thresholdValue,
46 thresholdUnits: getValue(entitlementPool.thresholdUnits),
47 increments: entitlementPool.increments,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048 time: entitlementPool.time,
49 startDate: entitlementPool.startDate,
svishnev8f133302018-08-06 23:08:39 +030050 expiryDate: entitlementPool.expiryDate,
51 manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020052 });
Michael Landoefa037d2017-02-19 12:57:33 +020053}
54
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020055function putEntitlementPool(
56 licenseModelId,
57 previousEntitlementPool,
58 entitlementPool,
59 version
60) {
61 return RestAPIUtil.put(
62 `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`,
63 {
64 name: entitlementPool.name,
65 description: entitlementPool.description,
Ben David, Elad (eb7504)ec7da6a2020-03-17 18:12:17 +020066 type: entitlementPool.type,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020067 thresholdValue: entitlementPool.thresholdValue,
68 thresholdUnits: getValue(entitlementPool.thresholdUnits),
69 increments: entitlementPool.increments,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020070 time: entitlementPool.time,
71 startDate: entitlementPool.startDate,
svishnev8f133302018-08-06 23:08:39 +030072 expiryDate: entitlementPool.expiryDate,
73 manufacturerReferenceNumber:
74 entitlementPool.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020075 }
76 );
Michael Landoefa037d2017-02-19 12:57:33 +020077}
78
AviZi280f8012017-06-09 02:39:56 +030079function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020080 return RestAPIUtil.destroy(
81 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
82 );
Michael Landoefa037d2017-02-19 12:57:33 +020083}
84
az2497644017c2017-08-10 17:49:40 +030085function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020086 return RestAPIUtil.fetch(
87 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
88 );
Avi Ziv61070c92017-07-26 17:37:57 +030089}
90
az2497644017c2017-08-10 17:49:40 +030091function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020092 return RestAPIUtil.destroy(
93 `${baseUrl(
94 licenseModelId,
95 version
96 )}/${entitlementPoolId}/limits/${limitId}`
97 );
Avi Ziv61070c92017-07-26 17:37:57 +030098}
99
az2497644017c2017-08-10 17:49:40 +0300100function postLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200101 return RestAPIUtil.post(
102 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`,
103 {
104 name: limit.name,
105 type: limit.type,
106 description: limit.description,
107 metric: getStrValue(limit.metric),
108 value: limit.value,
109 unit: getStrValue(limit.unit),
110 aggregationFunction: getValue(limit.aggregationFunction),
111 time: getValue(limit.time)
112 }
113 );
Avi Ziv61070c92017-07-26 17:37:57 +0300114}
115
116function putLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200117 return RestAPIUtil.put(
118 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${
119 limit.id
120 }`,
121 {
122 name: limit.name,
123 type: limit.type,
124 description: limit.description,
125 metric: getStrValue(limit.metric),
126 value: limit.value,
127 unit: getStrValue(limit.unit),
128 aggregationFunction: getValue(limit.aggregationFunction),
129 time: getValue(limit.time)
130 }
131 );
Avi Ziv61070c92017-07-26 17:37:57 +0300132}
Michael Landoefa037d2017-02-19 12:57:33 +0200133
svishnev8f133302018-08-06 23:08:39 +0300134const EntitlementPoolsActionHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200135 fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) {
136 return fetchEntitlementPoolsList(licenseModelId, version).then(
137 response =>
138 dispatch({
139 type:
140 entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
141 response
142 })
143 );
144 },
Avi Ziv61070c92017-07-26 17:37:57 +0300145
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200146 openEntitlementPoolsEditor(
147 dispatch,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300148 { entitlementPool, licenseModelId, version, isReadOnlyMode } = {}
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200149 ) {
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300150 if (licenseModelId && version && entitlementPool) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200151 this.fetchLimits(dispatch, {
152 licenseModelId,
153 version,
154 entitlementPool
155 });
156 }
157 dispatch({
158 type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
159 entitlementPool
160 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300161 dispatch({
162 type: modalActionTypes.GLOBAL_MODAL_SHOW,
163 data: {
164 modalComponentName: modalContentMapper.EP_EDITOR,
165 modalComponentProps: {
166 version,
167 licenseModelId,
168 isReadOnlyMode,
169 size: modalSizes.LARGE
170 },
171 title:
172 licenseModelId && version && entitlementPool
173 ? i18n('Edit Entitlement Pool')
174 : i18n('Create New Entitlement Pool')
175 }
176 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200177 },
Michael Landoefa037d2017-02-19 12:57:33 +0200178
svishnev8f133302018-08-06 23:08:39 +0300179 async deleteEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200180 dispatch,
181 { licenseModelId, entitlementPoolId, version }
182 ) {
svishnev8f133302018-08-06 23:08:39 +0300183 await deleteEntitlementPool(licenseModelId, entitlementPoolId, version);
184
185 await ItemsHelper.checkItemStatus(dispatch, {
186 itemId: licenseModelId,
187 versionId: version.id
188 });
189
190 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200191 licenseModelId,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200192 version
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200193 });
194 },
Michael Landoefa037d2017-02-19 12:57:33 +0200195
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200196 entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
197 dispatch({
198 type:
199 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
200 deltaData
201 });
202 },
Michael Landoefa037d2017-02-19 12:57:33 +0200203
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200204 closeEntitlementPoolsEditor(dispatch) {
205 dispatch({
206 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
207 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300208 dispatch({
209 type: modalActionTypes.GLOBAL_MODAL_CLOSE
210 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200211 },
Michael Landoefa037d2017-02-19 12:57:33 +0200212
svishnev8f133302018-08-06 23:08:39 +0300213 async saveEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200214 dispatch,
215 { licenseModelId, previousEntitlementPool, entitlementPool, version }
216 ) {
217 if (previousEntitlementPool) {
svishnev8f133302018-08-06 23:08:39 +0300218 await putEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200219 licenseModelId,
220 previousEntitlementPool,
221 entitlementPool,
222 version
svishnev8f133302018-08-06 23:08:39 +0300223 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200224 } else {
svishnev8f133302018-08-06 23:08:39 +0300225 await postEntitlementPool(licenseModelId, entitlementPool, version);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200226 }
svishnev8f133302018-08-06 23:08:39 +0300227 await ItemsHelper.checkItemStatus(dispatch, {
228 itemId: licenseModelId,
229 versionId: version.id
230 });
231 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
232 licenseModelId,
233 version
234 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200235 },
Michael Landoefa037d2017-02-19 12:57:33 +0200236
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200237 hideDeleteConfirm(dispatch) {
238 dispatch({
239 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
240 entitlementPoolToDelete: false
241 });
242 },
243 openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) {
244 dispatch({
245 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
246 entitlementPoolToDelete: entitlementPool
247 });
248 },
Michael Landoefa037d2017-02-19 12:57:33 +0200249
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200250 fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) {
251 return fetchLimitsList(
252 licenseModelId,
253 entitlementPool.id,
254 version
255 ).then(response => {
256 dispatch({
257 type:
258 entitlementPoolsActionTypes.entitlementPoolsEditor
259 .LIMITS_LIST_LOADED,
260 response
261 });
262 });
263 },
Michael Landoefa037d2017-02-19 12:57:33 +0200264
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200265 submitLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
266 const propmise = limit.id
267 ? putLimit(licenseModelId, entitlementPool.id, version, limit)
268 : postLimit(licenseModelId, entitlementPool.id, version, limit);
269 return propmise.then(() => {
270 dispatch({
271 type: limitEditorActions.CLOSE
272 });
273 this.fetchLimits(dispatch, {
274 licenseModelId,
275 version,
276 entitlementPool
277 });
278 return ItemsHelper.checkItemStatus(dispatch, {
279 itemId: licenseModelId,
280 versionId: version.id
281 });
282 });
283 },
Avi Ziv61070c92017-07-26 17:37:57 +0300284
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200285 deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
286 return deleteLimit(
287 licenseModelId,
288 entitlementPool.id,
289 version,
290 limit.id
291 ).then(() => {
292 this.fetchLimits(dispatch, {
293 licenseModelId,
294 version,
295 entitlementPool
296 });
297 return ItemsHelper.checkItemStatus(dispatch, {
298 itemId: licenseModelId,
299 versionId: version.id
300 });
301 });
302 }
Michael Landoefa037d2017-02-19 12:57:33 +0200303};
svishnev8f133302018-08-06 23:08:39 +0300304
305export default EntitlementPoolsActionHelper;