2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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';
25 actionTypes as modalActionTypes,
27 } from 'nfvo-components/modal/GlobalModalConstants.js';
28 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
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`;
36 function fetchEntitlementPoolsList(licenseModelId, version) {
37 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
40 function postEntitlementPool(licenseModelId, entitlementPool, version) {
41 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,
47 time: entitlementPool.time,
48 startDate: entitlementPool.startDate,
49 expiryDate: entitlementPool.expiryDate,
50 manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber
54 function putEntitlementPool(
56 previousEntitlementPool,
60 return RestAPIUtil.put(
61 `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`,
63 name: entitlementPool.name,
64 description: entitlementPool.description,
65 thresholdValue: entitlementPool.thresholdValue,
66 thresholdUnits: getValue(entitlementPool.thresholdUnits),
67 increments: entitlementPool.increments,
68 time: entitlementPool.time,
69 startDate: entitlementPool.startDate,
70 expiryDate: entitlementPool.expiryDate,
71 manufacturerReferenceNumber:
72 entitlementPool.manufacturerReferenceNumber
77 function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
78 return RestAPIUtil.destroy(
79 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
83 function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
84 return RestAPIUtil.fetch(
85 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
89 function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
90 return RestAPIUtil.destroy(
94 )}/${entitlementPoolId}/limits/${limitId}`
98 function postLimit(licenseModelId, entitlementPoolId, version, limit) {
99 return RestAPIUtil.post(
100 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`,
104 description: limit.description,
105 metric: getStrValue(limit.metric),
107 unit: getStrValue(limit.unit),
108 aggregationFunction: getValue(limit.aggregationFunction),
109 time: getValue(limit.time)
114 function putLimit(licenseModelId, entitlementPoolId, version, limit) {
115 return RestAPIUtil.put(
116 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${
122 description: limit.description,
123 metric: getStrValue(limit.metric),
125 unit: getStrValue(limit.unit),
126 aggregationFunction: getValue(limit.aggregationFunction),
127 time: getValue(limit.time)
132 const EntitlementPoolsActionHelper = {
133 fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) {
134 return fetchEntitlementPoolsList(licenseModelId, version).then(
138 entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
144 openEntitlementPoolsEditor(
146 { entitlementPool, licenseModelId, version, isReadOnlyMode } = {}
148 if (licenseModelId && version && entitlementPool) {
149 this.fetchLimits(dispatch, {
156 type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
160 type: modalActionTypes.GLOBAL_MODAL_SHOW,
162 modalComponentName: modalContentMapper.EP_EDITOR,
163 modalComponentProps: {
167 size: modalSizes.LARGE
170 licenseModelId && version && entitlementPool
171 ? i18n('Edit Entitlement Pool')
172 : i18n('Create New Entitlement Pool')
177 async deleteEntitlementPool(
179 { licenseModelId, entitlementPoolId, version }
181 await deleteEntitlementPool(licenseModelId, entitlementPoolId, version);
183 await ItemsHelper.checkItemStatus(dispatch, {
184 itemId: licenseModelId,
185 versionId: version.id
188 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
194 entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
197 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
202 closeEntitlementPoolsEditor(dispatch) {
204 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
207 type: modalActionTypes.GLOBAL_MODAL_CLOSE
211 async saveEntitlementPool(
213 { licenseModelId, previousEntitlementPool, entitlementPool, version }
215 if (previousEntitlementPool) {
216 await putEntitlementPool(
218 previousEntitlementPool,
223 await postEntitlementPool(licenseModelId, entitlementPool, version);
225 await ItemsHelper.checkItemStatus(dispatch, {
226 itemId: licenseModelId,
227 versionId: version.id
229 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
235 hideDeleteConfirm(dispatch) {
237 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
238 entitlementPoolToDelete: false
241 openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) {
243 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
244 entitlementPoolToDelete: entitlementPool
248 fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) {
249 return fetchLimitsList(
256 entitlementPoolsActionTypes.entitlementPoolsEditor
263 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(() => {
269 type: limitEditorActions.CLOSE
271 this.fetchLimits(dispatch, {
276 return ItemsHelper.checkItemStatus(dispatch, {
277 itemId: licenseModelId,
278 versionId: version.id
283 deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
290 this.fetchLimits(dispatch, {
295 return ItemsHelper.checkItemStatus(dispatch, {
296 itemId: licenseModelId,
297 versionId: version.id
303 export default EntitlementPoolsActionHelper;