2 * Copyright © 2016-2018 European Support Limited
4 * 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
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * 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.
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import { actionTypes as licenseKeyGroupsConstants } from './LicenseKeyGroupsConstants.js';
19 import { actionTypes as limitEditorActions } from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
20 import { default as getValue, getStrValue } from 'nfvo-utils/getValue.js';
21 import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
22 import i18n from 'nfvo-utils/i18n/i18n.js';
24 actionTypes as modalActionTypes,
26 } from 'nfvo-components/modal/GlobalModalConstants.js';
27 import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js';
29 function baseUrl(licenseModelId, version) {
30 const restPrefix = Configuration.get('restPrefix');
31 const { id: versionId } = version;
32 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-key-groups`;
35 function fetchLicenseKeyGroupsList(licenseModelId, version) {
36 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
39 function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) {
40 return RestAPIUtil.destroy(
41 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`
45 function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
46 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
47 name: licenseKeyGroup.name,
48 description: licenseKeyGroup.description,
49 type: licenseKeyGroup.type,
50 increments: licenseKeyGroup.increments,
51 thresholdValue: licenseKeyGroup.thresholdValue,
52 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
53 startDate: licenseKeyGroup.startDate,
54 expiryDate: licenseKeyGroup.expiryDate,
55 manufacturerReferenceNumber: licenseKeyGroup.manufacturerReferenceNumber
59 function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
60 return RestAPIUtil.put(
61 `${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`,
63 name: licenseKeyGroup.name,
64 description: licenseKeyGroup.description,
65 type: licenseKeyGroup.type,
66 increments: licenseKeyGroup.increments,
67 thresholdValue: licenseKeyGroup.thresholdValue,
68 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
69 startDate: licenseKeyGroup.startDate,
70 expiryDate: licenseKeyGroup.expiryDate,
71 manufacturerReferenceNumber:
72 licenseKeyGroup.manufacturerReferenceNumber
77 function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
78 return RestAPIUtil.fetch(
79 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`
83 function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
84 return RestAPIUtil.destroy(
88 )}/${licenseKeyGroupId}/limits/${limitId}`
92 function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
93 return RestAPIUtil.post(
94 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`,
98 description: limit.description,
99 metric: getStrValue(limit.metric),
101 unit: getStrValue(limit.unit),
102 aggregationFunction: getValue(limit.aggregationFunction),
103 time: getValue(limit.time)
108 function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
109 return RestAPIUtil.put(
110 `${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${
116 description: limit.description,
117 metric: getStrValue(limit.metric),
119 unit: getStrValue(limit.unit),
120 aggregationFunction: getValue(limit.aggregationFunction),
121 time: getValue(limit.time)
127 fetchLicenseKeyGroupsList(dispatch, { licenseModelId, version }) {
128 return fetchLicenseKeyGroupsList(licenseModelId, version).then(
132 licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
138 openLicenseKeyGroupsEditor(
140 { licenseKeyGroup, licenseModelId, version, isReadOnlyMode } = {}
142 if (licenseModelId && version && licenseKeyGroup) {
143 this.fetchLimits(dispatch, {
150 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
154 type: modalActionTypes.GLOBAL_MODAL_SHOW,
156 modalComponentName: modalContentMapper.LKG_EDITOR,
157 modalComponentProps: {
161 size: modalSizes.LARGE
164 licenseModelId && version && licenseKeyGroup
165 ? i18n('Edit License Key Group')
166 : i18n('Create New License Key Group')
171 closeLicenseKeyGroupEditor(dispatch) {
173 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
176 type: modalActionTypes.GLOBAL_MODAL_CLOSE
180 async saveLicenseKeyGroup(
182 { licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version }
184 if (previousLicenseKeyGroup) {
185 await putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version);
187 await postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version);
189 await ItemsHelper.checkItemStatus(dispatch, {
190 itemId: licenseModelId,
191 versionId: version.id
193 await this.fetchLicenseKeyGroupsList(dispatch, {
199 async deleteLicenseKeyGroup(
201 { licenseModelId, licenseKeyGroupId, version }
203 await deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version);
204 await ItemsHelper.checkItemStatus(dispatch, {
205 itemId: licenseModelId,
206 versionId: version.id
208 await this.fetchLicenseKeyGroupsList(dispatch, {
214 hideDeleteConfirm(dispatch) {
216 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
217 licenseKeyGroupToDelete: false
221 openDeleteLicenseAgreementConfirm(dispatch, { licenseKeyGroup }) {
223 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
224 licenseKeyGroupToDelete: licenseKeyGroup
228 fetchLimits(dispatch, { licenseModelId, version, licenseKeyGroup }) {
229 return fetchLimitsList(
236 licenseKeyGroupsConstants.licenseKeyGroupsEditor
243 submitLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
244 const promise = limit.id
245 ? putLimit(licenseModelId, licenseKeyGroup.id, version, limit)
246 : postLimit(licenseModelId, licenseKeyGroup.id, version, limit);
247 return promise.then(() => {
249 type: limitEditorActions.CLOSE
251 this.fetchLimits(dispatch, {
256 return ItemsHelper.checkItemStatus(dispatch, {
257 itemId: licenseModelId,
258 versionId: version.id
263 deleteLimit(dispatch, { licenseModelId, version, licenseKeyGroup, limit }) {
270 this.fetchLimits(dispatch, {
275 return ItemsHelper.checkItemStatus(dispatch, {
276 itemId: licenseModelId,
277 versionId: version.id