[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / featureGroups / FeatureGroupsActionHelper.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 featureGroupsActionConstants} from './FeatureGroupsConstants.js';
19 import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
20 import EntitlementPoolsActionHelper from 'sdc-app/onboarding/licenseModel/entitlementPools/EntitlementPoolsActionHelper.js';
21 import LicenseKeyGroupsActionHelper from 'sdc-app/onboarding/licenseModel/licenseKeyGroups/LicenseKeyGroupsActionHelper.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}/feature-groups`;
27 }
28
29 function fetchFeatureGroupsList(licenseModelId, version) {
30         return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
31 }
32
33 function deleteFeatureGroup(licenseModelId, featureGroupId, version) {
34         return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${featureGroupId}`);
35 }
36
37 function addFeatureGroup(licenseModelId, featureGroup, version) {
38         return RestAPIUtil.post(baseUrl(licenseModelId, version), {
39                 name: featureGroup.name,
40                 description: featureGroup.description,
41                 partNumber: featureGroup.partNumber,
42                 addedLicenseKeyGroupsIds: featureGroup.licenseKeyGroupsIds,
43                 addedEntitlementPoolsIds: featureGroup.entitlementPoolsIds
44         });
45 }
46
47 function updateFeatureGroup(licenseModelId, previousFeatureGroup, featureGroup, version) {
48
49         const {licenseKeyGroupsIds = []} = featureGroup;
50         const {licenseKeyGroupsIds: prevLicenseKeyGroupsIds = []} = previousFeatureGroup;
51         const {entitlementPoolsIds = []} = featureGroup;
52         const {entitlementPoolsIds: prevEntitlementPoolsIds = []} = previousFeatureGroup;
53         return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${featureGroup.id}`, {
54                 name: featureGroup.name,
55                 description: featureGroup.description,
56                 partNumber: featureGroup.partNumber,
57                 addedLicenseKeyGroupsIds: licenseKeyGroupsIds.filter(licenseKeyGroupId => prevLicenseKeyGroupsIds.indexOf(licenseKeyGroupId) === -1),
58                 removedLicenseKeyGroupsIds: prevLicenseKeyGroupsIds.filter(prevLicenseKeyGroupId => licenseKeyGroupsIds.indexOf(prevLicenseKeyGroupId) === -1),
59                 addedEntitlementPoolsIds: entitlementPoolsIds.filter(entitlementPoolId => prevEntitlementPoolsIds.indexOf(entitlementPoolId) === -1),
60                 removedEntitlementPoolsIds: prevEntitlementPoolsIds.filter(prevEntitlementPoolId => entitlementPoolsIds.indexOf(prevEntitlementPoolId) === -1)
61
62         });
63 }
64
65 export default {
66         fetchFeatureGroupsList(dispatch, {licenseModelId, version}) {
67                 return fetchFeatureGroupsList(licenseModelId, version).then(response => dispatch({
68                         type: featureGroupsActionConstants.FEATURE_GROUPS_LIST_LOADED,
69                         response
70                 }));
71         },
72
73         deleteFeatureGroup(dispatch, {licenseModelId, featureGroupId, version}) {
74                 return deleteFeatureGroup(licenseModelId, featureGroupId, version).then(() => dispatch({
75                         type: featureGroupsActionConstants.DELETE_FEATURE_GROUPS,
76                         featureGroupId
77                 }));
78         },
79
80         saveFeatureGroup(dispatch, {licenseModelId, previousFeatureGroup, featureGroup, version}) {
81                 if (previousFeatureGroup) {
82                         return updateFeatureGroup(licenseModelId, previousFeatureGroup, featureGroup, version).then(() =>{
83                                 dispatch({
84                                         type: featureGroupsActionConstants.EDIT_FEATURE_GROUPS,
85                                         featureGroup
86                                 });
87                                 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version});
88                                 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version});
89                         });
90                 }
91                 else {
92                         return addFeatureGroup(licenseModelId, featureGroup, version).then(response => {
93                                 dispatch({
94                                         type: featureGroupsActionConstants.ADD_FEATURE_GROUPS,
95                                         featureGroup: {
96                                                 ...featureGroup,
97                                                 id: response.value,
98                                                 referencingLicenseAgreements: []
99                                         }
100                                 });
101                                 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version});
102                                 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version});
103                         });
104                 }
105         },
106
107         selectEntitlementPoolsEditorTab(dispatch, {tab}) {
108                 dispatch({
109                         type: featureGroupsActionConstants.featureGroupsEditor.SELECT_TAB,
110                         tab
111                 });
112         },
113
114         openFeatureGroupsEditor(dispatch, {featureGroup, licenseModelId, version}) {
115                 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version});
116                 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version});
117                 dispatch({
118                         type: featureGroupsActionConstants.featureGroupsEditor.OPEN,
119                         featureGroup
120                 });
121         },
122
123         closeFeatureGroupsEditor(dispatch) {
124                 dispatch({
125                         type: featureGroupsActionConstants.featureGroupsEditor.CLOSE
126                 });
127         },
128
129
130         switchVersion(dispatch, {licenseModelId, version}) {
131                 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
132                         this.fetchFeatureGroupsList(dispatch, {licenseModelId, version});
133                 });
134         }
135 };