a75114825246d950cb26dad4194ad59250c904cb
[sdc/sdc-workflow-designer.git] /
1 /**\r
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
8  *\r
9  * Contributors:\r
10  *     ZTE - initial API and implementation and/or initial documentation\r
11  */\r
12 package org.onap.sdc.workflowdesigner.parser;\r
13 \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
20 \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
28 \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
35 \r
36 public class Bpmn4ToscaJsonParser {\r
37 \r
38         private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);\r
39         \r
40         private static ObjectMapper MAPPER = new ObjectMapper();\r
41         \r
42         static {\r
43                 MAPPER.enable(SerializationFeature.INDENT_OUTPUT);\r
44                 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);\r
45         }\r
46         \r
47         public Process parse(String processName, URI jsonFileUrl) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {\r
48                 Process process = new Process(processName);\r
49 \r
50                 JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);\r
51 \r
52                 log.debug("Creating Process models...");\r
53                 JsonNode nodes = rootNode.get("nodes");\r
54                 if(nodes == null) {\r
55                         return process;\r
56                 }\r
57                 \r
58                 Iterator<JsonNode> iter = nodes.iterator();\r
59                 while (iter.hasNext()) {\r
60                         JsonNode jsonNode = (JsonNode) iter.next();\r
61 \r
62                         // get element\r
63                         Element element = createElementFromJson(jsonNode);\r
64                         process.getElementList().add(element);\r
65                         \r
66                         // get sequence flows \r
67             List<SequenceFlow> flowList = getSequenceFlows(jsonNode);\r
68             process.getSequenceFlowList().addAll(flowList);\r
69                 }\r
70 \r
71                 return process;\r
72 \r
73         }\r
74         \r
75         private List<SequenceFlow> getSequenceFlows(JsonNode jsonNode) {\r
76         List<SequenceFlow> flowList = new ArrayList<SequenceFlow>();\r
77         JsonNode sequenceFlowNodes = jsonNode.get("sequenceFlows");\r
78         \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
90             flowList.add(flow);\r
91         }\r
92         \r
93         return flowList;\r
94     }\r
95         \r
96         private String getValueFromJsonNode(JsonNode jsonNode, String key) {\r
97         return jsonNode.get(key) == null ? null : jsonNode.get(key).asText();\r
98     }\r
99         \r
100         protected Element createElementFromJson(JsonNode jsonNode) throws JsonParseException, JsonMappingException, IOException {\r
101             String jsonObject = jsonNode.toString();\r
102         Element element;\r
103         \r
104         String nodeType = getValueFromJsonNode(jsonNode, "type");\r
105         switch (nodeType) {\r
106         case "startEvent":\r
107             element = MAPPER.readValue(jsonObject, StartEvent.class);\r
108             break;\r
109         case "endEvent":\r
110             element = MAPPER.readValue(jsonObject, EndEvent.class);\r
111             break;\r
112         default:\r
113             log.warn("Ignoring node: type '" + nodeType + "' is unkown");\r
114             return null;\r
115         }\r
116 \r
117         return element;\r
118         }\r
119         \r
120 }\r