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,
54 * VesCollector, Policy and Tca)
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 * topicSubscribes is the topicPublishes of the from Model Element (the
67 * previous one in the chain).
69 * @return the topicSubscribes
71 public String getTopicSubscribes() {
72 // get fromId for this type
73 String fromId = modelBpmn.getFromId(type);
74 // find the type of the from model element
75 String fromType = modelBpmn.getType(fromId);
76 // get the model element for the type
77 AbstractModelElement me = modelProp.getModelElementByType(fromType);
78 // get the topic publishes for the model element
79 return me.topicPublishes;
83 * @return the topicPublishes
85 public String getTopicPublishes() {
86 return topicPublishes;
90 * Return the value field of the json node element that has a name field
91 * equals to the given name.
93 public static String getValueByName(JsonNode nodeIn, String name) {
96 for (JsonNode node : nodeIn) {
97 if (node.path("name").asText().equals(name)) {
98 JsonNode vnode = node.path("value");
99 if (vnode.isArray()) {
100 // if array, assume value is in first element
101 value = vnode.path(0).asText();
103 // otherwise, just return text
104 value = vnode.asText();
109 if (value == null || value.length() == 0) {
110 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
112 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
118 * Return the Json value field of the json node element that has a name
119 * field equals to the given name.
121 public static JsonNode getJsonNodeByName(JsonNode nodeIn, String name) {
122 JsonNode vnode = null;
123 if (nodeIn != null) {
124 for (JsonNode node : nodeIn) {
125 if (node.path("name").asText().equals(name)) {
126 vnode = node.path("value");
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
140 * that equals the given name.
142 public static String getNodeValueByName(JsonNode nodeIn, String name) {
144 if (nodeIn != null) {
145 value = nodeIn.path(name).asText();
147 if (value == null || value.length() == 0) {
148 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
150 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
156 * Return the value field of the json node element that has a name field
157 * that equals the given name.
159 public static List<String> getNodeValuesByName(JsonNode nodeIn, String name) {
160 List<String> values = new ArrayList<>();
161 if (nodeIn != null) {
162 for (JsonNode node : nodeIn) {
163 if (node.path("name").asText().equals(name)) {
164 JsonNode vnode = node.path("value");
165 if (vnode.isArray()) {
166 // if array, assume value is in first element
167 values.add(vnode.path(0).asText());
169 // otherwise, just return text
170 values.add(vnode.asText());
179 * Return the int value field of the json node element that has a name field
180 * equals to the given name.
182 public static Integer getIntValueByName(JsonNode nodeIn, String name) {
183 String value = getValueByName(nodeIn, name);
184 return Integer.valueOf(value);
188 * Return an array of values for the field of the json node element that has
189 * a name field equals to the given name.
191 public static List<String> getValuesByName(JsonNode nodeIn, String name) {
192 List<String> values = null;
193 if (nodeIn != null) {
194 for (JsonNode node : nodeIn) {
195 if (node.path("name").asText().equals(name)) {
196 values = getValuesList(node);
200 if (values == null || values.isEmpty()) {
201 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
203 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
209 * Return an array of String values.
211 public static List<String> getValuesList(JsonNode nodeIn) {
212 ArrayList<String> al = new ArrayList<>();
213 if (nodeIn != null) {
214 Iterator<JsonNode> itr = nodeIn.path("value").elements();
215 while (itr.hasNext()) {
216 JsonNode node = itr.next();
217 al.add(node.asText());
224 * Return the value field of the json node element that has a name field
225 * equals to the given name.
227 public String getValueByName(String name) {
228 return getValueByName(modelElementJsonNode, name);
232 * Return the int value field of the json node element that has a name field
233 * equals to the given name.
235 public Integer getIntValueByName(String name) {
236 return getIntValueByName(modelElementJsonNode, name);
240 * Return an array of values for the field of the json node element that has
241 * a name field equals to the given name.
243 public List<String> getValuesByName(String name) {
244 return getValuesByName(modelElementJsonNode, name);
250 public String getId() {
255 * @return the isFound
257 public boolean isFound() {