blob: 39af2054c2d567383be8a9d1d0c70f6ecfc306e4 [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 licenseKeyGroupsConstants } from './LicenseKeyGroupsConstants.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}/license-key-groups`;
Michael Landoefa037d2017-02-19 12:57:33 +020027}
28
29function fetchLicenseKeyGroupsList(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
AviZi280f8012017-06-09 02:39:56 +030033function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020034 return RestAPIUtil.destroy(
35 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`
36 );
Michael Landoefa037d2017-02-19 12:57:33 +020037}
38
AviZi280f8012017-06-09 02:39:56 +030039function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020040 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
41 name: licenseKeyGroup.name,
42 description: licenseKeyGroup.description,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020043 type: licenseKeyGroup.type,
44 increments: licenseKeyGroup.increments,
45 thresholdValue: licenseKeyGroup.thresholdValue,
46 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
47 startDate: licenseKeyGroup.startDate,
48 expiryDate: licenseKeyGroup.expiryDate
49 });
Michael Landoefa037d2017-02-19 12:57:33 +020050}
51
AviZi280f8012017-06-09 02:39:56 +030052function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020053 return RestAPIUtil.put(
54 `${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`,
55 {
56 name: licenseKeyGroup.name,
57 description: licenseKeyGroup.description,
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020058 type: licenseKeyGroup.type,
59 increments: licenseKeyGroup.increments,
60 thresholdValue: licenseKeyGroup.thresholdValue,
61 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
62 startDate: licenseKeyGroup.startDate,
63 expiryDate: licenseKeyGroup.expiryDate
64 }
65 );
Michael Landoefa037d2017-02-19 12:57:33 +020066}
67
Avi Ziv61070c92017-07-26 17:37:57 +030068function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020069 return RestAPIUtil.fetch(
70 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`
71 );
Avi Ziv61070c92017-07-26 17:37:57 +030072}
73
74function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020075 return RestAPIUtil.destroy(
76 `${baseUrl(
77 licenseModelId,
78 version
79 )}/${licenseKeyGroupId}/limits/${limitId}`
80 );
Avi Ziv61070c92017-07-26 17:37:57 +030081}
82
83function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020084 return RestAPIUtil.post(
85 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`,
86 {
87 name: limit.name,
88 type: limit.type,
89 description: limit.description,
90 metric: getStrValue(limit.metric),
91 value: limit.value,
92 unit: getStrValue(limit.unit),
93 aggregationFunction: getValue(limit.aggregationFunction),
94 time: getValue(limit.time)
95 }
96 );
Avi Ziv61070c92017-07-26 17:37:57 +030097}
98
99function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200100 return RestAPIUtil.put(
101 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${
102 limit.id
103 }`,
104 {
105 name: limit.name,
106 type: limit.type,
107 description: limit.description,
108 metric: getStrValue(limit.metric),
109 value: limit.value,
110 unit: getStrValue(limit.unit),
111 aggregationFunction: getValue(limit.aggregationFunction),
112 time: getValue(limit.time)
113 }
114 );
Avi Ziv61070c92017-07-26 17:37:57 +0300115}
Michael Landoefa037d2017-02-19 12:57:33 +0200116
117export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200118 fetchLicenseKeyGroupsList(dispatch, { licenseModelId, version }) {
119 return fetchLicenseKeyGroupsList(licenseModelId, version).then(
120 response =>
121 dispatch({
122 type:
123 licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
124 response
125 })
126 );
127 },
Michael Landoefa037d2017-02-19 12:57:33 +0200128
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200129 openLicenseKeyGroupsEditor(
130 dispatch,
131 { licenseKeyGroup, licenseModelId, version } = {}
132 ) {
133 if (licenseModelId && version) {
134 this.fetchLimits(dispatch, {
135 licenseModelId,
136 version,
137 licenseKeyGroup
138 });
139 }
140 dispatch({
141 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
142 licenseKeyGroup
143 });
144 },
Michael Landoefa037d2017-02-19 12:57:33 +0200145
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200146 closeLicenseKeyGroupEditor(dispatch) {
147 dispatch({
148 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
149 });
150 },
Michael Landoefa037d2017-02-19 12:57:33 +0200151
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200152 saveLicenseKeyGroup(
153 dispatch,
154 { licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version }
155 ) {
156 if (previousLicenseKeyGroup) {
157 return putLicenseKeyGroup(
158 licenseModelId,
159 licenseKeyGroup,
160 version
161 ).then(() => {
162 dispatch({
163 type: licenseKeyGroupsConstants.EDIT_LICENSE_KEY_GROUP,
164 licenseKeyGroup
165 });
166 return ItemsHelper.checkItemStatus(dispatch, {
167 itemId: licenseModelId,
168 versionId: version.id
169 });
170 });
171 } else {
172 return postLicenseKeyGroup(
173 licenseModelId,
174 licenseKeyGroup,
175 version
176 ).then(response => {
177 dispatch({
178 type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP,
179 licenseKeyGroup: {
180 ...licenseKeyGroup,
181 referencingFeatureGroups: [],
182 id: response.value
183 }
184 });
185 return ItemsHelper.checkItemStatus(dispatch, {
186 itemId: licenseModelId,
187 versionId: version.id
188 });
189 });
190 }
191 },
Michael Landoefa037d2017-02-19 12:57:33 +0200192
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200193 deleteLicenseKeyGroup(
194 dispatch,
195 { licenseModelId, licenseKeyGroupId, version }
196 ) {
197 return deleteLicenseKeyGroup(
198 licenseModelId,
199 licenseKeyGroupId,
200 version
201 ).then(() => {
202 dispatch({
203 type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP,
204 licenseKeyGroupId
205 });
206 return ItemsHelper.checkItemStatus(dispatch, {
207 itemId: licenseModelId,
208 versionId: version.id
209 });
210 });
211 },
Michael Landoefa037d2017-02-19 12:57:33 +0200212
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200213 hideDeleteConfirm(dispatch) {
214 dispatch({
215 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
216 licenseKeyGroupToDelete: false
217 });
218 },
Michael Landoefa037d2017-02-19 12:57:33 +0200219
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200220 openDeleteLicenseAgreementConfirm(dispatch, { licenseKeyGroup }) {
221 dispatch({
222 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
223 licenseKeyGroupToDelete: licenseKeyGroup
224 });
225 },
Michael Landoefa037d2017-02-19 12:57:33 +0200226
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200227 fetchLimits(dispatch, { licenseModelId, version, licenseKeyGroup }) {
228 return fetchLimitsList(
229 licenseModelId,
230 licenseKeyGroup.id,
231 version
232 ).then(response => {
233 dispatch({
234 type:
235 licenseKeyGroupsConstants.licenseKeyGroupsEditor
236 .LIMITS_LIST_LOADED,
237 response
238 });
239 });
240 },
Michael Landoefa037d2017-02-19 12:57:33 +0200241
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200242 submitLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
243 const promise = limit.id
244 ? putLimit(licenseModelId, licenseKeyGroup.id, version, limit)
245 : postLimit(licenseModelId, licenseKeyGroup.id, version, limit);
246 return promise.then(() => {
247 dispatch({
248 type: limitEditorActions.CLOSE
249 });
250 this.fetchLimits(dispatch, {
251 licenseModelId,
252 version,
253 licenseKeyGroup
254 });
255 return ItemsHelper.checkItemStatus(dispatch, {
256 itemId: licenseModelId,
257 versionId: version.id
258 });
259 });
260 },
Michael Landoefa037d2017-02-19 12:57:33 +0200261
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200262 deleteLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
263 return deleteLimit(
264 licenseModelId,
265 licenseKeyGroup.id,
266 version,
267 limit.id
268 ).then(() => {
269 this.fetchLimits(dispatch, {
270 licenseModelId,
271 version,
272 licenseKeyGroup
273 });
274 return ItemsHelper.checkItemStatus(dispatch, {
275 itemId: licenseModelId,
276 versionId: version.id
277 });
278 });
279 }
Michael Landoefa037d2017-02-19 12:57:33 +0200280};