AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 1 | /*! |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 2 | * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 3 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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. |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 15 | */ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 16 | import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js'; |
| 17 | import Configuration from 'sdc-app/config/Configuration.js'; |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 18 | import { actionTypes, enums } from './FlowsConstants.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 19 | import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js'; |
| 20 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 21 | function baseUrl(serviceId, artifactId = '') { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 22 | const restCatalogPrefix = Configuration.get('restCatalogPrefix'); |
| 23 | return `${restCatalogPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | function encodeDataToBase64(dataAsString) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 27 | return window.btoa(dataAsString); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | function decodeDataToBase64(encodedData) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 31 | return window.atob(encodedData); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | function encodeContent(flowData) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 35 | let data = { |
| 36 | VERSION: { |
| 37 | major: 1, |
| 38 | minor: 0 |
| 39 | }, |
| 40 | description: flowData.description, |
| 41 | sequenceDiagramModel: flowData.sequenceDiagramModel |
| 42 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 43 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 44 | return encodeDataToBase64(JSON.stringify(data)); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | function decodeContent(base64Contents) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 48 | let description, sequenceDiagramModel; |
| 49 | let payload = JSON.parse(decodeDataToBase64(base64Contents)); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 50 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 51 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 60 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 61 | return { |
| 62 | description, |
| 63 | sequenceDiagramModel |
| 64 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | function createOrUpdate(flowData) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 68 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 76 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 77 | return RestAPIUtil.post( |
| 78 | baseUrl(flowData.serviceID, flowData.uniqueId), |
| 79 | createOrUpdateRequest, |
| 80 | { md5: true } |
| 81 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | const FlowsActions = Object.freeze({ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 85 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 121 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 122 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 130 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 131 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 148 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 149 | flow = { |
| 150 | ...flow, |
| 151 | ...other, |
| 152 | uniqueId, |
| 153 | artifactName, |
| 154 | sequenceDiagramModel |
| 155 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 156 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 157 | dispatch({ type: actionTypes.ARTIFACT_LOADED, flow }); |
| 158 | FlowsActions.openFlowDiagramEditor(dispatch, { flow }); |
| 159 | } |
| 160 | ); |
| 161 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 162 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 163 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 186 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 187 | 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 Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 196 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 197 | openFlowDetailsEditor(dispatch, flow) { |
| 198 | dispatch({ type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow }); |
| 199 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 200 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 201 | closeFlowDetailsEditor(dispatch) { |
| 202 | dispatch({ type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR }); |
| 203 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 204 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 205 | openFlowDiagramEditor(dispatch, { flow }) { |
| 206 | dispatch({ type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow }); |
| 207 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 208 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 209 | closeFlowDiagramEditor(dispatch) { |
| 210 | dispatch({ type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR }); |
| 211 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 212 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 213 | reset(dispatch) { |
| 214 | dispatch({ type: actionTypes.RESET }); |
| 215 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 216 | }); |
| 217 | |
| 218 | export default FlowsActions; |