aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/clamp/clds/model/prop/ModelElement.java
blob: 42333559eec29bf863e25ed8ba1184dd10bee9d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*-
 * ============LICENSE_START=======================================================
 * ONAP CLAMP
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights
 *                             reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END============================================
 * ===================================================================
 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 */

package org.onap.clamp.clds.model.prop;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;

/**
 * Provide base ModelElement functionality.
 */
public abstract class ModelElement {
    protected static final EELFLogger       logger      = EELFManager.getInstance().getLogger(ModelElement.class);
    protected static final EELFLogger auditLogger = EELFManager.getInstance().getAuditLogger();

    public static final String      TYPE_POLICY = "policy";
    public static final String      TYPE_TCA    = "tca";

    private final String            type;
    private final ModelBpmn         modelBpmn;
    private final String            id;
    protected String                topicPublishes;
    protected final JsonNode        meNode;
    private boolean                 isFound;

    private final ModelProperties   modelProp;

    /**
     * Perform base parsing of properties for a ModelElement (such as,
     * Collector, StringMatch, Policy and Tca)
     *
     * @param type
     * @param modelProp
     * @param modelBpmn
     * @param modelJson
     */
    protected ModelElement(String type, ModelProperties modelProp, ModelBpmn modelBpmn, JsonNode modelJson) {
        this.type = type;
        this.modelProp = modelProp;
        this.modelBpmn = modelBpmn;
        this.id = modelBpmn.getId(type);
        this.meNode = modelJson.get(id);
        this.isFound = modelBpmn.getModelElementFound(type);
    }

    /**
     * topicSubscribes is the topicPublishes of the from Model Element
     *
     * @return the topicSubscribes
     */
    public String getTopicSubscribes() {
        // get fromId for this type
        String fromId = modelBpmn.getFromId(type);
        // find the type of the from model element
        String fromType = modelBpmn.getType(fromId);
        // get the model element for the type
        ModelElement me = modelProp.getModelElementByType(fromType);
        // get the topic publishes for the model element
        return me.topicPublishes;
    }

    /**
     * @return the topicPublishes
     */
    public String getTopicPublishes() {
        return topicPublishes;
    }

    /**
     * Return the value field of the json node element that has a name field
     * equals to the given name.
     *
     * @param nodeIn
     * @param name
     * @return
     */
    public static String getValueByName(JsonNode nodeIn, String name) {
        String value = null;
        if (nodeIn != null) {
            for (JsonNode node : nodeIn) {
                if (node.path("name").asText().equals(name)) {
                    JsonNode vnode = node.path("value");
                    if (vnode.isArray()) {
                        // if array, assume value is in first element
                        value = vnode.path(0).asText();
                    } else {
                        // otherwise, just return text
                        value = vnode.asText();
                    }
                }
            }
        }
        if (value == null || value.length() == 0) {
            logger.warn(name + "=" + value);
        } else {
            logger.debug(name + "=" + value);
        }
        return value;
    }
	
	/**
	 * Return the value field of the json node element that has a name field that equals the given name.
	 * 
	 * @param nodeIn
	 * @param name
	 * @return
	 */
	public static String getNodeValueByName(JsonNode nodeIn, String name) {
		String value = null;
		if ( nodeIn != null ) {
			value = nodeIn.path(name).asText();
		}
		if ( value == null || value.length() == 0 ) {
			logger.warn(name + "=" + value);
		} else {
			logger.debug(name + "=" + value);
		}
		return value;
	}    
    
	
	/**
	 * Return the value field of the json node element that has a name field that equals the given name.
	 * 
	 * @param nodeIn
	 * @param name
	 * @return
	 */
	public static List<String> getNodeValuesByName(JsonNode nodeIn, String name) {
		List<String> values = new ArrayList<String>();
		if ( nodeIn != null ) {
			Iterator<JsonNode> i = nodeIn.iterator();
			while (i.hasNext()) {
				JsonNode node = i.next();
				if ( node.path("name").asText().equals(name) ) {
					String value = "";
					JsonNode vnode = node.path("value");
					if ( vnode.isArray() ) {
						// if array, assume value is in first element
						value = vnode.path(0).asText();
					} else {
						// otherwise, just return text
						value = vnode.asText();					
					}
					values.add(value);
				}
			}
		}
		return values;
	}    

    /**
     * Return the int value field of the json node element that has a name field
     * equals to the given name.
     *
     * @param nodeIn
     * @param name
     * @return
     */
    public static Integer getIntValueByName(JsonNode nodeIn, String name) {
        String value = getValueByName(nodeIn, name);
        return Integer.valueOf(value);
    }

    /**
     * Return an array of values for the field of the json node element that has
     * a name field equals to the given name.
     *
     * @param nodeIn
     * @param name
     * @return
     */
    public static List<String> getValuesByName(JsonNode nodeIn, String name) {
        List<String> values = null;
        if (nodeIn != null) {
            Iterator<JsonNode> i = nodeIn.iterator();
            while (i.hasNext()) {
                JsonNode node = i.next();
                if (node.path("name").asText().equals(name)) {
                    values = getValuesList(node);
                }
            }
        }
        if (values == null || values.size() == 0) {
            logger.warn(name + "=" + values);
        } else {
            logger.debug(name + "=" + values);
        }
        return values;
    }

    /**
     * Return an array of String values.
     *
     * @param nodeIn
     * @return
     */
    public static List<String> getValuesList(JsonNode nodeIn) {
        ArrayList<String> al = new ArrayList<>();
        if (nodeIn != null) {
            Iterator<JsonNode> itr = nodeIn.path("value").elements();
            while (itr.hasNext()) {
                JsonNode node = itr.next();
                al.add(node.asText());
            }
        }
        return al;
    }

    /**
     * Return the value field of the json node element that has a name field
     * equals to the given name.
     *
     * @param name
     * @return
     */
    public String getValueByName(String name) {
        return getValueByName(meNode, name);
    }

    /**
     * Return the int value field of the json node element that has a name field
     * equals to the given name.
     *
     * @param name
     * @return
     */
    public Integer getIntValueByName(String name) {
        return getIntValueByName(meNode, name);
    }

    /**
     * Return an array of values for the field of the json node element that has
     * a name field equals to the given name.
     *
     * @param name
     * @return
     */
    public List<String> getValuesByName(String name) {
        return getValuesByName(meNode, name);
    }

    /**
     * @return the id
     */
    public String getId() {
        return id;
    }

    /**
     * @return the isFound
     */
    public boolean isFound() {
        return isFound;
    }
}