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
10 * ZTE - initial API and implementation and/or initial documentation
12 package org.onap.sdc.workflowdesigner.parser;
14 import java.io.IOException;
15 import java.net.MalformedURLException;
17 import java.util.ArrayList;
18 import java.util.HashMap;
19 import java.util.Iterator;
20 import java.util.List;
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;
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;
48 public class Bpmn4ToscaJsonParser {
50 private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);
52 private static ObjectMapper MAPPER = new ObjectMapper();
54 private Map<String, JsonNode> restConfigMap = new HashMap<String, JsonNode>();
57 MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
58 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
61 public Process parse(String processName, URI jsonFileUrl)
62 throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
63 Process process = new Process(processName);
65 JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);
67 log.debug("Creating Process models...");
68 // JsonNode nodes = rootNode.get(JsonKeys.NODES);
69 JsonNode data = rootNode.get(JsonKeys.DATA);
73 JsonNode nodes = data.get(JsonKeys.NODES);
78 this.loadConfigs(rootNode.get(JsonKeys.CONFIGS));
80 Iterator<JsonNode> iter = nodes.iterator();
81 while (iter.hasNext()) {
82 JsonNode jsonNode = (JsonNode) iter.next();
85 Element element = createElementFromJson(jsonNode);
86 process.getElementList().add(element);
89 List<SequenceFlow> flowList = getSequenceFlows(jsonNode);
90 process.getSequenceFlowList().addAll(flowList);
93 if (element instanceof StartEvent) {
94 List<DataObject> dataObjects = this.getDataObject((StartEvent) element);
95 process.getDataObjectList().addAll(dataObjects);
103 private List<DataObject> getDataObject(StartEvent startEvent) {
104 List<DataObject> dataObjects = new ArrayList<DataObject>();
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());
112 dataObjects.add(dataObject);
118 private void loadConfigs(JsonNode config) {
122 loadRestConfigs(config.get(JsonKeys.REST_CONFIGS));
125 private void loadRestConfigs(JsonNode restConfigs) {
126 if(restConfigs == null) {
130 Iterator<JsonNode> iter = restConfigs.iterator();
131 while (iter.hasNext()) {
132 JsonNode restConfig = (JsonNode) iter.next();
134 String configId = getValueFromJsonNode(restConfig, JsonKeys.ID);
135 restConfigMap.put(configId, restConfig);
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);
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);
160 protected Element createElementFromJson(JsonNode jsonNode)
161 throws JsonParseException, JsonMappingException, IOException {
162 String jsonObject = jsonNode.toString();
165 String nodeType = getValueFromJsonNode(jsonNode, JsonKeys.TYPE);
166 if (nodeType == null) {
167 log.warn("Ignoring node: type is null");
173 element = MAPPER.readValue(jsonObject, StartEvent.class);
176 element = MAPPER.readValue(jsonObject, EndEvent.class);
178 case "errorStartEvent":
179 element = MAPPER.readValue(jsonObject, ErrorStartEvent.class);
181 case "errorEndEvent":
182 element = MAPPER.readValue(jsonObject, ErrorEndEvent.class);
184 case "intermediateCatchEvent":
185 element = MAPPER.readValue(jsonObject, IntermediateCatchEvent.class);
188 element = MAPPER.readValue(jsonObject, ServiceTask.class);
191 // element = this.createRestServiceTask(jsonObject);
192 element = MAPPER.readValue(jsonObject, RestServiceTask.class);
195 element = MAPPER.readValue(jsonObject, ScriptTask.class);
197 case "exclusiveGateway":
198 element = MAPPER.readValue(jsonObject, ExclusiveGateway.class);
200 case "parallelGateway":
201 element = MAPPER.readValue(jsonObject, ParallelGateway.class);
204 log.warn("Ignoring node: type '" + nodeType + "' is unkown");
212 private String getValueFromJsonNode(JsonNode jsonNode, String key) {
213 return jsonNode.get(key) == null ? null : jsonNode.get(key).asText();