2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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 * ===================================================================
24 package org.onap.clamp.clds.model.properties;
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
35 * Provide base ModelElement functionality. Perform base parsing of properties
36 * for a ModelElement (such as, VesCollector, Policy, Tca, Holmes, ...)
38 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 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 {}";
53 * Perform base parsing of properties for a ModelElement (such as, VesCollector,
56 protected AbstractModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
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);
66 * @return the topicPublishes
68 public String getTopicPublishes() {
69 return topicPublishes;
73 * Return the value field of the json node element that has a name field equals
76 public static String getValueByName(JsonNode nodeIn, String name) {
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();
86 // otherwise, just return text
87 value = vnode.asText();
92 if (value == null || value.length() == 0) {
93 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
95 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
101 * Return the Json value field of the json node element that has a name field
102 * equals to the given name.
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");
114 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
116 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
122 * Return the value field of the json node element that has a name field that
123 * equals the given name.
125 public static String getNodeValueByName(JsonNode nodeIn, String name) {
127 if (nodeIn != null) {
128 value = nodeIn.path(name).asText();
130 if (value == null || value.length() == 0) {
131 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
133 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
139 * Return the value field of the json node element that has a name field that
140 * equals the given name.
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());
152 // otherwise, just return text
153 values.add(vnode.asText());
162 * Return the int value field of the json node element that has a name field
163 * equals to the given name.
165 public static Integer getIntValueByName(JsonNode nodeIn, String name) {
166 String value = getValueByName(nodeIn, name);
167 return Integer.valueOf(value);
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.
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);
183 if (values == null || values.isEmpty()) {
184 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
186 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
192 * Return an array of String values.
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());
207 * Return the value field of the json node element that has a name field equals
210 public String getValueByName(String name) {
211 return getValueByName(modelElementJsonNode, name);
215 * Return the int value field of the json node element that has a name field
216 * equals to the given name.
218 public Integer getIntValueByName(String name) {
219 return getIntValueByName(modelElementJsonNode, name);
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.
226 public List<String> getValuesByName(String name) {
227 return getValuesByName(modelElementJsonNode, name);
233 public String getId() {
238 * @return the isFound
240 public boolean isFound() {