blob: 4d868152763616268d72e743376c0e5bb2ba2a49 [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';
18import {actionTypes as licenseKeyGroupsConstants} from './LicenseKeyGroupsConstants.js';
Avi Ziv61070c92017-07-26 17:37:57 +030019import {actionTypes as limitEditorActions} from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
az2497644017c2017-08-10 17:49:40 +030020import {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) {
Michael Landoefa037d2017-02-19 12:57:33 +020024 const restPrefix = Configuration.get('restPrefix');
AviZi280f8012017-06-09 02:39:56 +030025 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) {
AviZi280f8012017-06-09 02:39:56 +030030 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) {
34 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}`);
Michael Landoefa037d2017-02-19 12:57:33 +020035}
36
AviZi280f8012017-06-09 02:39:56 +030037function postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
38 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
Michael Landoefa037d2017-02-19 12:57:33 +020039 name: licenseKeyGroup.name,
40 description: licenseKeyGroup.description,
Avi Ziv61070c92017-07-26 17:37:57 +030041 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
Michael Landoefa037d2017-02-19 12:57:33 +020048 });
49}
50
AviZi280f8012017-06-09 02:39:56 +030051function putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version) {
52 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroup.id}`, {
Michael Landoefa037d2017-02-19 12:57:33 +020053 name: licenseKeyGroup.name,
54 description: licenseKeyGroup.description,
Avi Ziv61070c92017-07-26 17:37:57 +030055 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
Michael Landoefa037d2017-02-19 12:57:33 +020062 });
63}
64
Avi Ziv61070c92017-07-26 17:37:57 +030065function fetchLimitsList(licenseModelId, licenseKeyGroupId, version) {
66 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`);
67}
68
69function deleteLimit(licenseModelId, licenseKeyGroupId, version, limitId) {
70 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${limitId}`);
71}
72
73function postLimit(licenseModelId, licenseKeyGroupId, version, limit) {
74 return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits`, {
75 name: limit.name,
76 type: limit.type,
77 description: limit.description,
az2497644017c2017-08-10 17:49:40 +030078 metric: getStrValue(limit.metric),
Avi Ziv61070c92017-07-26 17:37:57 +030079 value: limit.value,
az2497644017c2017-08-10 17:49:40 +030080 unit: getStrValue(limit.unit),
Avi Ziv61070c92017-07-26 17:37:57 +030081 aggregationFunction: getValue(limit.aggregationFunction),
82 time: getValue(limit.time)
83 });
84}
85
86function putLimit(licenseModelId, licenseKeyGroupId, version, limit) {
87
88 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseKeyGroupId}/limits/${limit.id}`, {
89 name: limit.name,
90 type: limit.type,
91 description: limit.description,
az2497644017c2017-08-10 17:49:40 +030092 metric: getStrValue(limit.metric),
Avi Ziv61070c92017-07-26 17:37:57 +030093 value: limit.value,
az2497644017c2017-08-10 17:49:40 +030094 unit: getStrValue(limit.unit),
Avi Ziv61070c92017-07-26 17:37:57 +030095 aggregationFunction: getValue(limit.aggregationFunction),
96 time: getValue(limit.time)
97 });
98}
Michael Landoefa037d2017-02-19 12:57:33 +020099
100export default {
101 fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version}) {
102 return fetchLicenseKeyGroupsList(licenseModelId, version).then(response => dispatch({
103 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_LIST_LOADED,
104 response
105 }));
106 },
107
Avi Ziv61070c92017-07-26 17:37:57 +0300108 openLicenseKeyGroupsEditor(dispatch, {licenseKeyGroup, licenseModelId, version} = {}) {
109 if (licenseModelId && version) {
110 this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
111 }
Michael Landoefa037d2017-02-19 12:57:33 +0200112 dispatch({
113 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.OPEN,
114 licenseKeyGroup
115 });
116 },
117
118 closeLicenseKeyGroupEditor(dispatch){
119 dispatch({
120 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.CLOSE
121 });
122 },
123
AviZi280f8012017-06-09 02:39:56 +0300124 saveLicenseKeyGroup(dispatch, {licenseModelId, previousLicenseKeyGroup, licenseKeyGroup, version}) {
Michael Landoefa037d2017-02-19 12:57:33 +0200125 if (previousLicenseKeyGroup) {
AviZi280f8012017-06-09 02:39:56 +0300126 return putLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +0200127 dispatch({
128 type: licenseKeyGroupsConstants.EDIT_LICENSE_KEY_GROUP,
129 licenseKeyGroup
130 });
talig8e9c0652017-12-20 14:30:43 +0200131 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
Michael Landoefa037d2017-02-19 12:57:33 +0200132 });
133 }
134 else {
AviZi280f8012017-06-09 02:39:56 +0300135 return postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +0200136 dispatch({
137 type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP,
138 licenseKeyGroup: {
139 ...licenseKeyGroup,
AviZi280f8012017-06-09 02:39:56 +0300140 referencingFeatureGroups: [],
Michael Landoefa037d2017-02-19 12:57:33 +0200141 id: response.value
142 }
143 });
talig8e9c0652017-12-20 14:30:43 +0200144 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
Michael Landoefa037d2017-02-19 12:57:33 +0200145 });
146 }
147
148
149 },
150
AviZi280f8012017-06-09 02:39:56 +0300151 deleteLicenseKeyGroup(dispatch, {licenseModelId, licenseKeyGroupId, version}){
152 return deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version).then(()=> {
Michael Landoefa037d2017-02-19 12:57:33 +0200153 dispatch({
154 type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP,
155 licenseKeyGroupId
156 });
talig8e9c0652017-12-20 14:30:43 +0200157 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
Michael Landoefa037d2017-02-19 12:57:33 +0200158 });
159 },
160
Michael Landoefa037d2017-02-19 12:57:33 +0200161 hideDeleteConfirm(dispatch) {
162 dispatch({
163 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
164 licenseKeyGroupToDelete: false
165 });
166 },
167
168 openDeleteLicenseAgreementConfirm(dispatch, {licenseKeyGroup}) {
169 dispatch({
170 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
171 licenseKeyGroupToDelete: licenseKeyGroup
172 });
173 },
174
Avi Ziv61070c92017-07-26 17:37:57 +0300175
176 fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup}) {
177 return fetchLimitsList(licenseModelId, licenseKeyGroup.id, version).then(response => {
178 dispatch({
179 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.LIMITS_LIST_LOADED,
180 response
az2497644017c2017-08-10 17:49:40 +0300181 });
182 });
Avi Ziv61070c92017-07-26 17:37:57 +0300183 },
184
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(() => {
189 dispatch({
190 type: limitEditorActions.CLOSE
191 });
192 this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
talig8e9c0652017-12-20 14:30:43 +0200193 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
az2497644017c2017-08-10 17:49:40 +0300194 });
Avi Ziv61070c92017-07-26 17:37:57 +0300195 },
196
197 deleteLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) {
198 return deleteLimit(licenseModelId,licenseKeyGroup.id, version, limit.id).then(() => {
199 this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
talig8e9c0652017-12-20 14:30:43 +0200200 ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
az2497644017c2017-08-10 17:49:40 +0300201 });
Michael Landoefa037d2017-02-19 12:57:33 +0200202 }
Avi Ziv61070c92017-07-26 17:37:57 +0300203
204
Michael Landoefa037d2017-02-19 12:57:33 +0200205};