2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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.properties;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.node.ArrayNode;
30 import com.fasterxml.jackson.databind.node.ObjectNode;
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.Iterator;
36 import java.util.List;
38 import java.util.Map.Entry;
40 import org.onap.clamp.clds.exception.ModelBpmnException;
41 import org.onap.clamp.clds.service.CldsService;
42 import org.onap.clamp.clds.util.JacksonUtils;
45 * Parse Model BPMN properties.
47 * Example json: {"policy" :[{"id":"Policy_0oxeocn", "from":"StartEvent_1"}]}
49 public class ModelBpmn {
51 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsService.class);
52 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
53 // for each type, an array of entries
54 private final Map<String, List<ModelBpmnEntry>> entriesByType = new HashMap<>();
55 // for each id, an array of entries
56 private final Map<String, List<ModelBpmnEntry>> entriesById = new HashMap<>();
57 // List of all elementIds
58 private List<String> bpmnElementIds;
61 * Create ModelBpmn and populate maps from json
63 * @param modelBpmnPropText
66 public static ModelBpmn create(String modelBpmnPropText) {
68 ModelBpmn modelBpmn = new ModelBpmn();
69 ObjectNode root = JacksonUtils.getObjectMapperInstance().readValue(modelBpmnPropText, ObjectNode.class);
70 // iterate over each entry like:
71 // "Policy":[{"id":"Policy","from":"StartEvent_1"}]
72 Iterator<Entry<String, JsonNode>> entryItr = root.fields();
73 List<String> bpmnElementIdList = new ArrayList<>();
74 while (entryItr.hasNext()) {
76 Entry<String, JsonNode> entry = entryItr.next();
77 String type = entry.getKey();
78 ArrayNode arrayNode = (ArrayNode) entry.getValue();
79 // process each id/from object, like:
80 // {"id":"Policy","from":"StartEvent_1"}
81 for (JsonNode anArrayNode : arrayNode) {
82 ObjectNode node = (ObjectNode) anArrayNode;
83 String id = node.get("id").asText();
84 String fromId = node.get("from").asText();
85 ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId);
86 modelBpmn.addEntry(modelBpmnEntry);
87 bpmnElementIdList.add(id);
89 modelBpmn.setBpmnElementIds(bpmnElementIdList);
92 } catch (IOException e) {
93 throw new ModelBpmnException("Exception occurred during the decoding of the bpmn JSON", e);
98 * Add entry to both maps.
102 private void addEntry(ModelBpmnEntry entry) {
103 addEntry(entriesByType, entry, entry.getType());
104 addEntry(entriesById, entry, entry.getId());
108 * Add an entry to provided map with provided key.
114 private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
115 List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
120 * This method verifies if the ModelElement Type (holmes, tca, ...) is in
124 * A model Element type (tca, ...)
125 * @return true if the element is found or false otherwise
127 public boolean isModelElementTypeInList(String type) {
128 return entriesByType.get(type) != null;
132 * @return the id field given the ModelElement type
134 public String getId(String type) {
135 String modelElementId = "";
136 if (entriesByType.get(type) != null) {
137 modelElementId = entriesByType.get(type).get(0).getId();
139 return modelElementId;
143 * @return the fromId field given the ModelElement type
145 public String getFromId(String type) {
146 String modelElementFromIdId = "";
147 if (entriesByType.get(type) != null) {
148 modelElementFromIdId = entriesByType.get(type).get(0).getFromId();
150 return modelElementFromIdId;
154 * @return the ModelElement type given the ModelElement id
156 public String getType(String id) {
157 return entriesById.get(id).get(0).getType();
161 * @return list of elementIds from bpmn
163 public List<String> getBpmnElementIds() {
164 return bpmnElementIds;
167 public void setBpmnElementIds(List<String> bpmnElementIds) {
168 this.bpmnElementIds = bpmnElementIds;