f41faa3737e387df43a55a3e42f3bd097b16d26a
[sdc/sdc-workflow-designer.git] /
1 /**
2  * Copyright (c) 2017-2018 ZTE Corporation.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the Apache License, Version 2.0
5  * and the Eclipse Public License v1.0 which both accompany this distribution,
6  * and are available at http://www.eclipse.org/legal/epl-v10.html
7  * and http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Contributors:
10  *     ZTE - initial API and implementation and/or initial documentation
11  */
12 package org.onap.sdc.workflowdesigner.parser;
13
14 import java.io.IOException;
15 import java.net.MalformedURLException;
16 import java.net.URI;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
21 import java.util.Map;
22
23 import org.onap.sdc.workflowdesigner.model.DataObject;
24 import org.onap.sdc.workflowdesigner.model.Element;
25 import org.onap.sdc.workflowdesigner.model.EndEvent;
26 import org.onap.sdc.workflowdesigner.model.ErrorEndEvent;
27 import org.onap.sdc.workflowdesigner.model.ErrorStartEvent;
28 import org.onap.sdc.workflowdesigner.model.ExclusiveGateway;
29 import org.onap.sdc.workflowdesigner.model.IntermediateCatchEvent;
30 import org.onap.sdc.workflowdesigner.model.ParallelGateway;
31 import org.onap.sdc.workflowdesigner.model.Parameter;
32 import org.onap.sdc.workflowdesigner.model.Process;
33 import org.onap.sdc.workflowdesigner.model.RestServiceTask;
34 import org.onap.sdc.workflowdesigner.model.ScriptTask;
35 import org.onap.sdc.workflowdesigner.model.SequenceFlow;
36 import org.onap.sdc.workflowdesigner.model.ServiceTask;
37 import org.onap.sdc.workflowdesigner.model.StartEvent;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 import com.fasterxml.jackson.core.JsonParseException;
42 import com.fasterxml.jackson.databind.DeserializationFeature;
43 import com.fasterxml.jackson.databind.JsonMappingException;
44 import com.fasterxml.jackson.databind.JsonNode;
45 import com.fasterxml.jackson.databind.ObjectMapper;
46 import com.fasterxml.jackson.databind.SerializationFeature;
47
48 public class Bpmn4ToscaJsonParser {
49
50     private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);
51
52     private static ObjectMapper MAPPER = new ObjectMapper();
53     
54     private Map<String, JsonNode> restConfigMap = new HashMap<String, JsonNode>();
55
56     static {
57         MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
58         MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
59     }
60
61     public Process parse(String processName, URI jsonFileUrl)
62             throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
63         Process process = new Process(processName);
64
65         JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);
66
67         log.debug("Creating Process models...");
68 //      JsonNode nodes = rootNode.get(JsonKeys.NODES);
69         JsonNode data = rootNode.get(JsonKeys.DATA);
70         if(null == data) {
71             return process;
72         }
73         JsonNode nodes = data.get(JsonKeys.NODES);
74         if (nodes == null) {
75             return process;
76         }
77         
78         this.loadConfigs(rootNode.get(JsonKeys.CONFIGS));
79
80         Iterator<JsonNode> iter = nodes.iterator();
81         while (iter.hasNext()) {
82             JsonNode jsonNode = (JsonNode) iter.next();
83
84             // get element
85             Element element = createElementFromJson(jsonNode);
86             process.getElementList().add(element);
87
88             // get sequence flows
89             List<SequenceFlow> flowList = getSequenceFlows(jsonNode);
90             process.getSequenceFlowList().addAll(flowList);
91
92             // add dataObject
93             if (element instanceof StartEvent) {
94                 List<DataObject> dataObjects = this.getDataObject((StartEvent) element);
95                 process.getDataObjectList().addAll(dataObjects);
96             }
97         }
98
99         return process;
100
101     }
102
103     private List<DataObject> getDataObject(StartEvent startEvent) {
104         List<DataObject> dataObjects = new ArrayList<DataObject>();
105
106         for (Parameter parameter : startEvent.getParameters()) {
107             DataObject dataObject = new DataObject();
108             dataObject.setId(parameter.getName());
109             dataObject.setName(parameter.getName());
110             dataObject.setValue((String) parameter.getValue());
111
112             dataObjects.add(dataObject);
113         }
114
115         return dataObjects;
116     }
117     
118     private void loadConfigs(JsonNode config) {
119         if(config == null) {
120             return;
121         }
122         loadRestConfigs(config.get(JsonKeys.REST_CONFIGS));
123     }
124     
125     private void loadRestConfigs(JsonNode restConfigs) {
126         if(restConfigs == null) {
127             return;
128         }
129         
130         Iterator<JsonNode> iter = restConfigs.iterator();
131         while (iter.hasNext()) {
132             JsonNode restConfig = (JsonNode) iter.next();
133
134             String configId = getValueFromJsonNode(restConfig, JsonKeys.ID); 
135             restConfigMap.put(configId, restConfig);
136         }
137     }
138
139     private List<SequenceFlow> getSequenceFlows(JsonNode jsonNode) {
140         List<SequenceFlow> flowList = new ArrayList<SequenceFlow>();
141                 String elementId = getValueFromJsonNode(jsonNode, JsonKeys.ID);
142                 JsonNode connectionsNode = jsonNode.get(JsonKeys.CONNECTIONS);
143
144                 Iterator<JsonNode> iter = connectionsNode.iterator();
145         while (iter.hasNext()) {
146             JsonNode connectionEntry = (JsonNode) iter.next();
147             String targetRef = getValueFromJsonNode(connectionEntry, JsonKeys.TARGET_REF);
148             String condition = getValueFromJsonNode(connectionEntry, JsonKeys.CONDITION);
149             SequenceFlow flow = new SequenceFlow();
150                         flow.setId(elementId + targetRef);
151                         flow.setSourceRef(elementId);
152             flow.setTargetRef(targetRef);
153             flow.setCondition(condition);
154             flowList.add(flow);
155         }
156
157         return flowList;
158     }
159
160     protected Element createElementFromJson(JsonNode jsonNode)
161             throws JsonParseException, JsonMappingException, IOException {
162         String jsonObject = jsonNode.toString();
163         Element element;
164
165         String nodeType = getValueFromJsonNode(jsonNode, JsonKeys.TYPE);
166         if (nodeType == null) {
167           log.warn("Ignoring node: type is null");
168           return null;
169         }
170         
171         switch (nodeType) {
172         case "startEvent":
173             element = MAPPER.readValue(jsonObject, StartEvent.class);
174             break;
175         case "endEvent":
176             element = MAPPER.readValue(jsonObject, EndEvent.class);
177             break;
178         case "errorStartEvent":
179             element = MAPPER.readValue(jsonObject, ErrorStartEvent.class);
180             break;
181         case "errorEndEvent":
182             element = MAPPER.readValue(jsonObject, ErrorEndEvent.class);
183             break;
184         case "intermediateCatchEvent":
185             element = MAPPER.readValue(jsonObject, IntermediateCatchEvent.class);
186             break;
187         case "serviceTask":
188             element = MAPPER.readValue(jsonObject, ServiceTask.class);
189             break;
190         case "restTask":
191                         // element = this.createRestServiceTask(jsonObject);
192                         element = MAPPER.readValue(jsonObject, RestServiceTask.class);
193             break;
194         case "scriptTask":
195             element = MAPPER.readValue(jsonObject, ScriptTask.class);
196             break;
197         case "exclusiveGateway":
198             element = MAPPER.readValue(jsonObject, ExclusiveGateway.class);
199             break;
200         case "parallelGateway":
201             element = MAPPER.readValue(jsonObject, ParallelGateway.class);
202             break;
203         default:
204             log.warn("Ignoring node: type '" + nodeType + "' is unkown");
205             return null;
206         }
207
208         return element;
209     }
210     
211
212     private String getValueFromJsonNode(JsonNode jsonNode, String key) {
213         return jsonNode.get(key) == null ? null : jsonNode.get(key).asText();
214     }
215
216 }