Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2016-2018 European Support Limited |
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 | * |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 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, |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * 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'; |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 20 | import { actionTypes as modalActionTypes } from 'nfvo-components/modal/GlobalModalConstants.js'; |
| 21 | import { modalContentMapper } from 'sdc-app/common/modal/ModalContentMapper.js'; |
| 22 | import i18n from 'nfvo-utils/i18n/i18n.js'; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 23 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 24 | function baseUrl(serviceId, artifactId = '') { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 25 | const restCatalogPrefix = Configuration.get('restCatalogPrefix'); |
| 26 | return `${restCatalogPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | function encodeDataToBase64(dataAsString) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 30 | return window.btoa(dataAsString); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 31 | } |
| 32 | |
| 33 | function decodeDataToBase64(encodedData) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 34 | return window.atob(encodedData); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | function encodeContent(flowData) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 38 | let data = { |
| 39 | VERSION: { |
| 40 | major: 1, |
| 41 | minor: 0 |
| 42 | }, |
| 43 | description: flowData.description, |
| 44 | sequenceDiagramModel: flowData.sequenceDiagramModel |
| 45 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 46 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 47 | return encodeDataToBase64(JSON.stringify(data)); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | function decodeContent(base64Contents) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 51 | let description, sequenceDiagramModel; |
| 52 | let payload = JSON.parse(decodeDataToBase64(base64Contents)); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 53 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 54 | if (payload.VERSION === undefined) { |
| 55 | description = payload.description || 'Please, provide description...'; |
| 56 | sequenceDiagramModel = payload.data || payload; |
| 57 | sequenceDiagramModel = |
| 58 | sequenceDiagramModel.model || sequenceDiagramModel; |
| 59 | } else if (payload.VERSION.major === 1) { |
| 60 | description = payload.description; |
| 61 | sequenceDiagramModel = payload.sequenceDiagramModel; |
| 62 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 63 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 64 | return { |
| 65 | description, |
| 66 | sequenceDiagramModel |
| 67 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | function createOrUpdate(flowData) { |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 71 | let createOrUpdateRequest = { |
| 72 | payloadData: encodeContent(flowData), |
| 73 | artifactLabel: flowData.artifactLabel || flowData.artifactName, |
| 74 | artifactName: flowData.artifactName, |
| 75 | artifactType: flowData.artifactType, |
| 76 | artifactGroupType: enums.INFORMATIONAL, |
| 77 | description: flowData.description |
| 78 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 79 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 80 | return RestAPIUtil.post( |
| 81 | baseUrl(flowData.serviceID, flowData.uniqueId), |
| 82 | createOrUpdateRequest, |
| 83 | { md5: true } |
| 84 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | const FlowsActions = Object.freeze({ |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 88 | fetchFlowArtifacts( |
| 89 | dispatch, |
| 90 | { artifacts, diagramType, participants, serviceID, readonly } |
| 91 | ) { |
| 92 | let results = []; |
| 93 | if (!Object.keys(artifacts).length) { |
| 94 | dispatch({ |
| 95 | type: actionTypes.FLOW_LIST_LOADED, |
| 96 | results, |
| 97 | participants, |
| 98 | serviceID, |
| 99 | diagramType, |
| 100 | readonly |
| 101 | }); |
| 102 | if (!readonly) { |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 103 | FlowsActions.openEditCreateWFModal(dispatch); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 104 | } |
| 105 | } else { |
| 106 | Object.keys(artifacts).forEach(artifact => |
| 107 | results.push({ |
| 108 | artifactType: diagramType, |
| 109 | participants, |
| 110 | serviceID, |
| 111 | ...artifacts[artifact] |
| 112 | }) |
| 113 | ); |
| 114 | dispatch({ |
| 115 | type: actionTypes.FLOW_LIST_LOADED, |
| 116 | results, |
| 117 | participants, |
| 118 | serviceID, |
| 119 | diagramType, |
| 120 | readonly |
| 121 | }); |
| 122 | } |
| 123 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 124 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 125 | fetchArtifact(dispatch, { flow }) { |
| 126 | let { serviceID, uniqueId, participants } = flow; |
| 127 | return RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then( |
| 128 | response => { |
| 129 | let { artifactName, base64Contents } = response; |
| 130 | let { sequenceDiagramModel, ...other } = decodeContent( |
| 131 | base64Contents |
| 132 | ); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 133 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 134 | if (!sequenceDiagramModel) { |
| 135 | sequenceDiagramModel = SequenceDiagramModelHelper.createModel( |
| 136 | { |
| 137 | id: uniqueId, |
| 138 | name: artifactName, |
| 139 | lifelines: participants |
| 140 | } |
| 141 | ); |
| 142 | } else { |
| 143 | sequenceDiagramModel = SequenceDiagramModelHelper.updateModel( |
| 144 | sequenceDiagramModel, |
| 145 | { |
| 146 | name: artifactName, |
| 147 | lifelines: participants |
| 148 | } |
| 149 | ); |
| 150 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 151 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 152 | flow = { |
| 153 | ...flow, |
| 154 | ...other, |
| 155 | uniqueId, |
| 156 | artifactName, |
| 157 | sequenceDiagramModel |
| 158 | }; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 159 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 160 | dispatch({ type: actionTypes.ARTIFACT_LOADED, flow }); |
| 161 | FlowsActions.openFlowDiagramEditor(dispatch, { flow }); |
| 162 | } |
| 163 | ); |
| 164 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 165 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 166 | createOrUpdateFlow(dispatch, { flow }, isNew) { |
| 167 | if (!isNew && flow.sequenceDiagramModel) { |
| 168 | flow.sequenceDiagramModel = SequenceDiagramModelHelper.updateModel( |
| 169 | flow.sequenceDiagramModel, |
| 170 | { |
| 171 | name: flow.artifactName |
| 172 | } |
| 173 | ); |
| 174 | } |
| 175 | return createOrUpdate(flow).then(response => { |
| 176 | let { uniqueId, artifactLabel } = response; |
| 177 | flow = { ...flow, uniqueId, artifactLabel }; |
| 178 | if (isNew) { |
| 179 | flow.sequenceDiagramModel = SequenceDiagramModelHelper.createModel( |
| 180 | { |
| 181 | id: uniqueId, |
| 182 | name: flow.artifactName |
| 183 | } |
| 184 | ); |
| 185 | } |
| 186 | dispatch({ type: actionTypes.ADD_OR_UPDATE_FLOW, flow }); |
| 187 | }); |
| 188 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 189 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 190 | deleteFlow(dispatch, { flow }) { |
| 191 | return RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then( |
| 192 | () => |
| 193 | dispatch({ |
| 194 | type: actionTypes.DELETE_FLOW, |
| 195 | flow |
| 196 | }) |
| 197 | ); |
| 198 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 199 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 200 | openFlowDiagramEditor(dispatch, { flow }) { |
| 201 | dispatch({ type: actionTypes.OPEN_FLOW_DIAGRAM_EDITOR, flow }); |
| 202 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 203 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 204 | closeFlowDiagramEditor(dispatch) { |
| 205 | dispatch({ type: actionTypes.CLOSE_FLOW_DIAGRAM_EDITOR }); |
| 206 | }, |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 207 | |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 208 | reset(dispatch) { |
| 209 | dispatch({ type: actionTypes.RESET }); |
Einav Weiss Keidar | 1801b24 | 2018-08-13 16:19:46 +0300 | [diff] [blame] | 210 | }, |
| 211 | openEditCreateWFModal(dispatch, flow) { |
| 212 | dispatch({ type: actionTypes.OPEN_FLOW_DETAILS_EDITOR, flow }); |
| 213 | dispatch({ |
| 214 | type: modalActionTypes.GLOBAL_MODAL_SHOW, |
| 215 | data: { |
| 216 | modalComponentName: modalContentMapper.FLOWS_EDITOR, |
| 217 | modalComponentProps: { |
| 218 | isNewArtifact: Boolean(flow && flow.uniqueId) |
| 219 | }, |
| 220 | title: flow |
| 221 | ? i18n('Edit Workflow') |
| 222 | : i18n('Create New Workflow') |
| 223 | } |
| 224 | }); |
| 225 | }, |
| 226 | closeEditCreateWFModal(dispatch) { |
| 227 | dispatch({ |
| 228 | type: modalActionTypes.GLOBAL_MODAL_CLOSE |
| 229 | }); |
| 230 | dispatch({ type: actionTypes.CLOSE_FLOW_DETAILS_EDITOR }); |
Einav Weiss Keidar | 7fdf733 | 2018-03-20 14:45:40 +0200 | [diff] [blame] | 231 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 232 | }); |
| 233 | |
| 234 | export default FlowsActions; |