Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / licenseKeyGroups / LicenseKeyGroupsActionHelper.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 licenseKeyGroupsConstants} from './LicenseKeyGroupsConstants.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}/license-key-groups`;
27 }
28
29 function fetchLicenseKeyGroupsList(licenseModelId, version) {
30         return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
31 }
32
33 function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) {
34         return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`);
35 }
36
37 function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
38         return RestAPIUtil.post(baseUrl(licenseModelId, version), {
39                 name: licenseKeyGroup.name,
40                 description: licenseKeyGroup.description,
41                 operationalScope: getValue(licenseKeyGroup.operationalScope),
42                 type: licenseKeyGroup.type,
43                 increments: licenseKeyGroup.increments,
44                 thresholdValue: licenseKeyGroup.thresholdValue,
45                 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
46                 startDate: licenseKeyGroup.startDate,
47                 expiryDate: licenseKeyGroup.expiryDate
48         });
49 }
50
51 function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
52         return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`, {
53                 name: licenseKeyGroup.name,
54                 description: licenseKeyGroup.description,
55                 operationalScope: getValue(licenseKeyGroup.operationalScope),
56                 type: licenseKeyGroup.type,
57                 increments: licenseKeyGroup.increments,
58                 thresholdValue: licenseKeyGroup.thresholdValue,
59                 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
60                 startDate: licenseKeyGroup.startDate,
61                 expiryDate: licenseKeyGroup.expiryDate
62         });
63 }
64
65 function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
66         return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`);
67 }
68
69 function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
70         return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${limitId}`);
71 }
72
73 function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
74         return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`, {
75                 name: limit.name,
76                 type: limit.type,
77                 description: limit.description,
78                 metric: getStrValue(limit.metric),
79                 value: limit.value,
80                 unit: getStrValue(limit.unit),
81                 aggregationFunction: getValue(limit.aggregationFunction),
82                 time: getValue(limit.time)
83         });
84 }
85
86 function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
87
88         return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${limit.id}`, {
89                 name: limit.name,
90                 type: limit.type,
91                 description: limit.description,
92                 metric: getStrValue(limit.metric),
93                 value: limit.value,
94                 unit: getStrValue(limit.unit),
95                 aggregationFunction: getValue(limit.aggregationFunction),
96                 time: getValue(limit.time)
97         });
98 }
99
100 export default {
101         fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version}) {
102                 return fetchLicenseKeyGroupsList(licenseModelId, version).then(response => dispatch({
103                         type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
104                         response
105                 }));
106         },
107
108         openLicenseKeyGroupsEditor(dispatch, {licenseKeyGroup, licenseModelId, version} = {}) {
109                 if (licenseModelId && version) {
110                         this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
111                 }
112                 dispatch({
113                         type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
114                         licenseKeyGroup
115                 });
116         },
117
118         closeLicenseKeyGroupEditor(dispatch){
119                 dispatch({
120                         type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
121                 });
122         },
123
124         saveLicenseKeyGroup(dispatch, {licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version}) {
125                 if (previousLicenseKeyGroup) {
126                         return putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(() => {
127                                 dispatch({
128                                         type: licenseKeyGroupsConstants.EDIT_LICENSE_KEY_GROUP,
129                                         licenseKeyGroup
130                                 });
131                                 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
132                         });
133                 }
134                 else {
135                         return postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(response => {
136                                 dispatch({
137                                         type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP,
138                                         licenseKeyGroup: {
139                                                 ...licenseKeyGroup,
140                                                 referencingFeatureGroups: [],
141                                                 id: response.value
142                                         }
143                                 });
144                                 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
145                         });
146                 }
147
148
149         },
150
151         deleteLicenseKeyGroup(dispatch, {licenseModelId, licenseKeyGroupId, version}){
152                 return deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version).then(()=> {
153                         dispatch({
154                                 type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP,
155                                 licenseKeyGroupId
156                         });
157                         ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
158                 });
159         },
160
161         hideDeleteConfirm(dispatch) {
162                 dispatch({
163                         type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
164                         licenseKeyGroupToDelete: false
165                 });
166         },
167
168         openDeleteLicenseAgreementConfirm(dispatch, {licenseKeyGroup}) {
169                 dispatch({
170                         type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
171                         licenseKeyGroupToDelete: licenseKeyGroup
172                 });
173         },
174
175
176         fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup}) {
177                 return fetchLimitsList(licenseModelId, licenseKeyGroup.id, version).then(response => {
178                         dispatch({
179                                 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.LIMITS_LIST_LOADED,
180                                 response
181                         });
182                 });
183         },
184
185         submitLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) {
186                 const promise = limit.id ? putLimit(licenseModelId,licenseKeyGroup.id, version, limit) :
187                          postLimit(licenseModelId,licenseKeyGroup.id, version, limit);
188                 return promise.then(() => {
189                         dispatch({
190                                 type: limitEditorActions.CLOSE
191                         });
192                         this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
193                         ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
194                 });
195         },
196
197         deleteLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) {
198                 return deleteLimit(licenseModelId,licenseKeyGroup.id, version, limit.id).then(() => {
199                         this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
200                         ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
201                 });
202         }
203
204
205 };