Remove ECOMP in headers
[clamp.git] / src / main / java / org / onap / clamp / clds / model / properties / AbstractModelElement.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights
6  *                             reserved.
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
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
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  * 
22  */
23
24 package org.onap.clamp.clds.model.properties;
25
26 import com.att.eelf.configuration.EELFLogger;
27 import com.att.eelf.configuration.EELFManager;
28 import com.fasterxml.jackson.databind.JsonNode;
29
30 import java.util.ArrayList;
31 import java.util.Iterator;
32 import java.util.List;
33
34 /**
35  * Provide base ModelElement functionality. Perform base parsing of properties
36  * for a ModelElement (such as, VesCollector, Policy, Tca, Holmes, ...)
37  */
38 public abstract class AbstractModelElement {
39
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 {}";
51
52     /**
53      * Perform base parsing of properties for a ModelElement (such as,
54      * VesCollector, Policy and Tca)
55      */
56     protected AbstractModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
57         this.type = type;
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);
63     }
64
65     /**
66      * topicSubscribes is the topicPublishes of the from Model Element (the
67      * previous one in the chain).
68      *
69      * @return the topicSubscribes
70      */
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;
80     }
81
82     /**
83      * @return the topicPublishes
84      */
85     public String getTopicPublishes() {
86         return topicPublishes;
87     }
88
89     /**
90      * Return the value field of the json node element that has a name field
91      * equals to the given name.
92      */
93     public static String getValueByName(JsonNode nodeIn, String name) {
94         String value = null;
95         if (nodeIn != null) {
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();
102                     } else {
103                         // otherwise, just return text
104                         value = vnode.asText();
105                     }
106                 }
107             }
108         }
109         if (value == null || value.length() == 0) {
110             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
111         } else {
112             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
113         }
114         return value;
115     }
116
117     /**
118      * Return the Json value field of the json node element that has a name
119      * field equals to the given name.
120      */
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");
127                 }
128             }
129         }
130         if (vnode == null) {
131             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
132         } else {
133             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
134         }
135         return vnode;
136     }
137
138     /**
139      * Return the value field of the json node element that has a name field
140      * that equals the given name.
141      */
142     public static String getNodeValueByName(JsonNode nodeIn, String name) {
143         String value = null;
144         if (nodeIn != null) {
145             value = nodeIn.path(name).asText();
146         }
147         if (value == null || value.length() == 0) {
148             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
149         } else {
150             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
151         }
152         return value;
153     }
154
155     /**
156      * Return the value field of the json node element that has a name field
157      * that equals the given name.
158      */
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());
168                     } else {
169                         // otherwise, just return text
170                         values.add(vnode.asText());
171                     }
172                 }
173             }
174         }
175         return values;
176     }
177
178     /**
179      * Return the int value field of the json node element that has a name field
180      * equals to the given name.
181      */
182     public static Integer getIntValueByName(JsonNode nodeIn, String name) {
183         String value = getValueByName(nodeIn, name);
184         return Integer.valueOf(value);
185     }
186
187     /**
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.
190      */
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);
197                 }
198             }
199         }
200         if (values == null || values.isEmpty()) {
201             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
202         } else {
203             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
204         }
205         return values;
206     }
207
208     /**
209      * Return an array of String values.
210      */
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());
218             }
219         }
220         return al;
221     }
222
223     /**
224      * Return the value field of the json node element that has a name field
225      * equals to the given name.
226      */
227     public String getValueByName(String name) {
228         return getValueByName(modelElementJsonNode, name);
229     }
230
231     /**
232      * Return the int value field of the json node element that has a name field
233      * equals to the given name.
234      */
235     public Integer getIntValueByName(String name) {
236         return getIntValueByName(modelElementJsonNode, name);
237     }
238
239     /**
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.
242      */
243     public List<String> getValuesByName(String name) {
244         return getValuesByName(modelElementJsonNode, name);
245     }
246
247     /**
248      * @return the id
249      */
250     public String getId() {
251         return id;
252     }
253
254     /**
255      * @return the isFound
256      */
257     public boolean isFound() {
258         return isFound;
259     }
260 }