Remove useless code
[clamp.git] / src / main / java / org / onap / clamp / clds / model / properties / AbstractModelElement.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  *
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
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, VesCollector, Policy, Tca, Holmes, ...)
37  */
38 public abstract class AbstractModelElement {
39
40     protected static final EELFLogger logger = EELFManager.getInstance().getLogger(AbstractModelElement.class);
41     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
42     private final String type;
43     private final ModelBpmn modelBpmn;
44     private final String id;
45     protected String topicPublishes;
46     protected final JsonNode modelElementJsonNode;
47     private boolean isFound;
48     private final ModelProperties modelProp;
49     private static final String LOG_ELEMENT_NOT_FOUND = "Value '{}' for key 'name' not found in JSON";
50     private static final String LOG_ELEMENT_NOT_FOUND_IN_JSON = "Value '{}' for key 'name' not found in JSON {}";
51
52     /**
53      * Perform base parsing of properties for a ModelElement (such as, VesCollector,
54      * Policy and Tca)
55      */
56     protected AbstractModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
57         this.type = type;
58         this.modelProp = modelProp;
59         this.modelBpmn = modelBpmn;
60         this.id = modelBpmn.getId(type);
61         this.modelElementJsonNode = modelJson.get(id);
62         this.isFound = modelBpmn.isModelElementTypeInList(type);
63     }
64
65     /**
66      * @return the topicPublishes
67      */
68     public String getTopicPublishes() {
69         return topicPublishes;
70     }
71
72     /**
73      * Return the value field of the json node element that has a name field equals
74      * to the given name.
75      */
76     public static String getValueByName(JsonNode nodeIn, String name) {
77         String value = null;
78         if (nodeIn != null) {
79             for (JsonNode node : nodeIn) {
80                 if (node.path("name").asText().equals(name)) {
81                     JsonNode vnode = node.path("value");
82                     if (vnode.isArray()) {
83                         // if array, assume value is in first element
84                         value = vnode.path(0).asText();
85                     } else {
86                         // otherwise, just return text
87                         value = vnode.asText();
88                     }
89                 }
90             }
91         }
92         if (value == null || value.length() == 0) {
93             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
94         } else {
95             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
96         }
97         return value;
98     }
99
100     /**
101      * Return the Json value field of the json node element that has a name field
102      * equals to the given name.
103      */
104     public static JsonNode getJsonNodeByName(JsonNode nodeIn, String name) {
105         JsonNode vnode = null;
106         if (nodeIn != null) {
107             for (JsonNode node : nodeIn) {
108                 if (node.path("name").asText().equals(name)) {
109                     vnode = node.path("value");
110                 }
111             }
112         }
113         if (vnode == null) {
114             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
115         } else {
116             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
117         }
118         return vnode;
119     }
120
121     /**
122      * Return the value field of the json node element that has a name field that
123      * equals the given name.
124      */
125     public static String getNodeValueByName(JsonNode nodeIn, String name) {
126         String value = null;
127         if (nodeIn != null) {
128             value = nodeIn.path(name).asText();
129         }
130         if (value == null || value.length() == 0) {
131             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
132         } else {
133             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
134         }
135         return value;
136     }
137
138     /**
139      * Return the value field of the json node element that has a name field that
140      * equals the given name.
141      */
142     public static List<String> getNodeValuesByName(JsonNode nodeIn, String name) {
143         List<String> values = new ArrayList<>();
144         if (nodeIn != null) {
145             for (JsonNode node : nodeIn) {
146                 if (node.path("name").asText().equals(name)) {
147                     JsonNode vnode = node.path("value");
148                     if (vnode.isArray()) {
149                         // if array, assume value is in first element
150                         values.add(vnode.path(0).asText());
151                     } else {
152                         // otherwise, just return text
153                         values.add(vnode.asText());
154                     }
155                 }
156             }
157         }
158         return values;
159     }
160
161     /**
162      * Return the int value field of the json node element that has a name field
163      * equals to the given name.
164      */
165     public static Integer getIntValueByName(JsonNode nodeIn, String name) {
166         String value = getValueByName(nodeIn, name);
167         return Integer.valueOf(value);
168     }
169
170     /**
171      * Return an array of values for the field of the json node element that has a
172      * name field equals to the given name.
173      */
174     public static List<String> getValuesByName(JsonNode nodeIn, String name) {
175         List<String> values = null;
176         if (nodeIn != null) {
177             for (JsonNode node : nodeIn) {
178                 if (node.path("name").asText().equals(name)) {
179                     values = getValuesList(node);
180                 }
181             }
182         }
183         if (values == null || values.isEmpty()) {
184             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
185         } else {
186             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
187         }
188         return values;
189     }
190
191     /**
192      * Return an array of String values.
193      */
194     public static List<String> getValuesList(JsonNode nodeIn) {
195         ArrayList<String> al = new ArrayList<>();
196         if (nodeIn != null) {
197             Iterator<JsonNode> itr = nodeIn.path("value").elements();
198             while (itr.hasNext()) {
199                 JsonNode node = itr.next();
200                 al.add(node.asText());
201             }
202         }
203         return al;
204     }
205
206     /**
207      * Return the value field of the json node element that has a name field equals
208      * to the given name.
209      */
210     public String getValueByName(String name) {
211         return getValueByName(modelElementJsonNode, name);
212     }
213
214     /**
215      * Return the int value field of the json node element that has a name field
216      * equals to the given name.
217      */
218     public Integer getIntValueByName(String name) {
219         return getIntValueByName(modelElementJsonNode, name);
220     }
221
222     /**
223      * Return an array of values for the field of the json node element that has a
224      * name field equals to the given name.
225      */
226     public List<String> getValuesByName(String name) {
227         return getValuesByName(modelElementJsonNode, name);
228     }
229
230     /**
231      * @return the id
232      */
233     public String getId() {
234         return id;
235     }
236
237     /**
238      * @return the isFound
239      */
240     public boolean isFound() {
241         return isFound;
242     }
243 }