4bbcaa13d50ee7bb13665a3d53318ecbc739ed40
[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 {
19     actionTypes,
20     NEW_RULE_TEMP_ID
21 } from './SoftwareProductDependenciesConstants.js';
22
23 function baseUrl(softwareProductId, version) {
24     const versionId = version.id;
25     const restPrefix = Configuration.get('restPrefix');
26     return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/component-dependencies`;
27 }
28
29 function fetchDependencies(softwareProductId, version) {
30     return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
31 }
32
33 function addDepencency(softwareProductId, version, item) {
34     return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {
35         sourceId: item.sourceId,
36         targetId: item.targetId,
37         relationType: item.relationType
38     });
39 }
40
41 function updateDepencency(softwareProductId, version, item) {
42     return RestAPIUtil.put(
43         `${baseUrl(softwareProductId, version)}/${item.id}`,
44         {
45             sourceId: item.sourceId,
46             targetId: item.targetId,
47             relationType: item.relationType
48         }
49     );
50 }
51
52 function removeDependency(softwareProductId, version, item) {
53     return RestAPIUtil.destroy(
54         `${baseUrl(softwareProductId, version)}/${item.id}`
55     );
56 }
57
58 const SoftwareProductDependenciesActionHelper = {
59     updateDependency(dispatch, { softwareProductId, version, item }) {
60         // if change was made on existing item - we will update the server and refresh the list
61         // if change was made on the 'new' row - we will only fire the event
62         if (item.id !== NEW_RULE_TEMP_ID) {
63             return updateDepencency(softwareProductId, version, item).then(
64                 () => {
65                     return this.fetchDependencies(dispatch, {
66                         softwareProductId,
67                         version
68                     });
69                 }
70             );
71         } else {
72             dispatch({
73                 type: actionTypes.UPDATE_NEW_SOFTWARE_PRODUCT_DEPENDENCY,
74                 item: item
75             });
76         }
77     },
78
79     createDependency(dispatch, { softwareProductId, version, item }) {
80         // removing the temp id
81         delete item.id;
82         // creating the new dependency
83         return addDepencency(softwareProductId, version, item).then(() => {
84             dispatch({
85                 type: actionTypes.ADD_SOFTWARE_PRODUCT_DEPENDENCY
86             });
87             return this.fetchDependencies(dispatch, {
88                 softwareProductId,
89                 version
90             });
91         });
92     },
93
94     removeDependency(dispatch, { softwareProductId, version, item }) {
95         return removeDependency(softwareProductId, version, item).then(() => {
96             return this.fetchDependencies(dispatch, {
97                 softwareProductId,
98                 version
99             });
100         });
101     },
102
103     fetchDependencies(dispatch, { softwareProductId, version }) {
104         return fetchDependencies(softwareProductId, version).then(response => {
105             dispatch({
106                 type: actionTypes.SOFTWARE_PRODUCT_DEPENDENCIES_LIST_UPDATE,
107                 dependenciesList: response.results
108             });
109         });
110     }
111 };
112
113 export default SoftwareProductDependenciesActionHelper;