Code refactoring
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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.ObjectMapper;
30 import com.fasterxml.jackson.databind.node.ArrayNode;
31 import com.fasterxml.jackson.databind.node.ObjectNode;
32
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;
38 import java.util.Map;
39 import java.util.Map.Entry;
40
41 import org.onap.clamp.clds.exception.ModelBpmnException;
42 import org.onap.clamp.clds.service.CldsService;
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     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;
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             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()) {
76                 // process the entry
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);
89                 }
90                 modelBpmn.setBpmnElementIds(bpmnElementIdList);
91             }
92             return modelBpmn;
93         } catch (IOException e) {
94             throw new ModelBpmnException("Exception occurred during the decoding of the bpmn JSON", e);
95         }
96     }
97
98     /**
99      * Add entry to both maps.
100      *
101      * @param entry
102      */
103     private void addEntry(ModelBpmnEntry entry) {
104         addEntry(entriesByType, entry, entry.getType());
105         addEntry(entriesById, entry, entry.getId());
106     }
107
108     /**
109      * Add an entry to provided map with provided key.
110      *
111      * @param map
112      * @param entry
113      * @param key
114      */
115     private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
116         List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
117         list.add(entry);
118     }
119
120     /**
121      * This method verifies if the ModelElement Type (holmes, tca, ...) is in
122      * the list.
123      *
124      * @param type
125      *            A model Element type (tca, ...)
126      * @return true if the element is found or false otherwise
127      */
128     public boolean isModelElementTypeInList(String type) {
129         return entriesByType.get(type) != null;
130     }
131
132     /**
133      * @return the id field given the ModelElement type
134      */
135     public String getId(String type) {
136         String modelElementId = "";
137         if (entriesByType.get(type) != null) {
138             modelElementId = entriesByType.get(type).get(0).getId();
139         }
140         return modelElementId;
141     }
142
143     /**
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      * @return the ModelElement type given the ModelElement id
156      */
157     public String getType(String id) {
158         return entriesById.get(id).get(0).getType();
159     }
160
161     /**
162      * @return list of elementIds from bpmn
163      */
164     public List<String> getBpmnElementIds() {
165         return bpmnElementIds;
166     }
167
168     public void setBpmnElementIds(List<String> bpmnElementIds) {
169         this.bpmnElementIds = bpmnElementIds;
170     }
171 }