blob: cfff9f1fcd77a4ca021fc8dec392bf36f77e984f [file] [log] [blame]
svishnev091edfd2018-03-19 12:15:19 +02001/*
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
svishnev091edfd2018-03-19 12:15:19 +02007 *
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,
svishnev091edfd2018-03-19 12:15:19 +020012 * 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 } from './LicenseModelConstants.js';
19import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js';
20import { actionsEnum as vcActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020021import i18n from 'nfvo-utils/i18n/i18n.js';
AviZi280f8012017-06-09 02:39:56 +030022import LicenseAgreementActionHelper from './licenseAgreement/LicenseAgreementActionHelper.js';
23import FeatureGroupsActionHelper from './featureGroups/FeatureGroupsActionHelper.js';
24import EntitlementPoolsActionHelper from './entitlementPools/EntitlementPoolsActionHelper.js';
25import LicenseKeyGroupsActionHelper from './licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020026import { default as ItemsHelper } from 'sdc-app/common/helpers/ItemsHelper.js';
talig8e9c0652017-12-20 14:30:43 +020027import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020028import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
29import { CommitModalType } from 'nfvo-components/panel/versionController/components/CommitCommentModal.jsx';
talig8e9c0652017-12-20 14:30:43 +020030import versionPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031import { itemTypes } from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
32import { catalogItemStatuses } from 'sdc-app/onboarding/onboard/onboardingCatalog/OnboardingCatalogConstants.js';
33import { actionsEnum as VersionControllerActionsEnum } from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020034
35function baseUrl() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020036 const restPrefix = Configuration.get('restPrefix');
37 return `${restPrefix}/v1.0/vendor-license-models/`;
Michael Landoefa037d2017-02-19 12:57:33 +020038}
39
40function fetchLicenseModels() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020041 return RestAPIUtil.fetch(
42 `${baseUrl()}?versionFilter=${catalogItemStatuses.DRAFT}`
43 );
Michael Landoefa037d2017-02-19 12:57:33 +020044}
45
46function fetchFinalizedLicenseModels() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020047 return RestAPIUtil.fetch(
48 `${baseUrl()}?versionFilter=${catalogItemStatuses.CERTIFIED}`
49 );
Michael Landoefa037d2017-02-19 12:57:33 +020050}
svishnev091edfd2018-03-19 12:15:19 +020051function fetchArchivedLicenseModels() {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020052 return RestAPIUtil.fetch(
53 `${baseUrl()}?Status=${catalogItemStatuses.ARCHIVED}`
54 );
svishnev091edfd2018-03-19 12:15:19 +020055}
Michael Landoefa037d2017-02-19 12:57:33 +020056function fetchLicenseModelById(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020057 const { id: versionId } = version;
58 return RestAPIUtil.fetch(
59 `${baseUrl()}${licenseModelId}/versions/${versionId}`
60 );
Michael Landoefa037d2017-02-19 12:57:33 +020061}
62
AviZi280f8012017-06-09 02:39:56 +030063function putLicenseModel(licenseModel) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020064 let {
65 id,
66 vendorName,
67 description,
68 iconRef,
69 version: { id: versionId }
70 } = licenseModel;
71 return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, {
72 vendorName,
73 description,
74 iconRef
75 });
AviZi280f8012017-06-09 02:39:56 +030076}
77
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020078function putLicenseModelAction({ itemId, action, version }) {
79 const { id: versionId } = version;
80 return RestAPIUtil.put(
81 `${baseUrl()}${itemId}/versions/${versionId}/actions`,
82 { action: action }
83 );
Michael Landoefa037d2017-02-19 12:57:33 +020084}
85
86const LicenseModelActionHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020087 fetchLicenseModels(dispatch) {
88 return fetchLicenseModels().then(response => {
89 dispatch({
90 type: actionTypes.LICENSE_MODELS_LIST_LOADED,
91 response
92 });
93 });
94 },
Michael Landoefa037d2017-02-19 12:57:33 +020095
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020096 fetchFinalizedLicenseModels(dispatch) {
97 return fetchFinalizedLicenseModels().then(response =>
98 dispatch({
99 type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED,
100 response
101 })
102 );
103 },
Michael Landoefa037d2017-02-19 12:57:33 +0200104
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200105 fetchArchivedLicenseModels(dispatch) {
106 return fetchArchivedLicenseModels().then(response =>
107 dispatch({
108 type: actionTypes.ARCHIVED_LICENSE_MODELS_LIST_LOADED,
109 response
110 })
111 );
112 },
Michael Landoefa037d2017-02-19 12:57:33 +0200113
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200114 fetchLicenseModelById(dispatch, { licenseModelId, version }) {
115 return fetchLicenseModelById(licenseModelId, version).then(response => {
116 dispatch({
117 type: actionTypes.LICENSE_MODEL_LOADED,
118 response: { ...response, version }
119 });
120 });
121 },
Michael Landoefa037d2017-02-19 12:57:33 +0200122
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200123 fetchLicenseModelItems(dispatch, { licenseModelId, version }) {
124 return Promise.all([
125 LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {
126 licenseModelId,
127 version
128 }),
129 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
130 licenseModelId,
131 version
132 }),
133 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
134 licenseModelId,
135 version
136 }),
137 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {
138 licenseModelId,
139 version
140 })
141 ]);
142 },
svishnev091edfd2018-03-19 12:15:19 +0200143
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200144 manageSubmitAction(dispatch, { licenseModelId, version, isDirty }) {
145 if (isDirty) {
146 const onCommit = comment => {
147 return this.performVCAction(dispatch, {
148 licenseModelId,
149 action: vcActionsEnum.COMMIT,
150 version,
151 comment
152 }).then(() => {
153 return this.performSubmitAction(dispatch, {
154 licenseModelId,
155 version
156 });
157 });
158 };
159 dispatch({
160 type: modalActionTypes.GLOBAL_MODAL_SHOW,
161 data: {
162 modalComponentName: modalContentMapper.COMMIT_COMMENT,
163 modalComponentProps: {
164 onCommit,
165 type: CommitModalType.COMMIT_SUBMIT
166 },
167 title: i18n('Commit & Submit')
168 }
169 });
170 return Promise.reject();
171 }
172 return this.performSubmitAction(dispatch, { licenseModelId, version });
173 },
svishnev091edfd2018-03-19 12:15:19 +0200174
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200175 performSubmitAction(dispatch, { licenseModelId, version }) {
176 return putLicenseModelAction({
177 itemId: licenseModelId,
178 action: vcActionsEnum.SUBMIT,
179 version
180 }).then(() => {
181 return ItemsHelper.checkItemStatus(dispatch, {
182 itemId: licenseModelId,
183 versionId: version.id
184 }).then(updatedVersion => {
185 dispatch({
186 type: modalActionTypes.GLOBAL_MODAL_SUCCESS,
187 data: {
188 title: i18n('Submit Succeeded'),
189 msg: i18n('This license model successfully submitted'),
190 cancelButtonText: i18n('OK'),
191 timeout: 2000
192 }
193 });
194 versionPageActionHelper.fetchVersions(dispatch, {
195 itemType: itemTypes.LICENSE_MODEL,
196 itemId: licenseModelId
197 });
198 return Promise.resolve(updatedVersion);
199 });
200 });
201 },
talig8e9c0652017-12-20 14:30:43 +0200202
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200203 performVCAction(dispatch, { licenseModelId, action, version, comment }) {
204 return MergeEditorActionHelper.analyzeSyncResult(dispatch, {
205 itemId: licenseModelId,
206 version
207 }).then(({ inMerge, isDirty, updatedVersion }) => {
208 if (
209 (updatedVersion.status === catalogItemStatuses.CERTIFIED ||
210 updatedVersion.archivedStatus ===
211 catalogItemStatuses.ARCHIVED) &&
212 (action === VersionControllerActionsEnum.COMMIT ||
213 action === VersionControllerActionsEnum.SYNC)
214 ) {
215 versionPageActionHelper.fetchVersions(dispatch, {
216 itemType: itemTypes.LICENSE_MODEL,
217 itemId: licenseModelId
218 });
219 const msg =
220 updatedVersion.archivedStatus ===
221 catalogItemStatuses.ARCHIVED
222 ? i18n('Item was Archived')
223 : i18n('Item version already Certified');
224 dispatch({
225 type: modalActionTypes.GLOBAL_MODAL_WARNING,
226 data: {
227 title: i18n('Commit error'),
228 msg,
229 cancelButtonText: i18n('Cancel')
230 }
231 });
232 return Promise.resolve(updatedVersion);
233 }
234 if (!inMerge) {
235 if (action === vcActionsEnum.SUBMIT) {
236 return this.manageSubmitAction(dispatch, {
237 licenseModelId,
238 version,
239 isDirty
240 });
241 } else {
242 return ItemsHelper.performVCAction({
243 itemId: licenseModelId,
244 action,
245 version,
246 comment
247 }).then(() => {
248 versionPageActionHelper.fetchVersions(dispatch, {
249 itemType: itemTypes.LICENSE_MODEL,
250 itemId: licenseModelId
251 });
252 if (action === vcActionsEnum.SYNC) {
253 return MergeEditorActionHelper.analyzeSyncResult(
254 dispatch,
255 { itemId: licenseModelId, version }
256 ).then(({ updatedVersion }) => {
257 return Promise.resolve(updatedVersion);
258 });
259 } else {
260 return ItemsHelper.checkItemStatus(dispatch, {
261 itemId: licenseModelId,
262 versionId: version.id
263 });
264 }
265 });
266 }
267 }
268 });
269 },
Michael Landoefa037d2017-02-19 12:57:33 +0200270
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200271 saveLicenseModel(dispatch, { licenseModel }) {
272 return putLicenseModel(licenseModel).then(() => {
273 dispatch({
274 type: actionTypes.LICENSE_MODEL_LOADED,
275 response: licenseModel
276 });
277 const { id, version: { id: versionId } } = licenseModel;
278 return ItemsHelper.checkItemStatus(dispatch, {
279 itemId: id,
280 versionId
281 }).then(updatedVersion => {
282 if (updatedVersion.status !== licenseModel.version.status) {
283 versionPageActionHelper.fetchVersions(dispatch, {
284 itemType: itemTypes.LICENSE_MODEL,
285 itemId: licenseModel.id
286 });
287 }
288 });
289 });
290 }
Michael Landoefa037d2017-02-19 12:57:33 +0200291};
292
293export default LicenseModelActionHelper;