2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END============================================
20 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.model.prop;
26 import com.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ArrayNode;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
33 import java.io.IOException;
35 import java.util.Map.Entry;
36 import java.util.logging.Logger;
39 * Parse Model BPMN properties.
41 * Example json: {"collector":[{"id":"Collector_11r50j1", "from":"StartEvent_1"}],"stringMatch":[{"id":"StringMatch_0h6cbdv"}],"policy":[{"id":"Policy_0oxeocn", "from":"StringMatch_0h6cbdv"}]}
43 public class ModelBpmn {
44 private static final Logger logger = Logger.getLogger(ModelBpmn.class.getName());
46 // for each type, an array of entries
47 private final Map<String, List<ModelBpmnEntry>> entriesByType = new HashMap<>();
49 // for each id, an array of entries
50 private final Map<String, List<ModelBpmnEntry>> entriesById = new HashMap<>();
52 // List of all elementIds
53 private List<String> bpmnElementIds;
56 * Create ModelBpmn and populate maps from json
58 * @param modelBpmnPropText
61 * @throws JsonMappingException
62 * @throws JsonParseException
64 public static ModelBpmn create(String modelBpmnPropText) throws IOException {
65 ModelBpmn modelBpmn = new ModelBpmn();
66 ObjectMapper objectMapper = new ObjectMapper();
67 ObjectNode root = objectMapper.readValue(modelBpmnPropText, ObjectNode.class);
68 // iterate over each entry like: "collector":[{"id":"Collector_11r50j1","from":"StartEvent_1"}]
69 Iterator<Entry<String, JsonNode>> entryItr = root.fields();
70 List<String> bpmnElementIdList = new ArrayList<>();
71 while (entryItr.hasNext()) {
73 Entry<String, JsonNode> entry = entryItr.next();
74 String type = entry.getKey();
75 ArrayNode arrayNode = (ArrayNode) entry.getValue();
76 // process each id/from object, like: {"id":"Collector_11r50j1","from":"StartEvent_1"}
77 for (JsonNode anArrayNode : arrayNode) {
78 ObjectNode node = (ObjectNode) anArrayNode;
79 String id = node.get("id").asText();
80 String fromId = node.get("from").asText();
81 ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId);
82 modelBpmn.addEntry(modelBpmnEntry);
83 bpmnElementIdList.add(id);
85 modelBpmn.setBpmnElementIds(bpmnElementIdList);
91 * Add entry to both maps.
95 private void addEntry(ModelBpmnEntry entry) {
96 addEntry(entriesByType, entry, entry.getType());
97 addEntry(entriesById, entry, entry.getId());
101 * Add an entry to provided map with provided key.
107 private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
108 List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
116 * @return true if the element is found or false otherwise
118 public boolean getModelElementFound(String type) {
119 return entriesByType.get(type) != null;
123 * @return the id field given the ModelElement type
125 public String getId(String type) {
126 return entriesByType.get(type).get(0).getId();
130 * @return the fromId field given the ModelElement type
132 public String getFromId(String type) {
133 return entriesByType.get(type).get(0).getFromId();
137 * @return the ModelElement type given the ModelElement id
139 public String getType(String id) {
140 return entriesById.get(id).get(0).getType();
144 * @return list of elementIds from bpmn
146 public List<String> getBpmnElementIds() {
147 return bpmnElementIds;
150 public void setBpmnElementIds(List<String> bpmnElementIds) {
151 this.bpmnElementIds = bpmnElementIds;