aboutsummaryrefslogtreecommitdiffstats
path: root/sdc-workflow-designer-server/src/main/java/org/onap/sdc/workflowdesigner/parser/Bpmn4ToscaJsonParser.java
blob: e98acfb71d14454ede727a8ed08d944d3858c368 (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/**
 * Copyright (c) 2017-2018 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.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.onap.sdc.workflowdesigner.model.DataObject;
import org.onap.sdc.workflowdesigner.model.Element;
import org.onap.sdc.workflowdesigner.model.EndEvent;
import org.onap.sdc.workflowdesigner.model.ErrorEndEvent;
import org.onap.sdc.workflowdesigner.model.ErrorStartEvent;
import org.onap.sdc.workflowdesigner.model.ExclusiveGateway;
import org.onap.sdc.workflowdesigner.model.IntermediateCatchEvent;
import org.onap.sdc.workflowdesigner.model.ParallelGateway;
import org.onap.sdc.workflowdesigner.model.Parameter;
import org.onap.sdc.workflowdesigner.model.Process;
import org.onap.sdc.workflowdesigner.model.RestServiceTask;
import org.onap.sdc.workflowdesigner.model.ScriptTask;
import org.onap.sdc.workflowdesigner.model.SequenceFlow;
import org.onap.sdc.workflowdesigner.model.ServiceTask;
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();
    
    private Map<String, JsonNode> restConfigMap = new HashMap<String, JsonNode>();

    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);
        JsonNode data = rootNode.get(JsonKeys.DATA);
        if(null == data) {
            return process;
        }
        JsonNode nodes = data.get(JsonKeys.NODES);
        if (nodes == null) {
            return process;
        }
        
        this.loadConfigs(rootNode.get(JsonKeys.CONFIGS));

        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);

            // add dataObject
            if (element instanceof StartEvent) {
                List<DataObject> dataObjects = this.getDataObject((StartEvent) element);
                process.getDataObjectList().addAll(dataObjects);
            }
        }

        return process;

    }

    private List<DataObject> getDataObject(StartEvent startEvent) {
        List<DataObject> dataObjects = new ArrayList<DataObject>();

        for (Parameter parameter : startEvent.getParameters()) {
            DataObject dataObject = new DataObject();
            dataObject.setId(parameter.getName());
            dataObject.setName(parameter.getName());
            dataObject.setValue((String) parameter.getValue());

            dataObjects.add(dataObject);
        }

        return dataObjects;
    }
    
    private void loadConfigs(JsonNode config) {
        if(config == null) {
            return;
        }
        loadRestConfigs(config.get(JsonKeys.REST_CONFIGS));
    }
    
    private void loadRestConfigs(JsonNode restConfigs) {
        if(restConfigs == null) {
            return;
        }
        
        Iterator<JsonNode> iter = restConfigs.iterator();
        while (iter.hasNext()) {
            JsonNode restConfig = (JsonNode) iter.next();

            String configId = getValueFromJsonNode(restConfig, JsonKeys.ID); 
            restConfigMap.put(configId, restConfig);
        }
    }

    private List<SequenceFlow> getSequenceFlows(JsonNode jsonNode) {
        List<SequenceFlow> flowList = new ArrayList<SequenceFlow>();
		String elementId = getValueFromJsonNode(jsonNode, JsonKeys.ID);
		JsonNode connectionsNode = jsonNode.get(JsonKeys.CONNECTIONS);

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

        return flowList;
    }

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

        String nodeType = getValueFromJsonNode(jsonNode, JsonKeys.TYPE);
        if (nodeType == null) {
          log.warn("Ignoring node: type is null");
          return null;
        }
        
        switch (nodeType) {
        case "startEvent":
            element = MAPPER.readValue(jsonObject, StartEvent.class);
            break;
        case "endEvent":
            element = MAPPER.readValue(jsonObject, EndEvent.class);
            break;
        case "errorStartEvent":
            element = MAPPER.readValue(jsonObject, ErrorStartEvent.class);
            break;
        case "errorEndEvent":
            element = MAPPER.readValue(jsonObject, ErrorEndEvent.class);
            break;
        case "intermediateCatchEvent":
            element = MAPPER.readValue(jsonObject, IntermediateCatchEvent.class);
            break;
        case "serviceTask":
            element = MAPPER.readValue(jsonObject, ServiceTask.class);
            break;
        case "restTask":
			// element = this.createRestServiceTask(jsonObject);
			element = MAPPER.readValue(jsonObject, RestServiceTask.class);
            break;
        case "scriptTask":
            element = MAPPER.readValue(jsonObject, ScriptTask.class);
            break;
        case "exclusiveGateway":
            element = MAPPER.readValue(jsonObject, ExclusiveGateway.class);
            break;
        case "parallelGateway":
            element = MAPPER.readValue(jsonObject, ParallelGateway.class);
            break;
        default:
            log.warn("Ignoring node: type '" + nodeType + "' is unkown");
            return null;
        }

        return element;
    }
    

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

}