blob: 5d425a8aa4a3e947cf5acb64f7f4457e7827c042 [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
svishnev57c5c4a2018-04-22 14:14:31 +0300127 async saveLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200128 dispatch,
129 { licenseModelId, previousLicenseAgreement, licenseAgreement, version }
130 ) {
131 if (previousLicenseAgreement) {
svishnev57c5c4a2018-04-22 14:14:31 +0300132 await putLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200133 licenseModelId,
134 previousLicenseAgreement,
135 licenseAgreement,
136 version
svishnev57c5c4a2018-04-22 14:14:31 +0300137 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200138 } else {
svishnev57c5c4a2018-04-22 14:14:31 +0300139 await postLicenseAgreement(
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200140 licenseModelId,
141 licenseAgreement,
142 version
svishnev57c5c4a2018-04-22 14:14:31 +0300143 );
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200144 }
svishnev57c5c4a2018-04-22 14:14:31 +0300145 await this.fetchLicenseAgreementList(dispatch, {
146 licenseModelId,
147 version
148 });
149 await FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {
150 licenseModelId,
151 version
152 });
153
154 return ItemsHelper.checkItemStatus(dispatch, {
155 itemId: licenseModelId,
156 versionId: version.id
157 });
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200158 },
Michael Landoefa037d2017-02-19 12:57:33 +0200159
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200160 deleteLicenseAgreement(
161 dispatch,
162 { licenseModelId, licenseAgreementId, version }
163 ) {
164 return deleteLicenseAgreement(
165 licenseModelId,
166 licenseAgreementId,
167 version
168 ).then(() => {
169 dispatch({
170 type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT,
171 licenseAgreementId
172 });
173 return ItemsHelper.checkItemStatus(dispatch, {
174 itemId: licenseModelId,
175 versionId: version.id
176 });
177 });
178 },
Michael Landoefa037d2017-02-19 12:57:33 +0200179
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200180 selectLicenseAgreementEditorTab(dispatch, { tab }) {
181 dispatch({
182 type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB,
183 tab
184 });
185 }
Michael Landoefa037d2017-02-19 12:57:33 +0200186};