blob: a371d5635e67ef8a7a24e3a9e50002325842b038 [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 entitlementPoolsActionTypes } from './EntitlementPoolsConstants.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}/entitlement-pools`;
Michael Landoefa037d2017-02-19 12:57:33 +020027}
28
AviZi280f8012017-06-09 02:39:56 +030029function fetchEntitlementPoolsList(licenseModelId, version) {
30 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020031}
32
Avi Ziv61070c92017-07-26 17:37:57 +030033function postEntitlementPool(licenseModelId, entitlementPool, version) {
AviZi280f8012017-06-09 02:39:56 +030034 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
Michael Landoefa037d2017-02-19 12:57:33 +020035 name: entitlementPool.name,
36 description: entitlementPool.description,
37 thresholdValue: entitlementPool.thresholdValue,
Avi Ziv61070c92017-07-26 17:37:57 +030038 thresholdUnits: getValue(entitlementPool.thresholdUnits),
Michael Landoefa037d2017-02-19 12:57:33 +020039 entitlementMetric: entitlementPool.entitlementMetric,
40 increments: entitlementPool.increments,
Avi Ziv61070c92017-07-26 17:37:57 +030041 operationalScope: getValue(entitlementPool.operationalScope),
Michael Landoefa037d2017-02-19 12:57:33 +020042 time: entitlementPool.time,
Avi Zivb8e2faf2017-07-18 19:45:38 +030043 startDate: entitlementPool.startDate,
44 expiryDate: entitlementPool.expiryDate
Michael Landoefa037d2017-02-19 12:57:33 +020045 });
46}
47
48
AviZi280f8012017-06-09 02:39:56 +030049function putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version) {
Avi Ziv61070c92017-07-26 17:37:57 +030050
AviZi280f8012017-06-09 02:39:56 +030051 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPool.id}`, {
Michael Landoefa037d2017-02-19 12:57:33 +020052 name: entitlementPool.name,
53 description: entitlementPool.description,
54 thresholdValue: entitlementPool.thresholdValue,
Avi Ziv61070c92017-07-26 17:37:57 +030055 thresholdUnits: getValue(entitlementPool.thresholdUnits),
Michael Landoefa037d2017-02-19 12:57:33 +020056 entitlementMetric: entitlementPool.entitlementMetric,
57 increments: entitlementPool.increments,
Avi Ziv61070c92017-07-26 17:37:57 +030058 operationalScope: getValue(entitlementPool.operationalScope),
Michael Landoefa037d2017-02-19 12:57:33 +020059 time: entitlementPool.time,
Avi Zivb8e2faf2017-07-18 19:45:38 +030060 startDate: entitlementPool.startDate,
61 expiryDate: entitlementPool.expiryDate
Michael Landoefa037d2017-02-19 12:57:33 +020062 });
63}
64
AviZi280f8012017-06-09 02:39:56 +030065function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
66 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}`);
Michael Landoefa037d2017-02-19 12:57:33 +020067}
68
Avi Ziv61070c92017-07-26 17:37:57 +030069function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
70 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`);
71}
72
73function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
74 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limitId}`);
75}
76
77function postLimit(licenseModelId, entitlementPoolId, version, limit) {
78 return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`, {
79 name: limit.name,
80 type: limit.type,
81 description: limit.description,
82 metric: limit.metric,
83 value: limit.value,
84 unit: limit.unit,
85 aggregationFunction: getValue(limit.aggregationFunction),
86 time: getValue(limit.time)
87 });
88}
89
90function putLimit(licenseModelId, entitlementPoolId, version, limit) {
91
92 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limit.id}`, {
93 name: limit.name,
94 type: limit.type,
95 description: limit.description,
96 metric: limit.metric,
97 value: limit.value,
98 unit: limit.unit,
99 aggregationFunction: getValue(limit.aggregationFunction),
100 time: getValue(limit.time)
101 });
102}
Michael Landoefa037d2017-02-19 12:57:33 +0200103
104export default {
Avi Ziv61070c92017-07-26 17:37:57 +0300105
Michael Landoefa037d2017-02-19 12:57:33 +0200106 fetchEntitlementPoolsList(dispatch, {licenseModelId, version}) {
107 return fetchEntitlementPoolsList(licenseModelId, version).then(response => dispatch({
108 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
109 response
110 }));
111 },
112
Avi Ziv61070c92017-07-26 17:37:57 +0300113 openEntitlementPoolsEditor(dispatch, {entitlementPool, licenseModelId, version} = {}) {
114 if (licenseModelId && version) {
115 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
116 }
Michael Landoefa037d2017-02-19 12:57:33 +0200117 dispatch({
118 type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
119 entitlementPool
120 });
121 },
122
AviZi280f8012017-06-09 02:39:56 +0300123 deleteEntitlementPool(dispatch, {licenseModelId, entitlementPoolId, version}) {
124 return deleteEntitlementPool(licenseModelId, entitlementPoolId, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +0200125 dispatch({
126 type: entitlementPoolsActionTypes.DELETE_ENTITLEMENT_POOL,
127 entitlementPoolId
128 });
129 });
130 },
131
132 entitlementPoolsEditorDataChanged(dispatch, {deltaData}) {
133 dispatch({
134 type: entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
135 deltaData
136 });
137 },
138
139 closeEntitlementPoolsEditor(dispatch) {
140 dispatch({
141 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
142 });
143 },
144
AviZi280f8012017-06-09 02:39:56 +0300145 saveEntitlementPool(dispatch, {licenseModelId, previousEntitlementPool, entitlementPool, version}) {
Michael Landoefa037d2017-02-19 12:57:33 +0200146 if (previousEntitlementPool) {
AviZi280f8012017-06-09 02:39:56 +0300147 return putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +0200148 dispatch({
149 type: entitlementPoolsActionTypes.EDIT_ENTITLEMENT_POOL,
150 entitlementPool
151 });
152 });
153 }
154 else {
AviZi280f8012017-06-09 02:39:56 +0300155 return postEntitlementPool(licenseModelId, entitlementPool, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +0200156 dispatch({
157 type: entitlementPoolsActionTypes.ADD_ENTITLEMENT_POOL,
158 entitlementPool: {
159 ...entitlementPool,
AviZi280f8012017-06-09 02:39:56 +0300160 referencingFeatureGroups: [],
Michael Landoefa037d2017-02-19 12:57:33 +0200161 id: response.value
162 }
163 });
164 });
165 }
166 },
167
168 hideDeleteConfirm(dispatch) {
169 dispatch({
170 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
171 entitlementPoolToDelete: false
172 });
173 },
174 openDeleteEntitlementPoolConfirm(dispatch, {entitlementPool}) {
175 dispatch({
176 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
177 entitlementPoolToDelete: entitlementPool
178 });
179 },
180
181 switchVersion(dispatch, {licenseModelId, version}) {
182 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
183 this.fetchEntitlementPoolsList(dispatch, {licenseModelId, version});
184 });
Avi Ziv61070c92017-07-26 17:37:57 +0300185 },
186
187
188 fetchLimits(dispatch, {licenseModelId, version, entitlementPool}) {
189 return fetchLimitsList(licenseModelId, entitlementPool.id, version). then (response => {
190 dispatch({
191 type: entitlementPoolsActionTypes.entitlementPoolsEditor.LIMITS_LIST_LOADED,
192 response
193 });
194 });
195 },
196
197 submitLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) {
198 const propmise = limit.id ? putLimit(licenseModelId,entitlementPool.id, version, limit)
199 : postLimit(licenseModelId,entitlementPool.id, version, limit);
200 return propmise.then(() => {
201 dispatch({
202 type: limitEditorActions.CLOSE
203 });
204 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
205 });
206 },
207
208 deleteLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) {
209 return deleteLimit(licenseModelId,entitlementPool.id, version, limit.id).then(() => {
210 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
211 });
Michael Landoefa037d2017-02-19 12:57:33 +0200212 }
213};