2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.onap.clamp.clds.model.prop;
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,
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();
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;
50 private final ModelProperties modelProp;
52 private static final String LOG_ELEMENT = "Value '";
53 private static final String LOG_NOT_FOUND = "' for key 'name' not found in JSON";
56 * Perform base parsing of properties for a ModelElement (such as,
57 * VesCollector, Policy and Tca)
64 protected AbstractModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
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);
74 * topicSubscribes is the topicPublishes of the from Model Element (the
75 * previous one in the chain).
77 * @return the topicSubscribes
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;
91 * @return the topicPublishes
93 public String getTopicPublishes() {
94 return topicPublishes;
98 * Return the value field of the json node element that has a name field
99 * equals to the given name.
105 public static String getValueByName(JsonNode nodeIn, String name) {
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();
115 // otherwise, just return text
116 value = vnode.asText();
121 if (value == null || value.length() == 0) {
122 logger.warn(LOG_ELEMENT + name + LOG_NOT_FOUND);
124 logger.debug(LOG_ELEMENT + name + LOG_NOT_FOUND + nodeIn.toString());
130 * Return the value field of the json node element that has a name field
131 * that equals the given name.
137 public static String getNodeValueByName(JsonNode nodeIn, String name) {
139 if (nodeIn != null) {
140 value = nodeIn.path(name).asText();
142 if (value == null || value.length() == 0) {
143 logger.warn(LOG_ELEMENT + name + LOG_NOT_FOUND);
145 logger.debug(LOG_ELEMENT + name + LOG_NOT_FOUND + nodeIn.toString());
151 * Return the value field of the json node element that has a name field
152 * that equals the given name.
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());
170 // otherwise, just return text
171 values.add(vnode.asText());
180 * Return the int value field of the json node element that has a name field
181 * equals to the given name.
187 public static Integer getIntValueByName(JsonNode nodeIn, String name) {
188 String value = getValueByName(nodeIn, name);
189 return Integer.valueOf(value);
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.
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);
211 if (values == null || values.isEmpty()) {
212 logger.warn(LOG_ELEMENT + name + LOG_NOT_FOUND);
214 logger.debug(LOG_ELEMENT + name + LOG_NOT_FOUND + nodeIn.toString());
220 * Return an array of String values.
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());
238 * Return the value field of the json node element that has a name field
239 * equals to the given name.
244 public String getValueByName(String name) {
245 return getValueByName(modelElementJsonNode, name);
249 * Return the int value field of the json node element that has a name field
250 * equals to the given name.
255 public Integer getIntValueByName(String name) {
256 return getIntValueByName(modelElementJsonNode, name);
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.
266 public List<String> getValuesByName(String name) {
267 return getValuesByName(modelElementJsonNode, name);
273 public String getId() {
278 * @return the isFound
280 public boolean isFound() {