2 * Copyright (c) 2017 ZTE Corporation.
\r
3 * All rights reserved. This program and the accompanying materials
\r
4 * are made available under the Apache License, Version 2.0
\r
5 * and the Eclipse Public License v1.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)
\r
48 throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
\r
49 Process process = new Process(processName);
\r
51 JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);
\r
53 log.debug("Creating Process models...");
\r
54 JsonNode nodes = rootNode.get(JsonKeys.NODES);
\r
55 if (nodes == null) {
\r
59 Iterator<JsonNode> iter = nodes.iterator();
\r
60 while (iter.hasNext()) {
\r
61 JsonNode jsonNode = (JsonNode) iter.next();
\r
64 Element element = createElementFromJson(jsonNode);
\r
65 process.getElementList().add(element);
\r
67 // get sequence flows
\r
68 List<SequenceFlow> flowList = getSequenceFlows(jsonNode);
\r
69 process.getSequenceFlowList().addAll(flowList);
\r
76 private List<SequenceFlow> getSequenceFlows(JsonNode jsonNode) {
\r
77 List<SequenceFlow> flowList = new ArrayList<SequenceFlow>();
\r
78 JsonNode sequenceFlowNodes = jsonNode.get(JsonKeys.SEQUENCE_FLOWS);
\r
80 Iterator<JsonNode> iter = sequenceFlowNodes.iterator();
\r
81 while (iter.hasNext()) {
\r
82 JsonNode connectionEntry = (JsonNode) iter.next();
\r
83 String sourceRef = getValueFromJsonNode(connectionEntry, JsonKeys.SOURCE_REF);
\r
84 String targetRef = getValueFromJsonNode(connectionEntry, JsonKeys.TARGET_REF);
\r
85 String condition = getValueFromJsonNode(connectionEntry, JsonKeys.CONDITION);
\r
86 SequenceFlow flow = new SequenceFlow();
\r
87 flow.setId(sourceRef + targetRef);
\r
88 flow.setSourceRef(sourceRef);
\r
89 flow.setTargetRef(targetRef);
\r
90 flow.setCondition(condition);
\r
97 private String getValueFromJsonNode(JsonNode jsonNode, String key) {
\r
98 return jsonNode.get(key) == null ? null : jsonNode.get(key).asText();
\r
101 protected Element createElementFromJson(JsonNode jsonNode)
\r
102 throws JsonParseException, JsonMappingException, IOException {
\r
103 String jsonObject = jsonNode.toString();
\r
106 String nodeType = getValueFromJsonNode(jsonNode, JsonKeys.TYPE);
\r
107 switch (nodeType) {
\r
109 element = MAPPER.readValue(jsonObject, StartEvent.class);
\r
112 element = MAPPER.readValue(jsonObject, EndEvent.class);
\r
115 log.warn("Ignoring node: type '" + nodeType + "' is unkown");
\r