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.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
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;
34 import java.util.ArrayList;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.Map.Entry;
41 import org.onap.clamp.clds.exception.ModelBpmnException;
42 import org.onap.clamp.clds.service.CldsService;
45 * Parse Model BPMN properties.
47 * Example json: {"policy" :[{"id":"Policy_0oxeocn", "from":"StartEvent_1"}]}
49 public class ModelBpmn {
50 protected static final EELFLogger logger = EELFManager.getInstance()
51 .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 ObjectMapper objectMapper = new ObjectMapper();
70 ObjectNode root = objectMapper.readValue(modelBpmnPropText, ObjectNode.class);
71 // iterate over each entry like:
72 // "Policy":[{"id":"Policy","from":"StartEvent_1"}]
73 Iterator<Entry<String, JsonNode>> entryItr = root.fields();
74 List<String> bpmnElementIdList = new ArrayList<>();
75 while (entryItr.hasNext()) {
77 Entry<String, JsonNode> entry = entryItr.next();
78 String type = entry.getKey();
79 ArrayNode arrayNode = (ArrayNode) entry.getValue();
80 // process each id/from object, like:
81 // {"id":"Policy","from":"StartEvent_1"}
82 for (JsonNode anArrayNode : arrayNode) {
83 ObjectNode node = (ObjectNode) anArrayNode;
84 String id = node.get("id").asText();
85 String fromId = node.get("from").asText();
86 ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId);
87 modelBpmn.addEntry(modelBpmnEntry);
88 bpmnElementIdList.add(id);
90 modelBpmn.setBpmnElementIds(bpmnElementIdList);
93 } catch (IOException e) {
94 throw new ModelBpmnException("Exception occurred during the decoding of the bpmn JSON", e);
99 * Add entry to both maps.
103 private void addEntry(ModelBpmnEntry entry) {
104 addEntry(entriesByType, entry, entry.getType());
105 addEntry(entriesById, entry, entry.getId());
109 * Add an entry to provided map with provided key.
115 private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
116 List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
121 * This method verifies if the ModelElement Type (holmes, tca, ...) is in
125 * A model Element type (tca, ...)
126 * @return true if the element is found or false otherwise
128 public boolean isModelElementTypeInList(String type) {
129 return entriesByType.get(type) != null;
133 * @return the id field given the ModelElement type
135 public String getId(String type) {
136 String modelElementId = "";
137 if (entriesByType.get(type) != null) {
138 modelElementId = entriesByType.get(type).get(0).getId();
140 return modelElementId;
144 * @return the fromId field given the ModelElement type
146 public String getFromId(String type) {
147 String modelElementFromIdId = "";
148 if (entriesByType.get(type) != null) {
149 modelElementFromIdId = entriesByType.get(type).get(0).getFromId();
151 return modelElementFromIdId;
155 * @return the ModelElement type given the ModelElement id
157 public String getType(String id) {
158 return entriesById.get(id).get(0).getType();
162 * @return list of elementIds from bpmn
164 public List<String> getBpmnElementIds() {
165 return bpmnElementIds;
168 public void setBpmnElementIds(List<String> bpmnElementIds) {
169 this.bpmnElementIds = bpmnElementIds;