[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / softwareProduct / dependencies / SoftwareProductDependenciesActionHelper.js
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} from './SoftwareProductDependenciesConstants.js';
19 import uuid from 'uuid-js';
20
21 function baseUrl(softwareProductId, version) {
22         const versionId = version.id;
23         const restPrefix = Configuration.get('restPrefix');
24         return `${restPrefix}/v1.0/vendor-software-products/${softwareProductId}/versions/${versionId}/component-dependency-model`;
25 }
26
27 function fetchDependency(softwareProductId, version) {
28         return RestAPIUtil.fetch(`${baseUrl(softwareProductId, version)}`);
29 }
30
31 function postDependency(softwareProductId, version, dependenciesList) {
32         let modifedDependencyList = dependenciesList ? dependenciesList.filter(item => item.sourceId && item.targetId)
33         .map(item => ({sourceId: item.sourceId, targetId: item.targetId, relationType: item.relationType})) : [];
34         return RestAPIUtil.post(`${baseUrl(softwareProductId, version)}`, {componentDependencyModels:modifedDependencyList});
35 }
36
37 const SoftwareProductDependenciesActionHelper = {
38         updateDependencyList(dispatch, {dependenciesList}) {            
39                 dispatch({type: actionTypes.SOFTWARE_PRODUCT_DEPENDENCIES_LIST_UPDATE, dependenciesList});
40         },
41         addDependency(dispatch) {
42                 dispatch({type: actionTypes.ADD_SOFTWARE_PRODUCT_DEPENDENCY});
43         },
44         fetchDependencies(dispatch, {softwareProductId, version}) {
45                 return fetchDependency(softwareProductId, version).then( response => {  
46                         const dependenciesList = response.results ? response.results.map(item => {return {...item, id: uuid.create().toString()};}) : [];                                                                       
47                         dispatch({
48                                 type: actionTypes.SOFTWARE_PRODUCT_DEPENDENCIES_LIST_UPDATE,
49                                 dependenciesList
50                         });
51                 });
52         },
53         saveDependencies(dispatch, {softwareProductId, version, dependenciesList}) {
54                 return postDependency(softwareProductId, version, dependenciesList);
55         }       
56 };
57
58 export default SoftwareProductDependenciesActionHelper;