Rework the Clds DAO and properties associated
[clamp.git] / src / main / java / org / onap / clamp / clds / model / prop / ModelElement.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2017 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.prop;
25
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29
30 import com.att.eelf.configuration.EELFLogger;
31 import com.att.eelf.configuration.EELFManager;
32 import com.fasterxml.jackson.databind.JsonNode;
33
34 /**
35  * Provide base ModelElement functionality.
36  */
37 public abstract class ModelElement {
38     protected static final EELFLogger       logger      = EELFManager.getInstance().getLogger(ModelElement.class);
39     protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();
40
41     public static final String      TYPE_POLICY = "policy";
42     public static final String      TYPE_TCA    = "tca";
43
44     private final String            type;
45     private final ModelBpmn         modelBpmn;
46     private final String            id;
47     protected String                topicPublishes;
48     protected final JsonNode        meNode;
49     private boolean                 isFound;
50
51     private final ModelProperties   modelProp;
52
53     /**
54      * Perform base parsing of properties for a ModelElement (such as,
55      * Collector, StringMatch, Policy and Tca)
56      *
57      * @param type
58      * @param modelProp
59      * @param modelBpmn
60      * @param modelJson
61      */
62     protected ModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
63         this.type = type;
64         this.modelProp = modelProp;
65         this.modelBpmn = modelBpmn;
66         this.id = modelBpmn.getId(type);
67         this.meNode = modelJson.get(id);
68         this.isFound = modelBpmn.getModelElementFound(type);
69     }
70
71     /**
72      * topicSubscribes is the topicPublishes of the from Model Element
73      *
74      * @return the topicSubscribes
75      */
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         ModelElement me = modelProp.getModelElementByType(fromType);
83         // get the topic publishes for the model element
84         return me.topicPublishes;
85     }
86
87     /**
88      * @return the topicPublishes
89      */
90     public String getTopicPublishes() {
91         return topicPublishes;
92     }
93
94     /**
95      * Return the value field of the json node element that has a name field
96      * equals to the given name.
97      *
98      * @param nodeIn
99      * @param name
100      * @return
101      */
102     public static String getValueByName(JsonNode nodeIn, String name) {
103         String value = null;
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();
111                     } else {
112                         // otherwise, just return text
113                         value = vnode.asText();
114                     }
115                 }
116             }
117         }
118         if (value == null || value.length() == 0) {
119             logger.warn(name + "=" + value);
120         } else {
121             logger.debug(name + "=" + value);
122         }
123         return value;
124     }
125
126     /**
127      * Return the int value field of the json node element that has a name field
128      * equals to the given name.
129      *
130      * @param nodeIn
131      * @param name
132      * @return
133      */
134     public static Integer getIntValueByName(JsonNode nodeIn, String name) {
135         String value = getValueByName(nodeIn, name);
136         return Integer.valueOf(value);
137     }
138
139     /**
140      * Return an array of values for the field of the json node element that has
141      * a name field equals to the given name.
142      *
143      * @param nodeIn
144      * @param name
145      * @return
146      */
147     public static List<String> getValuesByName(JsonNode nodeIn, String name) {
148         List<String> values = null;
149         if (nodeIn != null) {
150             Iterator<JsonNode> i = nodeIn.iterator();
151             while (i.hasNext()) {
152                 JsonNode node = i.next();
153                 if (node.path("name").asText().equals(name)) {
154                     values = getValuesList(node);
155                 }
156             }
157         }
158         if (values == null || values.size() == 0) {
159             logger.warn(name + "=" + values);
160         } else {
161             logger.debug(name + "=" + values);
162         }
163         return values;
164     }
165
166     /**
167      * Return an array of String values.
168      *
169      * @param nodeIn
170      * @return
171      */
172     public static List<String> getValuesList(JsonNode nodeIn) {
173         ArrayList<String> al = new ArrayList<>();
174         if (nodeIn != null) {
175             Iterator<JsonNode> itr = nodeIn.path("value").elements();
176             while (itr.hasNext()) {
177                 JsonNode node = itr.next();
178                 al.add(node.asText());
179             }
180         }
181         return al;
182     }
183
184     /**
185      * Return the value field of the json node element that has a name field
186      * equals to the given name.
187      *
188      * @param name
189      * @return
190      */
191     public String getValueByName(String name) {
192         return getValueByName(meNode, name);
193     }
194
195     /**
196      * Return the int value field of the json node element that has a name field
197      * equals to the given name.
198      *
199      * @param name
200      * @return
201      */
202     public Integer getIntValueByName(String name) {
203         return getIntValueByName(meNode, name);
204     }
205
206     /**
207      * Return an array of values for the field of the json node element that has
208      * a name field equals to the given name.
209      *
210      * @param name
211      * @return
212      */
213     public List<String> getValuesByName(String name) {
214         return getValuesByName(meNode, name);
215     }
216
217     /**
218      * @return the id
219      */
220     public String getId() {
221         return id;
222     }
223
224     /**
225      * @return the isFound
226      */
227     public boolean isFound() {
228         return isFound;
229     }
230 }