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