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 * ===================================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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;
29 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
34 * Provide base ModelElement functionality. Perform base parsing of properties
35 * 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();
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_NOT_FOUND = "Value '{}' for key 'name' not found in JSON";
53 private static final String LOG_ELEMENT_NOT_FOUND_IN_JSON = "Value '{}' for key 'name' not found in JSON {}";
56 * Perform base parsing of properties for a ModelElement (such as,
57 * VesCollector, Policy and Tca)
59 protected AbstractModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
61 this.modelProp = modelProp;
62 this.modelBpmn = modelBpmn;
63 this.id = modelBpmn.getId(type);
64 this.modelElementJsonNode = modelJson.get(id);
65 this.isFound = modelBpmn.isModelElementTypeInList(type);
69 * topicSubscribes is the topicPublishes of the from Model Element (the
70 * previous one in the chain).
72 * @return the topicSubscribes
74 public String getTopicSubscribes() {
75 // get fromId for this type
76 String fromId = modelBpmn.getFromId(type);
77 // find the type of the from model element
78 String fromType = modelBpmn.getType(fromId);
79 // get the model element for the type
80 AbstractModelElement me = modelProp.getModelElementByType(fromType);
81 // get the topic publishes for the model element
82 return me.topicPublishes;
86 * @return the topicPublishes
88 public String getTopicPublishes() {
89 return topicPublishes;
93 * Return the value field of the json node element that has a name field
94 * equals to the given name.
96 public static String getValueByName(JsonNode nodeIn, String name) {
99 for (JsonNode node : nodeIn) {
100 if (node.path("name").asText().equals(name)) {
101 JsonNode vnode = node.path("value");
102 if (vnode.isArray()) {
103 // if array, assume value is in first element
104 value = vnode.path(0).asText();
106 // otherwise, just return text
107 value = vnode.asText();
112 if (value == null || value.length() == 0) {
113 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
115 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
121 * Return the value field of the json node element that has a name field
122 * that equals the given name.
124 public static String getNodeValueByName(JsonNode nodeIn, String name) {
126 if (nodeIn != null) {
127 value = nodeIn.path(name).asText();
129 if (value == null || value.length() == 0) {
130 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
132 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
138 * Return the value field of the json node element that has a name field
139 * that equals the given name.
141 public static List<String> getNodeValuesByName(JsonNode nodeIn, String name) {
142 List<String> values = new ArrayList<>();
143 if (nodeIn != null) {
144 for (JsonNode node : nodeIn) {
145 if (node.path("name").asText().equals(name)) {
146 JsonNode vnode = node.path("value");
147 if (vnode.isArray()) {
148 // if array, assume value is in first element
149 values.add(vnode.path(0).asText());
151 // otherwise, just return text
152 values.add(vnode.asText());
161 * Return the int value field of the json node element that has a name field
162 * equals to the given name.
164 public static Integer getIntValueByName(JsonNode nodeIn, String name) {
165 String value = getValueByName(nodeIn, name);
166 return Integer.valueOf(value);
170 * Return an array of values for the field of the json node element that has
171 * a name field equals to the given name.
173 public static List<String> getValuesByName(JsonNode nodeIn, String name) {
174 List<String> values = null;
175 if (nodeIn != null) {
176 for (JsonNode node : nodeIn) {
177 if (node.path("name").asText().equals(name)) {
178 values = getValuesList(node);
182 if (values == null || values.isEmpty()) {
183 logger.warn(LOG_ELEMENT_NOT_FOUND, name);
185 logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
191 * Return an array of String values.
193 public static List<String> getValuesList(JsonNode nodeIn) {
194 ArrayList<String> al = new ArrayList<>();
195 if (nodeIn != null) {
196 Iterator<JsonNode> itr = nodeIn.path("value").elements();
197 while (itr.hasNext()) {
198 JsonNode node = itr.next();
199 al.add(node.asText());
206 * Return the value field of the json node element that has a name field
207 * equals to the given name.
209 public String getValueByName(String name) {
210 return getValueByName(modelElementJsonNode, name);
214 * Return the int value field of the json node element that has a name field
215 * equals to the given name.
217 public Integer getIntValueByName(String name) {
218 return getIntValueByName(modelElementJsonNode, name);
222 * Return an array of values for the field of the json node element that has
223 * a name field equals to the given name.
225 public List<String> getValuesByName(String name) {
226 return getValuesByName(modelElementJsonNode, name);
232 public String getId() {
237 * @return the isFound
239 public boolean isFound() {