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