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 as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js'; |
| 19 | import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js'; |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 20 | import {actionTypes as limitEditorActions} from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js'; |
| 21 | import getValue from 'nfvo-utils/getValue.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 22 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 23 | function baseUrl(licenseModelId, version) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 24 | const restPrefix = Configuration.get('restPrefix'); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 25 | const {id: versionId} = version; |
| 26 | return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | } |
| 28 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 29 | function fetchEntitlementPoolsList(licenseModelId, version) { |
| 30 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 31 | } |
| 32 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 33 | function postEntitlementPool(licenseModelId, entitlementPool, version) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 34 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 35 | name: entitlementPool.name, |
| 36 | description: entitlementPool.description, |
| 37 | thresholdValue: entitlementPool.thresholdValue, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 38 | thresholdUnits: getValue(entitlementPool.thresholdUnits), |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 39 | entitlementMetric: entitlementPool.entitlementMetric, |
| 40 | increments: entitlementPool.increments, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 41 | operationalScope: getValue(entitlementPool.operationalScope), |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 42 | time: entitlementPool.time, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 43 | startDate: entitlementPool.startDate, |
| 44 | expiryDate: entitlementPool.expiryDate |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 45 | }); |
| 46 | } |
| 47 | |
| 48 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 49 | function putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version) { |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 50 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 51 | return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPool.id}`, { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 52 | name: entitlementPool.name, |
| 53 | description: entitlementPool.description, |
| 54 | thresholdValue: entitlementPool.thresholdValue, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 55 | thresholdUnits: getValue(entitlementPool.thresholdUnits), |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 56 | entitlementMetric: entitlementPool.entitlementMetric, |
| 57 | increments: entitlementPool.increments, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 58 | operationalScope: getValue(entitlementPool.operationalScope), |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 59 | time: entitlementPool.time, |
Avi Ziv | b8e2faf | 2017-07-18 19:45:38 +0300 | [diff] [blame] | 60 | startDate: entitlementPool.startDate, |
| 61 | expiryDate: entitlementPool.expiryDate |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 62 | }); |
| 63 | } |
| 64 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 65 | function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) { |
| 66 | return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 67 | } |
| 68 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 69 | function fetchLimitsList(licenseModelId, entitlementPoolId, version) { |
| 70 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`); |
| 71 | } |
| 72 | |
| 73 | function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) { |
| 74 | return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limitId}`); |
| 75 | } |
| 76 | |
| 77 | function postLimit(licenseModelId, entitlementPoolId, version, limit) { |
| 78 | return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`, { |
| 79 | name: limit.name, |
| 80 | type: limit.type, |
| 81 | description: limit.description, |
| 82 | metric: limit.metric, |
| 83 | value: limit.value, |
| 84 | unit: limit.unit, |
| 85 | aggregationFunction: getValue(limit.aggregationFunction), |
| 86 | time: getValue(limit.time) |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | function putLimit(licenseModelId, entitlementPoolId, version, limit) { |
| 91 | |
| 92 | return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limit.id}`, { |
| 93 | name: limit.name, |
| 94 | type: limit.type, |
| 95 | description: limit.description, |
| 96 | metric: limit.metric, |
| 97 | value: limit.value, |
| 98 | unit: limit.unit, |
| 99 | aggregationFunction: getValue(limit.aggregationFunction), |
| 100 | time: getValue(limit.time) |
| 101 | }); |
| 102 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 103 | |
| 104 | export default { |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 105 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 106 | fetchEntitlementPoolsList(dispatch, {licenseModelId, version}) { |
| 107 | return fetchEntitlementPoolsList(licenseModelId, version).then(response => dispatch({ |
| 108 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED, |
| 109 | response |
| 110 | })); |
| 111 | }, |
| 112 | |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 113 | openEntitlementPoolsEditor(dispatch, {entitlementPool, licenseModelId, version} = {}) { |
| 114 | if (licenseModelId && version) { |
| 115 | this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool}); |
| 116 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 117 | dispatch({ |
| 118 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN, |
| 119 | entitlementPool |
| 120 | }); |
| 121 | }, |
| 122 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 123 | deleteEntitlementPool(dispatch, {licenseModelId, entitlementPoolId, version}) { |
| 124 | return deleteEntitlementPool(licenseModelId, entitlementPoolId, version).then(() => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 125 | dispatch({ |
| 126 | type: entitlementPoolsActionTypes.DELETE_ENTITLEMENT_POOL, |
| 127 | entitlementPoolId |
| 128 | }); |
| 129 | }); |
| 130 | }, |
| 131 | |
| 132 | entitlementPoolsEditorDataChanged(dispatch, {deltaData}) { |
| 133 | dispatch({ |
| 134 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED, |
| 135 | deltaData |
| 136 | }); |
| 137 | }, |
| 138 | |
| 139 | closeEntitlementPoolsEditor(dispatch) { |
| 140 | dispatch({ |
| 141 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE |
| 142 | }); |
| 143 | }, |
| 144 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 145 | saveEntitlementPool(dispatch, {licenseModelId, previousEntitlementPool, entitlementPool, version}) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 146 | if (previousEntitlementPool) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 147 | return putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version).then(() => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 148 | dispatch({ |
| 149 | type: entitlementPoolsActionTypes.EDIT_ENTITLEMENT_POOL, |
| 150 | entitlementPool |
| 151 | }); |
| 152 | }); |
| 153 | } |
| 154 | else { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 155 | return postEntitlementPool(licenseModelId, entitlementPool, version).then(response => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 156 | dispatch({ |
| 157 | type: entitlementPoolsActionTypes.ADD_ENTITLEMENT_POOL, |
| 158 | entitlementPool: { |
| 159 | ...entitlementPool, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 160 | referencingFeatureGroups: [], |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 161 | id: response.value |
| 162 | } |
| 163 | }); |
| 164 | }); |
| 165 | } |
| 166 | }, |
| 167 | |
| 168 | hideDeleteConfirm(dispatch) { |
| 169 | dispatch({ |
| 170 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM, |
| 171 | entitlementPoolToDelete: false |
| 172 | }); |
| 173 | }, |
| 174 | openDeleteEntitlementPoolConfirm(dispatch, {entitlementPool}) { |
| 175 | dispatch({ |
| 176 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM, |
| 177 | entitlementPoolToDelete: entitlementPool |
| 178 | }); |
| 179 | }, |
| 180 | |
| 181 | switchVersion(dispatch, {licenseModelId, version}) { |
| 182 | LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => { |
| 183 | this.fetchEntitlementPoolsList(dispatch, {licenseModelId, version}); |
| 184 | }); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame^] | 185 | }, |
| 186 | |
| 187 | |
| 188 | fetchLimits(dispatch, {licenseModelId, version, entitlementPool}) { |
| 189 | return fetchLimitsList(licenseModelId, entitlementPool.id, version). then (response => { |
| 190 | dispatch({ |
| 191 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.LIMITS_LIST_LOADED, |
| 192 | response |
| 193 | }); |
| 194 | }); |
| 195 | }, |
| 196 | |
| 197 | submitLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) { |
| 198 | const propmise = limit.id ? putLimit(licenseModelId,entitlementPool.id, version, limit) |
| 199 | : postLimit(licenseModelId,entitlementPool.id, version, limit); |
| 200 | return propmise.then(() => { |
| 201 | dispatch({ |
| 202 | type: limitEditorActions.CLOSE |
| 203 | }); |
| 204 | this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool}); |
| 205 | }); |
| 206 | }, |
| 207 | |
| 208 | deleteLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) { |
| 209 | return deleteLimit(licenseModelId,entitlementPool.id, version, limit.id).then(() => { |
| 210 | this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool}); |
| 211 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 212 | } |
| 213 | }; |