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 * ===================================================================
24 package org.onap.clamp.clds.model.properties;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
29 import com.google.gson.JsonArray;
30 import com.google.gson.JsonElement;
31 import com.google.gson.JsonObject;
32 import com.google.gson.JsonParseException;
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.JsonUtils;
45 * Parse Model BPMN properties.
46 * Example json: {"policy" :[{"id":"Policy_0oxeocn", "from":"StartEvent_1"}]}
48 public class ModelBpmn {
50 protected static final EELFLogger logger = EELFManager.getInstance().getLogger(CldsService.class);
51 protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
52 // for each type, an array of entries
53 private final Map<String, List<ModelBpmnEntry>> entriesByType = new HashMap<>();
54 // for each id, an array of entries
55 private final Map<String, List<ModelBpmnEntry>> entriesById = new HashMap<>();
56 // List of all elementIds
57 private List<String> bpmnElementIds;
60 * Create ModelBpmn and populate maps from json.
62 * @param modelBpmnPropText The model bpmn properties text
63 * @return The model bpmn
65 public static ModelBpmn create(String modelBpmnPropText) {
67 ModelBpmn modelBpmn = new ModelBpmn();
68 JsonObject root = JsonUtils.GSON.fromJson(modelBpmnPropText, JsonObject.class);
69 // iterate over each entry like:
70 // "Policy":[{"id":"Policy","from":"StartEvent_1"}]
71 Iterator<Entry<String, JsonElement>> entryItr = root.entrySet().iterator();
72 List<String> bpmnElementIdList = new ArrayList<>();
73 while (entryItr.hasNext()) {
75 Entry<String, JsonElement> entry = entryItr.next();
76 String type = entry.getKey();
77 JsonArray arrayNode = entry.getValue().getAsJsonArray();
78 // process each id/from object, like:
79 // {"id":"Policy","from":"StartEvent_1"}
80 for (JsonElement anArrayNode : arrayNode) {
81 JsonObject node = anArrayNode.getAsJsonObject();
82 String id = node.get("id").getAsString();
83 String fromId = node.get("from").getAsString();
84 ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId);
85 modelBpmn.addEntry(modelBpmnEntry);
86 bpmnElementIdList.add(id);
88 modelBpmn.setBpmnElementIds(bpmnElementIdList);
91 } catch (JsonParseException e) {
92 throw new ModelBpmnException("Exception occurred during the decoding of the bpmn JSON", e);
97 * Add entry to both maps.
99 * @param entry The model bpmn entry.
101 private void addEntry(ModelBpmnEntry entry) {
102 addEntry(entriesByType, entry, entry.getType());
103 addEntry(entriesById, entry, entry.getId());
107 * Add an entry to provided map with provided key.
109 * @param map The map to add the model bpmn entry
110 * @param entry The model bmpn entry
113 private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
114 List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
119 * This method verifies if the ModelElement Type (holmes, tca, ...) is in
123 * A model Element type (tca, ...)
124 * @return true if the element is found or false otherwise
126 public boolean isModelElementTypeInList(String type) {
127 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;
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;
156 * @return the ModelElement type given the ModelElement id
158 public String getType(String id) {
159 return entriesById.get(id).get(0).getType();
163 * Get the bpmn element ids.
164 * @return list of elementIds from bpmn
166 public List<String> getBpmnElementIds() {
167 return bpmnElementIds;
170 public void setBpmnElementIds(List<String> bpmnElementIds) {
171 this.bpmnElementIds = bpmnElementIds;