Add new code new version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / entitlementPools / EntitlementPoolsActionHelper.js
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
22 import Configuration from 'sdc-app/config/Configuration.js';
23 import {actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js';
24 import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
25
26 function baseUrl(licenseModelId) {
27         const restPrefix = Configuration.get('restPrefix');
28         return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/entitlement-pools`;
29 }
30
31 function fetchEntitlementPoolsList(licenseModelId, version) {
32         let versionQuery = version ? `?version=${version}` : '';
33         return RestAPIUtil.fetch(`${baseUrl(licenseModelId)}${versionQuery}`);
34 }
35
36 function postEntitlementPool(licenseModelId, entitlementPool) {
37         return RestAPIUtil.create(baseUrl(licenseModelId), {
38                 name: entitlementPool.name,
39                 description: entitlementPool.description,
40                 thresholdValue: entitlementPool.thresholdValue,
41                 thresholdUnits: entitlementPool.thresholdUnits,
42                 entitlementMetric: entitlementPool.entitlementMetric,
43                 increments: entitlementPool.increments,
44                 aggregationFunction: entitlementPool.aggregationFunction,
45                 operationalScope: entitlementPool.operationalScope,
46                 time: entitlementPool.time,
47                 manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber
48         });
49 }
50
51
52 function putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool) {
53         return RestAPIUtil.save(`${baseUrl(licenseModelId)}/${entitlementPool.id}`, {
54                 name: entitlementPool.name,
55                 description: entitlementPool.description,
56                 thresholdValue: entitlementPool.thresholdValue,
57                 thresholdUnits: entitlementPool.thresholdUnits,
58                 entitlementMetric: entitlementPool.entitlementMetric,
59                 increments: entitlementPool.increments,
60                 aggregationFunction: entitlementPool.aggregationFunction,
61                 operationalScope: entitlementPool.operationalScope,
62                 time: entitlementPool.time,
63                 manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber
64         });
65 }
66
67 function deleteEntitlementPool(licenseModelId, entitlementPoolId) {
68         return RestAPIUtil.destroy(`${baseUrl(licenseModelId)}/${entitlementPoolId}`);
69 }
70
71
72 export default {
73         fetchEntitlementPoolsList(dispatch, {licenseModelId, version}) {
74                 return fetchEntitlementPoolsList(licenseModelId, version).then(response => dispatch({
75                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
76                         response
77                 }));
78         },
79
80         openEntitlementPoolsEditor(dispatch, {entitlementPool} = {}) {
81                 dispatch({
82                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
83                         entitlementPool
84                 });
85         },
86
87         deleteEntitlementPool(dispatch, {licenseModelId, entitlementPoolId}) {
88                 return deleteEntitlementPool(licenseModelId, entitlementPoolId).then(() => {
89                         dispatch({
90                                 type: entitlementPoolsActionTypes.DELETE_ENTITLEMENT_POOL,
91                                 entitlementPoolId
92                         });
93                 });
94         },
95
96         entitlementPoolsEditorDataChanged(dispatch, {deltaData}) {
97                 dispatch({
98                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
99                         deltaData
100                 });
101         },
102
103         closeEntitlementPoolsEditor(dispatch) {
104                 dispatch({
105                         type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
106                 });
107         },
108
109         saveEntitlementPool(dispatch, {licenseModelId, previousEntitlementPool, entitlementPool}) {
110                 if (previousEntitlementPool) {
111                         return putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool).then(() => {
112                                 dispatch({
113                                         type: entitlementPoolsActionTypes.EDIT_ENTITLEMENT_POOL,
114                                         entitlementPool
115                                 });
116                         });
117                 }
118                 else {
119                         return postEntitlementPool(licenseModelId, entitlementPool).then(response => {
120                                 dispatch({
121                                         type: entitlementPoolsActionTypes.ADD_ENTITLEMENT_POOL,
122                                         entitlementPool: {
123                                                 ...entitlementPool,
124                                                 id: response.value
125                                         }
126                                 });
127                         });
128                 }
129         },
130
131         hideDeleteConfirm(dispatch) {
132                 dispatch({
133                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
134                         entitlementPoolToDelete: false
135                 });
136         },
137         openDeleteEntitlementPoolConfirm(dispatch, {entitlementPool}) {
138                 dispatch({
139                         type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
140                         entitlementPoolToDelete: entitlementPool
141                 });
142         },
143
144         switchVersion(dispatch, {licenseModelId, version}) {
145                 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
146                         this.fetchEntitlementPoolsList(dispatch, {licenseModelId, version});
147                 });
148         }
149 };