64bff27ba4687d6ddb072cfee1ff6030d719522d
[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.Process;\r
23 import org.onap.sdc.workflowdesigner.model.SequenceFlow;\r
24 import org.slf4j.Logger;\r
25 import org.slf4j.LoggerFactory;\r
26 \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
33 \r
34 public class Bpmn4ToscaJsonParser {\r
35 \r
36         private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);\r
37         \r
38         private static ObjectMapper MAPPER = new ObjectMapper();\r
39         \r
40         static {\r
41                 MAPPER.enable(SerializationFeature.INDENT_OUTPUT);\r
42                 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);\r
43         }\r
44         \r
45         public Process parse(String processName, URI jsonFileUrl) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {\r
46                 Process process = new Process(processName);\r
47 \r
48                 JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);\r
49 \r
50                 log.debug("Creating Process models...");\r
51                 JsonNode nodes = rootNode.get("nodes");\r
52                 if(nodes == null) {\r
53                         return process;\r
54                 }\r
55                 \r
56                 Iterator<JsonNode> iter = nodes.iterator();\r
57                 while (iter.hasNext()) {\r
58                         JsonNode jsonNode = (JsonNode) iter.next();\r
59 \r
60                         // get element\r
61                         Element element = createElementFromJson(jsonNode);\r
62                         process.getElementList().add(element);\r
63                         \r
64                         // get sequence flows \r
65             List<SequenceFlow> flowList = getSequenceFlows(jsonNode);\r
66             process.getSequenceFlowList().addAll(flowList);\r
67                 }\r
68 \r
69                 return process;\r
70 \r
71         }\r
72         \r
73         private List<SequenceFlow> getSequenceFlows(JsonNode jsonNode) {\r
74         List<SequenceFlow> flowList = new ArrayList<SequenceFlow>();\r
75         JsonNode sequenceFlowNodes = jsonNode.get("sequenceFlows");\r
76         \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
88             flowList.add(flow);\r
89         }\r
90         \r
91         return flowList;\r
92     }\r
93         \r
94         private String getValueFromJsonNode(JsonNode jsonNode, String key) {\r
95         return jsonNode.get(key) == null ? null : jsonNode.get(key).asText();\r
96     }\r
97         \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
101         }\r
102         \r
103 }\r