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, Collector, StringMatch, 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;
53 * Perform base parsing of properties for a ModelElement (such as,
54 * Collector, StringMatch, Policy and Tca)
61 protected AbstractModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
63 this.modelProp = modelProp;
64 this.modelBpmn = modelBpmn;
65 this.id = modelBpmn.getId(type);
66 this.modelElementJsonNode = modelJson.get(id);
67 this.isFound = modelBpmn.isModelElementTypeInList(type);
71 * topicSubscribes is the topicPublishes of the from Model Element (the
72 * previous one in the chain).
74 * @return the topicSubscribes
76 public String getTopicSubscribes() {
77 // get fromId for this type
78 String fromId = modelBpmn.getFromId(type);
79 // find the type of the from model element
80 String fromType = modelBpmn.getType(fromId);
81 // get the model element for the type
82 AbstractModelElement me = modelProp.getModelElementByType(fromType);
83 // get the topic publishes for the model element
84 return me.topicPublishes;
88 * @return the topicPublishes
90 public String getTopicPublishes() {
91 return topicPublishes;
95 * Return the value field of the json node element that has a name field
96 * equals to the given name.
102 public static String getValueByName(JsonNode nodeIn, String name) {
104 if (nodeIn != null) {
105 for (JsonNode node : nodeIn) {
106 if (node.path("name").asText().equals(name)) {
107 JsonNode vnode = node.path("value");
108 if (vnode.isArray()) {
109 // if array, assume value is in first element
110 value = vnode.path(0).asText();
112 // otherwise, just return text
113 value = vnode.asText();
118 if (value == null || value.length() == 0) {
119 logger.warn(name + "=" + value);
121 logger.debug(name + "=" + value);
127 * Return the value field of the json node element that has a name field
128 * that equals the given name.
134 public static String getNodeValueByName(JsonNode nodeIn, String name) {
136 if (nodeIn != null) {
137 value = nodeIn.path(name).asText();
139 if (value == null || value.length() == 0) {
140 logger.warn(name + "=" + value);
142 logger.debug(name + "=" + value);
148 * Return the value field of the json node element that has a name field
149 * that equals the given name.
155 public static List<String> getNodeValuesByName(JsonNode nodeIn, String name) {
156 List<String> values = new ArrayList<>();
157 if (nodeIn != null) {
158 Iterator<JsonNode> i = nodeIn.iterator();
159 while (i.hasNext()) {
160 JsonNode node = i.next();
161 if (node.path("name").asText().equals(name)) {
162 JsonNode vnode = node.path("value");
163 if (vnode.isArray()) {
164 // if array, assume value is in first element
165 values.add(vnode.path(0).asText());
167 // otherwise, just return text
168 values.add(vnode.asText());
177 * Return the int value field of the json node element that has a name field
178 * equals to the given name.
184 public static Integer getIntValueByName(JsonNode nodeIn, String name) {
185 String value = getValueByName(nodeIn, name);
186 return Integer.valueOf(value);
190 * Return an array of values for the field of the json node element that has
191 * a name field equals to the given name.
197 public static List<String> getValuesByName(JsonNode nodeIn, String name) {
198 List<String> values = null;
199 if (nodeIn != null) {
200 Iterator<JsonNode> i = nodeIn.iterator();
201 while (i.hasNext()) {
202 JsonNode node = i.next();
203 if (node.path("name").asText().equals(name)) {
204 values = getValuesList(node);
208 if (values == null || values.isEmpty()) {
209 logger.warn(name + "=" + values);
211 logger.debug(name + "=" + values);
217 * Return an array of String values.
222 public static List<String> getValuesList(JsonNode nodeIn) {
223 ArrayList<String> al = new ArrayList<>();
224 if (nodeIn != null) {
225 Iterator<JsonNode> itr = nodeIn.path("value").elements();
226 while (itr.hasNext()) {
227 JsonNode node = itr.next();
228 al.add(node.asText());
235 * Return the value field of the json node element that has a name field
236 * equals to the given name.
241 public String getValueByName(String name) {
242 return getValueByName(modelElementJsonNode, name);
246 * Return the int value field of the json node element that has a name field
247 * equals to the given name.
252 public Integer getIntValueByName(String name) {
253 return getIntValueByName(modelElementJsonNode, name);
257 * Return an array of values for the field of the json node element that has
258 * a name field equals to the given name.
263 public List<String> getValuesByName(String name) {
264 return getValuesByName(modelElementJsonNode, name);
270 public String getId() {
275 * @return the isFound
277 public boolean isFound() {