[SDC] Full OnBoard health-check and NFoD support
[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 LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
20 import {actionTypes as limitEditorActions} from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
21 import getValue from 'nfvo-utils/getValue.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                 entitlementMetric: entitlementPool.entitlementMetric,
40                 increments: entitlementPool.increments,
41                 operationalScope: getValue(entitlementPool.operationalScope),
42                 time: entitlementPool.time,
43                 startDate: entitlementPool.startDate,
44                 expiryDate: entitlementPool.expiryDate
45         });
46 }
47
48
49 function putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version) {
50         
51         return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPool.id}`, {
52                 name: entitlementPool.name,
53                 description: entitlementPool.description,
54                 thresholdValue: entitlementPool.thresholdValue,
55                 thresholdUnits: getValue(entitlementPool.thresholdUnits),
56                 entitlementMetric: entitlementPool.entitlementMetric,
57                 increments: entitlementPool.increments,
58                 operationalScope: getValue(entitlementPool.operationalScope),
59                 time: entitlementPool.time,
60                 startDate: entitlementPool.startDate,
61                 expiryDate: entitlementPool.expiryDate
62         });
63 }
64
65 function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
66         return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}`);
67 }
68
69 function fetchLimitsList(licenseModelId, entitlementPoolId, version) {  
70         return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`);
71 }
72
73 function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {     
74         return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limitId}`);
75 }
76
77 function postLimit(licenseModelId, entitlementPoolId, version, limit) { 
78         return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`, {
79                 name: limit.name,
80                 type: limit.type,
81                 description: limit.description,
82                 metric: limit.metric,
83                 value: limit.value,
84                 unit: limit.unit,
85                 aggregationFunction: getValue(limit.aggregationFunction),
86                 time: getValue(limit.time)
87         });
88 }
89
90 function putLimit(licenseModelId, entitlementPoolId, version, limit) {
91         
92         return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limit.id}`, {
93                 name: limit.name,
94                 type: limit.type,
95                 description: limit.description,
96                 metric: limit.metric,
97                 value: limit.value,
98                 unit: limit.unit,
99                 aggregationFunction: getValue(limit.aggregationFunction),
100                 time: getValue(limit.time)
101         });     
102 }
103
104 export default {
105
106         fetchEntitlementPoolsList(dispatch, {licenseModelId, version}) {
107                 return fetchEntitlementPoolsList(licenseModelId, version).then(response => dispatch({
108                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
109                         response
110                 }));
111         },
112
113         openEntitlementPoolsEditor(dispatch, {entitlementPool, licenseModelId, version} = {}) {
114                 if (licenseModelId && version) {
115                         this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
116                 }
117                 dispatch({
118                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
119                         entitlementPool
120                 });
121         },
122
123         deleteEntitlementPool(dispatch, {licenseModelId, entitlementPoolId, version}) {
124                 return deleteEntitlementPool(licenseModelId, entitlementPoolId, version).then(() => {
125                         dispatch({
126                                 type: entitlementPoolsActionTypes.DELETE_ENTITLEMENT_POOL,
127                                 entitlementPoolId
128                         });
129                 });
130         },
131
132         entitlementPoolsEditorDataChanged(dispatch, {deltaData}) {
133                 dispatch({
134                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
135                         deltaData
136                 });
137         },
138
139         closeEntitlementPoolsEditor(dispatch) {
140                 dispatch({
141                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
142                 });
143         },
144
145         saveEntitlementPool(dispatch, {licenseModelId, previousEntitlementPool, entitlementPool, version}) {
146                 if (previousEntitlementPool) {
147                         return putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version).then(() => {
148                                 dispatch({
149                                         type: entitlementPoolsActionTypes.EDIT_ENTITLEMENT_POOL,
150                                         entitlementPool
151                                 });
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                         });
165                 }
166         },
167
168         hideDeleteConfirm(dispatch) {
169                 dispatch({
170                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
171                         entitlementPoolToDelete: false
172                 });
173         },
174         openDeleteEntitlementPoolConfirm(dispatch, {entitlementPool}) {
175                 dispatch({
176                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
177                         entitlementPoolToDelete: entitlementPool
178                 });
179         },
180
181         switchVersion(dispatch, {licenseModelId, version}) {
182                 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
183                         this.fetchEntitlementPoolsList(dispatch, {licenseModelId, version});
184                 });
185         },
186
187
188         fetchLimits(dispatch, {licenseModelId, version, entitlementPool}) {
189                 return fetchLimitsList(licenseModelId, entitlementPool.id, version). then (response => {
190                         dispatch({
191                                 type: entitlementPoolsActionTypes.entitlementPoolsEditor.LIMITS_LIST_LOADED,
192                                 response
193                         });
194                 });             
195         },
196
197         submitLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) {      
198                 const propmise  =  limit.id ? putLimit(licenseModelId,entitlementPool.id, version, limit)
199                         : postLimit(licenseModelId,entitlementPool.id, version, limit);
200                 return propmise.then(() => {
201                         dispatch({
202                                 type: limitEditorActions.CLOSE
203                         });
204                         this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
205                 });             
206         },
207
208         deleteLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) {                              
209                 return  deleteLimit(licenseModelId,entitlementPool.id, version, limit.id).then(() => {
210                         this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});         
211                 });                             
212         }
213 };