Entitlement Pool - Support Type Field
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / entitlementPools / EntitlementPoolsActionHelper.js
1 /*!
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * 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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * 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.
15  */
16
17 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
18 import Configuration from 'sdc-app/config/Configuration.js';
19 import { actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js';
20 import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
21 import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js';
22 import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
23 import i18n from 'nfvo-utils/i18n/i18n.js';
24 import {
25     actionTypes as modalActionTypes,
26     modalSizes
27 } from 'nfvo-components/modal/GlobalModalConstants.js';
28 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
29
30 function baseUrl(licenseModelId, version) {
31     const restPrefix = Configuration.get('restPrefix');
32     const { id: versionId } = version;
33     return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`;
34 }
35
36 function fetchEntitlementPoolsList(licenseModelId, version) {
37     return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
38 }
39
40 function postEntitlementPool(licenseModelId, entitlementPool, version) {
41     return RestAPIUtil.post(baseUrl(licenseModelId, version), {
42         name: entitlementPool.name,
43         description: entitlementPool.description,
44         type: entitlementPool.type,
45         thresholdValue: entitlementPool.thresholdValue,
46         thresholdUnits: getValue(entitlementPool.thresholdUnits),
47         increments: entitlementPool.increments,
48         time: entitlementPool.time,
49         startDate: entitlementPool.startDate,
50         expiryDate: entitlementPool.expiryDate,
51         manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber
52     });
53 }
54
55 function 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,
66             type: entitlementPool.type,
67             thresholdValue: entitlementPool.thresholdValue,
68             thresholdUnits: getValue(entitlementPool.thresholdUnits),
69             increments: entitlementPool.increments,
70             time: entitlementPool.time,
71             startDate: entitlementPool.startDate,
72             expiryDate: entitlementPool.expiryDate,
73             manufacturerReferenceNumber:
74                 entitlementPool.manufacturerReferenceNumber
75         }
76     );
77 }
78
79 function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
80     return RestAPIUtil.destroy(
81         `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
82     );
83 }
84
85 function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
86     return RestAPIUtil.fetch(
87         `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
88     );
89 }
90
91 function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
92     return RestAPIUtil.destroy(
93         `${baseUrl(
94             licenseModelId,
95             version
96         )}/${entitlementPoolId}/limits/${limitId}`
97     );
98 }
99
100 function postLimit(licenseModelId, entitlementPoolId, version, limit) {
101     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     );
114 }
115
116 function putLimit(licenseModelId, entitlementPoolId, version, limit) {
117     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     );
132 }
133
134 const EntitlementPoolsActionHelper = {
135     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     },
145
146     openEntitlementPoolsEditor(
147         dispatch,
148         { entitlementPool, licenseModelId, version, isReadOnlyMode } = {}
149     ) {
150         if (licenseModelId && version && entitlementPool) {
151             this.fetchLimits(dispatch, {
152                 licenseModelId,
153                 version,
154                 entitlementPool
155             });
156         }
157         dispatch({
158             type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
159             entitlementPool
160         });
161         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         });
177     },
178
179     async deleteEntitlementPool(
180         dispatch,
181         { licenseModelId, entitlementPoolId, version }
182     ) {
183         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, {
191             licenseModelId,
192             version
193         });
194     },
195
196     entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
197         dispatch({
198             type:
199                 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
200             deltaData
201         });
202     },
203
204     closeEntitlementPoolsEditor(dispatch) {
205         dispatch({
206             type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
207         });
208         dispatch({
209             type: modalActionTypes.GLOBAL_MODAL_CLOSE
210         });
211     },
212
213     async saveEntitlementPool(
214         dispatch,
215         { licenseModelId, previousEntitlementPool, entitlementPool, version }
216     ) {
217         if (previousEntitlementPool) {
218             await putEntitlementPool(
219                 licenseModelId,
220                 previousEntitlementPool,
221                 entitlementPool,
222                 version
223             );
224         } else {
225             await postEntitlementPool(licenseModelId, entitlementPool, version);
226         }
227         await ItemsHelper.checkItemStatus(dispatch, {
228             itemId: licenseModelId,
229             versionId: version.id
230         });
231         await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
232             licenseModelId,
233             version
234         });
235     },
236
237     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     },
249
250     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     },
264
265     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     },
284
285     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     }
303 };
304
305 export default EntitlementPoolsActionHelper;