blob: b8772edb081b8f925c35eb81352dda2b4e59bc6a [file] [log] [blame]
Michael Landoefa037d2017-02-19 12:57:33 +02001/*-
2 * ============LICENSE_START=======================================================
3 * SDC
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 */
20
21import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
22import Configuration from 'sdc-app/config/Configuration.js';
23import {actionTypes, enums} from './FlowsConstants.js';
24import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js';
25
26
27function baseUrl(serviceId, artifactId = '') {
28 const restATTPrefix = Configuration.get('restATTPrefix');
29 return `${restATTPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
30}
31
32function encodeDataToBase64(dataAsString) {
33 return window.btoa(dataAsString);
34}
35
36function decodeDataToBase64(encodedData) {
37 return window.atob(encodedData);
38}
39
40function encodeContent(flowData) {
41 let data = {
42 VERSION: {
43 major: 1,
44 minor: 0
45 },
46 description: flowData.description,
47 sequenceDiagramModel: flowData.sequenceDiagramModel
48 };
49
50 return encodeDataToBase64(JSON.stringify(data));
51}
52
53function decodeContent(base64Contents) {
54 let description, sequenceDiagramModel;
55 let payload = JSON.parse(decodeDataToBase64(base64Contents));
56
57 if (payload.VERSION === undefined) {
58 description = payload.description || 'Please, provide description...';
59 sequenceDiagramModel = payload.data || payload;
60 sequenceDiagramModel = sequenceDiagramModel.model || sequenceDiagramModel;
61
62 } else if (payload.VERSION.major === 1) {
63 description = payload.description;
64 sequenceDiagramModel = payload.sequenceDiagramModel;
65 }
66
67 return {
68 description,
69 sequenceDiagramModel
70 };
71}
72
73function createOrUpdate(flowData) {
74 let createOrUpdateRequest = {
75 payloadData: encodeContent(flowData),
76 artifactLabel: flowData.artifactLabel || flowData.artifactName,
77 artifactName: flowData.artifactName,
78 artifactType: flowData.artifactType,
79 artifactGroupType: enums.INFORMATIONAL,
80 description: flowData.description
81 };
82
83 return RestAPIUtil.create(
84 baseUrl(flowData.serviceID, flowData.uniqueId),
85 createOrUpdateRequest,
86 {md5: true}
87 );
88}
89
90const FlowsActions = Object.freeze({
91
92 fetchFlowArtifacts(dispatch, {artifacts, diagramType, participants, serviceID}) {
93 let results = [];
94 if (!Object.keys(artifacts).length) {
95 dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType});
96 FlowsActions.openFlowDetailsEditor(dispatch);
97 }
98 else {
99 Object.keys(artifacts).forEach(artifact => results.push({
100 artifactType: diagramType,
101 participants,
102 serviceID,
103 ...artifacts[artifact]
104 }));
105 dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType});
106 }
107 },
108
109 fetchArtifact(dispatch, {flow}){
110 let {serviceID, uniqueId, participants} = flow;
111 RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(response => {
112
113 let {artifactName, base64Contents} = response;
114 let {sequenceDiagramModel, ...other} = decodeContent(base64Contents);
115
116 if (!sequenceDiagramModel) {
117 sequenceDiagramModel = SequenceDiagramModelHelper.createModel({
118 id: uniqueId,
119 name: artifactName,
120 lifelines: participants
121 });
122 }
123 else {
124 sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(sequenceDiagramModel, {
125 name: artifactName,
126 lifelines: participants
127 });
128 }
129
130 flow = {
131 ...flow,
132 ...other,
133 uniqueId,
134 artifactName,
135 sequenceDiagramModel
136 };
137
138 dispatch({type: actionTypes.ARTIFACT_LOADED, flow});
139 FlowsActions.openFlowDiagramEditor(dispatch, {flow});
140 });
141 },
142
143 createOrUpdateFlow(dispatch, {flow}, isNew) {
144 if (!isNew && flow.sequenceDiagramModel) {
145 flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(flow.sequenceDiagramModel, {
146 name: flow.artifactName
147 });
148 }
149 createOrUpdate(flow).then(response => {
150 let {uniqueId, artifactLabel} = response;
151 flow = {...flow, uniqueId, artifactLabel};
152 if (isNew) {
153 flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel({id: uniqueId, name: flow.artifactName});
154 }
155 dispatch({type: actionTypes.ADD_OR_UPDATE_FLOW, flow});
156 });
157 },
158
159 deleteFlow(dispatch, {flow}) {
160 RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(() => dispatch({
161 type: actionTypes.DELETE_FLOW,
162 flow
163 }));
164 },
165
166 openFlowDetailsEditor(dispatch, flow) {
167 dispatch({type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow});
168 },
169
170 closeFlowDetailsEditor(dispatch) {
171 dispatch({type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR});
172 },
173
174 openFlowDiagramEditor(dispatch, {flow}) {
175 dispatch({type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow});
176 },
177
178 closeFlowDiagramEditor(dispatch) {
179 dispatch({type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR});
180 },
181
182 flowDetailsDataChanged(dispatch, {deltaData}) {
183 dispatch({type: actionTypes.CURRENT_FLOW_DATA_CHANGED, deltaData});
184 },
185
186 reset(dispatch) {
187 dispatch({type: actionTypes.RESET});
188 }
189});
190
191export default FlowsActions;