blob: f5017f6d7aa94424aad06a8b0d688beee7607980 [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';
19import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
Avi Ziv61070c92017-07-26 17:37:57 +030020import {actionTypes as limitEditorActions} from 'sdc-app/onboarding/licenseModel/limits/LimitEditorConstants.js';
21import getValue from 'nfvo-utils/getValue.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,
78 metric: limit.metric,
79 value: limit.value,
80 unit: limit.unit,
81 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,
92 metric: limit.metric,
93 value: limit.value,
94 unit: limit.unit,
95 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 });
131 });
132 }
133 else {
AviZi280f8012017-06-09 02:39:56 +0300134 return postLicenseKeyGroup(licenseModelId, licenseKeyGroup, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +0200135 dispatch({
136 type: licenseKeyGroupsConstants.ADD_LICENSE_KEY_GROUP,
137 licenseKeyGroup: {
138 ...licenseKeyGroup,
AviZi280f8012017-06-09 02:39:56 +0300139 referencingFeatureGroups: [],
Michael Landoefa037d2017-02-19 12:57:33 +0200140 id: response.value
141 }
142 });
143 });
144 }
145
146
147 },
148
AviZi280f8012017-06-09 02:39:56 +0300149 deleteLicenseKeyGroup(dispatch, {licenseModelId, licenseKeyGroupId, version}){
150 return deleteLicenseKeyGroup(licenseModelId, licenseKeyGroupId, version).then(()=> {
Michael Landoefa037d2017-02-19 12:57:33 +0200151 dispatch({
152 type: licenseKeyGroupsConstants.DELETE_LICENSE_KEY_GROUP,
153 licenseKeyGroupId
154 });
155 });
156 },
157
Michael Landoefa037d2017-02-19 12:57:33 +0200158 hideDeleteConfirm(dispatch) {
159 dispatch({
160 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
161 licenseKeyGroupToDelete: false
162 });
163 },
164
165 openDeleteLicenseAgreementConfirm(dispatch, {licenseKeyGroup}) {
166 dispatch({
167 type: licenseKeyGroupsConstants.LICENSE_KEY_GROUPS_DELETE_CONFIRM,
168 licenseKeyGroupToDelete: licenseKeyGroup
169 });
170 },
171
172 switchVersion(dispatch, {licenseModelId, version}) {
173 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
174 this.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version});
175 });
Avi Ziv61070c92017-07-26 17:37:57 +0300176 },
177
178
179 fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup}) {
180 return fetchLimitsList(licenseModelId, licenseKeyGroup.id, version).then(response => {
181 dispatch({
182 type: licenseKeyGroupsConstants.licenseKeyGroupsEditor.LIMITS_LIST_LOADED,
183 response
184 });
185 });
186 },
187
188 submitLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) {
189 const promise = limit.id ? putLimit(licenseModelId,licenseKeyGroup.id, version, limit) :
190 postLimit(licenseModelId,licenseKeyGroup.id, version, limit);
191 return promise.then(() => {
192 dispatch({
193 type: limitEditorActions.CLOSE
194 });
195 this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
196 });
197 },
198
199 deleteLimit(dispatch, {licenseModelId, version, licenseKeyGroup, limit}) {
200 return deleteLimit(licenseModelId,licenseKeyGroup.id, version, limit.id).then(() => {
201 this.fetchLimits(dispatch, {licenseModelId, version, licenseKeyGroup});
202 });
Michael Landoefa037d2017-02-19 12:57:33 +0200203 }
Avi Ziv61070c92017-07-26 17:37:57 +0300204
205
Michael Landoefa037d2017-02-19 12:57:33 +0200206};