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 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
55 function putEntitlementPool(
57 previousEntitlementPool,
61 return RestAPIUtil.put(
62 `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`,
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
79 function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
80 return RestAPIUtil.destroy(
81 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
85 function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
86 return RestAPIUtil.fetch(
87 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
91 function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
92 return RestAPIUtil.destroy(
96 )}/${entitlementPoolId}/limits/${limitId}`
100 function postLimit(licenseModelId, entitlementPoolId, version, limit) {
101 return RestAPIUtil.post(
102 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`,
106 description: limit.description,
107 metric: getStrValue(limit.metric),
109 unit: getStrValue(limit.unit),
110 aggregationFunction: getValue(limit.aggregationFunction),
111 time: getValue(limit.time)
116 function putLimit(licenseModelId, entitlementPoolId, version, limit) {
117 return RestAPIUtil.put(
118 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${
124 description: limit.description,
125 metric: getStrValue(limit.metric),
127 unit: getStrValue(limit.unit),
128 aggregationFunction: getValue(limit.aggregationFunction),
129 time: getValue(limit.time)
134 const EntitlementPoolsActionHelper = {
135 fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) {
136 return fetchEntitlementPoolsList(licenseModelId, version).then(
140 entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
146 openEntitlementPoolsEditor(
148 { entitlementPool, licenseModelId, version, isReadOnlyMode } = {}
150 if (licenseModelId && version && entitlementPool) {
151 this.fetchLimits(dispatch, {
158 type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
162 type: modalActionTypes.GLOBAL_MODAL_SHOW,
164 modalComponentName: modalContentMapper.EP_EDITOR,
165 modalComponentProps: {
169 size: modalSizes.LARGE
172 licenseModelId && version && entitlementPool
173 ? i18n('Edit Entitlement Pool')
174 : i18n('Create New Entitlement Pool')
179 async deleteEntitlementPool(
181 { licenseModelId, entitlementPoolId, version }
183 await deleteEntitlementPool(licenseModelId, entitlementPoolId, version);
185 await ItemsHelper.checkItemStatus(dispatch, {
186 itemId: licenseModelId,
187 versionId: version.id
190 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
196 entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
199 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
204 closeEntitlementPoolsEditor(dispatch) {
206 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
209 type: modalActionTypes.GLOBAL_MODAL_CLOSE
213 async saveEntitlementPool(
215 { licenseModelId, previousEntitlementPool, entitlementPool, version }
217 if (previousEntitlementPool) {
218 await putEntitlementPool(
220 previousEntitlementPool,
225 await postEntitlementPool(licenseModelId, entitlementPool, version);
227 await ItemsHelper.checkItemStatus(dispatch, {
228 itemId: licenseModelId,
229 versionId: version.id
231 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
237 hideDeleteConfirm(dispatch) {
239 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
240 entitlementPoolToDelete: false
243 openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) {
245 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
246 entitlementPoolToDelete: entitlementPool
250 fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) {
251 return fetchLimitsList(
258 entitlementPoolsActionTypes.entitlementPoolsEditor
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(() => {
271 type: limitEditorActions.CLOSE
273 this.fetchLimits(dispatch, {
278 return ItemsHelper.checkItemStatus(dispatch, {
279 itemId: licenseModelId,
280 versionId: version.id
285 deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
292 this.fetchLimits(dispatch, {
297 return ItemsHelper.checkItemStatus(dispatch, {
298 itemId: licenseModelId,
299 versionId: version.id
305 export default EntitlementPoolsActionHelper;