blob: 51f353f7d9cd569f815fedd45bc183eac350c65b [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,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020040 time: entitlementPool.time,
41 startDate: entitlementPool.startDate,
42 expiryDate: entitlementPool.expiryDate
43 });
Michael Landoefa037d2017-02-19 12:57:33 +020044}
45
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020046function 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 Keidar7fdf7332018-03-20 14:45:40 +020060 time: entitlementPool.time,
61 startDate: entitlementPool.startDate,
62 expiryDate: entitlementPool.expiryDate
63 }
64 );
Michael Landoefa037d2017-02-19 12:57:33 +020065}
66
AviZi280f8012017-06-09 02:39:56 +030067function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020068 return RestAPIUtil.destroy(
69 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
70 );
Michael Landoefa037d2017-02-19 12:57:33 +020071}
72
az2497644017c2017-08-10 17:49:40 +030073function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020074 return RestAPIUtil.fetch(
75 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
76 );
Avi Ziv61070c92017-07-26 17:37:57 +030077}
78
az2497644017c2017-08-10 17:49:40 +030079function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020080 return RestAPIUtil.destroy(
81 `${baseUrl(
82 licenseModelId,
83 version
84 )}/${entitlementPoolId}/limits/${limitId}`
85 );
Avi Ziv61070c92017-07-26 17:37:57 +030086}
87
az2497644017c2017-08-10 17:49:40 +030088function postLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020089 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 Ziv61070c92017-07-26 17:37:57 +0300102}
103
104function putLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200105 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 Ziv61070c92017-07-26 17:37:57 +0300120}
Michael Landoefa037d2017-02-19 12:57:33 +0200121
122export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200123 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 Ziv61070c92017-07-26 17:37:57 +0300133
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200134 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 Landoefa037d2017-02-19 12:57:33 +0200150
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200151 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 Landoefa037d2017-02-19 12:57:33 +0200170
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200171 entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
172 dispatch({
173 type:
174 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
175 deltaData
176 });
177 },
Michael Landoefa037d2017-02-19 12:57:33 +0200178
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200179 closeEntitlementPoolsEditor(dispatch) {
180 dispatch({
181 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
182 });
183 },
Michael Landoefa037d2017-02-19 12:57:33 +0200184
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200185 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 Landoefa037d2017-02-19 12:57:33 +0200226
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200227 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 Landoefa037d2017-02-19 12:57:33 +0200239
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200240 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 Landoefa037d2017-02-19 12:57:33 +0200254
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200255 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 Ziv61070c92017-07-26 17:37:57 +0300274
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200275 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 Landoefa037d2017-02-19 12:57:33 +0200293};