summaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/parser/Bpmn4ToscaJsonParser.java
blob: 8cad4d8cbdddaf68eba576c163059380ab4f5813 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/**
 * Copyright (c) 2017 ZTE Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the Apache License, Version 2.0
 * and the Eclipse Public License v1.0 which both accompany this distribution,
 * and are available at http://www.eclipse.org/legal/epl-v10.html
 * and http://www.apache.org/licenses/LICENSE-2.0
 *
 * Contributors:
 *     ZTE - initial API and implementation and/or initial documentation
 */
package org.onap.sdc.workflowdesigner.parser;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.onap.sdc.workflowdesigner.model.Element;
import org.onap.sdc.workflowdesigner.model.EndEvent;
import org.onap.sdc.workflowdesigner.model.Process;
import org.onap.sdc.workflowdesigner.model.SequenceFlow;
import org.onap.sdc.workflowdesigner.model.StartEvent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

public class Bpmn4ToscaJsonParser {

    private static Logger log = LoggerFactory.getLogger(Bpmn4ToscaJsonParser.class);

    private static ObjectMapper MAPPER = new ObjectMapper();

    static {
        MAPPER.enable(SerializationFeature.INDENT_OUTPUT);
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public Process parse(String processName, URI jsonFileUrl)
            throws JsonParseException, JsonMappingException, MalformedURLException, IOException {
        Process process = new Process(processName);

        JsonNode rootNode = MAPPER.readValue(jsonFileUrl.toURL(), JsonNode.class);

        log.debug("Creating Process models...");
        JsonNode nodes = rootNode.get(JsonKeys.NODES);
        if (nodes == null) {
            return process;
        }

        Iterator<JsonNode> iter = nodes.iterator();
        while (iter.hasNext()) {
            JsonNode jsonNode = (JsonNode) iter.next();

            // get element
            Element element = createElementFromJson(jsonNode);
            process.getElementList().add(element);

            // get sequence flows
            List<SequenceFlow> flowList = getSequenceFlows(jsonNode);
            process.getSequenceFlowList().addAll(flowList);
        }

        return process;

    }

    private List<SequenceFlow> getSequenceFlows(JsonNode jsonNode) {
        List<SequenceFlow> flowList = new ArrayList<SequenceFlow>();
        JsonNode sequenceFlowNodes = jsonNode.get(JsonKeys.SEQUENCE_FLOWS);

        Iterator<JsonNode> iter = sequenceFlowNodes.iterator();
        while (iter.hasNext()) {
            JsonNode connectionEntry = (JsonNode) iter.next();
            String sourceRef = getValueFromJsonNode(connectionEntry, JsonKeys.SOURCE_REF);
            String targetRef = getValueFromJsonNode(connectionEntry, JsonKeys.TARGET_REF);
            String condition = getValueFromJsonNode(connectionEntry, JsonKeys.CONDITION);
            SequenceFlow flow = new SequenceFlow();
            flow.setId(sourceRef + targetRef);
            flow.setSourceRef(sourceRef);
            flow.setTargetRef(targetRef);
            flow.setCondition(condition);
            flowList.add(flow);
        }

        return flowList;
    }

    private String getValueFromJsonNode(JsonNode jsonNode, String key) {
        return jsonNode.get(key) == null ? null : jsonNode.get(key).asText();
    }

    protected Element createElementFromJson(JsonNode jsonNode)
            throws JsonParseException, JsonMappingException, IOException {
        String jsonObject = jsonNode.toString();
        Element element;

        String nodeType = getValueFromJsonNode(jsonNode, JsonKeys.TYPE);
        switch (nodeType) {
        case "startEvent":
            element = MAPPER.readValue(jsonObject, StartEvent.class);
            break;
        case "endEvent":
            element = MAPPER.readValue(jsonObject, EndEvent.class);
            break;
        default:
            log.warn("Ignoring node: type '" + nodeType + "' is unkown");
            return null;
        }

        return element;
    }

}