Fix check style issues
[clamp.git] / src / main / java / org / onap / clamp / clds / model / properties / ModelBpmn.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * 
22  */
23
24 package org.onap.clamp.clds.model.properties;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28
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;
37 import java.util.Map;
38 import java.util.Map.Entry;
39
40 import org.onap.clamp.clds.exception.ModelBpmnException;
41 import org.onap.clamp.clds.service.CldsService;
42 import org.onap.clamp.clds.util.JsonUtils;
43
44 /**
45  * Parse Model BPMN properties.
46  * Example json: {"policy" :[{"id":"Policy_0oxeocn", "from":"StartEvent_1"}]}
47  */
48 public class ModelBpmn {
49
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;
58
59     /**
60      * Create ModelBpmn and populate maps from json.
61      *
62      * @param modelBpmnPropText The model bpmn properties text
63      * @return The model bpmn
64      */
65     public static ModelBpmn create(String modelBpmnPropText) {
66         try {
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()) {
74                 // process the entry
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);
87                 }
88                 modelBpmn.setBpmnElementIds(bpmnElementIdList);
89             }
90             return modelBpmn;
91         } catch (JsonParseException e) {
92             throw new ModelBpmnException("Exception occurred during the decoding of the bpmn JSON", e);
93         }
94     }
95
96     /**
97      * Add entry to both maps.
98      *
99      * @param entry The model bpmn entry.
100      */
101     private void addEntry(ModelBpmnEntry entry) {
102         addEntry(entriesByType, entry, entry.getType());
103         addEntry(entriesById, entry, entry.getId());
104     }
105
106     /**
107      * Add an entry to provided map with provided key.
108      *
109      * @param map The map to add the model bpmn entry
110      * @param entry The model bmpn entry
111      * @param key The key
112      */
113     private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
114         List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
115         list.add(entry);
116     }
117
118     /**
119      * This method verifies if the ModelElement Type (holmes, tca, ...) is in
120      * the list.
121      *
122      * @param type
123      *            A model Element type (tca, ...)
124      * @return true if the element is found or false otherwise
125      */
126     public boolean isModelElementTypeInList(String type) {
127         return entriesByType.get(type) != null;
128     }
129
130     /**
131      * Get the id.
132      * @return the id field given the ModelElement type
133      */
134     public String getId(String type) {
135         String modelElementId = "";
136         if (entriesByType.get(type) != null) {
137             modelElementId = entriesByType.get(type).get(0).getId();
138         }
139         return modelElementId;
140     }
141
142     /**
143      * Get the form id.
144      * @return the fromId field given the ModelElement type
145      */
146     public String getFromId(String type) {
147         String modelElementFromIdId = "";
148         if (entriesByType.get(type) != null) {
149             modelElementFromIdId = entriesByType.get(type).get(0).getFromId();
150         }
151         return modelElementFromIdId;
152     }
153
154     /**
155      * Get the type.
156      * @return the ModelElement type given the ModelElement id
157      */
158     public String getType(String id) {
159         return entriesById.get(id).get(0).getType();
160     }
161
162     /**
163      * Get the bpmn element ids.
164      * @return list of elementIds from bpmn
165      */
166     public List<String> getBpmnElementIds() {
167         return bpmnElementIds;
168     }
169
170     public void setBpmnElementIds(List<String> bpmnElementIds) {
171         this.bpmnElementIds = bpmnElementIds;
172     }
173 }