2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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.
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import {actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js';
19 import {actionTypes as limitEditorActions} from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
20 import {default as getValue, getStrValue} from 'nfvo-utils/getValue.js';
21 import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
23 function baseUrl(licenseModelId, version) {
24 const restPrefix = Configuration.get('restPrefix');
25 const {id: versionId} = version;
26 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`;
29 function fetchEntitlementPoolsList(licenseModelId, version) {
30 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
33 function postEntitlementPool(licenseModelId, entitlementPool, version) {
34 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
35 name: entitlementPool.name,
36 description: entitlementPool.description,
37 thresholdValue: entitlementPool.thresholdValue,
38 thresholdUnits: getValue(entitlementPool.thresholdUnits),
39 increments: entitlementPool.increments,
40 operationalScope: getValue(entitlementPool.operationalScope),
41 time: entitlementPool.time,
42 startDate: entitlementPool.startDate,
43 expiryDate: entitlementPool.expiryDate
48 function putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version) {
50 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPool.id}`, {
51 name: entitlementPool.name,
52 description: entitlementPool.description,
53 thresholdValue: entitlementPool.thresholdValue,
54 thresholdUnits: getValue(entitlementPool.thresholdUnits),
55 increments: entitlementPool.increments,
56 operationalScope: getValue(entitlementPool.operationalScope),
57 time: entitlementPool.time,
58 startDate: entitlementPool.startDate,
59 expiryDate: entitlementPool.expiryDate
63 function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
64 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}`);
67 function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
68 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`);
71 function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
72 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limitId}`);
75 function postLimit(licenseModelId, entitlementPoolId, version, limit) {
76 return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`, {
79 description: limit.description,
80 metric: getStrValue(limit.metric),
82 unit: getStrValue(limit.unit),
83 aggregationFunction: getValue(limit.aggregationFunction),
84 time: getValue(limit.time)
88 function putLimit(licenseModelId, entitlementPoolId, version, limit) {
90 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limit.id}`, {
93 description: limit.description,
94 metric: getStrValue(limit.metric),
96 unit: getStrValue(limit.unit),
97 aggregationFunction: getValue(limit.aggregationFunction),
98 time: getValue(limit.time)
104 fetchEntitlementPoolsList(dispatch, {licenseModelId, version}) {
105 return fetchEntitlementPoolsList(licenseModelId, version).then(response => dispatch({
106 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
111 openEntitlementPoolsEditor(dispatch, {entitlementPool, licenseModelId, version} = {}) {
112 if (licenseModelId && version) {
113 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
116 type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
121 deleteEntitlementPool(dispatch, {licenseModelId, entitlementPoolId, version}) {
122 return deleteEntitlementPool(licenseModelId, entitlementPoolId, version).then(() => {
124 type: entitlementPoolsActionTypes.DELETE_ENTITLEMENT_POOL,
127 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
131 entitlementPoolsEditorDataChanged(dispatch, {deltaData}) {
133 type: entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
138 closeEntitlementPoolsEditor(dispatch) {
140 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
144 saveEntitlementPool(dispatch, {licenseModelId, previousEntitlementPool, entitlementPool, version}) {
145 if (previousEntitlementPool) {
146 return putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version).then(() => {
148 type: entitlementPoolsActionTypes.EDIT_ENTITLEMENT_POOL,
151 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
155 return postEntitlementPool(licenseModelId, entitlementPool, version).then(response => {
157 type: entitlementPoolsActionTypes.ADD_ENTITLEMENT_POOL,
160 referencingFeatureGroups: [],
164 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
169 hideDeleteConfirm(dispatch) {
171 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
172 entitlementPoolToDelete: false
175 openDeleteEntitlementPoolConfirm(dispatch, {entitlementPool}) {
177 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
178 entitlementPoolToDelete: entitlementPool
184 fetchLimits(dispatch, {licenseModelId, version, entitlementPool}) {
185 return fetchLimitsList(licenseModelId, entitlementPool.id, version). then (response => {
187 type: entitlementPoolsActionTypes.entitlementPoolsEditor.LIMITS_LIST_LOADED,
193 submitLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) {
194 const propmise = limit.id ? putLimit(licenseModelId,entitlementPool.id, version, limit)
195 : postLimit(licenseModelId,entitlementPool.id, version, limit);
196 return propmise.then(() => {
198 type: limitEditorActions.CLOSE
200 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
201 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
205 deleteLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) {
206 return deleteLimit(licenseModelId,entitlementPool.id, version, limit.id).then(() => {
207 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
208 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});