blob: 22e21a613ad52111d658a93fc17589482132f411 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17import Configuration from 'sdc-app/config/Configuration.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { actionTypes as featureGroupsActionConstants } from './FeatureGroupsConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020019import EntitlementPoolsActionHelper from 'sdc-app/onboarding/licenseModel/entitlementPools/EntitlementPoolsActionHelper.js';
20import LicenseKeyGroupsActionHelper from 'sdc-app/onboarding/licenseModel/licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
talig8e9c0652017-12-20 14:30:43 +020021import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020022
AviZi280f8012017-06-09 02:39:56 +030023function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020024 const restPrefix = Configuration.get('restPrefix');
25 const { id: versionId } = version;
26 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/feature-groups`;
Michael Landoefa037d2017-02-19 12:57:33 +020027}
28
talig8e9c0652017-12-20 14:30:43 +020029function fetchFeatureGroup(licenseModelId, featureGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 return RestAPIUtil.fetch(
31 `${baseUrl(licenseModelId, version)}/${featureGroupId}`
32 );
talig8e9c0652017-12-20 14:30:43 +020033}
34
Michael Landoefa037d2017-02-19 12:57:33 +020035function fetchFeatureGroupsList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020036 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020037}
38
AviZi280f8012017-06-09 02:39:56 +030039function deleteFeatureGroup(licenseModelId, featureGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020040 return RestAPIUtil.destroy(
41 `${baseUrl(licenseModelId, version)}/${featureGroupId}`
42 );
Michael Landoefa037d2017-02-19 12:57:33 +020043}
44
AviZi280f8012017-06-09 02:39:56 +030045function addFeatureGroup(licenseModelId, featureGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020046 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
47 name: featureGroup.name,
48 description: featureGroup.description,
49 partNumber: featureGroup.partNumber,
50 manufacturerReferenceNumber: featureGroup.manufacturerReferenceNumber,
51 addedLicenseKeyGroupsIds: featureGroup.licenseKeyGroupsIds,
52 addedEntitlementPoolsIds: featureGroup.entitlementPoolsIds
53 });
Michael Landoefa037d2017-02-19 12:57:33 +020054}
55
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020056function updateFeatureGroup(
57 licenseModelId,
58 previousFeatureGroup,
59 featureGroup,
60 version
61) {
62 const { licenseKeyGroupsIds = [] } = featureGroup;
63 const {
64 licenseKeyGroupsIds: prevLicenseKeyGroupsIds = []
65 } = previousFeatureGroup;
66 const { entitlementPoolsIds = [] } = featureGroup;
67 const {
68 entitlementPoolsIds: prevEntitlementPoolsIds = []
69 } = previousFeatureGroup;
70 return RestAPIUtil.put(
71 `${baseUrl(licenseModelId, version)}/${featureGroup.id}`,
72 {
73 name: featureGroup.name,
74 description: featureGroup.description,
75 partNumber: featureGroup.partNumber,
76 manufacturerReferenceNumber:
77 featureGroup.manufacturerReferenceNumber,
78 addedLicenseKeyGroupsIds: licenseKeyGroupsIds.filter(
79 licenseKeyGroupId =>
80 prevLicenseKeyGroupsIds.indexOf(licenseKeyGroupId) === -1
81 ),
82 removedLicenseKeyGroupsIds: prevLicenseKeyGroupsIds.filter(
83 prevLicenseKeyGroupId =>
84 licenseKeyGroupsIds.indexOf(prevLicenseKeyGroupId) === -1
85 ),
86 addedEntitlementPoolsIds: entitlementPoolsIds.filter(
87 entitlementPoolId =>
88 prevEntitlementPoolsIds.indexOf(entitlementPoolId) === -1
89 ),
90 removedEntitlementPoolsIds: prevEntitlementPoolsIds.filter(
91 prevEntitlementPoolId =>
92 entitlementPoolsIds.indexOf(prevEntitlementPoolId) === -1
93 )
94 }
95 );
Michael Landoefa037d2017-02-19 12:57:33 +020096}
97
98export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020099 fetchFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) {
100 return fetchFeatureGroup(licenseModelId, featureGroupId, version);
101 },
talig8e9c0652017-12-20 14:30:43 +0200102
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200103 fetchFeatureGroupsList(dispatch, { licenseModelId, version }) {
104 return fetchFeatureGroupsList(licenseModelId, version).then(response =>
105 dispatch({
106 type: featureGroupsActionConstants.FEATURE_GROUPS_LIST_LOADED,
107 response
108 })
109 );
110 },
Michael Landoefa037d2017-02-19 12:57:33 +0200111
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200112 deleteFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) {
113 return deleteFeatureGroup(licenseModelId, featureGroupId, version).then(
114 () => {
115 dispatch({
116 type: featureGroupsActionConstants.DELETE_FEATURE_GROUPS,
117 featureGroupId
118 });
119 return ItemsHelper.checkItemStatus(dispatch, {
120 itemId: licenseModelId,
121 versionId: version.id
122 });
123 }
124 );
125 },
Michael Landoefa037d2017-02-19 12:57:33 +0200126
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200127 saveFeatureGroup(
128 dispatch,
129 { licenseModelId, previousFeatureGroup, featureGroup, version }
130 ) {
131 if (previousFeatureGroup) {
132 return updateFeatureGroup(
133 licenseModelId,
134 previousFeatureGroup,
135 featureGroup,
136 version
137 ).then(() => {
138 dispatch({
139 type: featureGroupsActionConstants.EDIT_FEATURE_GROUPS,
140 featureGroup
141 });
142 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(
143 dispatch,
144 { licenseModelId, version }
145 );
146 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(
147 dispatch,
148 { licenseModelId, version }
149 );
150 return ItemsHelper.checkItemStatus(dispatch, {
151 itemId: licenseModelId,
152 versionId: version.id
153 });
154 });
155 } else {
156 return addFeatureGroup(licenseModelId, featureGroup, version).then(
157 response => {
158 dispatch({
159 type: featureGroupsActionConstants.ADD_FEATURE_GROUPS,
160 featureGroup: {
161 ...featureGroup,
162 id: response.value,
163 referencingLicenseAgreements: []
164 }
165 });
166 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(
167 dispatch,
168 { licenseModelId, version }
169 );
170 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(
171 dispatch,
172 { licenseModelId, version }
173 );
174 return ItemsHelper.checkItemStatus(dispatch, {
175 itemId: licenseModelId,
176 versionId: version.id
177 });
178 }
179 );
180 }
181 },
Michael Landoefa037d2017-02-19 12:57:33 +0200182
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200183 selectEntitlementPoolsEditorTab(dispatch, { tab }) {
184 dispatch({
185 type: featureGroupsActionConstants.featureGroupsEditor.SELECT_TAB,
186 tab
187 });
188 },
Michael Landoefa037d2017-02-19 12:57:33 +0200189
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200190 openFeatureGroupsEditor(
191 dispatch,
192 { featureGroup, licenseModelId, version }
193 ) {
194 return Promise.all([
195 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
196 licenseModelId,
197 version
198 }),
199 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {
200 licenseModelId,
201 version
202 })
203 ]).then(() => {
204 dispatch({
205 type: featureGroupsActionConstants.featureGroupsEditor.OPEN,
206 featureGroup
207 });
208 });
209 },
Michael Landoefa037d2017-02-19 12:57:33 +0200210
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200211 closeFeatureGroupsEditor(dispatch) {
212 dispatch({
213 type: featureGroupsActionConstants.featureGroupsEditor.CLOSE
214 });
215 }
Michael Landoefa037d2017-02-19 12:57:33 +0200216};