blob: 788528fd756ab6b0879c78d109dbcb3262d7ff4a [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';
az2497644017c2017-08-10 17:49:40 +030026import OnboardingActionHelper from 'sdc-app/onboarding/OnboardingActionHelper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020027
28function baseUrl() {
29 const restPrefix = Configuration.get('restPrefix');
30 return `${restPrefix}/v1.0/vendor-license-models/`;
31}
32
33function fetchLicenseModels() {
34 return RestAPIUtil.fetch(baseUrl());
35}
36
37function fetchFinalizedLicenseModels() {
38 return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Final`);
39}
40
41function fetchLicenseModelById(licenseModelId, version) {
AviZi280f8012017-06-09 02:39:56 +030042 const {id: versionId} = version;
43 return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`);
Michael Landoefa037d2017-02-19 12:57:33 +020044}
45
AviZi280f8012017-06-09 02:39:56 +030046function putLicenseModelAction(id, action, version) {
47 const {id: versionId} = version;
48 return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}/actions`, {action: action});
49}
50
51function putLicenseModel(licenseModel) {
52 let {id, vendorName, description, iconRef, version: {id: versionId}} = licenseModel;
53 return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, {
54 vendorName,
55 description,
56 iconRef
57 });
58}
59
60function adjustMinorVersion(version, value) {
61 let ar = version.split('.');
62 return ar[0] + '.' + (parseInt(ar[1]) + value);
63}
64
65function adjustMajorVersion(version, value) {
66 let ar = version.split('.');
67 return (parseInt(ar[0]) + value) + '.0';
Michael Landoefa037d2017-02-19 12:57:33 +020068}
69
70const LicenseModelActionHelper = {
71
72 fetchLicenseModels(dispatch) {
73 return fetchLicenseModels().then(response => {
74 dispatch({
75 type: actionTypes.LICENSE_MODELS_LIST_LOADED,
76 response
77 });
78 });
79 },
80
81 fetchFinalizedLicenseModels(dispatch) {
82 return fetchFinalizedLicenseModels().then(response => dispatch({
83 type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED,
84 response
85 }));
86
87 },
88
89 fetchLicenseModelById(dispatch, {licenseModelId, version}) {
AviZi280f8012017-06-09 02:39:56 +030090
91 return fetchLicenseModelById(licenseModelId, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +020092 dispatch({
93 type: actionTypes.LICENSE_MODEL_LOADED,
AviZi280f8012017-06-09 02:39:56 +030094 response: {...response, version}
Michael Landoefa037d2017-02-19 12:57:33 +020095 });
96 });
97 },
98
99 addLicenseModel(dispatch, {licenseModel}){
100 dispatch({
101 type: actionTypes.ADD_LICENSE_MODEL,
102 licenseModel
103 });
104 },
105
AviZi280f8012017-06-09 02:39:56 +0300106 fetchLicenseModelItems(dispatch, {licenseModelId, version}) {
107 return Promise.all([
108 LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {licenseModelId, version}),
109 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version}),
110 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version}),
111 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version})
112 ]);
113 },
114
115 performVCAction(dispatch, {licenseModelId, action, version}) {
116 return putLicenseModelAction(licenseModelId, action, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +0200117 if(action === vcActionsEnum.SUBMIT){
118 dispatch({
AviZi280f8012017-06-09 02:39:56 +0300119 type: modalActionTypes.GLOBAL_MODAL_SUCCESS,
120 data: {
121 title: i18n('Submit Succeeded'),
122 msg: i18n('This license model successfully submitted'),
123 cancelButtonText: i18n('OK'),
124 timeout: 2000
125 }
Michael Landoefa037d2017-02-19 12:57:33 +0200126 });
127 }
AviZi280f8012017-06-09 02:39:56 +0300128
129 let newVersionId = version.id;
130 /*
131 TODO Temorary switch to change version label
132 */
133 switch(action) {
134 case vcActionsEnum.CHECK_OUT:
135 newVersionId = adjustMinorVersion(version.label, 1);
136 break;
137 case vcActionsEnum.UNDO_CHECK_OUT:
138 newVersionId = adjustMinorVersion(version.label, -1);
139 break;
140 case vcActionsEnum.SUBMIT:
141 newVersionId = adjustMajorVersion(version.label, 1);
142 }
143
az2497644017c2017-08-10 17:49:40 +0300144 OnboardingActionHelper.updateCurrentScreenVersion(dispatch, {label: newVersionId, id: newVersionId});
145
AviZi280f8012017-06-09 02:39:56 +0300146 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version:{id: newVersionId, label: newVersionId}});
147 return Promise.resolve({id: newVersionId, label: newVersionId});
148 });
149 },
150
151 switchVersion(dispatch, {licenseModelId, version}) {
152 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version: {id: version.id, label: version.label}}).then(() => {
153 LicenseModelActionHelper.fetchLicenseModelItems(dispatch, {licenseModelId, version});
154 });
155 },
156
157 saveLicenseModel(dispatch, {licenseModel}) {
158 return putLicenseModel(licenseModel).then(() => {
159 dispatch({
160 type: actionTypes.ADD_LICENSE_MODEL,
161 licenseModel
162 });
163 dispatch({
164 type: actionTypes.LICENSE_MODEL_LOADED,
165 response: licenseModel
166 });
Michael Landoefa037d2017-02-19 12:57:33 +0200167 });
168 }
AviZi280f8012017-06-09 02:39:56 +0300169
Michael Landoefa037d2017-02-19 12:57:33 +0200170};
171
172export default LicenseModelActionHelper;