blob: ce4327d8100f2efd1ae4cb7830913837fbf2c05d [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';
az2497644017c2017-08-10 17:49:40 +030021import {default as getValue, getStrValue} 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
az2497644017c2017-08-10 17:49:40 +030029function fetchEntitlementPoolsList(licenseModelId, version) {
AviZi280f8012017-06-09 02:39:56 +030030 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020031}
32
az2497644017c2017-08-10 17:49:40 +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 increments: entitlementPool.increments,
Avi Ziv61070c92017-07-26 17:37:57 +030040 operationalScope: getValue(entitlementPool.operationalScope),
Michael Landoefa037d2017-02-19 12:57:33 +020041 time: entitlementPool.time,
Avi Zivb8e2faf2017-07-18 19:45:38 +030042 startDate: entitlementPool.startDate,
43 expiryDate: entitlementPool.expiryDate
Michael Landoefa037d2017-02-19 12:57:33 +020044 });
45}
46
47
AviZi280f8012017-06-09 02:39:56 +030048function putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version) {
az2497644017c2017-08-10 17:49:40 +030049
AviZi280f8012017-06-09 02:39:56 +030050 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPool.id}`, {
Michael Landoefa037d2017-02-19 12:57:33 +020051 name: entitlementPool.name,
52 description: entitlementPool.description,
53 thresholdValue: entitlementPool.thresholdValue,
Avi Ziv61070c92017-07-26 17:37:57 +030054 thresholdUnits: getValue(entitlementPool.thresholdUnits),
Michael Landoefa037d2017-02-19 12:57:33 +020055 increments: entitlementPool.increments,
Avi Ziv61070c92017-07-26 17:37:57 +030056 operationalScope: getValue(entitlementPool.operationalScope),
Michael Landoefa037d2017-02-19 12:57:33 +020057 time: entitlementPool.time,
Avi Zivb8e2faf2017-07-18 19:45:38 +030058 startDate: entitlementPool.startDate,
59 expiryDate: entitlementPool.expiryDate
Michael Landoefa037d2017-02-19 12:57:33 +020060 });
61}
62
AviZi280f8012017-06-09 02:39:56 +030063function deleteEntitlementPool(licenseModelId, entitlementPoolId, version) {
64 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}`);
Michael Landoefa037d2017-02-19 12:57:33 +020065}
66
az2497644017c2017-08-10 17:49:40 +030067function fetchLimitsList(licenseModelId, entitlementPoolId, version) {
Avi Ziv61070c92017-07-26 17:37:57 +030068 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`);
69}
70
az2497644017c2017-08-10 17:49:40 +030071function deleteLimit(licenseModelId, entitlementPoolId, version, limitId) {
Avi Ziv61070c92017-07-26 17:37:57 +030072 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limitId}`);
73}
74
az2497644017c2017-08-10 17:49:40 +030075function postLimit(licenseModelId, entitlementPoolId, version, limit) {
Avi Ziv61070c92017-07-26 17:37:57 +030076 return RestAPIUtil.post(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits`, {
77 name: limit.name,
78 type: limit.type,
79 description: limit.description,
az2497644017c2017-08-10 17:49:40 +030080 metric: getStrValue(limit.metric),
Avi Ziv61070c92017-07-26 17:37:57 +030081 value: limit.value,
az2497644017c2017-08-10 17:49:40 +030082 unit: getStrValue(limit.unit),
Avi Ziv61070c92017-07-26 17:37:57 +030083 aggregationFunction: getValue(limit.aggregationFunction),
84 time: getValue(limit.time)
85 });
86}
87
88function putLimit(licenseModelId, entitlementPoolId, version, limit) {
az2497644017c2017-08-10 17:49:40 +030089
Avi Ziv61070c92017-07-26 17:37:57 +030090 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${entitlementPoolId}/limits/${limit.id}`, {
91 name: limit.name,
92 type: limit.type,
93 description: limit.description,
az2497644017c2017-08-10 17:49:40 +030094 metric: getStrValue(limit.metric),
Avi Ziv61070c92017-07-26 17:37:57 +030095 value: limit.value,
az2497644017c2017-08-10 17:49:40 +030096 unit: getStrValue(limit.unit),
Avi Ziv61070c92017-07-26 17:37:57 +030097 aggregationFunction: getValue(limit.aggregationFunction),
98 time: getValue(limit.time)
az2497644017c2017-08-10 17:49:40 +030099 });
Avi Ziv61070c92017-07-26 17:37:57 +0300100}
Michael Landoefa037d2017-02-19 12:57:33 +0200101
102export default {
Avi Ziv61070c92017-07-26 17:37:57 +0300103
Michael Landoefa037d2017-02-19 12:57:33 +0200104 fetchEntitlementPoolsList(dispatch, {licenseModelId, version}) {
105 return fetchEntitlementPoolsList(licenseModelId, version).then(response => dispatch({
106 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_LIST_LOADED,
107 response
108 }));
109 },
110
Avi Ziv61070c92017-07-26 17:37:57 +0300111 openEntitlementPoolsEditor(dispatch, {entitlementPool, licenseModelId, version} = {}) {
112 if (licenseModelId && version) {
113 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
114 }
Michael Landoefa037d2017-02-19 12:57:33 +0200115 dispatch({
116 type: entitlementPoolsActionTypes.entitlementPoolsEditor.OPEN,
117 entitlementPool
118 });
119 },
120
AviZi280f8012017-06-09 02:39:56 +0300121 deleteEntitlementPool(dispatch, {licenseModelId, entitlementPoolId, version}) {
122 return deleteEntitlementPool(licenseModelId, entitlementPoolId, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +0200123 dispatch({
124 type: entitlementPoolsActionTypes.DELETE_ENTITLEMENT_POOL,
125 entitlementPoolId
126 });
127 });
128 },
129
130 entitlementPoolsEditorDataChanged(dispatch, {deltaData}) {
131 dispatch({
132 type: entitlementPoolsActionTypes.entitlementPoolsEditor.DATA_CHANGED,
133 deltaData
134 });
135 },
136
137 closeEntitlementPoolsEditor(dispatch) {
138 dispatch({
139 type: entitlementPoolsActionTypes.entitlementPoolsEditor.CLOSE
140 });
141 },
142
AviZi280f8012017-06-09 02:39:56 +0300143 saveEntitlementPool(dispatch, {licenseModelId, previousEntitlementPool, entitlementPool, version}) {
Michael Landoefa037d2017-02-19 12:57:33 +0200144 if (previousEntitlementPool) {
AviZi280f8012017-06-09 02:39:56 +0300145 return putEntitlementPool(licenseModelId, previousEntitlementPool, entitlementPool, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +0200146 dispatch({
147 type: entitlementPoolsActionTypes.EDIT_ENTITLEMENT_POOL,
148 entitlementPool
149 });
150 });
151 }
152 else {
AviZi280f8012017-06-09 02:39:56 +0300153 return postEntitlementPool(licenseModelId, entitlementPool, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +0200154 dispatch({
155 type: entitlementPoolsActionTypes.ADD_ENTITLEMENT_POOL,
156 entitlementPool: {
157 ...entitlementPool,
AviZi280f8012017-06-09 02:39:56 +0300158 referencingFeatureGroups: [],
Michael Landoefa037d2017-02-19 12:57:33 +0200159 id: response.value
160 }
161 });
162 });
163 }
164 },
165
166 hideDeleteConfirm(dispatch) {
167 dispatch({
168 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
169 entitlementPoolToDelete: false
170 });
171 },
172 openDeleteEntitlementPoolConfirm(dispatch, {entitlementPool}) {
173 dispatch({
174 type: entitlementPoolsActionTypes.ENTITLEMENT_POOLS_DELETE_CONFIRM,
175 entitlementPoolToDelete: entitlementPool
176 });
177 },
178
179 switchVersion(dispatch, {licenseModelId, version}) {
180 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
181 this.fetchEntitlementPoolsList(dispatch, {licenseModelId, version});
182 });
Avi Ziv61070c92017-07-26 17:37:57 +0300183 },
184
185
186 fetchLimits(dispatch, {licenseModelId, version, entitlementPool}) {
187 return fetchLimitsList(licenseModelId, entitlementPool.id, version). then (response => {
188 dispatch({
189 type: entitlementPoolsActionTypes.entitlementPoolsEditor.LIMITS_LIST_LOADED,
190 response
191 });
az2497644017c2017-08-10 17:49:40 +0300192 });
Avi Ziv61070c92017-07-26 17:37:57 +0300193 },
194
az2497644017c2017-08-10 17:49:40 +0300195 submitLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) {
Avi Ziv61070c92017-07-26 17:37:57 +0300196 const propmise = limit.id ? putLimit(licenseModelId,entitlementPool.id, version, limit)
197 : postLimit(licenseModelId,entitlementPool.id, version, limit);
198 return propmise.then(() => {
199 dispatch({
200 type: limitEditorActions.CLOSE
201 });
202 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
az2497644017c2017-08-10 17:49:40 +0300203 });
Avi Ziv61070c92017-07-26 17:37:57 +0300204 },
205
az2497644017c2017-08-10 17:49:40 +0300206 deleteLimit(dispatch, {licenseModelId, version, entitlementPool, limit}) {
Avi Ziv61070c92017-07-26 17:37:57 +0300207 return deleteLimit(licenseModelId,entitlementPool.id, version, limit.id).then(() => {
az2497644017c2017-08-10 17:49:40 +0300208 this.fetchLimits(dispatch, {licenseModelId, version, entitlementPool});
209 });
Michael Landoefa037d2017-02-19 12:57:33 +0200210 }
211};