blob: ebbe84abd33cb6979dd35dd93e7468c3bdb6b3e6 [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,
43 operationalScope: getValue(licenseKeyGroup.operationalScope),
44 type: licenseKeyGroup.type,
45 increments: licenseKeyGroup.increments,
46 thresholdValue: licenseKeyGroup.thresholdValue,
47 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
48 startDate: licenseKeyGroup.startDate,
49 expiryDate: licenseKeyGroup.expiryDate
50 });
Michael Landoefa037d2017-02-19 12:57:33 +020051}
52
AviZi280f8012017-06-09 02:39:56 +030053function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020054 return RestAPIUtil.put(
55 `${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`,
56 {
57 name: licenseKeyGroup.name,
58 description: licenseKeyGroup.description,
59 operationalScope: getValue(licenseKeyGroup.operationalScope),
60 type: licenseKeyGroup.type,
61 increments: licenseKeyGroup.increments,
62 thresholdValue: licenseKeyGroup.thresholdValue,
63 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
64 startDate: licenseKeyGroup.startDate,
65 expiryDate: licenseKeyGroup.expiryDate
66 }
67 );
Michael Landoefa037d2017-02-19 12:57:33 +020068}
69
Avi Ziv61070c92017-07-26 17:37:57 +030070function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020071 return RestAPIUtil.fetch(
72 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`
73 );
Avi Ziv61070c92017-07-26 17:37:57 +030074}
75
76function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020077 return RestAPIUtil.destroy(
78 `${baseUrl(
79 licenseModelId,
80 version
81 )}/${licenseKeyGroupId}/limits/${limitId}`
82 );
Avi Ziv61070c92017-07-26 17:37:57 +030083}
84
85function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020086 return RestAPIUtil.post(
87 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`,
88 {
89 name: limit.name,
90 type: limit.type,
91 description: limit.description,
92 metric: getStrValue(limit.metric),
93 value: limit.value,
94 unit: getStrValue(limit.unit),
95 aggregationFunction: getValue(limit.aggregationFunction),
96 time: getValue(limit.time)
97 }
98 );
Avi Ziv61070c92017-07-26 17:37:57 +030099}
100
101function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200102 return RestAPIUtil.put(
103 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${
104 limit.id
105 }`,
106 {
107 name: limit.name,
108 type: limit.type,
109 description: limit.description,
110 metric: getStrValue(limit.metric),
111 value: limit.value,
112 unit: getStrValue(limit.unit),
113 aggregationFunction: getValue(limit.aggregationFunction),
114 time: getValue(limit.time)
115 }
116 );
Avi Ziv61070c92017-07-26 17:37:57 +0300117}
Michael Landoefa037d2017-02-19 12:57:33 +0200118
119export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200120 fetchLicenseKeyGroupsList(dispatch, { licenseModelId, version }) {
121 return fetchLicenseKeyGroupsList(licenseModelId, version).then(
122 response =>
123 dispatch({
124 type:
125 licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
126 response
127 })
128 );
129 },
Michael Landoefa037d2017-02-19 12:57:33 +0200130
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200131 openLicenseKeyGroupsEditor(
132 dispatch,
133 { licenseKeyGroup, licenseModelId, version } = {}
134 ) {
135 if (licenseModelId && version) {
136 this.fetchLimits(dispatch, {
137 licenseModelId,
138 version,
139 licenseKeyGroup
140 });
141 }
142 dispatch({
143 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
144 licenseKeyGroup
145 });
146 },
Michael Landoefa037d2017-02-19 12:57:33 +0200147
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200148 closeLicenseKeyGroupEditor(dispatch) {
149 dispatch({
150 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
151 });
152 },
Michael Landoefa037d2017-02-19 12:57:33 +0200153
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200154 saveLicenseKeyGroup(
155 dispatch,
156 { licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version }
157 ) {
158 if (previousLicenseKeyGroup) {
159 return putLicenseKeyGroup(
160 licenseModelId,
161 licenseKeyGroup,
162 version
163 ).then(() => {
164 dispatch({
165 type: licenseKeyGroupsConstants.EDIT_LICENSE_KEY_GROUP,
166 licenseKeyGroup
167 });
168 return ItemsHelper.checkItemStatus(dispatch, {
169 itemId: licenseModelId,
170 versionId: version.id
171 });
172 });
173 } else {
174 return postLicenseKeyGroup(
175 licenseModelId,
176 licenseKeyGroup,
177 version
178 ).then(response => {
179 dispatch({
180 type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP,
181 licenseKeyGroup: {
182 ...licenseKeyGroup,
183 referencingFeatureGroups: [],
184 id: response.value
185 }
186 });
187 return ItemsHelper.checkItemStatus(dispatch, {
188 itemId: licenseModelId,
189 versionId: version.id
190 });
191 });
192 }
193 },
Michael Landoefa037d2017-02-19 12:57:33 +0200194
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200195 deleteLicenseKeyGroup(
196 dispatch,
197 { licenseModelId, licenseKeyGroupId, version }
198 ) {
199 return deleteLicenseKeyGroup(
200 licenseModelId,
201 licenseKeyGroupId,
202 version
203 ).then(() => {
204 dispatch({
205 type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP,
206 licenseKeyGroupId
207 });
208 return ItemsHelper.checkItemStatus(dispatch, {
209 itemId: licenseModelId,
210 versionId: version.id
211 });
212 });
213 },
Michael Landoefa037d2017-02-19 12:57:33 +0200214
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200215 hideDeleteConfirm(dispatch) {
216 dispatch({
217 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
218 licenseKeyGroupToDelete: false
219 });
220 },
Michael Landoefa037d2017-02-19 12:57:33 +0200221
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200222 openDeleteLicenseAgreementConfirm(dispatch, { licenseKeyGroup }) {
223 dispatch({
224 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
225 licenseKeyGroupToDelete: licenseKeyGroup
226 });
227 },
Michael Landoefa037d2017-02-19 12:57:33 +0200228
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200229 fetchLimits(dispatch, { licenseModelId, version, licenseKeyGroup }) {
230 return fetchLimitsList(
231 licenseModelId,
232 licenseKeyGroup.id,
233 version
234 ).then(response => {
235 dispatch({
236 type:
237 licenseKeyGroupsConstants.licenseKeyGroupsEditor
238 .LIMITS_LIST_LOADED,
239 response
240 });
241 });
242 },
Michael Landoefa037d2017-02-19 12:57:33 +0200243
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200244 submitLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
245 const promise = limit.id
246 ? putLimit(licenseModelId, licenseKeyGroup.id, version, limit)
247 : postLimit(licenseModelId, licenseKeyGroup.id, version, limit);
248 return promise.then(() => {
249 dispatch({
250 type: limitEditorActions.CLOSE
251 });
252 this.fetchLimits(dispatch, {
253 licenseModelId,
254 version,
255 licenseKeyGroup
256 });
257 return ItemsHelper.checkItemStatus(dispatch, {
258 itemId: licenseModelId,
259 versionId: version.id
260 });
261 });
262 },
Michael Landoefa037d2017-02-19 12:57:33 +0200263
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200264 deleteLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
265 return deleteLimit(
266 licenseModelId,
267 licenseKeyGroup.id,
268 version,
269 limit.id
270 ).then(() => {
271 this.fetchLimits(dispatch, {
272 licenseModelId,
273 version,
274 licenseKeyGroup
275 });
276 return ItemsHelper.checkItemStatus(dispatch, {
277 itemId: licenseModelId,
278 versionId: version.id
279 });
280 });
281 }
Michael Landoefa037d2017-02-19 12:57:33 +0200282};