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