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 java.io.IOException;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.Iterator;
30 import java.util.List;
32 import java.util.Map.Entry;
34 import org.onap.clamp.clds.service.CldsService;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.fasterxml.jackson.core.JsonParseException;
39 import com.fasterxml.jackson.databind.JsonMappingException;
40 import com.fasterxml.jackson.databind.JsonNode;
41 import com.fasterxml.jackson.databind.ObjectMapper;
42 import com.fasterxml.jackson.databind.node.ArrayNode;
43 import com.fasterxml.jackson.databind.node.ObjectNode;
46 * Parse Model BPMN properties.
48 * Example json: {"collector":[{"id":"Collector_11r50j1",
49 * "from":"StartEvent_1"}],"stringMatch":[{"id":"StringMatch_0h6cbdv"}],"policy"
50 * :[{"id":"Policy_0oxeocn", "from":"StringMatch_0h6cbdv"}]}
52 public class ModelBpmn {
53 protected static final EELFLogger logger = EELFManager.getInstance()
54 .getLogger(CldsService.class);
55 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
57 // for each type, an array of entries
58 private final Map<String, List<ModelBpmnEntry>> entriesByType = new HashMap<>();
60 // for each id, an array of entries
61 private final Map<String, List<ModelBpmnEntry>> entriesById = new HashMap<>();
63 // List of all elementIds
64 private List<String> bpmnElementIds;
67 * Create ModelBpmn and populate maps from json
69 * @param modelBpmnPropText
72 * @throws JsonMappingException
73 * @throws JsonParseException
75 public static ModelBpmn create(String modelBpmnPropText) throws IOException {
76 ModelBpmn modelBpmn = new ModelBpmn();
77 ObjectMapper objectMapper = new ObjectMapper();
78 ObjectNode root = objectMapper.readValue(modelBpmnPropText, ObjectNode.class);
79 // iterate over each entry like:
80 // "collector":[{"id":"Collector_11r50j1","from":"StartEvent_1"}]
81 Iterator<Entry<String, JsonNode>> entryItr = root.fields();
82 List<String> bpmnElementIdList = new ArrayList<>();
83 while (entryItr.hasNext()) {
85 Entry<String, JsonNode> entry = entryItr.next();
86 String type = entry.getKey();
87 ArrayNode arrayNode = (ArrayNode) entry.getValue();
88 // process each id/from object, like:
89 // {"id":"Collector_11r50j1","from":"StartEvent_1"}
90 for (JsonNode anArrayNode : arrayNode) {
91 ObjectNode node = (ObjectNode) anArrayNode;
92 String id = node.get("id").asText();
93 String fromId = node.get("from").asText();
94 ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId);
95 modelBpmn.addEntry(modelBpmnEntry);
96 bpmnElementIdList.add(id);
98 modelBpmn.setBpmnElementIds(bpmnElementIdList);
104 * Add entry to both maps.
108 private void addEntry(ModelBpmnEntry entry) {
109 addEntry(entriesByType, entry, entry.getType());
110 addEntry(entriesById, entry, entry.getId());
114 * Add an entry to provided map with provided key.
120 private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
121 List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
129 * @return true if the element is found or false otherwise
131 public boolean getModelElementFound(String type) {
132 return entriesByType.get(type) != null;
136 * @return the id field given the ModelElement type
138 public String getId(String type) {
139 String modelElementId = "";
140 if (entriesByType.get(type) != null) {
141 modelElementId = entriesByType.get(type).get(0).getId();
143 return modelElementId;
147 * @return the fromId field given the ModelElement type
149 public String getFromId(String type) {
150 String modelElementFromIdId = "";
151 if (entriesByType.get(type) != null) {
152 modelElementFromIdId = entriesByType.get(type).get(0).getFromId();
154 return modelElementFromIdId;
158 * @return the ModelElement type given the ModelElement id
160 public String getType(String id) {
161 return entriesById.get(id).get(0).getType();
165 * @return list of elementIds from bpmn
167 public List<String> getBpmnElementIds() {
168 return bpmnElementIds;
171 public void setBpmnElementIds(List<String> bpmnElementIds) {
172 this.bpmnElementIds = bpmnElementIds;