[SDC-29] Amdocs OnBoard 1707 initial commit.
[sdc.git] / openecomp-ui / src / sdc-app / flows / FlowsActions.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, enums} from './FlowsConstants.js';
19 import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js';
20
21
22 function baseUrl(serviceId, artifactId = '') {
23         const restATTPrefix = Configuration.get('restATTPrefix');
24         return `${restATTPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
25 }
26
27 function encodeDataToBase64(dataAsString) {
28         return window.btoa(dataAsString);
29 }
30
31 function decodeDataToBase64(encodedData) {
32         return window.atob(encodedData);
33 }
34
35 function encodeContent(flowData) {
36         let data = {
37                 VERSION: {
38                         major: 1,
39                         minor: 0
40                 },
41                 description: flowData.description,
42                 sequenceDiagramModel: flowData.sequenceDiagramModel
43         };
44
45         return encodeDataToBase64(JSON.stringify(data));
46 }
47
48 function decodeContent(base64Contents) {
49         let description, sequenceDiagramModel;
50         let payload = JSON.parse(decodeDataToBase64(base64Contents));
51
52         if (payload.VERSION === undefined) {
53                 description = payload.description || 'Please, provide description...';
54                 sequenceDiagramModel = payload.data || payload;
55                 sequenceDiagramModel = sequenceDiagramModel.model || sequenceDiagramModel;
56
57         }
58         else if (payload.VERSION.major === 1) {
59                 description = payload.description;
60                 sequenceDiagramModel = payload.sequenceDiagramModel;
61         }
62
63         return {
64                 description,
65                 sequenceDiagramModel
66         };
67 }
68
69 function createOrUpdate(flowData) {
70         let createOrUpdateRequest = {
71                 payloadData: encodeContent(flowData),
72                 artifactLabel: flowData.artifactLabel || flowData.artifactName,
73                 artifactName: flowData.artifactName,
74                 artifactType: flowData.artifactType,
75                 artifactGroupType: enums.INFORMATIONAL,
76                 description: flowData.description
77         };
78
79         return RestAPIUtil.post(
80                 baseUrl(flowData.serviceID, flowData.uniqueId),
81                 createOrUpdateRequest,
82                 {md5: true}
83         );
84 }
85
86 const FlowsActions = Object.freeze({
87
88         fetchFlowArtifacts(dispatch, {artifacts, diagramType, participants, serviceID, readonly}) {
89                 let results = [];
90                 if (!Object.keys(artifacts).length) {
91                         dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType, readonly});
92                         if (!readonly) {
93                                 FlowsActions.openFlowDetailsEditor(dispatch);
94                         }
95                 }
96                 else {
97                         Object.keys(artifacts).forEach(artifact => results.push({
98                                 artifactType: diagramType,
99                                 participants,
100                                 serviceID,
101                                 ...artifacts[artifact]
102                         }));
103                         dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType, readonly});
104                 }
105         },
106
107         fetchArtifact(dispatch, {flow}){
108                 let {serviceID, uniqueId, participants} = flow;
109                 return RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(response => {
110
111                         let {artifactName, base64Contents} = response;
112                         let {sequenceDiagramModel, ...other} = decodeContent(base64Contents);
113
114                         if (!sequenceDiagramModel) {
115                                 sequenceDiagramModel = SequenceDiagramModelHelper.createModel({
116                                         id: uniqueId,
117                                         name: artifactName,
118                                         lifelines: participants
119                                 });
120                         }
121                         else {
122                                 sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(sequenceDiagramModel, {
123                                         name: artifactName,
124                                         lifelines: participants
125                                 });
126                         }
127
128                         flow = {
129                                 ...flow,
130                                 ...other,
131                                 uniqueId,
132                                 artifactName,
133                                 sequenceDiagramModel
134                         };
135
136                         dispatch({type: actionTypes.ARTIFACT_LOADED, flow});
137                         FlowsActions.openFlowDiagramEditor(dispatch, {flow});
138                 });
139         },
140
141         createOrUpdateFlow(dispatch, {flow}, isNew) {
142                 if (!isNew && flow.sequenceDiagramModel) {
143                         flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(flow.sequenceDiagramModel, {
144                                 name: flow.artifactName
145                         });
146                 }
147                 return createOrUpdate(flow).then(response => {
148                         let {uniqueId, artifactLabel} = response;
149                         flow = {...flow, uniqueId, artifactLabel};
150                         if (isNew) {
151                                 flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel({id: uniqueId, name: flow.artifactName});
152                         }
153                         dispatch({type: actionTypes.ADD_OR_UPDATE_FLOW, flow});
154                 });
155         },
156
157         deleteFlow(dispatch, {flow}) {
158                 return RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(() => dispatch({
159                         type: actionTypes.DELETE_FLOW,
160                         flow
161                 }));
162         },
163
164         openFlowDetailsEditor(dispatch, flow) {
165                 dispatch({type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow});
166         },
167
168         closeFlowDetailsEditor(dispatch) {
169                 dispatch({type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR});
170         },
171
172         openFlowDiagramEditor(dispatch, {flow}) {
173                 dispatch({type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow});
174         },
175
176         closeFlowDiagramEditor(dispatch) {
177                 dispatch({type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR});
178         },
179
180         reset(dispatch) {
181                 dispatch({type: actionTypes.RESET});
182         }
183 });
184
185 export default FlowsActions;