Update the dependencies to use project version
[aai/sparky-be.git] / src / test / java / org / onap / aai / sparky / viewandinspect / ActiveInventoryNodeTester.java
1 /* 
2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
7 * All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12
13 *      http://www.apache.org/licenses/LICENSE-2.0
14
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
21
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
24 */
25
26 package org.onap.aai.sparky.viewandinspect;
27
28 import com.fasterxml.jackson.annotation.JsonInclude.Include;
29 import com.fasterxml.jackson.core.JsonProcessingException;
30 import com.fasterxml.jackson.databind.JsonNode;
31 import com.fasterxml.jackson.databind.ObjectMapper;
32 import com.fasterxml.jackson.databind.PropertyNamingStrategy;
33
34 import java.io.IOException;
35 import java.util.Iterator;
36
37 import org.onap.aai.sparky.viewandinspect.config.VisualizationConfig;
38 import org.onap.aai.sparky.viewandinspect.entity.ActiveInventoryNode;
39
40 /**
41  * The Class ActiveInventoryNodeTester.
42  */
43 public class ActiveInventoryNodeTester {
44
45   /**
46    * Builds the tree 1.
47    *
48    * @return the active inventory node
49    */
50   public ActiveInventoryNode buildTree1() {
51
52     ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
53     nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
54     nodeA.addProperty("a1", "a1");
55     nodeA.addProperty("a2", "a2");
56     nodeA.addProperty("a3", "a3");
57
58     createChildNode("C", nodeA, "c1", "c2", "c3");
59     createChildNode("D", nodeA, "d1", "d2", "d3");
60     createChildNode("E", nodeA, "e1", "e2", "e3");
61
62     /*
63      * Assume key uniqueness within a single tree. Safe?? Can we say that every nodeId is unique?
64      */
65
66
67     return nodeA;
68
69   }
70
71   /**
72    * Builds the tree 2.
73    *
74    * @return the active inventory node
75    */
76   public ActiveInventoryNode buildTree2() {
77
78     ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
79     nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
80     nodeA.addProperty("a4", "a4");
81
82     ActiveInventoryNode nodeD = createChildNode("D", nodeA, "d7", "d8");
83     ActiveInventoryNode nodeW = createChildNode("W", nodeD, "w1", "w2", "w3");
84
85     createChildNode("H", nodeA, "h2", "h4", "h6");
86
87     return nodeA;
88   }
89
90   private String selfLinkFormat = "https://aai-hostname:9292/aai/v7/network/generic-vnfs/%s/%s";
91
92
93   /**
94    * Creates the child node.
95    *
96    * @param key the key
97    * @param parent the parent
98    * @param propertyNames the property names
99    * @return the active inventory node
100    */
101   private ActiveInventoryNode createChildNode(String key, ActiveInventoryNode parent,
102       String... propertyNames) {
103     // ActiveInventoryNode ain = parent.addNode(new ActiveInventoryNode(key));
104     // ain.setSelfLink(String.format(SELF_LINK_FORMAT, key, key));
105     /*
106      * if (propertyNames != null) { for (String p : propertyNames) { ain.addProperty(p, p); } }
107      */
108
109     ActiveInventoryNode ain = new ActiveInventoryNode();
110
111     return ain;
112
113   }
114
115   /**
116    * Builds the tree 3.
117    *
118    * @return the active inventory node
119    */
120   public ActiveInventoryNode buildTree3() {
121
122     ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
123     nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
124     nodeA.addProperty("a1", "a1");
125
126     createChildNode("B", nodeA, "b1");
127     createChildNode("C", nodeA, "c1");
128     createChildNode("D", nodeA, "d1");
129     createChildNode("E", nodeA, "e1");
130     createChildNode("F", nodeA, "f1");
131     createChildNode("G", nodeA, "g1");
132
133     return nodeA;
134   }
135
136   /**
137    * Builds the tree 4.
138    *
139    * @return the active inventory node
140    */
141   public ActiveInventoryNode buildTree4() {
142
143     ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
144     nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
145     nodeA.addProperty("a2", "a2");
146
147     ActiveInventoryNode nodeB = createChildNode("B", nodeA, "b2");
148     ActiveInventoryNode nodeC = createChildNode("C", nodeB, "c2");
149     ActiveInventoryNode nodeD = createChildNode("D", nodeC, "d2");
150     ActiveInventoryNode nodeE = createChildNode("E", nodeD, "e2");
151     ActiveInventoryNode nodeF = createChildNode("F", nodeE, "f2");
152     ActiveInventoryNode nodeG = createChildNode("G", nodeF, "g2");
153
154     return nodeA;
155   }
156
157   /**
158    * Do test 1.
159    */
160   public void doTest1() {
161
162     ActiveInventoryNode one = buildTree1();
163     ActiveInventoryNode two = buildTree2();
164
165     one.dumpNodeTree(true);
166     System.out.println("---");
167     two.dumpNodeTree(true);
168
169     System.out.println("---");
170     // one.merge(two);
171     one.dumpNodeTree(true);
172
173   }
174
175   /**
176    * Do test 2.
177    *
178    * @param showProps the show props
179    */
180   public void doTest2(boolean showProps) {
181
182     VisualizationConfig.getConfig().setVisualizationDebugEnabled(false);
183
184     ActiveInventoryNode one = buildTree3();
185     ActiveInventoryNode two = buildTree4();
186
187     System.out.println(one.dumpNodeTree(showProps));
188     System.out.println("---");
189     System.out.println(two.dumpNodeTree(showProps));
190
191     System.out.println("---");
192     // MergeResult mr = one.merge(two);
193     // System.out.println("merge result = " + mr.name());
194     System.out.println(one.dumpNodeTree(showProps));
195
196   }
197
198   public static String DIRECT_COMPLEX_SELF_LINK_JSON_RESPONSE =
199       "{\"complex\":{\"physical-location-id\":\"MJ-1604-COMPLEX\",\"data-center-code\":\"DAYTONNJ\",\"complex-name\":\"complex-name-MDTWNJ23A4\",\"resource-version\":\"1470195143\",\"physical-location-type\":\"SBC/VHO and Mega Pop\",\"street1\":\"451 Western Ave\",\"street2\":\"CU-212\",\"city\":\"dayton\",\"state\":\"NJ\",\"postal-code\":\"08852\",\"country\":\"USA\",\"region\":\"Northeast\",\"latitude\":\"40.3896\",\"longitude\":\"-74.5463\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"pserver\",\"related-link\":\"https://aai-hostname:8443/aai/v8/cloud-infrastructure/pservers/pserver/MJ-1604-PSERVER/\",\"relationship-data\":[{\"relationship-key\":\"pserver.hostname\",\"relationship-value\":\"MJ-1604-PSERVER\"}],\"related-to-property\":[{\"property-key\":\"pserver.pserver-name2\",\"property-value\":\"MJ-1604-PSERVER\"}]}]}}}";
200   public static String DIRECT_PSERVER_SELF_LINK_JSON_RESPONSE =
201       "{\"pserver\":{\"hostname\":\"MJ-1604-PSERVER\",\"equip-type\":\"JUNIPER UCPE\",\"equip-vendor\":\"JUNIPER\",\"equip-model\":\"QFX5100-24P-AA\",\"ipv4-oam-address\":\"10.402.143.1\",\"serial-number\":\"VX371521MAHI\",\"pserver-id\":\"1C2B8D47-AVAE-4721-0110-E2C41A07MAHI\",\"in-maint\":false,\"resource-version\":\"1456765026\",\"pserver-name2\":\"MJ-1604-PSERVER\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"complex\",\"related-link\":\"https://aai-hostname:8443/aai/v8/cloud-infrastructure/complexes/complex/MJ-1604-COMPLEX/\",\"relationship-data\":[{\"relationship-key\":\"complex.physical-location-id\",\"relationship-value\":\"MJ-1604-COMPLEX\"}]}]},\"p-interfaces\":{\"p-interface\":[{\"interface-name\":\"ge-0/2/0\",\"speed-value\":\"1\",\"speed-units\":\"GBPS\",\"resource-version\":\"1456723241\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"physical-link\",\"related-link\":\"https://aai-hostname:8443/aai/v8/network/physical-links/physical-link/BBEC.112430..ATI/\",\"relationship-data\":[{\"relationship-key\":\"physical-link.link-name\",\"relationship-value\":\"BBEC.112430..ATI\"}]}]}},{\"interface-name\":\"ge-0/2/1\",\"speed-value\":\"1\",\"speed-units\":\"GBPS\",\"resource-version\":\"1456723241\",\"relationship-list\":{\"relationship\":[{\"related-to\":\"physical-link\",\"related-link\":\"https://aai-hostname:8443/aai/v8/network/physical-links/physical-link/BBEC.112431..ATI/\",\"relationship-data\":[{\"relationship-key\":\"physical-link.link-name\",\"relationship-value\":\"BBEC.112431..ATI\"}]}]}}]}}}";
202
203   /**
204    * Parses the direct self link json response.
205    *
206    * @param selfLinkJsonResponse the self link json response
207    * @throws JsonProcessingException the json processing exception
208    * @throws IOException Signals that an I/O exception has occurred.
209    */
210   public void parseDirectSelfLinkJsonResponse(String selfLinkJsonResponse)
211       throws JsonProcessingException, IOException {
212
213     ObjectMapper mapper = new ObjectMapper();
214     mapper.setSerializationInclusion(Include.NON_EMPTY);
215     mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.KebabCaseStrategy());
216
217
218     // try {
219     JsonNode jsonNodeArray = mapper.readTree(selfLinkJsonResponse);
220
221     Iterator<String> iterator = jsonNodeArray.fieldNames();
222     JsonNode entityNode = null;
223     String entityTypeStr = null;
224     String entityNodeFieldName = null;
225
226     while (iterator.hasNext()) {
227       entityTypeStr = iterator.next();
228       entityNode = jsonNodeArray.get(entityTypeStr);
229
230       Iterator<String> entityNodeFields = entityNode.fieldNames();
231
232       while (entityNodeFields.hasNext()) {
233         entityNodeFieldName = entityNodeFields.next();
234         System.out.println(String.format("%s.%s", entityTypeStr, entityNodeFieldName));
235       }
236     }
237
238     /*
239      * Iterator<Entry<String, JsonNode>> fieldNames = jsonNode.fields(); Entry<String,JsonNode>
240      * field = null; List<String> entitiesToFilter = null;
241      */
242
243     /*
244      * try { entitiesToFilter =
245      * ActiveInventoryConfig.getConfig().getAaiRestConfig().getFilteredEntities(); } catch (
246      * Exception e ) { LOG.error(
247      * "Caught an exception while retrieving filtered entities.  Error Cause = " +
248      * e.getLocalizedMessage());; return; }
249      */
250
251     /*
252      * JsonNode entityNode = jsonNode.
253      * 
254      * /*String entityType = entityNode.textValue(); fieldNames = entityNode.fields();
255      * 
256      * while ( fieldNames.hasNext() ) {
257      * 
258      * field = fieldNames.next();
259      * 
260      * /* Is there a way to tell if the field is an aggregate or an atomic value? This is where our
261      * flattening code needs to live
262      */
263
264     /*
265      * String fieldName = field.getKey();
266      * 
267      * System.out.println(
268      * "processDirectSelfLinkResponse(), fieldName for current node with entityType = " + entityType
269      * + " and field name " + fieldName);
270      * 
271      * 
272      * /*if ( "relationship-list".equals( fieldName ) ) {
273      * 
274      * /* Parse the relationship list like we were doing before, or at least navigate it so we can
275      * extract the relationship data
276      */
277
278     /*
279      * cloud-region is the only exception to this rule where we don't want to collect the
280      * relationship data from the self-link (for now).
281      */
282
283     /*
284      * if ( !entitiesToFilter.contains(entityType) ) {
285      * 
286      * // if the current depth >= maxTraversal depth, stop analyzing relationships RelationshipList
287      * relationships = null;
288      * 
289      * /* At each level we traverse, we want the properties + relationship-list, until we reach the
290      * max traversal depth, then we only the properties, and we want to ignore the relationship-list
291      * to avoid excessive traversal.
292      */
293
294     /*
295      * if ( linkDepth < VisualizationConfig.getConfig().getMaxSelfLinkTraversalDepth()) {
296      * relationships = analyzeSelfLinkRelationshipList(field.getValue().toString());
297      * addSelfLinkRelationshipChildren( relationships, linkDepth ); } else { LOG.warn(
298      * "Ignoring relationship-list for entity = " + entityType + " at traversal depth = " +
299      * linkDepth); }
300      * 
301      * } else { LOG.warn(String.format(
302      * "Ignoring relationship-list attribute for '%s' based on configuration", entityType)); }
303      * 
304      * } else {
305      * 
306      * JsonNode nodeValue = field.getValue();
307      * 
308      * if ( nodeValue.isValueNode() ) {
309      * 
310      * // current behavior, but we need to discover how to translate groups into flattened text by
311      * using the Jackson JsonNode API addProperty(fieldName, nodeValue.asText()); } else { // need
312      * special handling for collections
313      * 
314      * if ( LOG.isDebugEnabled()) { LOG.debug("Complex field discovered = " + fieldName); }
315      * 
316      * Iterator<String> childFields = nodeValue.fieldNames(); StringBuilder sb = new
317      * StringBuilder(128);
318      * 
319      * while ( childFields.hasNext() ) { String f= childFields.next();
320      * 
321      * if ( LOG.isDebugEnabled()) { LOG.debug("found field = " + f + " for parent field = " +
322      * fieldName); } sb.append(fieldName + "=" + nodeValue.get(f).asText()); }
323      * 
324      * addProperty(fieldName, sb.toString());
325      * 
326      * }
327      * 
328      * }
329      */
330
331     /*
332      * Conscious choice to not log the filtered out resources because it would dump on every node.
333      * We can always re-visit that choice and put a debug log here if need to / want to.
334      */
335
336     /*
337      * }
338      * 
339      * 
340      * } catch (IOException exc) {
341      * 
342      * System.out.println("Argh an io exception occurred with message = " +
343      * e.getLocalizedMessage());
344      * 
345      * /*LOG.error("An error occurred while converting JSON into POJO = " +
346      * e.getLocalizedMessage());
347      * 
348      * this.setProcessingErrorOccurred(true); this.addErrorCause(
349      * "An error occurred while converting JSON into POJO = " + e.getLocalizedMessage());
350      */
351     // }
352
353   }
354 }