AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
| 17 | import Configuration from 'sdc-app/config/Configuration.js'; |
| 18 | import {actionTypes} from './LicenseModelConstants.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 19 | import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 20 | import {actionsEnum as vcActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js'; |
| 21 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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'; |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 26 | import OnboardingActionHelper from 'sdc-app/onboarding/OnboardingActionHelper.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | |
| 28 | function baseUrl() { |
| 29 | const restPrefix = Configuration.get('restPrefix'); |
| 30 | return `${restPrefix}/v1.0/vendor-license-models/`; |
| 31 | } |
| 32 | |
| 33 | function fetchLicenseModels() { |
| 34 | return RestAPIUtil.fetch(baseUrl()); |
| 35 | } |
| 36 | |
| 37 | function fetchFinalizedLicenseModels() { |
| 38 | return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Final`); |
| 39 | } |
| 40 | |
| 41 | function fetchLicenseModelById(licenseModelId, version) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 42 | const {id: versionId} = version; |
| 43 | return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 44 | } |
| 45 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 46 | function putLicenseModelAction(id, action, version) { |
| 47 | const {id: versionId} = version; |
| 48 | return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}/actions`, {action: action}); |
| 49 | } |
| 50 | |
| 51 | function 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 | |
| 60 | function adjustMinorVersion(version, value) { |
| 61 | let ar = version.split('.'); |
| 62 | return ar[0] + '.' + (parseInt(ar[1]) + value); |
| 63 | } |
| 64 | |
| 65 | function adjustMajorVersion(version, value) { |
| 66 | let ar = version.split('.'); |
| 67 | return (parseInt(ar[0]) + value) + '.0'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | const 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}) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 90 | |
| 91 | return fetchLicenseModelById(licenseModelId, version).then(response => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 92 | dispatch({ |
| 93 | type: actionTypes.LICENSE_MODEL_LOADED, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 94 | response: {...response, version} |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | }); |
| 96 | }); |
| 97 | }, |
| 98 | |
| 99 | addLicenseModel(dispatch, {licenseModel}){ |
| 100 | dispatch({ |
| 101 | type: actionTypes.ADD_LICENSE_MODEL, |
| 102 | licenseModel |
| 103 | }); |
| 104 | }, |
| 105 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 106 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 117 | if(action === vcActionsEnum.SUBMIT){ |
| 118 | dispatch({ |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 119 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 126 | }); |
| 127 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 128 | |
| 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 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 144 | OnboardingActionHelper.updateCurrentScreenVersion(dispatch, {label: newVersionId, id: newVersionId}); |
| 145 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 146 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 167 | }); |
| 168 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 169 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 170 | }; |
| 171 | |
| 172 | export default LicenseModelActionHelper; |