2 * Copyright (c) 2017 ZTE Corporation.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the terms of the Eclipse Public License v1.0
\r
5 * and the Apache License 2.0 which both accompany this distribution,
\r
6 * and are available at http://www.eclipse.org/legal/epl-v10.html
\r
7 * and http://www.apache.org/licenses/LICENSE-2.0
\r
10 * ZTE - initial API and implementation and/or initial documentation
\r
12 package org.onap.sdc.workflowdesigner.parser;
\r
14 import java.io.IOException;
\r
15 import java.net.MalformedURLException;
\r
16 import java.net.URI;
\r
17 import java.util.ArrayList;
\r
18 import java.util.Iterator;
\r
19 import java.util.List;
\r
21 import org.onap.sdc.workflowdesigner.model.Element;
\r
22 import org.onap.sdc.workflowdesigner.model.EndEvent;
\r
23 import org.onap.sdc.workflowdesigner.model.Process;
\r
24 import org.onap.sdc.workflowdesigner.model.SequenceFlow;
\r
25 import org.onap.sdc.workflowdesigner.model.StartEvent;
\r
26 import org.slf4j.Logger;
\r
27 import org.slf4j.LoggerFactory;
\r
29 import com.fasterxml.jackson.core.JsonParseException;
\r
30 import com.fasterxml.jackson.databind.DeserializationFeature;
\r
31 import com.fasterxml.jackson.databind.JsonMappingException;
\r
32 import com.fasterxml.jackson.databind.JsonNode;
\r
33 import com.fasterxml.jackson.databind.ObjectMapper;
\r
34 import com.fasterxml.jackson.databind.SerializationFeature;
\r
36 public class Bpmn4ToscaJsonParser {
\r
38 private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);
\r
40 private static ObjectMapper MAPPER = new ObjectMapper();
\r
43 MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
\r
44 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
\r
47 public Process parse(String processName, URI jsonFileUrl) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
\r
48 Process process = new Process(processName);
\r
50 JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);
\r
52 log.debug("Creating Process models...");
\r
53 JsonNode nodes = rootNode.get("nodes");
\r
58 Iterator<JsonNode> iter = nodes.iterator();
\r
59 while (iter.hasNext()) {
\r
60 JsonNode jsonNode = (JsonNode) iter.next();
\r
63 Element element = createElementFromJson(jsonNode);
\r
64 process.getElementList().add(element);
\r
66 // get sequence flows
\r
67 List<SequenceFlow> flowList = getSequenceFlows(jsonNode);
\r
68 process.getSequenceFlowList().addAll(flowList);
\r
75 private List<SequenceFlow> getSequenceFlows(JsonNode jsonNode) {
\r
76 List<SequenceFlow> flowList = new ArrayList<SequenceFlow>();
\r
77 JsonNode sequenceFlowNodes = jsonNode.get("sequenceFlows");
\r
79 Iterator<JsonNode> iter = sequenceFlowNodes.iterator();
\r
80 while (iter.hasNext()) {
\r
81 JsonNode connectionEntry = (JsonNode) iter.next();
\r
82 String sourceRef = getValueFromJsonNode(connectionEntry, "sourceRef");
\r
83 String targetRef = getValueFromJsonNode(connectionEntry, "targetRef");
\r
84 String condition = getValueFromJsonNode(connectionEntry, "condition");
\r
85 SequenceFlow flow = new SequenceFlow();
\r
86 flow.setId(sourceRef + targetRef);
\r
87 flow.setSourceRef(sourceRef);
\r
88 flow.setTargetRef(targetRef);
\r
89 flow.setCondition(condition);
\r
96 private String getValueFromJsonNode(JsonNode jsonNode, String key) {
\r
97 return jsonNode.get(key) == null ? null : jsonNode.get(key).asText();
\r
100 protected Element createElementFromJson(JsonNode jsonNode) throws JsonParseException, JsonMappingException, IOException {
\r
101 String jsonObject = jsonNode.toString();
\r
104 String nodeType = getValueFromJsonNode(jsonNode, "type");
\r
105 switch (nodeType) {
\r
107 element = MAPPER.readValue(jsonObject, StartEvent.class);
\r
110 element = MAPPER.readValue(jsonObject, EndEvent.class);
\r
113 log.warn("Ignoring node: type '" + nodeType + "' is unkown");
\r