blob: 186f1cbc7b50abd740131855d4630e4afcc5aa70 [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';
Michael Landoefa037d2017-02-19 12:57:33 +020026
27function baseUrl() {
28 const restPrefix = Configuration.get('restPrefix');
29 return `${restPrefix}/v1.0/vendor-license-models/`;
30}
31
32function fetchLicenseModels() {
33 return RestAPIUtil.fetch(baseUrl());
34}
35
36function fetchFinalizedLicenseModels() {
37 return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Final`);
38}
39
40function fetchLicenseModelById(licenseModelId, version) {
AviZi280f8012017-06-09 02:39:56 +030041 const {id: versionId} = version;
42 return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`);
Michael Landoefa037d2017-02-19 12:57:33 +020043}
44
AviZi280f8012017-06-09 02:39:56 +030045function putLicenseModelAction(id, action, version) {
46 const {id: versionId} = version;
47 return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}/actions`, {action: action});
48}
49
50function putLicenseModel(licenseModel) {
51 let {id, vendorName, description, iconRef, version: {id: versionId}} = licenseModel;
52 return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, {
53 vendorName,
54 description,
55 iconRef
56 });
57}
58
59function adjustMinorVersion(version, value) {
60 let ar = version.split('.');
61 return ar[0] + '.' + (parseInt(ar[1]) + value);
62}
63
64function adjustMajorVersion(version, value) {
65 let ar = version.split('.');
66 return (parseInt(ar[0]) + value) + '.0';
Michael Landoefa037d2017-02-19 12:57:33 +020067}
68
69const LicenseModelActionHelper = {
70
71 fetchLicenseModels(dispatch) {
72 return fetchLicenseModels().then(response => {
73 dispatch({
74 type: actionTypes.LICENSE_MODELS_LIST_LOADED,
75 response
76 });
77 });
78 },
79
80 fetchFinalizedLicenseModels(dispatch) {
81 return fetchFinalizedLicenseModels().then(response => dispatch({
82 type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED,
83 response
84 }));
85
86 },
87
88 fetchLicenseModelById(dispatch, {licenseModelId, version}) {
AviZi280f8012017-06-09 02:39:56 +030089
90 return fetchLicenseModelById(licenseModelId, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +020091 dispatch({
92 type: actionTypes.LICENSE_MODEL_LOADED,
AviZi280f8012017-06-09 02:39:56 +030093 response: {...response, version}
Michael Landoefa037d2017-02-19 12:57:33 +020094 });
95 });
96 },
97
98 addLicenseModel(dispatch, {licenseModel}){
99 dispatch({
100 type: actionTypes.ADD_LICENSE_MODEL,
101 licenseModel
102 });
103 },
104
AviZi280f8012017-06-09 02:39:56 +0300105 fetchLicenseModelItems(dispatch, {licenseModelId, version}) {
106 return Promise.all([
107 LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {licenseModelId, version}),
108 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version}),
109 EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version}),
110 LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version})
111 ]);
112 },
113
114 performVCAction(dispatch, {licenseModelId, action, version}) {
115 return putLicenseModelAction(licenseModelId, action, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +0200116 if(action === vcActionsEnum.SUBMIT){
117 dispatch({
AviZi280f8012017-06-09 02:39:56 +0300118 type: modalActionTypes.GLOBAL_MODAL_SUCCESS,
119 data: {
120 title: i18n('Submit Succeeded'),
121 msg: i18n('This license model successfully submitted'),
122 cancelButtonText: i18n('OK'),
123 timeout: 2000
124 }
Michael Landoefa037d2017-02-19 12:57:33 +0200125 });
126 }
AviZi280f8012017-06-09 02:39:56 +0300127
128 let newVersionId = version.id;
129 /*
130 TODO Temorary switch to change version label
131 */
132 switch(action) {
133 case vcActionsEnum.CHECK_OUT:
134 newVersionId = adjustMinorVersion(version.label, 1);
135 break;
136 case vcActionsEnum.UNDO_CHECK_OUT:
137 newVersionId = adjustMinorVersion(version.label, -1);
138 break;
139 case vcActionsEnum.SUBMIT:
140 newVersionId = adjustMajorVersion(version.label, 1);
141 }
142
143 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version:{id: newVersionId, label: newVersionId}});
144 return Promise.resolve({id: newVersionId, label: newVersionId});
145 });
146 },
147
148 switchVersion(dispatch, {licenseModelId, version}) {
149 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version: {id: version.id, label: version.label}}).then(() => {
150 LicenseModelActionHelper.fetchLicenseModelItems(dispatch, {licenseModelId, version});
151 });
152 },
153
154 saveLicenseModel(dispatch, {licenseModel}) {
155 return putLicenseModel(licenseModel).then(() => {
156 dispatch({
157 type: actionTypes.ADD_LICENSE_MODEL,
158 licenseModel
159 });
160 dispatch({
161 type: actionTypes.LICENSE_MODEL_LOADED,
162 response: licenseModel
163 });
Michael Landoefa037d2017-02-19 12:57:33 +0200164 });
165 }
AviZi280f8012017-06-09 02:39:56 +0300166
Michael Landoefa037d2017-02-19 12:57:33 +0200167};
168
169export default LicenseModelActionHelper;