Code Improvement
[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: {"policy"
49  * :[{"id":"Policy_0oxeocn", "from":"StartEvent_1"}]}
50  */
51 public class ModelBpmn {
52     protected static final EELFLogger               logger        = EELFManager.getInstance()
53             .getLogger(CldsService.class);
54     protected static final EELFLogger               auditLogger   = EELFManager.getInstance().getAuditLogger();
55
56     // for each type, an array of entries
57     private final Map<String, List<ModelBpmnEntry>> entriesByType = new HashMap<>();
58
59     // for each id, an array of entries
60     private final Map<String, List<ModelBpmnEntry>> entriesById   = new HashMap<>();
61
62     // List of all elementIds
63     private List<String>                            bpmnElementIds;
64
65     /**
66      * Create ModelBpmn and populate maps from json
67      *
68      * @param modelBpmnPropText
69      * @return
70      * @throws IOException
71      * @throws JsonMappingException
72      * @throws JsonParseException
73      */
74     public static ModelBpmn create(String modelBpmnPropText) throws IOException {
75         ModelBpmn modelBpmn = new ModelBpmn();
76         ObjectMapper objectMapper = new ObjectMapper();
77         ObjectNode root = objectMapper.readValue(modelBpmnPropText, ObjectNode.class);
78         // iterate over each entry like:
79         // "Policy":[{"id":"Policy","from":"StartEvent_1"}]
80         Iterator<Entry<String, JsonNode>> entryItr = root.fields();
81         List<String> bpmnElementIdList = new ArrayList<>();
82         while (entryItr.hasNext()) {
83             // process the entry
84             Entry<String, JsonNode> entry = entryItr.next();
85             String type = entry.getKey();
86             ArrayNode arrayNode = (ArrayNode) entry.getValue();
87             // process each id/from object, like:
88             // {"id":"Policy","from":"StartEvent_1"}
89             for (JsonNode anArrayNode : arrayNode) {
90                 ObjectNode node = (ObjectNode) anArrayNode;
91                 String id = node.get("id").asText();
92                 String fromId = node.get("from").asText();
93                 ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId);
94                 modelBpmn.addEntry(modelBpmnEntry);
95                 bpmnElementIdList.add(id);
96             }
97             modelBpmn.setBpmnElementIds(bpmnElementIdList);
98         }
99         return modelBpmn;
100     }
101
102     /**
103      * Add entry to both maps.
104      *
105      * @param entry
106      */
107     private void addEntry(ModelBpmnEntry entry) {
108         addEntry(entriesByType, entry, entry.getType());
109         addEntry(entriesById, entry, entry.getId());
110     }
111
112     /**
113      * Add an entry to provided map with provided key.
114      *
115      * @param map
116      * @param entry
117      * @param key
118      */
119     private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
120         List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
121         list.add(entry);
122     }
123
124     /**
125      * This method verifies if the ModelElement Type (holmes, tca,
126      * ...) is in the list.
127      *
128      * @param type
129      *            A model Element type (tca, ...)
130      * @return true if the element is found or false otherwise
131      */
132     public boolean isModelElementTypeInList(String type) {
133         return entriesByType.get(type) != null;
134     }
135
136     /**
137      * @return the id field given the ModelElement type
138      */
139     public String getId(String type) {
140         String modelElementId = "";
141         if (entriesByType.get(type) != null) {
142             modelElementId = entriesByType.get(type).get(0).getId();
143         }
144         return modelElementId;
145     }
146
147     /**
148      * @return the fromId field given the ModelElement type
149      */
150     public String getFromId(String type) {
151         String modelElementFromIdId = "";
152         if (entriesByType.get(type) != null) {
153             modelElementFromIdId = entriesByType.get(type).get(0).getFromId();
154         }
155         return modelElementFromIdId;
156     }
157
158     /**
159      * @return the ModelElement type given the ModelElement id
160      */
161     public String getType(String id) {
162         return entriesById.get(id).get(0).getType();
163     }
164
165     /**
166      * @return list of elementIds from bpmn
167      */
168     public List<String> getBpmnElementIds() {
169         return bpmnElementIds;
170     }
171
172     public void setBpmnElementIds(List<String> bpmnElementIds) {
173         this.bpmnElementIds = bpmnElementIds;
174     }
175 }