81b41d987fa631a2a04782d20fee5b6f58c90ace
[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.Iterator;\r
18 \r
19 import org.onap.sdc.workflowdesigner.model.Element;\r
20 import org.onap.sdc.workflowdesigner.model.Process;\r
21 import org.slf4j.Logger;\r
22 import org.slf4j.LoggerFactory;\r
23 \r
24 import com.fasterxml.jackson.core.JsonParseException;\r
25 import com.fasterxml.jackson.databind.DeserializationFeature;\r
26 import com.fasterxml.jackson.databind.JsonMappingException;\r
27 import com.fasterxml.jackson.databind.JsonNode;\r
28 import com.fasterxml.jackson.databind.ObjectMapper;\r
29 import com.fasterxml.jackson.databind.SerializationFeature;\r
30 \r
31 public class Bpmn4ToscaJsonParser {\r
32 \r
33         private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);\r
34         \r
35         private static ObjectMapper MAPPER = new ObjectMapper();\r
36         \r
37         static {\r
38                 MAPPER.enable(SerializationFeature.INDENT_OUTPUT);\r
39                 MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);\r
40         }\r
41         \r
42         public Process parse(String processName, URI jsonFileUrl) throws JsonParseException, JsonMappingException, MalformedURLException, IOException {\r
43                 Process process = new Process(processName);\r
44 \r
45                 JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);\r
46 \r
47                 log.debug("Creating Process models...");\r
48                 JsonNode nodes = rootNode.get("nodes");\r
49                 if(nodes == null) {\r
50                         return process;\r
51                 }\r
52                 \r
53                 Iterator<JsonNode> iter = nodes.iterator();\r
54                 while (iter.hasNext()) {\r
55                         JsonNode jsonNode = (JsonNode) iter.next();\r
56 \r
57                         // get element\r
58                         Element element = createElementFromJson(jsonNode);\r
59                         process.getElementList().add(element);\r
60                 }\r
61 \r
62                 return process;\r
63 \r
64         }\r
65         \r
66         protected Element createElementFromJson(JsonNode jsonNode) throws JsonParseException, JsonMappingException, IOException {\r
67                 String jsonObject = jsonNode.toString();\r
68                 return MAPPER.readValue(jsonObject, Element.class);\r
69         }\r
70         \r
71 }\r