blob: ff264b3003d05d50d47595cd92839b41997614da [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';
18import {actionTypes} from './LicenseModelConstants.js';
AviZi280f8012017-06-09 02:39:56 +030019import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020020import {actionsEnum as vcActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
21import 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';
talig8e9c0652017-12-20 14:30:43 +020026import ItemsHelper from '../../common/helpers/ItemsHelper.js';
27import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js';
28import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js';
29import {CommitModalType} from 'nfvo-components/panel/versionController/components/CommitCommentModal.jsx';
30import versionPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
31import {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() {
36 const restPrefix = Configuration.get('restPrefix');
37 return `${restPrefix}/v1.0/vendor-license-models/`;
38}
39
40function fetchLicenseModels() {
talig8e9c0652017-12-20 14:30:43 +020041 return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Draft`);
Michael Landoefa037d2017-02-19 12:57:33 +020042}
43
44function fetchFinalizedLicenseModels() {
talig8e9c0652017-12-20 14:30:43 +020045 return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Certified`);
Michael Landoefa037d2017-02-19 12:57:33 +020046}
47
48function fetchLicenseModelById(licenseModelId, version) {
AviZi280f8012017-06-09 02:39:56 +030049 const {id: versionId} = version;
50 return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`);
Michael Landoefa037d2017-02-19 12:57:33 +020051}
52
AviZi280f8012017-06-09 02:39:56 +030053function putLicenseModel(licenseModel) {
54 let {id, vendorName, description, iconRef, version: {id: versionId}} = licenseModel;
55 return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, {
56 vendorName,
57 description,
58 iconRef
59 });
60}
61
talig8e9c0652017-12-20 14:30:43 +020062function putLicenseModelAction({itemId, action, version}) {
63 const {id: versionId} = version;
64 return RestAPIUtil.put(`${baseUrl()}${itemId}/versions/${versionId}/actions`, {action: action});
Michael Landoefa037d2017-02-19 12:57:33 +020065}
66
67const LicenseModelActionHelper = {
68
69 fetchLicenseModels(dispatch) {
70 return fetchLicenseModels().then(response => {
71 dispatch({
72 type: actionTypes.LICENSE_MODELS_LIST_LOADED,
73 response
74 });
75 });
76 },
77
78 fetchFinalizedLicenseModels(dispatch) {
79 return fetchFinalizedLicenseModels().then(response => dispatch({
80 type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED,
81 response
82 }));
83
84 },
85
86 fetchLicenseModelById(dispatch, {licenseModelId, version}) {
talig8e9c0652017-12-20 14:30:43 +020087
88 return fetchLicenseModelById(licenseModelId, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +020089 dispatch({
90 type: actionTypes.LICENSE_MODEL_LOADED,
AviZi280f8012017-06-09 02:39:56 +030091 response: {...response, version}
Michael Landoefa037d2017-02-19 12:57:33 +020092 });
93 });
94 },
95
AviZi280f8012017-06-09 02:39:56 +030096 fetchLicenseModelItems(dispatch, {licenseModelId, version}) {
97 return Promise.all([
98 LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {licenseModelId, version}),
99 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version}),
100 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version}),
101 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version})
102 ]);
103 },
104
talig8e9c0652017-12-20 14:30:43 +0200105 manageSubmitAction(dispatch, {licenseModelId, version, isDirty}) {
106 if(isDirty) {
107 const onCommit = comment => {
108 return this.performVCAction(dispatch, {licenseModelId, action: vcActionsEnum.COMMIT, version, comment}).then(() => {
109 return this.performSubmitAction(dispatch, {licenseModelId, version});
110 });
111 };
112 dispatch({
113 type: modalActionTypes.GLOBAL_MODAL_SHOW,
114 data: {
115 modalComponentName: modalContentMapper.COMMIT_COMMENT,
116 modalComponentProps: {
117 onCommit,
118 type: CommitModalType.COMMIT_SUBMIT
119 },
120 title: i18n('Commit & Submit')
121 }
122 });
123 return Promise.reject();
124 }
125 return this.performSubmitAction(dispatch, {licenseModelId, version});
126 },
127
128 performSubmitAction(dispatch, {licenseModelId, version}) {
129 return putLicenseModelAction({itemId: licenseModelId, action: vcActionsEnum.SUBMIT, version}).then(() => {
130 return ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}).then(updatedVersion => {
Michael Landoefa037d2017-02-19 12:57:33 +0200131 dispatch({
AviZi280f8012017-06-09 02:39:56 +0300132 type: modalActionTypes.GLOBAL_MODAL_SUCCESS,
133 data: {
talig8e9c0652017-12-20 14:30:43 +0200134 title: i18n('Submit Succeeded'),
AviZi280f8012017-06-09 02:39:56 +0300135 msg: i18n('This license model successfully submitted'),
talig8e9c0652017-12-20 14:30:43 +0200136 cancelButtonText: i18n('OK'),
AviZi280f8012017-06-09 02:39:56 +0300137 timeout: 2000
138 }
Michael Landoefa037d2017-02-19 12:57:33 +0200139 });
talig8e9c0652017-12-20 14:30:43 +0200140 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
141 return Promise.resolve(updatedVersion);
142 });
AviZi280f8012017-06-09 02:39:56 +0300143 });
144 },
145
talig8e9c0652017-12-20 14:30:43 +0200146 performVCAction(dispatch, {licenseModelId, action, version, comment}) {
147 return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({inMerge, isDirty, updatedVersion}) => {
148 if (updatedVersion.status === catalogItemStatuses.CERTIFIED &&
149 (action === VersionControllerActionsEnum.COMMIT || action === VersionControllerActionsEnum.SYNC)) {
150 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
151 dispatch({
152 type: modalActionTypes.GLOBAL_MODAL_WARNING,
153 data: {
154 title: i18n('Commit error'),
155 msg: i18n('Item version already Certified'),
156 cancelButtonText: i18n('Cancel')
157 }
158 });
159 return Promise.resolve(updatedVersion);
160 }
161 if (!inMerge) {
162 if(action === vcActionsEnum.SUBMIT) {
163 return this.manageSubmitAction(dispatch, {licenseModelId, version, isDirty});
164 }
165 else {
166 return ItemsHelper.performVCAction({itemId: licenseModelId, action, version, comment}).then(() => {
167 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
168 if (action === vcActionsEnum.SYNC) {
169 return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({updatedVersion}) => {
170 return Promise.resolve(updatedVersion);
171 });
172 } else {
173 return ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
174 }
175 });
176 }
177 }
AviZi280f8012017-06-09 02:39:56 +0300178 });
179 },
180
181 saveLicenseModel(dispatch, {licenseModel}) {
182 return putLicenseModel(licenseModel).then(() => {
183 dispatch({
AviZi280f8012017-06-09 02:39:56 +0300184 type: actionTypes.LICENSE_MODEL_LOADED,
185 response: licenseModel
186 });
talig8e9c0652017-12-20 14:30:43 +0200187 const {id, version: {id: versionId}} = licenseModel;
188 return ItemsHelper.checkItemStatus(dispatch, {itemId: id, versionId}).then(updatedVersion => {
189 if (updatedVersion.status !== licenseModel.version.status) {
190 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModel.id});
191 }
192 });
Michael Landoefa037d2017-02-19 12:57:33 +0200193 });
194 }
AviZi280f8012017-06-09 02:39:56 +0300195
Michael Landoefa037d2017-02-19 12:57:33 +0200196};
197
198export default LicenseModelActionHelper;