Remove ECOMP in headers
[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 import com.fasterxml.jackson.databind.JsonNode;
29 import com.fasterxml.jackson.databind.node.ArrayNode;
30 import com.fasterxml.jackson.databind.node.ObjectNode;
31
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;
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.JacksonUtils;
43
44 /**
45  * Parse Model BPMN properties.
46  * <p>
47  * Example json: {"policy" :[{"id":"Policy_0oxeocn", "from":"StartEvent_1"}]}
48  */
49 public class ModelBpmn {
50
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;
59
60     /**
61      * Create ModelBpmn and populate maps from json
62      *
63      * @param modelBpmnPropText
64      * @return
65      */
66     public static ModelBpmn create(String modelBpmnPropText) {
67         try {
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()) {
75                 // process the entry
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);
88                 }
89                 modelBpmn.setBpmnElementIds(bpmnElementIdList);
90             }
91             return modelBpmn;
92         } catch (IOException e) {
93             throw new ModelBpmnException("Exception occurred during the decoding of the bpmn JSON", e);
94         }
95     }
96
97     /**
98      * Add entry to both maps.
99      *
100      * @param entry
101      */
102     private void addEntry(ModelBpmnEntry entry) {
103         addEntry(entriesByType, entry, entry.getType());
104         addEntry(entriesById, entry, entry.getId());
105     }
106
107     /**
108      * Add an entry to provided map with provided key.
109      *
110      * @param map
111      * @param entry
112      * @param key
113      */
114     private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
115         List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
116         list.add(entry);
117     }
118
119     /**
120      * This method verifies if the ModelElement Type (holmes, tca, ...) is in
121      * the list.
122      *
123      * @param type
124      *            A model Element type (tca, ...)
125      * @return true if the element is found or false otherwise
126      */
127     public boolean isModelElementTypeInList(String type) {
128         return entriesByType.get(type) != null;
129     }
130
131     /**
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      * @return the fromId field given the ModelElement type
144      */
145     public String getFromId(String type) {
146         String modelElementFromIdId = "";
147         if (entriesByType.get(type) != null) {
148             modelElementFromIdId = entriesByType.get(type).get(0).getFromId();
149         }
150         return modelElementFromIdId;
151     }
152
153     /**
154      * @return the ModelElement type given the ModelElement id
155      */
156     public String getType(String id) {
157         return entriesById.get(id).get(0).getType();
158     }
159
160     /**
161      * @return list of elementIds from bpmn
162      */
163     public List<String> getBpmnElementIds() {
164         return bpmnElementIds;
165     }
166
167     public void setBpmnElementIds(List<String> bpmnElementIds) {
168         this.bpmnElementIds = bpmnElementIds;
169     }
170 }