[CLAMP-1] Initial ONAP CLAMP seed code commit
[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.fasterxml.jackson.core.JsonParseException;
27 import com.fasterxml.jackson.databind.JsonMappingException;
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.*;
35 import java.util.Map.Entry;
36 import java.util.logging.Logger;
37
38 /**
39  * Parse Model BPMN properties.
40  * <p>
41  * Example json: {"collector":[{"id":"Collector_11r50j1", "from":"StartEvent_1"}],"stringMatch":[{"id":"StringMatch_0h6cbdv"}],"policy":[{"id":"Policy_0oxeocn", "from":"StringMatch_0h6cbdv"}]}
42  */
43 public class ModelBpmn {
44     private static final Logger logger = Logger.getLogger(ModelBpmn.class.getName());
45
46     // for each type, an array of entries
47     private final Map<String, List<ModelBpmnEntry>> entriesByType = new HashMap<>();
48
49     // for each id, an array of entries
50     private final Map<String, List<ModelBpmnEntry>> entriesById   = new HashMap<>();
51
52     // List of all elementIds
53     private List<String>                            bpmnElementIds;
54
55     /**
56      * Create ModelBpmn and populate maps from json
57      *
58      * @param modelBpmnPropText
59      * @return
60      * @throws IOException
61      * @throws JsonMappingException
62      * @throws JsonParseException
63      */
64     public static ModelBpmn create(String modelBpmnPropText) throws IOException {
65         ModelBpmn modelBpmn = new ModelBpmn();
66         ObjectMapper objectMapper = new ObjectMapper();
67         ObjectNode root = objectMapper.readValue(modelBpmnPropText, ObjectNode.class);
68         // iterate over each entry like: "collector":[{"id":"Collector_11r50j1","from":"StartEvent_1"}]
69         Iterator<Entry<String, JsonNode>> entryItr = root.fields();
70         List<String> bpmnElementIdList = new ArrayList<>();
71         while (entryItr.hasNext()) {
72             // process the entry
73             Entry<String, JsonNode> entry = entryItr.next();
74             String type = entry.getKey();
75             ArrayNode arrayNode = (ArrayNode) entry.getValue();
76             // process each id/from object, like: {"id":"Collector_11r50j1","from":"StartEvent_1"}
77             for (JsonNode anArrayNode : arrayNode) {
78                 ObjectNode node = (ObjectNode) anArrayNode;
79                 String id = node.get("id").asText();
80                 String fromId = node.get("from").asText();
81                 ModelBpmnEntry modelBpmnEntry = new ModelBpmnEntry(type, id, fromId);
82                 modelBpmn.addEntry(modelBpmnEntry);
83                 bpmnElementIdList.add(id);
84             }
85             modelBpmn.setBpmnElementIds(bpmnElementIdList);
86         }
87         return modelBpmn;
88     }
89
90     /**
91      * Add entry to both maps.
92      *
93      * @param entry
94      */
95     private void addEntry(ModelBpmnEntry entry) {
96         addEntry(entriesByType, entry, entry.getType());
97         addEntry(entriesById, entry, entry.getId());
98     }
99
100     /**
101      * Add an entry to provided map with provided key.
102      *
103      * @param map
104      * @param entry
105      * @param key
106      */
107     private static void addEntry(Map<String, List<ModelBpmnEntry>> map, ModelBpmnEntry entry, String key) {
108         List<ModelBpmnEntry> list = map.computeIfAbsent(key, k -> new ArrayList<>());
109         list.add(entry);
110     }
111
112     /**
113      * 
114      * 
115      * @param type
116      * @return true if the element is found or false otherwise
117      */
118     public boolean getModelElementFound(String type) {
119         return entriesByType.get(type) != null;
120     }
121
122     /**
123      * @return the id field given the ModelElement type
124      */
125     public String getId(String type) {
126         return entriesByType.get(type).get(0).getId();
127     }
128
129     /**
130      * @return the fromId field given the ModelElement type
131      */
132     public String getFromId(String type) {
133         return entriesByType.get(type).get(0).getFromId();
134     }
135
136     /**
137      * @return the ModelElement type given the ModelElement id
138      */
139     public String getType(String id) {
140         return entriesById.get(id).get(0).getType();
141     }
142
143     /**
144      * @return list of elementIds from bpmn
145      */
146     public List<String> getBpmnElementIds() {
147         return bpmnElementIds;
148     }
149
150     public void setBpmnElementIds(List<String> bpmnElementIds) {
151         this.bpmnElementIds = bpmnElementIds;
152     }
153 }