9b8d9d9ce019e20baad9dee2cf6217294ff2b2a1
[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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
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 import java.util.ArrayList;
30 import java.util.Iterator;
31 import java.util.List;
32
33 /**
34  * Provide base ModelElement functionality. Perform base parsing of properties
35  * for a ModelElement (such as, VesCollector, Policy, Tca, Holmes,
36  * ...)
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
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;
49
50     private final ModelProperties modelProp;
51
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 {}";
54
55     /**
56      * Perform base parsing of properties for a ModelElement (such as,
57      * VesCollector, Policy and Tca)
58      */
59     protected AbstractModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
60         this.type = type;
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);
66     }
67
68     /**
69      * topicSubscribes is the topicPublishes of the from Model Element (the
70      * previous one in the chain).
71      *
72      * @return the topicSubscribes
73      */
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;
83     }
84
85     /**
86      * @return the topicPublishes
87      */
88     public String getTopicPublishes() {
89         return topicPublishes;
90     }
91
92     /**
93      * Return the value field of the json node element that has a name field
94      * equals to the given name.
95      */
96     public static String getValueByName(JsonNode nodeIn, String name) {
97         String value = null;
98         if (nodeIn != null) {
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();
105                     } else {
106                         // otherwise, just return text
107                         value = vnode.asText();
108                     }
109                 }
110             }
111         }
112         if (value == null || value.length() == 0) {
113             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
114         } else {
115             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
116         }
117         return value;
118     }
119
120     /**
121      * Return the value field of the json node element that has a name field
122      * that equals the given name.
123      */
124     public static String getNodeValueByName(JsonNode nodeIn, String name) {
125         String value = null;
126         if (nodeIn != null) {
127             value = nodeIn.path(name).asText();
128         }
129         if (value == null || value.length() == 0) {
130             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
131         } else {
132             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
133         }
134         return value;
135     }
136
137     /**
138      * Return the value field of the json node element that has a name field
139      * that equals the given name.
140      */
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());
150                     } else {
151                         // otherwise, just return text
152                         values.add(vnode.asText());
153                     }
154                 }
155             }
156         }
157         return values;
158     }
159
160     /**
161      * Return the int value field of the json node element that has a name field
162      * equals to the given name.
163      */
164     public static Integer getIntValueByName(JsonNode nodeIn, String name) {
165         String value = getValueByName(nodeIn, name);
166         return Integer.valueOf(value);
167     }
168
169     /**
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.
172      */
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);
179                 }
180             }
181         }
182         if (values == null || values.isEmpty()) {
183             logger.warn(LOG_ELEMENT_NOT_FOUND, name);
184         } else {
185             logger.debug(LOG_ELEMENT_NOT_FOUND_IN_JSON, name, nodeIn.toString());
186         }
187         return values;
188     }
189
190     /**
191      * Return an array of String values.
192      */
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());
200             }
201         }
202         return al;
203     }
204
205     /**
206      * Return the value field of the json node element that has a name field
207      * equals to the given name.
208      */
209     public String getValueByName(String name) {
210         return getValueByName(modelElementJsonNode, name);
211     }
212
213     /**
214      * Return the int value field of the json node element that has a name field
215      * equals to the given name.
216      */
217     public Integer getIntValueByName(String name) {
218         return getIntValueByName(modelElementJsonNode, name);
219     }
220
221     /**
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.
224      */
225     public List<String> getValuesByName(String name) {
226         return getValuesByName(modelElementJsonNode, name);
227     }
228
229     /**
230      * @return the id
231      */
232     public String getId() {
233         return id;
234     }
235
236     /**
237      * @return the isFound
238      */
239     public boolean isFound() {
240         return isFound;
241     }
242 }