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.Process;
\r
23 import org.onap.sdc.workflowdesigner.model.SequenceFlow;
\r
24 import org.slf4j.Logger;
\r
25 import org.slf4j.LoggerFactory;
\r
27 import com.fasterxml.jackson.core.JsonParseException;
\r
28 import com.fasterxml.jackson.databind.DeserializationFeature;
\r
29 import com.fasterxml.jackson.databind.JsonMappingException;
\r
30 import com.fasterxml.jackson.databind.JsonNode;
\r
31 import com.fasterxml.jackson.databind.ObjectMapper;
\r
32 import com.fasterxml.jackson.databind.SerializationFeature;
\r
34 public class Bpmn4ToscaJsonParser {
\r
36 private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);
\r
38 private static ObjectMapper MAPPER = new ObjectMapper();
\r
41 MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
\r
42 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
\r
45 public Process parse(String processName, URI jsonFileUrl) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
\r
46 Process process = new Process(processName);
\r
48 JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);
\r
50 log.debug("Creating Process models...");
\r
51 JsonNode nodes = rootNode.get("nodes");
\r
56 Iterator<JsonNode> iter = nodes.iterator();
\r
57 while (iter.hasNext()) {
\r
58 JsonNode jsonNode = (JsonNode) iter.next();
\r
61 Element element = createElementFromJson(jsonNode);
\r
62 process.getElementList().add(element);
\r
64 // get sequence flows
\r
65 List<SequenceFlow> flowList = getSequenceFlows(jsonNode);
\r
66 process.getSequenceFlowList().addAll(flowList);
\r
73 private List<SequenceFlow> getSequenceFlows(JsonNode jsonNode) {
\r
74 List<SequenceFlow> flowList = new ArrayList<SequenceFlow>();
\r
75 JsonNode sequenceFlowNodes = jsonNode.get("sequenceFlows");
\r
77 Iterator<JsonNode> iter = sequenceFlowNodes.iterator();
\r
78 while (iter.hasNext()) {
\r
79 JsonNode connectionEntry = (JsonNode) iter.next();
\r
80 String sourceRef = getValueFromJsonNode(connectionEntry, "sourceRef");
\r
81 String targetRef = getValueFromJsonNode(connectionEntry, "targetRef");
\r
82 String condition = getValueFromJsonNode(connectionEntry, "condition");
\r
83 SequenceFlow flow = new SequenceFlow();
\r
84 flow.setId(sourceRef + targetRef);
\r
85 flow.setSourceRef(sourceRef);
\r
86 flow.setTargetRef(targetRef);
\r
87 flow.setCondition(condition);
\r
94 private String getValueFromJsonNode(JsonNode jsonNode, String key) {
\r
95 return jsonNode.get(key) == null ? null : jsonNode.get(key).asText();
\r
98 protected Element createElementFromJson(JsonNode jsonNode) throws JsonParseException, JsonMappingException, IOException {
\r
99 String jsonObject = jsonNode.toString();
\r
100 return MAPPER.readValue(jsonObject, Element.class);
\r