blob: e07777f69e544136ddc7c2c48ec8c5aaca3e7ce7 [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';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { actionTypes as licenseAgreementActionTypes } from './LicenseAgreementConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020019import FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js';
talig8e9c0652017-12-20 14:30:43 +020020import ItemsHelper from 'sdc-app/common/helpers/ItemsHelper.js';
Michael Landoefa037d2017-02-19 12:57:33 +020021
AviZi280f8012017-06-09 02:39:56 +030022function baseUrl(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020023 const restPrefix = Configuration.get('restPrefix');
24 const { id: versionId } = version;
25 return `${restPrefix}/v1.0/vendor-license-models/${licenseModelId}/versions/${versionId}/license-agreements`;
Michael Landoefa037d2017-02-19 12:57:33 +020026}
27
28function fetchLicenseAgreementList(licenseModelId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020029 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020030}
31
talig8e9c0652017-12-20 14:30:43 +020032function fetchLicenseAgreement(licenseModelId, licenseAgreementId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020033 return RestAPIUtil.fetch(
34 `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
35 );
talig8e9c0652017-12-20 14:30:43 +020036}
37
AviZi280f8012017-06-09 02:39:56 +030038function postLicenseAgreement(licenseModelId, licenseAgreement, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020039 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
40 name: licenseAgreement.name,
41 description: licenseAgreement.description,
42 licenseTerm: licenseAgreement.licenseTerm,
43 requirementsAndConstrains: licenseAgreement.requirementsAndConstrains,
44 addedFeatureGroupsIds: licenseAgreement.featureGroupsIds
45 });
Michael Landoefa037d2017-02-19 12:57:33 +020046}
47
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048function putLicenseAgreement(
49 licenseModelId,
50 previousLicenseAgreement,
51 licenseAgreement,
52 version
53) {
54 const { featureGroupsIds = [] } = licenseAgreement;
55 const {
56 featureGroupsIds: prevFeatureGroupsIds = []
57 } = previousLicenseAgreement;
58 return RestAPIUtil.put(
59 `${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`,
60 {
61 name: licenseAgreement.name,
62 description: licenseAgreement.description,
63 licenseTerm: licenseAgreement.licenseTerm,
64 requirementsAndConstrains:
65 licenseAgreement.requirementsAndConstrains,
66 addedFeatureGroupsIds: featureGroupsIds.filter(
67 featureGroupId =>
68 prevFeatureGroupsIds.indexOf(featureGroupId) === -1
69 ),
70 removedFeatureGroupsIds: prevFeatureGroupsIds.filter(
71 prevFeatureGroupsId =>
72 featureGroupsIds.indexOf(prevFeatureGroupsId) === -1
73 )
74 }
75 );
Michael Landoefa037d2017-02-19 12:57:33 +020076}
77
AviZi280f8012017-06-09 02:39:56 +030078function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020079 return RestAPIUtil.destroy(
80 `${baseUrl(licenseModelId, version)}/${licenseAgreementId}`
81 );
Michael Landoefa037d2017-02-19 12:57:33 +020082}
83
84export default {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020085 fetchLicenseAgreementList(dispatch, { licenseModelId, version }) {
86 return fetchLicenseAgreementList(licenseModelId, version).then(
87 response =>
88 dispatch({
89 type:
90 licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED,
91 response
92 })
93 );
94 },
Michael Landoefa037d2017-02-19 12:57:33 +020095
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020096 fetchLicenseAgreement(
97 dispatch,
98 { licenseModelId, licenseAgreementId, version }
99 ) {
100 return fetchLicenseAgreement(
101 licenseModelId,
102 licenseAgreementId,
103 version
104 );
105 },
Michael Landoefa037d2017-02-19 12:57:33 +0200106
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200107 openLicenseAgreementEditor(
108 dispatch,
109 { licenseModelId, licenseAgreement, version }
110 ) {
111 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
112 licenseModelId,
113 version
114 });
115 dispatch({
116 type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN,
117 licenseAgreement
118 });
119 },
talig8e9c0652017-12-20 14:30:43 +0200120
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200121 closeLicenseAgreementEditor(dispatch) {
122 dispatch({
123 type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE
124 });
125 },
Michael Landoefa037d2017-02-19 12:57:33 +0200126
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200127 saveLicenseAgreement(
128 dispatch,
129 { licenseModelId, previousLicenseAgreement, licenseAgreement, version }
130 ) {
131 if (previousLicenseAgreement) {
132 return putLicenseAgreement(
133 licenseModelId,
134 previousLicenseAgreement,
135 licenseAgreement,
136 version
137 ).then(() => {
138 this.fetchLicenseAgreementList(dispatch, {
139 licenseModelId,
140 version
141 });
142 return ItemsHelper.checkItemStatus(dispatch, {
143 itemId: licenseModelId,
144 versionId: version.id
145 });
146 });
147 } else {
148 return postLicenseAgreement(
149 licenseModelId,
150 licenseAgreement,
151 version
152 ).then(() => {
153 this.fetchLicenseAgreementList(dispatch, {
154 licenseModelId,
155 version
156 });
157 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
158 licenseModelId,
159 version
160 });
161 return ItemsHelper.checkItemStatus(dispatch, {
162 itemId: licenseModelId,
163 versionId: version.id
164 });
165 });
166 }
167 },
Michael Landoefa037d2017-02-19 12:57:33 +0200168
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200169 deleteLicenseAgreement(
170 dispatch,
171 { licenseModelId, licenseAgreementId, version }
172 ) {
173 return deleteLicenseAgreement(
174 licenseModelId,
175 licenseAgreementId,
176 version
177 ).then(() => {
178 dispatch({
179 type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT,
180 licenseAgreementId
181 });
182 return ItemsHelper.checkItemStatus(dispatch, {
183 itemId: licenseModelId,
184 versionId: version.id
185 });
186 });
187 },
Michael Landoefa037d2017-02-19 12:57:33 +0200188
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200189 selectLicenseAgreementEditorTab(dispatch, { tab }) {
190 dispatch({
191 type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB,
192 tab
193 });
194 }
Michael Landoefa037d2017-02-19 12:57:33 +0200195};