blob: 9fb65f37d4f02920bc080c697522acce11459cb2 [file] [log] [blame]
AviZi280f8012017-06-09 02:39:56 +03001/*!
Michael Landoefa037d2017-02-19 12:57:33 +02002 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
AviZi280f8012017-06-09 02:39:56 +03003 *
Michael Landoefa037d2017-02-19 12:57:33 +02004 * 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
AviZi280f8012017-06-09 02:39:56 +03007 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
Michael Landoefa037d2017-02-19 12:57:33 +020010 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
AviZi280f8012017-06-09 02:39:56 +030012 * 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.
Michael Landoefa037d2017-02-19 12:57:33 +020015 */
Michael Landoefa037d2017-02-19 12:57:33 +020016import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17import Configuration from 'sdc-app/config/Configuration.js';
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020018import { actionTypes, enums } from './FlowsConstants.js';
Michael Landoefa037d2017-02-19 12:57:33 +020019import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js';
20
Michael Landoefa037d2017-02-19 12:57:33 +020021function baseUrl(serviceId, artifactId = '') {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020022 const restCatalogPrefix = Configuration.get('restCatalogPrefix');
23 return `${restCatalogPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`;
Michael Landoefa037d2017-02-19 12:57:33 +020024}
25
26function encodeDataToBase64(dataAsString) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020027 return window.btoa(dataAsString);
Michael Landoefa037d2017-02-19 12:57:33 +020028}
29
30function decodeDataToBase64(encodedData) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020031 return window.atob(encodedData);
Michael Landoefa037d2017-02-19 12:57:33 +020032}
33
34function encodeContent(flowData) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020035 let data = {
36 VERSION: {
37 major: 1,
38 minor: 0
39 },
40 description: flowData.description,
41 sequenceDiagramModel: flowData.sequenceDiagramModel
42 };
Michael Landoefa037d2017-02-19 12:57:33 +020043
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020044 return encodeDataToBase64(JSON.stringify(data));
Michael Landoefa037d2017-02-19 12:57:33 +020045}
46
47function decodeContent(base64Contents) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020048 let description, sequenceDiagramModel;
49 let payload = JSON.parse(decodeDataToBase64(base64Contents));
Michael Landoefa037d2017-02-19 12:57:33 +020050
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020051 if (payload.VERSION === undefined) {
52 description = payload.description || 'Please, provide description...';
53 sequenceDiagramModel = payload.data || payload;
54 sequenceDiagramModel =
55 sequenceDiagramModel.model || sequenceDiagramModel;
56 } else if (payload.VERSION.major === 1) {
57 description = payload.description;
58 sequenceDiagramModel = payload.sequenceDiagramModel;
59 }
Michael Landoefa037d2017-02-19 12:57:33 +020060
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020061 return {
62 description,
63 sequenceDiagramModel
64 };
Michael Landoefa037d2017-02-19 12:57:33 +020065}
66
67function createOrUpdate(flowData) {
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020068 let createOrUpdateRequest = {
69 payloadData: encodeContent(flowData),
70 artifactLabel: flowData.artifactLabel || flowData.artifactName,
71 artifactName: flowData.artifactName,
72 artifactType: flowData.artifactType,
73 artifactGroupType: enums.INFORMATIONAL,
74 description: flowData.description
75 };
Michael Landoefa037d2017-02-19 12:57:33 +020076
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020077 return RestAPIUtil.post(
78 baseUrl(flowData.serviceID, flowData.uniqueId),
79 createOrUpdateRequest,
80 { md5: true }
81 );
Michael Landoefa037d2017-02-19 12:57:33 +020082}
83
84const FlowsActions = Object.freeze({
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +020085 fetchFlowArtifacts(
86 dispatch,
87 { artifacts, diagramType, participants, serviceID, readonly }
88 ) {
89 let results = [];
90 if (!Object.keys(artifacts).length) {
91 dispatch({
92 type: actionTypes.FLOW_LIST_LOADED,
93 results,
94 participants,
95 serviceID,
96 diagramType,
97 readonly
98 });
99 if (!readonly) {
100 FlowsActions.openFlowDetailsEditor(dispatch);
101 }
102 } else {
103 Object.keys(artifacts).forEach(artifact =>
104 results.push({
105 artifactType: diagramType,
106 participants,
107 serviceID,
108 ...artifacts[artifact]
109 })
110 );
111 dispatch({
112 type: actionTypes.FLOW_LIST_LOADED,
113 results,
114 participants,
115 serviceID,
116 diagramType,
117 readonly
118 });
119 }
120 },
Michael Landoefa037d2017-02-19 12:57:33 +0200121
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200122 fetchArtifact(dispatch, { flow }) {
123 let { serviceID, uniqueId, participants } = flow;
124 return RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(
125 response => {
126 let { artifactName, base64Contents } = response;
127 let { sequenceDiagramModel, ...other } = decodeContent(
128 base64Contents
129 );
Michael Landoefa037d2017-02-19 12:57:33 +0200130
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200131 if (!sequenceDiagramModel) {
132 sequenceDiagramModel = SequenceDiagramModelHelper.createModel(
133 {
134 id: uniqueId,
135 name: artifactName,
136 lifelines: participants
137 }
138 );
139 } else {
140 sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(
141 sequenceDiagramModel,
142 {
143 name: artifactName,
144 lifelines: participants
145 }
146 );
147 }
Michael Landoefa037d2017-02-19 12:57:33 +0200148
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200149 flow = {
150 ...flow,
151 ...other,
152 uniqueId,
153 artifactName,
154 sequenceDiagramModel
155 };
Michael Landoefa037d2017-02-19 12:57:33 +0200156
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200157 dispatch({ type: actionTypes.ARTIFACT_LOADED, flow });
158 FlowsActions.openFlowDiagramEditor(dispatch, { flow });
159 }
160 );
161 },
Michael Landoefa037d2017-02-19 12:57:33 +0200162
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200163 createOrUpdateFlow(dispatch, { flow }, isNew) {
164 if (!isNew && flow.sequenceDiagramModel) {
165 flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel(
166 flow.sequenceDiagramModel,
167 {
168 name: flow.artifactName
169 }
170 );
171 }
172 return createOrUpdate(flow).then(response => {
173 let { uniqueId, artifactLabel } = response;
174 flow = { ...flow, uniqueId, artifactLabel };
175 if (isNew) {
176 flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel(
177 {
178 id: uniqueId,
179 name: flow.artifactName
180 }
181 );
182 }
183 dispatch({ type: actionTypes.ADD_OR_UPDATE_FLOW, flow });
184 });
185 },
Michael Landoefa037d2017-02-19 12:57:33 +0200186
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200187 deleteFlow(dispatch, { flow }) {
188 return RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(
189 () =>
190 dispatch({
191 type: actionTypes.DELETE_FLOW,
192 flow
193 })
194 );
195 },
Michael Landoefa037d2017-02-19 12:57:33 +0200196
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200197 openFlowDetailsEditor(dispatch, flow) {
198 dispatch({ type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow });
199 },
Michael Landoefa037d2017-02-19 12:57:33 +0200200
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200201 closeFlowDetailsEditor(dispatch) {
202 dispatch({ type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR });
203 },
Michael Landoefa037d2017-02-19 12:57:33 +0200204
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200205 openFlowDiagramEditor(dispatch, { flow }) {
206 dispatch({ type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow });
207 },
Michael Landoefa037d2017-02-19 12:57:33 +0200208
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200209 closeFlowDiagramEditor(dispatch) {
210 dispatch({ type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR });
211 },
Michael Landoefa037d2017-02-19 12:57:33 +0200212
Einav Weiss Keidar7fdf7332018-03-20 14:45:40 +0200213 reset(dispatch) {
214 dispatch({ type: actionTypes.RESET });
215 }
Michael Landoefa037d2017-02-19 12:57:33 +0200216});
217
218export default FlowsActions;