blob: ef705d67e4c1b3f3a30fb5767d0e07c08532087a [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
svishnev8f133302018-08-06 23:08:39 +03002 * Copyright © 2016-2018 European Support Limited
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 */
svishnev8f133302018-08-06 23:08:39 +030016
Michael Landoefa037d2017-02-19 12:57:33 +020017import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
18import Configuration from 'sdc-app/config/Configuration.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020019import { actionTypes as entitlementPoolsActionTypes } from './EntitlementPoolsConstants.js';
20import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
21import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js';
talig8e9c0652017-12-20 14:30:43 +020022import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020023
AviZi280f8012017-06-09 02:39:56 +030024function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020025 const restPrefix = Configuration.get('restPrefix');
26 const { id: versionId } = version;
27 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/entitlement-pools`;
Michael Landoefa037d2017-02-19 12:57:33 +020028}
29
az2497644017c2017-08-10 17:49:40 +030030function fetchEntitlementPoolsList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020032}
33
az2497644017c2017-08-10 17:49:40 +030034function postEntitlementPool(licenseModelId, entitlementPool, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020035 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
36 name: entitlementPool.name,
37 description: entitlementPool.description,
38 thresholdValue: entitlementPool.thresholdValue,
39 thresholdUnits: getValue(entitlementPool.thresholdUnits),
40 increments: entitlementPool.increments,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020041 time: entitlementPool.time,
42 startDate: entitlementPool.startDate,
svishnev8f133302018-08-06 23:08:39 +030043 expiryDate: entitlementPool.expiryDate,
44 manufacturerReferenceNumber: entitlementPool.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020045 });
Michael Landoefa037d2017-02-19 12:57:33 +020046}
47
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048function putEntitlementPool(
49 licenseModelId,
50 previousEntitlementPool,
51 entitlementPool,
52 version
53) {
54 return RestAPIUtil.put(
55 `${baseUrl(licenseModelId, version)}/${entitlementPool.id}`,
56 {
57 name: entitlementPool.name,
58 description: entitlementPool.description,
59 thresholdValue: entitlementPool.thresholdValue,
60 thresholdUnits: getValue(entitlementPool.thresholdUnits),
61 increments: entitlementPool.increments,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020062 time: entitlementPool.time,
63 startDate: entitlementPool.startDate,
svishnev8f133302018-08-06 23:08:39 +030064 expiryDate: entitlementPool.expiryDate,
65 manufacturerReferenceNumber:
66 entitlementPool.manufacturerReferenceNumber
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020067 }
68 );
Michael Landoefa037d2017-02-19 12:57:33 +020069}
70
AviZi280f8012017-06-09 02:39:56 +030071function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020072 return RestAPIUtil.destroy(
73 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}`
74 );
Michael Landoefa037d2017-02-19 12:57:33 +020075}
76
az2497644017c2017-08-10 17:49:40 +030077function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020078 return RestAPIUtil.fetch(
79 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`
80 );
Avi Ziv61070c92017-07-26 17:37:57 +030081}
82
az2497644017c2017-08-10 17:49:40 +030083function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020084 return RestAPIUtil.destroy(
85 `${baseUrl(
86 licenseModelId,
87 version
88 )}/${entitlementPoolId}/limits/${limitId}`
89 );
Avi Ziv61070c92017-07-26 17:37:57 +030090}
91
az2497644017c2017-08-10 17:49:40 +030092function postLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020093 return RestAPIUtil.post(
94 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`,
95 {
96 name: limit.name,
97 type: limit.type,
98 description: limit.description,
99 metric: getStrValue(limit.metric),
100 value: limit.value,
101 unit: getStrValue(limit.unit),
102 aggregationFunction: getValue(limit.aggregationFunction),
103 time: getValue(limit.time)
104 }
105 );
Avi Ziv61070c92017-07-26 17:37:57 +0300106}
107
108function putLimit(licenseModelId, entitlementPoolId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200109 return RestAPIUtil.put(
110 `${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${
111 limit.id
112 }`,
113 {
114 name: limit.name,
115 type: limit.type,
116 description: limit.description,
117 metric: getStrValue(limit.metric),
118 value: limit.value,
119 unit: getStrValue(limit.unit),
120 aggregationFunction: getValue(limit.aggregationFunction),
121 time: getValue(limit.time)
122 }
123 );
Avi Ziv61070c92017-07-26 17:37:57 +0300124}
Michael Landoefa037d2017-02-19 12:57:33 +0200125
svishnev8f133302018-08-06 23:08:39 +0300126const EntitlementPoolsActionHelper = {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200127 fetchEntitlementPoolsList(dispatch, { licenseModelId, version }) {
128 return fetchEntitlementPoolsList(licenseModelId, version).then(
129 response =>
130 dispatch({
131 type:
132 entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
133 response
134 })
135 );
136 },
Avi Ziv61070c92017-07-26 17:37:57 +0300137
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200138 openEntitlementPoolsEditor(
139 dispatch,
140 { entitlementPool, licenseModelId, version } = {}
141 ) {
142 if (licenseModelId && version) {
143 this.fetchLimits(dispatch, {
144 licenseModelId,
145 version,
146 entitlementPool
147 });
148 }
149 dispatch({
150 type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
151 entitlementPool
152 });
153 },
Michael Landoefa037d2017-02-19 12:57:33 +0200154
svishnev8f133302018-08-06 23:08:39 +0300155 async deleteEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200156 dispatch,
157 { licenseModelId, entitlementPoolId, version }
158 ) {
svishnev8f133302018-08-06 23:08:39 +0300159 await deleteEntitlementPool(licenseModelId, entitlementPoolId, version);
160
161 await ItemsHelper.checkItemStatus(dispatch, {
162 itemId: licenseModelId,
163 versionId: version.id
164 });
165
166 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200167 licenseModelId,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200168 version
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200169 });
170 },
Michael Landoefa037d2017-02-19 12:57:33 +0200171
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200172 entitlementPoolsEditorDataChanged(dispatch, { deltaData }) {
173 dispatch({
174 type:
175 entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
176 deltaData
177 });
178 },
Michael Landoefa037d2017-02-19 12:57:33 +0200179
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200180 closeEntitlementPoolsEditor(dispatch) {
181 dispatch({
182 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
183 });
184 },
Michael Landoefa037d2017-02-19 12:57:33 +0200185
svishnev8f133302018-08-06 23:08:39 +0300186 async saveEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200187 dispatch,
188 { licenseModelId, previousEntitlementPool, entitlementPool, version }
189 ) {
190 if (previousEntitlementPool) {
svishnev8f133302018-08-06 23:08:39 +0300191 await putEntitlementPool(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200192 licenseModelId,
193 previousEntitlementPool,
194 entitlementPool,
195 version
svishnev8f133302018-08-06 23:08:39 +0300196 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200197 } else {
svishnev8f133302018-08-06 23:08:39 +0300198 await postEntitlementPool(licenseModelId, entitlementPool, version);
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200199 }
svishnev8f133302018-08-06 23:08:39 +0300200 await ItemsHelper.checkItemStatus(dispatch, {
201 itemId: licenseModelId,
202 versionId: version.id
203 });
204 await EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {
205 licenseModelId,
206 version
207 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200208 },
Michael Landoefa037d2017-02-19 12:57:33 +0200209
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200210 hideDeleteConfirm(dispatch) {
211 dispatch({
212 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
213 entitlementPoolToDelete: false
214 });
215 },
216 openDeleteEntitlementPoolConfirm(dispatch, { entitlementPool }) {
217 dispatch({
218 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
219 entitlementPoolToDelete: entitlementPool
220 });
221 },
Michael Landoefa037d2017-02-19 12:57:33 +0200222
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200223 fetchLimits(dispatch, { licenseModelId, version, entitlementPool }) {
224 return fetchLimitsList(
225 licenseModelId,
226 entitlementPool.id,
227 version
228 ).then(response => {
229 dispatch({
230 type:
231 entitlementPoolsActionTypes.entitlementPoolsEditor
232 .LIMITS_LIST_LOADED,
233 response
234 });
235 });
236 },
Michael Landoefa037d2017-02-19 12:57:33 +0200237
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200238 submitLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
239 const propmise = limit.id
240 ? putLimit(licenseModelId, entitlementPool.id, version, limit)
241 : postLimit(licenseModelId, entitlementPool.id, version, limit);
242 return propmise.then(() => {
243 dispatch({
244 type: limitEditorActions.CLOSE
245 });
246 this.fetchLimits(dispatch, {
247 licenseModelId,
248 version,
249 entitlementPool
250 });
251 return ItemsHelper.checkItemStatus(dispatch, {
252 itemId: licenseModelId,
253 versionId: version.id
254 });
255 });
256 },
Avi Ziv61070c92017-07-26 17:37:57 +0300257
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200258 deleteLimit(dispatch, { licenseModelId, version, entitlementPool, limit }) {
259 return deleteLimit(
260 licenseModelId,
261 entitlementPool.id,
262 version,
263 limit.id
264 ).then(() => {
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 }
Michael Landoefa037d2017-02-19 12:57:33 +0200276};
svishnev8f133302018-08-06 23:08:39 +0300277
278export default EntitlementPoolsActionHelper;