Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / entitlementPools / EntitlementPoolsActionHelper.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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 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';
22
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`;
27 }
28
29 function fetchEntitlementPoolsList(licenseModelId, version) {
30         return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
31 }
32
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
44         });
45 }
46
47
48 function putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version) {
49
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
60         });
61 }
62
63 function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
64         return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}`);
65 }
66
67 function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
68         return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`);
69 }
70
71 function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
72         return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limitId}`);
73 }
74
75 function postLimit(licenseModelId, entitlementPoolId, version, limit) {
76         return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`, {
77                 name: limit.name,
78                 type: limit.type,
79                 description: limit.description,
80                 metric: getStrValue(limit.metric),
81                 value: limit.value,
82                 unit: getStrValue(limit.unit),
83                 aggregationFunction: getValue(limit.aggregationFunction),
84                 time: getValue(limit.time)
85         });
86 }
87
88 function putLimit(licenseModelId, entitlementPoolId, version, limit) {
89
90         return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limit.id}`, {
91                 name: limit.name,
92                 type: limit.type,
93                 description: limit.description,
94                 metric: getStrValue(limit.metric),
95                 value: limit.value,
96                 unit: getStrValue(limit.unit),
97                 aggregationFunction: getValue(limit.aggregationFunction),
98                 time: getValue(limit.time)
99         });
100 }
101
102 export default {
103
104         fetchEntitlementPoolsList(dispatch, {licenseModelId, version}) {
105                 return fetchEntitlementPoolsList(licenseModelId, version).then(response => dispatch({
106                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
107                         response
108                 }));
109         },
110
111         openEntitlementPoolsEditor(dispatch, {entitlementPool, licenseModelId, version} = {}) {
112                 if (licenseModelId && version) {
113                         this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
114                 }
115                 dispatch({
116                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
117                         entitlementPool
118                 });
119         },
120
121         deleteEntitlementPool(dispatch, {licenseModelId, entitlementPoolId, version}) {
122                 return deleteEntitlementPool(licenseModelId, entitlementPoolId, version).then(() => {
123                         dispatch({
124                                 type: entitlementPoolsActionTypes.DELETE_ENTITLEMENT_POOL,
125                                 entitlementPoolId
126                         });
127                         ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
128                 });
129         },
130
131         entitlementPoolsEditorDataChanged(dispatch, {deltaData}) {
132                 dispatch({
133                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
134                         deltaData
135                 });
136         },
137
138         closeEntitlementPoolsEditor(dispatch) {
139                 dispatch({
140                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
141                 });
142         },
143
144         saveEntitlementPool(dispatch, {licenseModelId, previousEntitlementPool, entitlementPool, version}) {
145                 if (previousEntitlementPool) {
146                         return putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version).then(() => {
147                                 dispatch({
148                                         type: entitlementPoolsActionTypes.EDIT_ENTITLEMENT_POOL,
149                                         entitlementPool
150                                 });
151                                 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
152                         });
153                 }
154                 else {
155                         return postEntitlementPool(licenseModelId, entitlementPool, version).then(response => {
156                                 dispatch({
157                                         type: entitlementPoolsActionTypes.ADD_ENTITLEMENT_POOL,
158                                         entitlementPool: {
159                                                 ...entitlementPool,
160                                                 referencingFeatureGroups: [],
161                                                 id: response.value
162                                         }
163                                 });
164                                 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
165                         });
166                 }
167         },
168
169         hideDeleteConfirm(dispatch) {
170                 dispatch({
171                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
172                         entitlementPoolToDelete: false
173                 });
174         },
175         openDeleteEntitlementPoolConfirm(dispatch, {entitlementPool}) {
176                 dispatch({
177                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
178                         entitlementPoolToDelete: entitlementPool
179                 });
180         },
181
182
183
184         fetchLimits(dispatch, {licenseModelId, version, entitlementPool}) {
185                 return fetchLimitsList(licenseModelId, entitlementPool.id, version). then (response => {
186                         dispatch({
187                                 type: entitlementPoolsActionTypes.entitlementPoolsEditor.LIMITS_LIST_LOADED,
188                                 response
189                         });
190                 });
191         },
192
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(() => {
197                         dispatch({
198                                 type: limitEditorActions.CLOSE
199                         });
200                         this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
201                         ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
202                 });
203         },
204
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});
209                 });
210         }
211 };