f04f8faf564feb13ae3a8fbc1b9d58d71cadf09e
[sdc.git] /
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * 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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * 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.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import {actionTypes, NEW_RULE_TEMP_ID} from './SoftwareProductDependenciesConstants.js';
19
20 function baseUrl(softwareProductId, version) {
21         const versionId = version.id;
22         const restPrefix = Configuration.get('restPrefix');
23         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/component-dependencies`;
24 }
25
26 function fetchDependencies(softwareProductId, version) {
27         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
28 }
29
30 function addDepencency(softwareProductId, version, item) {
31         return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {
32                 sourceId: item.sourceId,
33                 targetId: item.targetId,
34                 relationType: item.relationType
35         });
36 }
37
38
39 function updateDepencency(softwareProductId, version, item) {
40         return RestAPIUtil.put(`${baseUrl(softwareProductId, version)}/${item.id}`,
41                 {
42                         sourceId: item.sourceId,
43                         targetId: item.targetId,
44                         relationType: item.relationType
45                 });
46 }
47
48 function removeDependency(softwareProductId, version, item) {
49         return RestAPIUtil.destroy(`${baseUrl(softwareProductId, version)}/${item.id}`);
50 }
51
52
53 const SoftwareProductDependenciesActionHelper = {
54         updateDependency(dispatch, {softwareProductId, version, item}) {
55                 // if change was made on existing item - we will update the server and refresh the list
56                 // if change was made on the 'new' row - we will only fire the event
57                 if (item.id !== NEW_RULE_TEMP_ID) {
58                         return updateDepencency(softwareProductId, version, item).then(() => {
59                                 return this.fetchDependencies(dispatch, {softwareProductId, version});
60                         });
61                 } else {
62                         dispatch({
63                                 type: actionTypes.UPDATE_NEW_SOFTWARE_PRODUCT_DEPENDENCY,
64                                 item: item
65                         });
66                 }
67         },
68
69         createDependency(dispatch, {softwareProductId, version, item}) {
70                 // removing the temp id
71                 delete item.id;
72                 // creating the new dependency
73                 return addDepencency(softwareProductId, version, item).then(() => {
74                         dispatch({
75                                 type: actionTypes.ADD_SOFTWARE_PRODUCT_DEPENDENCY
76                         });
77                         return this.fetchDependencies(dispatch, {softwareProductId, version});
78                 });
79         },
80
81         removeDependency(dispatch, {softwareProductId, version, item}) {
82                 return removeDependency(softwareProductId, version, item).then( () => {
83                         return this.fetchDependencies(dispatch, {softwareProductId, version});
84                 });
85         },
86
87         fetchDependencies(dispatch, {softwareProductId, version}) {
88                 return fetchDependencies(softwareProductId, version).then( response => {
89                         dispatch({
90                                 type: actionTypes.SOFTWARE_PRODUCT_DEPENDENCIES_LIST_UPDATE,
91                                 dependenciesList : response.results
92                         });
93                 });
94         }
95 };
96
97 export default SoftwareProductDependenciesActionHelper;