2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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';
23 function baseUrl(licenseModelId, version) {
24 const restPrefix = Configuration.get('restPrefix');
25 const {id: versionId} = version;
26 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-key-groups`;
29 function fetchLicenseKeyGroupsList(licenseModelId, version) {
30 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
33 function deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version) {
34 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`);
37 function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
38 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
39 name: licenseKeyGroup.name,
40 description: licenseKeyGroup.description,
41 operationalScope: getValue(licenseKeyGroup.operationalScope),
42 type: licenseKeyGroup.type,
43 increments: licenseKeyGroup.increments,
44 thresholdValue: licenseKeyGroup.thresholdValue,
45 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
46 startDate: licenseKeyGroup.startDate,
47 expiryDate: licenseKeyGroup.expiryDate
51 function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
52 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`, {
53 name: licenseKeyGroup.name,
54 description: licenseKeyGroup.description,
55 operationalScope: getValue(licenseKeyGroup.operationalScope),
56 type: licenseKeyGroup.type,
57 increments: licenseKeyGroup.increments,
58 thresholdValue: licenseKeyGroup.thresholdValue,
59 thresholdUnits: getValue(licenseKeyGroup.thresholdUnits),
60 startDate: licenseKeyGroup.startDate,
61 expiryDate: licenseKeyGroup.expiryDate
65 function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
66 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`);
69 function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
70 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${limitId}`);
73 function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
74 return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`, {
77 description: limit.description,
78 metric: getStrValue(limit.metric),
80 unit: getStrValue(limit.unit),
81 aggregationFunction: getValue(limit.aggregationFunction),
82 time: getValue(limit.time)
86 function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
88 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${limit.id}`, {
91 description: limit.description,
92 metric: getStrValue(limit.metric),
94 unit: getStrValue(limit.unit),
95 aggregationFunction: getValue(limit.aggregationFunction),
96 time: getValue(limit.time)
101 fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version}) {
102 return fetchLicenseKeyGroupsList(licenseModelId, version).then(response => dispatch({
103 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
108 openLicenseKeyGroupsEditor(dispatch, {licenseKeyGroup, licenseModelId, version} = {}) {
109 if (licenseModelId && version) {
110 this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
113 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
118 closeLicenseKeyGroupEditor(dispatch){
120 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
124 saveLicenseKeyGroup(dispatch, {licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version}) {
125 if (previousLicenseKeyGroup) {
126 return putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(() => {
128 type: licenseKeyGroupsConstants.EDIT_LICENSE_KEY_GROUP,
131 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
135 return postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(response => {
137 type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP,
140 referencingFeatureGroups: [],
144 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
151 deleteLicenseKeyGroup(dispatch, {licenseModelId, licenseKeyGroupId, version}){
152 return deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version).then(()=> {
154 type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP,
157 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
161 hideDeleteConfirm(dispatch) {
163 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
164 licenseKeyGroupToDelete: false
168 openDeleteLicenseAgreementConfirm(dispatch, {licenseKeyGroup}) {
170 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
171 licenseKeyGroupToDelete: licenseKeyGroup
176 fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup}) {
177 return fetchLimitsList(licenseModelId, licenseKeyGroup.id, version).then(response => {
179 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.LIMITS_LIST_LOADED,
185 submitLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) {
186 const promise = limit.id ? putLimit(licenseModelId,licenseKeyGroup.id, version, limit) :
187 postLimit(licenseModelId,licenseKeyGroup.id, version, limit);
188 return promise.then(() => {
190 type: limitEditorActions.CLOSE
192 this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
193 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
197 deleteLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) {
198 return deleteLimit(licenseModelId,licenseKeyGroup.id, version, limit.id).then(() => {
199 this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
200 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});