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'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 18 | import { actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js'; |
| 19 | import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js'; |
| 20 | import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js'; |
talig | 8e9c065 | 2017-12-20 14:30:43 +0200 | [diff] [blame] | 21 | import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.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) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 24 | const restPrefix = Configuration.get('restPrefix'); |
| 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 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 29 | function fetchEntitlementPoolsList(licenseModelId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 30 | return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 31 | } |
| 32 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 33 | function postEntitlementPool(licenseModelId, entitlementPool, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 34 | return RestAPIUtil.post(baseUrl(licenseModelId, version), { |
| 35 | name: entitlementPool.name, |
| 36 | description: entitlementPool.description, |
| 37 | thresholdValue: entitlementPool.thresholdValue, |
| 38 | thresholdUnits: getValue(entitlementPool.thresholdUnits), |
| 39 | increments: entitlementPool.increments, |
| 40 | operationalScope: getValue(entitlementPool.operationalScope), |
| 41 | time: entitlementPool.time, |
| 42 | startDate: entitlementPool.startDate, |
| 43 | expiryDate: entitlementPool.expiryDate |
| 44 | }); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 45 | } |
| 46 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 47 | function putEntitlementPool( |
| 48 | licenseModelId, |
| 49 | previousEntitlementPool, |
| 50 | entitlementPool, |
| 51 | version |
| 52 | ) { |
| 53 | return RestAPIUtil.put( |
| 54 | `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`, |
| 55 | { |
| 56 | name: entitlementPool.name, |
| 57 | description: entitlementPool.description, |
| 58 | thresholdValue: entitlementPool.thresholdValue, |
| 59 | thresholdUnits: getValue(entitlementPool.thresholdUnits), |
| 60 | increments: entitlementPool.increments, |
| 61 | operationalScope: getValue(entitlementPool.operationalScope), |
| 62 | time: entitlementPool.time, |
| 63 | startDate: entitlementPool.startDate, |
| 64 | expiryDate: entitlementPool.expiryDate |
| 65 | } |
| 66 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 67 | } |
| 68 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 69 | function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 70 | return RestAPIUtil.destroy( |
| 71 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}` |
| 72 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 73 | } |
| 74 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 75 | function fetchLimitsList(licenseModelId, entitlementPoolId, version) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 76 | return RestAPIUtil.fetch( |
| 77 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits` |
| 78 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 79 | } |
| 80 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 81 | function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 82 | return RestAPIUtil.destroy( |
| 83 | `${baseUrl( |
| 84 | licenseModelId, |
| 85 | version |
| 86 | )}/${entitlementPoolId}/limits/${limitId}` |
| 87 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 88 | } |
| 89 | |
az2497 | 644017c | 2017-08-10 17:49:40 +0300 | [diff] [blame] | 90 | function postLimit(licenseModelId, entitlementPoolId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 91 | return RestAPIUtil.post( |
| 92 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`, |
| 93 | { |
| 94 | name: limit.name, |
| 95 | type: limit.type, |
| 96 | description: limit.description, |
| 97 | metric: getStrValue(limit.metric), |
| 98 | value: limit.value, |
| 99 | unit: getStrValue(limit.unit), |
| 100 | aggregationFunction: getValue(limit.aggregationFunction), |
| 101 | time: getValue(limit.time) |
| 102 | } |
| 103 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | function putLimit(licenseModelId, entitlementPoolId, version, limit) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 107 | return RestAPIUtil.put( |
| 108 | `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${ |
| 109 | limit.id |
| 110 | }`, |
| 111 | { |
| 112 | name: limit.name, |
| 113 | type: limit.type, |
| 114 | description: limit.description, |
| 115 | metric: getStrValue(limit.metric), |
| 116 | value: limit.value, |
| 117 | unit: getStrValue(limit.unit), |
| 118 | aggregationFunction: getValue(limit.aggregationFunction), |
| 119 | time: getValue(limit.time) |
| 120 | } |
| 121 | ); |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 122 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 123 | |
| 124 | export default { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 125 | fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) { |
| 126 | return fetchEntitlementPoolsList(licenseModelId, version).then( |
| 127 | response => |
| 128 | dispatch({ |
| 129 | type: |
| 130 | entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED, |
| 131 | response |
| 132 | }) |
| 133 | ); |
| 134 | }, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 135 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 136 | openEntitlementPoolsEditor( |
| 137 | dispatch, |
| 138 | { entitlementPool, licenseModelId, version } = {} |
| 139 | ) { |
| 140 | if (licenseModelId && version) { |
| 141 | this.fetchLimits(dispatch, { |
| 142 | licenseModelId, |
| 143 | version, |
| 144 | entitlementPool |
| 145 | }); |
| 146 | } |
| 147 | dispatch({ |
| 148 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN, |
| 149 | entitlementPool |
| 150 | }); |
| 151 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 152 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 153 | deleteEntitlementPool( |
| 154 | dispatch, |
| 155 | { licenseModelId, entitlementPoolId, version } |
| 156 | ) { |
| 157 | return deleteEntitlementPool( |
| 158 | licenseModelId, |
| 159 | entitlementPoolId, |
| 160 | version |
| 161 | ).then(() => { |
| 162 | dispatch({ |
| 163 | type: entitlementPoolsActionTypes.DELETE_ENTITLEMENT_POOL, |
| 164 | entitlementPoolId |
| 165 | }); |
| 166 | return ItemsHelper.checkItemStatus(dispatch, { |
| 167 | itemId: licenseModelId, |
| 168 | versionId: version.id |
| 169 | }); |
| 170 | }); |
| 171 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 172 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 173 | entitlementPoolsEditorDataChanged(dispatch, { deltaData }) { |
| 174 | dispatch({ |
| 175 | type: |
| 176 | entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED, |
| 177 | deltaData |
| 178 | }); |
| 179 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 180 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 181 | closeEntitlementPoolsEditor(dispatch) { |
| 182 | dispatch({ |
| 183 | type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE |
| 184 | }); |
| 185 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 186 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 187 | saveEntitlementPool( |
| 188 | dispatch, |
| 189 | { licenseModelId, previousEntitlementPool, entitlementPool, version } |
| 190 | ) { |
| 191 | if (previousEntitlementPool) { |
| 192 | return putEntitlementPool( |
| 193 | licenseModelId, |
| 194 | previousEntitlementPool, |
| 195 | entitlementPool, |
| 196 | version |
| 197 | ).then(() => { |
| 198 | dispatch({ |
| 199 | type: entitlementPoolsActionTypes.EDIT_ENTITLEMENT_POOL, |
| 200 | entitlementPool |
| 201 | }); |
| 202 | return ItemsHelper.checkItemStatus(dispatch, { |
| 203 | itemId: licenseModelId, |
| 204 | versionId: version.id |
| 205 | }); |
| 206 | }); |
| 207 | } else { |
| 208 | return postEntitlementPool( |
| 209 | licenseModelId, |
| 210 | entitlementPool, |
| 211 | version |
| 212 | ).then(response => { |
| 213 | dispatch({ |
| 214 | type: entitlementPoolsActionTypes.ADD_ENTITLEMENT_POOL, |
| 215 | entitlementPool: { |
| 216 | ...entitlementPool, |
| 217 | referencingFeatureGroups: [], |
| 218 | id: response.value |
| 219 | } |
| 220 | }); |
| 221 | return ItemsHelper.checkItemStatus(dispatch, { |
| 222 | itemId: licenseModelId, |
| 223 | versionId: version.id |
| 224 | }); |
| 225 | }); |
| 226 | } |
| 227 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 228 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 229 | hideDeleteConfirm(dispatch) { |
| 230 | dispatch({ |
| 231 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM, |
| 232 | entitlementPoolToDelete: false |
| 233 | }); |
| 234 | }, |
| 235 | openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) { |
| 236 | dispatch({ |
| 237 | type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM, |
| 238 | entitlementPoolToDelete: entitlementPool |
| 239 | }); |
| 240 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 241 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 242 | fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) { |
| 243 | return fetchLimitsList( |
| 244 | licenseModelId, |
| 245 | entitlementPool.id, |
| 246 | version |
| 247 | ).then(response => { |
| 248 | dispatch({ |
| 249 | type: |
| 250 | entitlementPoolsActionTypes.entitlementPoolsEditor |
| 251 | .LIMITS_LIST_LOADED, |
| 252 | response |
| 253 | }); |
| 254 | }); |
| 255 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 256 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 257 | submitLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) { |
| 258 | const propmise = limit.id |
| 259 | ? putLimit(licenseModelId, entitlementPool.id, version, limit) |
| 260 | : postLimit(licenseModelId, entitlementPool.id, version, limit); |
| 261 | return propmise.then(() => { |
| 262 | dispatch({ |
| 263 | type: limitEditorActions.CLOSE |
| 264 | }); |
| 265 | this.fetchLimits(dispatch, { |
| 266 | licenseModelId, |
| 267 | version, |
| 268 | entitlementPool |
| 269 | }); |
| 270 | return ItemsHelper.checkItemStatus(dispatch, { |
| 271 | itemId: licenseModelId, |
| 272 | versionId: version.id |
| 273 | }); |
| 274 | }); |
| 275 | }, |
Avi Ziv | 61070c9 | 2017-07-26 17:37:57 +0300 | [diff] [blame] | 276 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 277 | deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) { |
| 278 | return deleteLimit( |
| 279 | licenseModelId, |
| 280 | entitlementPool.id, |
| 281 | version, |
| 282 | limit.id |
| 283 | ).then(() => { |
| 284 | this.fetchLimits(dispatch, { |
| 285 | licenseModelId, |
| 286 | version, |
| 287 | entitlementPool |
| 288 | }); |
| 289 | return ItemsHelper.checkItemStatus(dispatch, { |
| 290 | itemId: licenseModelId, |
| 291 | versionId: version.id |
| 292 | }); |
| 293 | }); |
| 294 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 295 | }; |