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'; |
| 18 | import {actionTypes, enums} from './FlowsConstants.js'; |
| 19 | import SequenceDiagramModelHelper from './SequenceDiagramModelHelper.js'; |
| 20 | |
| 21 | |
| 22 | function baseUrl(serviceId, artifactId = '') { |
ilanap | 785dc1e | 2018-01-08 15:50:18 +0200 | [diff] [blame^] | 23 | const restCatalogPrefix = Configuration.get('restCatalogPrefix'); |
| 24 | return `${restCatalogPrefix}/v1/catalog/services/${serviceId}/artifacts/${artifactId}`; |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 57 | } |
| 58 | else if (payload.VERSION.major === 1) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 79 | return RestAPIUtil.post( |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 80 | baseUrl(flowData.serviceID, flowData.uniqueId), |
| 81 | createOrUpdateRequest, |
| 82 | {md5: true} |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | const FlowsActions = Object.freeze({ |
| 87 | |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 88 | fetchFlowArtifacts(dispatch, {artifacts, diagramType, participants, serviceID, readonly}) { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 89 | let results = []; |
| 90 | if (!Object.keys(artifacts).length) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 91 | dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType, readonly}); |
| 92 | if (!readonly) { |
| 93 | FlowsActions.openFlowDetailsEditor(dispatch); |
| 94 | } |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 95 | } |
| 96 | else { |
| 97 | Object.keys(artifacts).forEach(artifact => results.push({ |
| 98 | artifactType: diagramType, |
| 99 | participants, |
| 100 | serviceID, |
| 101 | ...artifacts[artifact] |
| 102 | })); |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 103 | dispatch({type: actionTypes.FLOW_LIST_LOADED, results, participants, serviceID, diagramType, readonly}); |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 104 | } |
| 105 | }, |
| 106 | |
| 107 | fetchArtifact(dispatch, {flow}){ |
| 108 | let {serviceID, uniqueId, participants} = flow; |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 109 | return RestAPIUtil.fetch(baseUrl(serviceID, uniqueId)).then(response => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | } |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 147 | return createOrUpdate(flow).then(response => { |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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}) { |
AviZi | 280f801 | 2017-06-09 02:39:56 +0300 | [diff] [blame] | 158 | return RestAPIUtil.destroy(baseUrl(flow.serviceID, flow.uniqueId)).then(() => dispatch({ |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 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 | |
Michael Lando | efa037d | 2017-02-19 12:57:33 +0200 | [diff] [blame] | 180 | reset(dispatch) { |
| 181 | dispatch({type: actionTypes.RESET}); |
| 182 | } |
| 183 | }); |
| 184 | |
| 185 | export default FlowsActions; |