ae7028dcdd2a9f75438a2304f5480dff9f0fe295
[clamp.git] / src / main / java / org / onap / clamp / clds / model / prop / ModelBpmn.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23
24 package org.onap.clamp.clds.model.prop;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.core.JsonParseException;
29 import com.fasterxml.jackson.databind.JsonMappingException;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.node.ArrayNode;
33 import com.fasterxml.jackson.databind.node.ObjectNode;
34
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Map.Entry;
42
43 import org.onap.clamp.clds.service.CldsService;
44
45 /**
46  * Parse Model BPMN properties.
47  * <p>
48  * Example json: {"collector":[{"id":"Collector_11r50j1",
49  * "from":"StartEvent_1"}],"stringMatch":[{"id":"StringMatch_0h6cbdv"}],"policy"
50  * :[{"id":"Policy_0oxeocn", "from":"StringMatch_0h6cbdv"}]}
51  */
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();
56
57     // for each type, an array of entries
58     private final Map<String, List<ModelBpmnEntry>> entriesByType = new HashMap<>();
59
60     // for each id, an array of entries
61     private final Map<String, List<ModelBpmnEntry>> entriesById   = new HashMap<>();
62
63     // List of all elementIds
64     private List<String>                            bpmnElementIds;
65
66     /**
67      * Create ModelBpmn and populate maps from json
68      *
69      * @param modelBpmnPropText
70      * @return
71      * @throws IOException
72      * @throws JsonMappingException
73      * @throws JsonParseException
74      */
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()) {
84             // process the entry
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);
97             }
98             modelBpmn.setBpmnElementIds(bpmnElementIdList);
99         }
100         return modelBpmn;
101     }
102
103     /**
104      * Add entry to both maps.
105      *
106      * @param entry
107      */
108     private void addEntry(ModelBpmnEntry entry) {
109         addEntry(entriesByType, entry, entry.getType());
110         addEntry(entriesById, entry, entry.getId());
111     }
112
113     /**
114      * Add an entry to provided map with provided key.
115      *
116      * @param map
117      * @param entry
118      * @param key
119      */
120     private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
121         List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
122         list.add(entry);
123     }
124
125     /**
126      * This method verifies if the ModelElement Type (collector, holmes, tca,
127      * ...) is in the list.
128      *
129      * @param type
130      *            A model Element type (tca, collector, ...)
131      * @return true if the element is found or false otherwise
132      */
133     public boolean isModelElementTypeInList(String type) {
134         return entriesByType.get(type) != null;
135     }
136
137     /**
138      * @return the id field given the ModelElement type
139      */
140     public String getId(String type) {
141         String modelElementId = "";
142         if (entriesByType.get(type) != null) {
143             modelElementId = entriesByType.get(type).get(0).getId();
144         }
145         return modelElementId;
146     }
147
148     /**
149      * @return the fromId field given the ModelElement type
150      */
151     public String getFromId(String type) {
152         String modelElementFromIdId = "";
153         if (entriesByType.get(type) != null) {
154             modelElementFromIdId = entriesByType.get(type).get(0).getFromId();
155         }
156         return modelElementFromIdId;
157     }
158
159     /**
160      * @return the ModelElement type given the ModelElement id
161      */
162     public String getType(String id) {
163         return entriesById.get(id).get(0).getType();
164     }
165
166     /**
167      * @return list of elementIds from bpmn
168      */
169     public List<String> getBpmnElementIds() {
170         return bpmnElementIds;
171     }
172
173     public void setBpmnElementIds(List<String> bpmnElementIds) {
174         this.bpmnElementIds = bpmnElementIds;
175     }
176 }