blob: efc4fb758fefc89e222afd9fba4a63d6d81647b7 [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 licenseAgreementActionTypes} from './LicenseAgreementConstants.js';
19import FeatureGroupsActionHelper from 'sdc-app/onboarding/licenseModel/featureGroups/FeatureGroupsActionHelper.js';
20import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
21
AviZi280f8012017-06-09 02:39:56 +030022function baseUrl(licenseModelId, version) {
Michael Landoefa037d2017-02-19 12:57:33 +020023 const restPrefix = Configuration.get('restPrefix');
AviZi280f8012017-06-09 02:39:56 +030024 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) {
AviZi280f8012017-06-09 02:39:56 +030029 return RestAPIUtil.fetch(`${baseUrl(licenseModelId, version)}`);
Michael Landoefa037d2017-02-19 12:57:33 +020030}
31
AviZi280f8012017-06-09 02:39:56 +030032function postLicenseAgreement(licenseModelId, licenseAgreement, version) {
33 return RestAPIUtil.post(baseUrl(licenseModelId, version), {
Michael Landoefa037d2017-02-19 12:57:33 +020034 name: licenseAgreement.name,
35 description: licenseAgreement.description,
36 licenseTerm: licenseAgreement.licenseTerm,
37 requirementsAndConstrains: licenseAgreement.requirementsAndConstrains,
38 addedFeatureGroupsIds: licenseAgreement.featureGroupsIds
39 });
40}
41
AviZi280f8012017-06-09 02:39:56 +030042function putLicenseAgreement(licenseModelId, previousLicenseAgreement, licenseAgreement, version) {
Michael Landoefa037d2017-02-19 12:57:33 +020043 const {featureGroupsIds = []} = licenseAgreement;
44 const {featureGroupsIds: prevFeatureGroupsIds = []} = previousLicenseAgreement;
AviZi280f8012017-06-09 02:39:56 +030045 return RestAPIUtil.put(`${baseUrl(licenseModelId, version)}/${licenseAgreement.id}`, {
Michael Landoefa037d2017-02-19 12:57:33 +020046 name: licenseAgreement.name,
47 description: licenseAgreement.description,
48 licenseTerm: licenseAgreement.licenseTerm,
49 requirementsAndConstrains: licenseAgreement.requirementsAndConstrains,
50 addedFeatureGroupsIds: featureGroupsIds.filter(featureGroupId => prevFeatureGroupsIds.indexOf(featureGroupId) === -1),
51 removedFeatureGroupsIds: prevFeatureGroupsIds.filter(prevFeatureGroupsId => featureGroupsIds.indexOf(prevFeatureGroupsId) === -1)
52 });
53}
54
AviZi280f8012017-06-09 02:39:56 +030055function deleteLicenseAgreement(licenseModelId, licenseAgreementId, version) {
56 return RestAPIUtil.destroy(`${baseUrl(licenseModelId, version)}/${licenseAgreementId}`);
Michael Landoefa037d2017-02-19 12:57:33 +020057}
58
59export default {
60
61 fetchLicenseAgreementList(dispatch, {licenseModelId, version}) {
62 return fetchLicenseAgreementList(licenseModelId, version).then(response => dispatch({
63 type: licenseAgreementActionTypes.LICENSE_AGREEMENT_LIST_LOADED,
64 response
65 }));
66 },
67
AviZi280f8012017-06-09 02:39:56 +030068 openLicenseAgreementEditor(dispatch, {licenseModelId, licenseAgreement, version}) {
69 FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version});
Michael Landoefa037d2017-02-19 12:57:33 +020070 dispatch({
71 type: licenseAgreementActionTypes.licenseAgreementEditor.OPEN,
72 licenseAgreement
73 });
74 },
75
Michael Landoefa037d2017-02-19 12:57:33 +020076 closeLicenseAgreementEditor(dispatch) {
77 dispatch({
78 type: licenseAgreementActionTypes.licenseAgreementEditor.CLOSE
79 });
80 },
81
82
AviZi280f8012017-06-09 02:39:56 +030083 saveLicenseAgreement(dispatch, {licenseModelId, previousLicenseAgreement, licenseAgreement, version}) {
Michael Landoefa037d2017-02-19 12:57:33 +020084 if (previousLicenseAgreement) {
AviZi280f8012017-06-09 02:39:56 +030085 return putLicenseAgreement(licenseModelId, previousLicenseAgreement, licenseAgreement, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +020086 dispatch({
87 type: licenseAgreementActionTypes.EDIT_LICENSE_AGREEMENT,
88 licenseAgreement
89 });
90 });
91 }
92 else {
AviZi280f8012017-06-09 02:39:56 +030093 return postLicenseAgreement(licenseModelId, licenseAgreement, version).then(response => {
Michael Landoefa037d2017-02-19 12:57:33 +020094 dispatch({
95 type: licenseAgreementActionTypes.ADD_LICENSE_AGREEMENT,
96 licenseAgreement: {
97 ...licenseAgreement,
98 id: response.value
99 }
100 });
101 });
102 }
103 },
104
AviZi280f8012017-06-09 02:39:56 +0300105 deleteLicenseAgreement(dispatch, {licenseModelId, licenseAgreementId, version}) {
106 return deleteLicenseAgreement(licenseModelId, licenseAgreementId, version).then(() => {
Michael Landoefa037d2017-02-19 12:57:33 +0200107 dispatch({
108 type: licenseAgreementActionTypes.DELETE_LICENSE_AGREEMENT,
109 licenseAgreementId
110 });
111 });
112 },
113
114 selectLicenseAgreementEditorTab(dispatch, {tab}) {
115 dispatch({
116 type: licenseAgreementActionTypes.licenseAgreementEditor.SELECT_TAB,
117 tab
118 });
119 },
120
Michael Landoefa037d2017-02-19 12:57:33 +0200121 switchVersion(dispatch, {licenseModelId, version}) {
122 LicenseModelActionHelper.fetchLicenseModelById(dispatch, {licenseModelId, version}).then(() => {
123 this.fetchLicenseAgreementList(dispatch, {licenseModelId, version});
124 });
125 }
126};