a7dd5cbf03d91a2882c51a93129c597fdc444fe3
[clamp.git] / src / main / java / org / onap / clamp / clds / model / prop / AbstractModelElement.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.databind.JsonNode;
29
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
33
34 /**
35  * Provide base ModelElement functionality. Perform base parsing of properties
36  * for a ModelElement (such as, Collector, StringMatch, Policy, Tca, Holmes,
37  * ...)
38  */
39 public abstract class AbstractModelElement {
40     protected static final EELFLogger logger      = EELFManager.getInstance().getLogger(AbstractModelElement.class);
41     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
42
43     private final String              type;
44     private final ModelBpmn           modelBpmn;
45     private final String              id;
46     protected String                  topicPublishes;
47     protected final JsonNode          modelElementJsonNode;
48     private boolean                   isFound;
49
50     private final ModelProperties     modelProp;
51
52     /**
53      * Perform base parsing of properties for a ModelElement (such as,
54      * Collector, StringMatch, Policy and Tca)
55      *
56      * @param type
57      * @param modelProp
58      * @param modelBpmn
59      * @param modelJson
60      */
61     protected AbstractModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
62         this.type = type;
63         this.modelProp = modelProp;
64         this.modelBpmn = modelBpmn;
65         this.id = modelBpmn.getId(type);
66         this.modelElementJsonNode = modelJson.get(id);
67         this.isFound = modelBpmn.isModelElementTypeInList(type);
68     }
69
70     /**
71      * topicSubscribes is the topicPublishes of the from Model Element (the
72      * previous one in the chain).
73      *
74      * @return the topicSubscribes
75      */
76     public String getTopicSubscribes() {
77         // get fromId for this type
78         String fromId = modelBpmn.getFromId(type);
79         // find the type of the from model element
80         String fromType = modelBpmn.getType(fromId);
81         // get the model element for the type
82         AbstractModelElement me = modelProp.getModelElementByType(fromType);
83         // get the topic publishes for the model element
84         return me.topicPublishes;
85     }
86
87     /**
88      * @return the topicPublishes
89      */
90     public String getTopicPublishes() {
91         return topicPublishes;
92     }
93
94     /**
95      * Return the value field of the json node element that has a name field
96      * equals to the given name.
97      *
98      * @param nodeIn
99      * @param name
100      * @return
101      */
102     public static String getValueByName(JsonNode nodeIn, String name) {
103         String value = null;
104         if (nodeIn != null) {
105             for (JsonNode node : nodeIn) {
106                 if (node.path("name").asText().equals(name)) {
107                     JsonNode vnode = node.path("value");
108                     if (vnode.isArray()) {
109                         // if array, assume value is in first element
110                         value = vnode.path(0).asText();
111                     } else {
112                         // otherwise, just return text
113                         value = vnode.asText();
114                     }
115                 }
116             }
117         }
118         if (value == null || value.length() == 0) {
119             logger.warn(name + "=" + value);
120         } else {
121             logger.debug(name + "=" + value);
122         }
123         return value;
124     }
125
126     /**
127      * Return the value field of the json node element that has a name field
128      * that equals the given name.
129      * 
130      * @param nodeIn
131      * @param name
132      * @return
133      */
134     public static String getNodeValueByName(JsonNode nodeIn, String name) {
135         String value = null;
136         if (nodeIn != null) {
137             value = nodeIn.path(name).asText();
138         }
139         if (value == null || value.length() == 0) {
140             logger.warn(name + "=" + value);
141         } else {
142             logger.debug(name + "=" + value);
143         }
144         return value;
145     }
146
147     /**
148      * Return the value field of the json node element that has a name field
149      * that equals the given name.
150      * 
151      * @param nodeIn
152      * @param name
153      * @return
154      */
155     public static List<String> getNodeValuesByName(JsonNode nodeIn, String name) {
156         List<String> values = new ArrayList<>();
157         if (nodeIn != null) {
158             Iterator<JsonNode> i = nodeIn.iterator();
159             while (i.hasNext()) {
160                 JsonNode node = i.next();
161                 if (node.path("name").asText().equals(name)) {
162                     JsonNode vnode = node.path("value");
163                     if (vnode.isArray()) {
164                         // if array, assume value is in first element
165                         values.add(vnode.path(0).asText());
166                     } else {
167                         // otherwise, just return text
168                         values.add(vnode.asText());
169                     }
170                 }
171             }
172         }
173         return values;
174     }
175
176     /**
177      * Return the int value field of the json node element that has a name field
178      * equals to the given name.
179      *
180      * @param nodeIn
181      * @param name
182      * @return
183      */
184     public static Integer getIntValueByName(JsonNode nodeIn, String name) {
185         String value = getValueByName(nodeIn, name);
186         return Integer.valueOf(value);
187     }
188
189     /**
190      * Return an array of values for the field of the json node element that has
191      * a name field equals to the given name.
192      *
193      * @param nodeIn
194      * @param name
195      * @return
196      */
197     public static List<String> getValuesByName(JsonNode nodeIn, String name) {
198         List<String> values = null;
199         if (nodeIn != null) {
200             Iterator<JsonNode> i = nodeIn.iterator();
201             while (i.hasNext()) {
202                 JsonNode node = i.next();
203                 if (node.path("name").asText().equals(name)) {
204                     values = getValuesList(node);
205                 }
206             }
207         }
208         if (values == null || values.isEmpty()) {
209             logger.warn(name + "=" + values);
210         } else {
211             logger.debug(name + "=" + values);
212         }
213         return values;
214     }
215
216     /**
217      * Return an array of String values.
218      *
219      * @param nodeIn
220      * @return
221      */
222     public static List<String> getValuesList(JsonNode nodeIn) {
223         ArrayList<String> al = new ArrayList<>();
224         if (nodeIn != null) {
225             Iterator<JsonNode> itr = nodeIn.path("value").elements();
226             while (itr.hasNext()) {
227                 JsonNode node = itr.next();
228                 al.add(node.asText());
229             }
230         }
231         return al;
232     }
233
234     /**
235      * Return the value field of the json node element that has a name field
236      * equals to the given name.
237      *
238      * @param name
239      * @return
240      */
241     public String getValueByName(String name) {
242         return getValueByName(modelElementJsonNode, name);
243     }
244
245     /**
246      * Return the int value field of the json node element that has a name field
247      * equals to the given name.
248      *
249      * @param name
250      * @return
251      */
252     public Integer getIntValueByName(String name) {
253         return getIntValueByName(modelElementJsonNode, name);
254     }
255
256     /**
257      * Return an array of values for the field of the json node element that has
258      * a name field equals to the given name.
259      *
260      * @param name
261      * @return
262      */
263     public List<String> getValuesByName(String name) {
264         return getValuesByName(modelElementJsonNode, name);
265     }
266
267     /**
268      * @return the id
269      */
270     public String getId() {
271         return id;
272     }
273
274     /**
275      * @return the isFound
276      */
277     public boolean isFound() {
278         return isFound;
279     }
280 }