Add new code new version
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / featureGroups / FeatureGroupsActionHelper.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 featureGroupsActionConstants} from './FeatureGroupsConstants.js';
24 import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
25 import EntitlementPoolsActionHelper from 'sdc-app/onboarding/licenseModel/entitlementPools/EntitlementPoolsActionHelper.js';
26 import LicenseKeyGroupsActionHelper from 'sdc-app/onboarding/licenseModel/licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
27
28 function baseUrl(licenseModelId) {
29         const restPrefix = Configuration.get('restPrefix');
30         return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/feature-groups`;
31 }
32
33 function fetchFeatureGroupsList(licenseModelId, version) {
34         let versionQuery = version ? `?version=${version}` : '';
35         return RestAPIUtil.fetch(`${baseUrl(licenseModelId)}${versionQuery}`);
36 }
37
38 function deleteFeatureGroup(licenseModelId, featureGroupId) {
39         return RestAPIUtil.destroy(`${baseUrl(licenseModelId)}/${featureGroupId}`);
40 }
41
42 function addFeatureGroup(licenseModelId, featureGroup) {
43         return RestAPIUtil.create(baseUrl(licenseModelId), {
44                 name: featureGroup.name,
45                 description: featureGroup.description,
46                 partNumber: featureGroup.partNumber,
47                 addedLicenseKeyGroupsIds: featureGroup.licenseKeyGroupsIds,
48                 addedEntitlementPoolsIds: featureGroup.entitlementPoolsIds
49         });
50 }
51
52 function updateFeatureGroup(licenseModelId, previousFeatureGroup, featureGroup) {
53
54         const {licenseKeyGroupsIds = []} = featureGroup;
55         const {licenseKeyGroupsIds: prevLicenseKeyGroupsIds = []} = previousFeatureGroup;
56         const {entitlementPoolsIds = []} = featureGroup;
57         const {entitlementPoolsIds: prevEntitlementPoolsIds = []} = previousFeatureGroup;
58         return RestAPIUtil.save(`${baseUrl(licenseModelId)}/${featureGroup.id}`, {
59                 name: featureGroup.name,
60                 description: featureGroup.description,
61                 partNumber: featureGroup.partNumber,
62                 addedLicenseKeyGroupsIds: licenseKeyGroupsIds.filter(licenseKeyGroupId => prevLicenseKeyGroupsIds.indexOf(licenseKeyGroupId) === -1),
63                 removedLicenseKeyGroupsIds: prevLicenseKeyGroupsIds.filter(prevLicenseKeyGroupId => licenseKeyGroupsIds.indexOf(prevLicenseKeyGroupId) === -1),
64                 addedEntitlementPoolsIds: entitlementPoolsIds.filter(entitlementPoolId => prevEntitlementPoolsIds.indexOf(entitlementPoolId) === -1),
65                 removedEntitlementPoolsIds: prevEntitlementPoolsIds.filter(prevEntitlementPoolId => entitlementPoolsIds.indexOf(prevEntitlementPoolId) === -1)
66
67         });
68 }
69
70 export default {
71         fetchFeatureGroupsList(dispatch, {licenseModelId, version}) {
72                 return fetchFeatureGroupsList(licenseModelId, version).then(response => dispatch({
73                         type: featureGroupsActionConstants.FEATURE_GROUPS_LIST_LOADED,
74                         response
75                 }));
76         },
77
78         deleteFeatureGroup(dispatch, {licenseModelId, featureGroupId}) {
79                 return deleteFeatureGroup(licenseModelId, featureGroupId).then(() => dispatch({
80                         type: featureGroupsActionConstants.DELETE_FEATURE_GROUPS,
81                         featureGroupId
82                 }));
83         },
84
85         saveFeatureGroup(dispatch, {licenseModelId, previousFeatureGroup, featureGroup}) {
86                 if (previousFeatureGroup) {
87                         return updateFeatureGroup(licenseModelId, previousFeatureGroup, featureGroup).then(() => dispatch({
88                                 type: featureGroupsActionConstants.EDIT_FEATURE_GROUPS,
89                                 featureGroup
90                         }));
91                 }
92                 else {
93                         return addFeatureGroup(licenseModelId, featureGroup).then(response => dispatch({
94                                 type: featureGroupsActionConstants.ADD_FEATURE_GROUPS,
95                                 featureGroup: {
96                                         ...featureGroup,
97                                         id: response.value
98                                 }
99                         }));
100                 }
101         },
102
103         selectEntitlementPoolsEditorTab(dispatch, {tab}) {
104                 dispatch({
105                         type: featureGroupsActionConstants.featureGroupsEditor.SELECT_TAB,
106                         tab
107                 });
108         },
109
110         selectFeatureGroupsEditorEntitlementPoolsButtonTab(dispatch, {buttonTab}) {
111                 dispatch({
112                         type: featureGroupsActionConstants.featureGroupsEditor.SELECTED_ENTITLEMENT_POOLS_BUTTONTAB,
113                         buttonTab
114                 });
115         },
116
117         selectFeatureGroupsEditorLicenseKeyGroupsButtonTab(dispatch, {buttonTab}) {
118                 dispatch({
119                         type: featureGroupsActionConstants.featureGroupsEditor.SELECTED_LICENSE_KEY_GROUPS_BUTTONTAB,
120                         buttonTab
121                 });
122         },
123
124         openFeatureGroupsEditor(dispatch, {featureGroup, licenseModelId}) {
125                 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId});
126                 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId});
127                 dispatch({
128                         type: featureGroupsActionConstants.featureGroupsEditor.OPEN,
129                         featureGroup
130                 });
131         },
132
133         closeFeatureGroupsEditor(dispatch) {
134                 dispatch({
135                         type: featureGroupsActionConstants.featureGroupsEditor.CLOSE
136                 });
137         },
138
139         featureGroupsEditorDataChanged(dispatch, {deltaData}) {
140                 dispatch({
141                         type: featureGroupsActionConstants.featureGroupsEditor.DATA_CHANGED,
142                         deltaData
143                 });
144         },
145
146         hideDeleteConfirm(dispatch) {
147                 dispatch({
148                         type: featureGroupsActionConstants.FEATURE_GROUPS_DELETE_CONFIRM,
149                         featureGroupToDelete: false
150                 });
151         },
152
153         openDeleteFeatureGroupConfirm(dispatch, {featureGroup}) {
154                 dispatch({
155                         type: featureGroupsActionConstants.FEATURE_GROUPS_DELETE_CONFIRM,
156                         featureGroupToDelete: featureGroup
157                 });
158         },
159
160         switchVersion(dispatch, {licenseModelId, version}) {
161                 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
162                         this.fetchFeatureGroupsList(dispatch, {licenseModelId, version});
163                 });
164         }
165 };