blob: 181fc11c9d78a131ab6b8bef42607ea19e0b3fad [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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 Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17import Configuration from 'sdc-app/config/Configuration.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js';
19import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
20import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js';
talig8e9c0652017-12-20 14:30:43 +020021import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020022
AviZi280f8012017-06-09 02:39:56 +030023function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020024 const restPrefix = Configuration.get('restPrefix');
25 const { id: versionId } = version;
26 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`;
Michael Landoefa037d2017-02-19 12:57:33 +020027}
28
az2497644017c2017-08-10 17:49:40 +030029function fetchEntitlementPoolsList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020030 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020031}
32
az2497644017c2017-08-10 17:49:40 +030033function postEntitlementPool(licenseModelId, entitlementPool, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020034 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 Landoefa037d2017-02-19 12:57:33 +020045}
46
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020047function 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 Landoefa037d2017-02-19 12:57:33 +020067}
68
AviZi280f8012017-06-09 02:39:56 +030069function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020070 return RestAPIUtil.destroy(
71 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
72 );
Michael Landoefa037d2017-02-19 12:57:33 +020073}
74
az2497644017c2017-08-10 17:49:40 +030075function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020076 return RestAPIUtil.fetch(
77 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
78 );
Avi Ziv61070c92017-07-26 17:37:57 +030079}
80
az2497644017c2017-08-10 17:49:40 +030081function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020082 return RestAPIUtil.destroy(
83 `${baseUrl(
84 licenseModelId,
85 version
86 )}/${entitlementPoolId}/limits/${limitId}`
87 );
Avi Ziv61070c92017-07-26 17:37:57 +030088}
89
az2497644017c2017-08-10 17:49:40 +030090function postLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020091 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 Ziv61070c92017-07-26 17:37:57 +0300104}
105
106function putLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200107 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 Ziv61070c92017-07-26 17:37:57 +0300122}
Michael Landoefa037d2017-02-19 12:57:33 +0200123
124export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200125 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 Ziv61070c92017-07-26 17:37:57 +0300135
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200136 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 Landoefa037d2017-02-19 12:57:33 +0200152
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200153 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 Landoefa037d2017-02-19 12:57:33 +0200172
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200173 entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
174 dispatch({
175 type:
176 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
177 deltaData
178 });
179 },
Michael Landoefa037d2017-02-19 12:57:33 +0200180
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200181 closeEntitlementPoolsEditor(dispatch) {
182 dispatch({
183 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
184 });
185 },
Michael Landoefa037d2017-02-19 12:57:33 +0200186
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200187 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 Landoefa037d2017-02-19 12:57:33 +0200228
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200229 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 Landoefa037d2017-02-19 12:57:33 +0200241
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200242 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 Landoefa037d2017-02-19 12:57:33 +0200256
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200257 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 Ziv61070c92017-07-26 17:37:57 +0300276
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200277 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 Landoefa037d2017-02-19 12:57:33 +0200295};