[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / LicenseModelActionHelper.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * 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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * 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.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import {actionTypes} from './LicenseModelConstants.js';
19 import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
20 import {actionsEnum as vcActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
21 import i18n from 'nfvo-utils/i18n/i18n.js';
22 import LicenseAgreementActionHelper from './licenseAgreement/LicenseAgreementActionHelper.js';
23 import FeatureGroupsActionHelper from './featureGroups/FeatureGroupsActionHelper.js';
24 import EntitlementPoolsActionHelper from './entitlementPools/EntitlementPoolsActionHelper.js';
25 import LicenseKeyGroupsActionHelper from './licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
26
27 function baseUrl() {
28         const restPrefix = Configuration.get('restPrefix');
29         return `${restPrefix}/v1.0/vendor-license-models/`;
30 }
31
32 function fetchLicenseModels() {
33         return RestAPIUtil.fetch(baseUrl());
34 }
35
36 function fetchFinalizedLicenseModels() {
37         return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Final`);
38 }
39
40 function fetchLicenseModelById(licenseModelId, version) {
41         const {id: versionId} = version;
42         return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`);
43 }
44
45 function putLicenseModelAction(id, action, version) {
46         const {id: versionId} = version;
47         return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}/actions`, {action: action});
48 }
49
50 function 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
59 function adjustMinorVersion(version, value) {
60         let ar = version.split('.');
61         return ar[0] + '.' + (parseInt(ar[1]) + value);
62 }
63
64 function adjustMajorVersion(version, value) {
65         let ar = version.split('.');
66         return (parseInt(ar[0]) + value) + '.0';
67 }
68
69 const 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}) {
89                 
90                 return fetchLicenseModelById(licenseModelId, version).then(response => {                
91                         dispatch({
92                                 type: actionTypes.LICENSE_MODEL_LOADED,
93                                 response: {...response, version}
94                         });
95                 });
96         },
97
98         addLicenseModel(dispatch, {licenseModel}){
99                 dispatch({
100                         type: actionTypes.ADD_LICENSE_MODEL,
101                         licenseModel
102                 });
103         },
104
105         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(() => {
116                         if(action === vcActionsEnum.SUBMIT){
117                                 dispatch({
118                                         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                                         }
125                                 });
126                         }
127
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                         });
164                 });
165         }
166
167 };
168
169 export default LicenseModelActionHelper;