blob: c6479a74817450c2f892c5fa5e54a309644f3d2d [file] [log] [blame]
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03001/*
2 * Copyright © 2016-2018 European Support Limited
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 *
Einav Weiss Keidar1801b242018-08-13 16:19:46 +03008 * http://www.apache.org/licenses/LICENSE-2.0
AviZi280f8012017-06-09 02:39:56 +03009 *
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,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * 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';
Einav Weiss Keidar1801b242018-08-13 16:19:46 +030022import {
23 actionTypes as modalActionTypes,
24 modalSizes
25} from 'nfvo-components/modal/GlobalModalConstants.js';
26import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
27import i18n from 'nfvo-utils/i18n/i18n.js';
Michael Landoefa037d2017-02-19 12:57:33 +020028
AviZi280f8012017-06-09 02:39:56 +030029function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 const restPrefix = Configuration.get('restPrefix');
31 const { id: versionId } = version;
32 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/feature-groups`;
Michael Landoefa037d2017-02-19 12:57:33 +020033}
34
talig8e9c0652017-12-20 14:30:43 +020035function fetchFeatureGroup(licenseModelId, featureGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020036 return RestAPIUtil.fetch(
37 `${baseUrl(licenseModelId, version)}/${featureGroupId}`
38 );
talig8e9c0652017-12-20 14:30:43 +020039}
40
Michael Landoefa037d2017-02-19 12:57:33 +020041function fetchFeatureGroupsList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020042 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020043}
44
AviZi280f8012017-06-09 02:39:56 +030045function deleteFeatureGroup(licenseModelId, featureGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020046 return RestAPIUtil.destroy(
47 `${baseUrl(licenseModelId, version)}/${featureGroupId}`
48 );
Michael Landoefa037d2017-02-19 12:57:33 +020049}
50
AviZi280f8012017-06-09 02:39:56 +030051function addFeatureGroup(licenseModelId, featureGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020052 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
53 name: featureGroup.name,
54 description: featureGroup.description,
55 partNumber: featureGroup.partNumber,
56 manufacturerReferenceNumber: featureGroup.manufacturerReferenceNumber,
57 addedLicenseKeyGroupsIds: featureGroup.licenseKeyGroupsIds,
58 addedEntitlementPoolsIds: featureGroup.entitlementPoolsIds
59 });
Michael Landoefa037d2017-02-19 12:57:33 +020060}
61
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020062function updateFeatureGroup(
63 licenseModelId,
64 previousFeatureGroup,
65 featureGroup,
66 version
67) {
68 const { licenseKeyGroupsIds = [] } = featureGroup;
69 const {
70 licenseKeyGroupsIds: prevLicenseKeyGroupsIds = []
71 } = previousFeatureGroup;
72 const { entitlementPoolsIds = [] } = featureGroup;
73 const {
74 entitlementPoolsIds: prevEntitlementPoolsIds = []
75 } = previousFeatureGroup;
76 return RestAPIUtil.put(
77 `${baseUrl(licenseModelId, version)}/${featureGroup.id}`,
78 {
79 name: featureGroup.name,
80 description: featureGroup.description,
81 partNumber: featureGroup.partNumber,
82 manufacturerReferenceNumber:
83 featureGroup.manufacturerReferenceNumber,
84 addedLicenseKeyGroupsIds: licenseKeyGroupsIds.filter(
85 licenseKeyGroupId =>
86 prevLicenseKeyGroupsIds.indexOf(licenseKeyGroupId) === -1
87 ),
88 removedLicenseKeyGroupsIds: prevLicenseKeyGroupsIds.filter(
89 prevLicenseKeyGroupId =>
90 licenseKeyGroupsIds.indexOf(prevLicenseKeyGroupId) === -1
91 ),
92 addedEntitlementPoolsIds: entitlementPoolsIds.filter(
93 entitlementPoolId =>
94 prevEntitlementPoolsIds.indexOf(entitlementPoolId) === -1
95 ),
96 removedEntitlementPoolsIds: prevEntitlementPoolsIds.filter(
97 prevEntitlementPoolId =>
98 entitlementPoolsIds.indexOf(prevEntitlementPoolId) === -1
99 )
100 }
101 );
Michael Landoefa037d2017-02-19 12:57:33 +0200102}
103
104export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200105 fetchFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) {
106 return fetchFeatureGroup(licenseModelId, featureGroupId, version);
107 },
talig8e9c0652017-12-20 14:30:43 +0200108
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200109 fetchFeatureGroupsList(dispatch, { licenseModelId, version }) {
110 return fetchFeatureGroupsList(licenseModelId, version).then(response =>
111 dispatch({
112 type: featureGroupsActionConstants.FEATURE_GROUPS_LIST_LOADED,
113 response
114 })
115 );
116 },
Michael Landoefa037d2017-02-19 12:57:33 +0200117
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200118 deleteFeatureGroup(dispatch, { licenseModelId, featureGroupId, version }) {
119 return deleteFeatureGroup(licenseModelId, featureGroupId, version).then(
120 () => {
121 dispatch({
122 type: featureGroupsActionConstants.DELETE_FEATURE_GROUPS,
123 featureGroupId
124 });
125 return ItemsHelper.checkItemStatus(dispatch, {
126 itemId: licenseModelId,
127 versionId: version.id
128 });
129 }
130 );
131 },
Michael Landoefa037d2017-02-19 12:57:33 +0200132
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200133 saveFeatureGroup(
134 dispatch,
135 { licenseModelId, previousFeatureGroup, featureGroup, version }
136 ) {
137 if (previousFeatureGroup) {
138 return updateFeatureGroup(
139 licenseModelId,
140 previousFeatureGroup,
141 featureGroup,
142 version
143 ).then(() => {
144 dispatch({
145 type: featureGroupsActionConstants.EDIT_FEATURE_GROUPS,
146 featureGroup
147 });
148 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(
149 dispatch,
150 { licenseModelId, version }
151 );
152 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(
153 dispatch,
154 { licenseModelId, version }
155 );
156 return ItemsHelper.checkItemStatus(dispatch, {
157 itemId: licenseModelId,
158 versionId: version.id
159 });
160 });
161 } else {
162 return addFeatureGroup(licenseModelId, featureGroup, version).then(
163 response => {
164 dispatch({
165 type: featureGroupsActionConstants.ADD_FEATURE_GROUPS,
166 featureGroup: {
167 ...featureGroup,
168 id: response.value,
169 referencingLicenseAgreements: []
170 }
171 });
172 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(
173 dispatch,
174 { licenseModelId, version }
175 );
176 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(
177 dispatch,
178 { licenseModelId, version }
179 );
180 return ItemsHelper.checkItemStatus(dispatch, {
181 itemId: licenseModelId,
182 versionId: version.id
183 });
184 }
185 );
186 }
187 },
Michael Landoefa037d2017-02-19 12:57:33 +0200188
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200189 selectEntitlementPoolsEditorTab(dispatch, { tab }) {
190 dispatch({
191 type: featureGroupsActionConstants.featureGroupsEditor.SELECT_TAB,
192 tab
193 });
194 },
Michael Landoefa037d2017-02-19 12:57:33 +0200195
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200196 openFeatureGroupsEditor(
197 dispatch,
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300198 { featureGroup, licenseModelId, version, isReadOnlyMode }
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200199 ) {
200 return Promise.all([
201 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
202 licenseModelId,
203 version
204 }),
205 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {
206 licenseModelId,
207 version
208 })
209 ]).then(() => {
210 dispatch({
211 type: featureGroupsActionConstants.featureGroupsEditor.OPEN,
212 featureGroup
213 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300214 dispatch({
215 type: featureGroupsActionConstants.featureGroupsEditor.OPEN,
216 featureGroup
217 });
218 dispatch({
219 type: modalActionTypes.GLOBAL_MODAL_SHOW,
220 data: {
221 modalComponentName: modalContentMapper.FG_EDITOR,
222 modalComponentProps: {
223 version,
224 licenseModelId,
225 isReadOnlyMode,
226 size: modalSizes.LARGE
227 },
228 title:
229 licenseModelId && version && featureGroup
230 ? i18n('Edit Feature Group')
231 : i18n('Create New Feature Group')
232 }
233 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200234 });
235 },
Michael Landoefa037d2017-02-19 12:57:33 +0200236
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200237 closeFeatureGroupsEditor(dispatch) {
238 dispatch({
239 type: featureGroupsActionConstants.featureGroupsEditor.CLOSE
240 });
Einav Weiss Keidar1801b242018-08-13 16:19:46 +0300241 dispatch({
242 type: modalActionTypes.GLOBAL_MODAL_CLOSE
243 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200244 }
Michael Landoefa037d2017-02-19 12:57:33 +0200245};