Initial commit for AAI-UI(sparky-backend)
[aai/sparky-be.git] / src / test / java / org / openecomp / 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.openecomp.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.openecomp.sparky.viewandinspect.config.VisualizationConfig;
38 import org.openecomp.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 =
91       "https://aai-ext1.test.att.com:9292/aai/v7/network/generic-vnfs/%s/%s";
92
93
94   /**
95    * Creates the child node.
96    *
97    * @param key the key
98    * @param parent the parent
99    * @param propertyNames the property names
100    * @return the active inventory node
101    */
102   private ActiveInventoryNode createChildNode(String key, ActiveInventoryNode parent,
103       String... propertyNames) {
104     // ActiveInventoryNode ain = parent.addNode(new ActiveInventoryNode(key));
105     // ain.setSelfLink(String.format(SELF_LINK_FORMAT, key, key));
106     /*
107      * if (propertyNames != null) { for (String p : propertyNames) { ain.addProperty(p, p); } }
108      */
109
110     ActiveInventoryNode ain = new ActiveInventoryNode();
111
112     return ain;
113
114   }
115
116   /**
117    * Builds the tree 3.
118    *
119    * @return the active inventory node
120    */
121   public ActiveInventoryNode buildTree3() {
122
123     ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
124     nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
125     nodeA.addProperty("a1", "a1");
126
127     createChildNode("B", nodeA, "b1");
128     createChildNode("C", nodeA, "c1");
129     createChildNode("D", nodeA, "d1");
130     createChildNode("E", nodeA, "e1");
131     createChildNode("F", nodeA, "f1");
132     createChildNode("G", nodeA, "g1");
133
134     return nodeA;
135   }
136
137   /**
138    * Builds the tree 4.
139    *
140    * @return the active inventory node
141    */
142   public ActiveInventoryNode buildTree4() {
143
144     ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
145     nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
146     nodeA.addProperty("a2", "a2");
147
148     ActiveInventoryNode nodeB = createChildNode("B", nodeA, "b2");
149     ActiveInventoryNode nodeC = createChildNode("C", nodeB, "c2");
150     ActiveInventoryNode nodeD = createChildNode("D", nodeC, "d2");
151     ActiveInventoryNode nodeE = createChildNode("E", nodeD, "e2");
152     ActiveInventoryNode nodeF = createChildNode("F", nodeE, "f2");
153     ActiveInventoryNode nodeG = createChildNode("G", nodeF, "g2");
154
155     return nodeA;
156   }
157
158   /**
159    * Do test 1.
160    */
161   public void doTest1() {
162
163     ActiveInventoryNode one = buildTree1();
164     ActiveInventoryNode two = buildTree2();
165
166     one.dumpNodeTree(true);
167     System.out.println("---");
168     two.dumpNodeTree(true);
169
170     System.out.println("---");
171     // one.merge(two);
172     one.dumpNodeTree(true);
173
174   }
175
176   /**
177    * Do test 2.
178    *
179    * @param showProps the show props
180    */
181   public void doTest2(boolean showProps) {
182
183     VisualizationConfig.getConfig().setVisualizationDebugEnabled(false);
184
185     ActiveInventoryNode one = buildTree3();
186     ActiveInventoryNode two = buildTree4();
187
188     System.out.println(one.dumpNodeTree(showProps));
189     System.out.println("---");
190     System.out.println(two.dumpNodeTree(showProps));
191
192     System.out.println("---");
193     // MergeResult mr = one.merge(two);
194     // System.out.println("merge result = " + mr.name());
195     System.out.println(one.dumpNodeTree(showProps));
196
197   }
198
199   public static String DIRECT_COMPLEX_SELF_LINK_JSON_RESPONSE =
200       "{\"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-int1.test.att.com: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\"}]}]}}}";
201   public static String DIRECT_PSERVER_SELF_LINK_JSON_RESPONSE =
202       "{\"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-int1.test.att.com: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-int1.test.att.com: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-int1.test.att.com:8443/aai/v8/network/physical-links/physical-link/BBEC.112431..ATI/\",\"relationship-data\":[{\"relationship-key\":\"physical-link.link-name\",\"relationship-value\":\"BBEC.112431..ATI\"}]}]}}]}}}";
203
204   /**
205    * Parses the direct self link json response.
206    *
207    * @param selfLinkJsonResponse the self link json response
208    * @throws JsonProcessingException the json processing exception
209    * @throws IOException Signals that an I/O exception has occurred.
210    */
211   public void parseDirectSelfLinkJsonResponse(String selfLinkJsonResponse)
212       throws JsonProcessingException, IOException {
213
214     ObjectMapper mapper = new ObjectMapper();
215     mapper.setSerializationInclusion(Include.NON_EMPTY);
216     mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.KebabCaseStrategy());
217
218
219     // try {
220     JsonNode jsonNodeArray = mapper.readTree(selfLinkJsonResponse);
221
222     Iterator<String> iterator = jsonNodeArray.fieldNames();
223     JsonNode entityNode = null;
224     String entityTypeStr = null;
225     String entityNodeFieldName = null;
226
227     while (iterator.hasNext()) {
228       entityTypeStr = iterator.next();
229       entityNode = jsonNodeArray.get(entityTypeStr);
230
231       Iterator<String> entityNodeFields = entityNode.fieldNames();
232
233       while (entityNodeFields.hasNext()) {
234         entityNodeFieldName = entityNodeFields.next();
235         System.out.println(String.format("%s.%s", entityTypeStr, entityNodeFieldName));
236       }
237     }
238
239     /*
240      * Iterator<Entry<String, JsonNode>> fieldNames = jsonNode.fields(); Entry<String,JsonNode>
241      * field = null; List<String> entitiesToFilter = null;
242      */
243
244     /*
245      * try { entitiesToFilter =
246      * ActiveInventoryConfig.getConfig().getAaiRestConfig().getFilteredEntities(); } catch (
247      * Exception e ) { LOG.error(
248      * "Caught an exception while retrieving filtered entities.  Error Cause = " +
249      * e.getLocalizedMessage());; return; }
250      */
251
252     /*
253      * JsonNode entityNode = jsonNode.
254      * 
255      * /*String entityType = entityNode.textValue(); fieldNames = entityNode.fields();
256      * 
257      * while ( fieldNames.hasNext() ) {
258      * 
259      * field = fieldNames.next();
260      * 
261      * /* Is there a way to tell if the field is an aggregate or an atomic value? This is where our
262      * flattening code needs to live
263      */
264
265     /*
266      * String fieldName = field.getKey();
267      * 
268      * System.out.println(
269      * "processDirectSelfLinkResponse(), fieldName for current node with entityType = " + entityType
270      * + " and field name " + fieldName);
271      * 
272      * 
273      * /*if ( "relationship-list".equals( fieldName ) ) {
274      * 
275      * /* Parse the relationship list like we were doing before, or at least navigate it so we can
276      * extract the relationship data
277      */
278
279     /*
280      * cloud-region is the only exception to this rule where we don't want to collect the
281      * relationship data from the self-link (for now).
282      */
283
284     /*
285      * if ( !entitiesToFilter.contains(entityType) ) {
286      * 
287      * // if the current depth >= maxTraversal depth, stop analyzing relationships RelationshipList
288      * relationships = null;
289      * 
290      * /* At each level we traverse, we want the properties + relationship-list, until we reach the
291      * max traversal depth, then we only the properties, and we want to ignore the relationship-list
292      * to avoid excessive traversal.
293      */
294
295     /*
296      * if ( linkDepth < VisualizationConfig.getConfig().getMaxSelfLinkTraversalDepth()) {
297      * relationships = analyzeSelfLinkRelationshipList(field.getValue().toString());
298      * addSelfLinkRelationshipChildren( relationships, linkDepth ); } else { LOG.warn(
299      * "Ignoring relationship-list for entity = " + entityType + " at traversal depth = " +
300      * linkDepth); }
301      * 
302      * } else { LOG.warn(String.format(
303      * "Ignoring relationship-list attribute for '%s' based on configuration", entityType)); }
304      * 
305      * } else {
306      * 
307      * JsonNode nodeValue = field.getValue();
308      * 
309      * if ( nodeValue.isValueNode() ) {
310      * 
311      * // current behavior, but we need to discover how to translate groups into flattened text by
312      * using the Jackson JsonNode API addProperty(fieldName, nodeValue.asText()); } else { // need
313      * special handling for collections
314      * 
315      * if ( LOG.isDebugEnabled()) { LOG.debug("Complex field discovered = " + fieldName); }
316      * 
317      * Iterator<String> childFields = nodeValue.fieldNames(); StringBuilder sb = new
318      * StringBuilder(128);
319      * 
320      * while ( childFields.hasNext() ) { String f= childFields.next();
321      * 
322      * if ( LOG.isDebugEnabled()) { LOG.debug("found field = " + f + " for parent field = " +
323      * fieldName); } sb.append(fieldName + "=" + nodeValue.get(f).asText()); }
324      * 
325      * addProperty(fieldName, sb.toString());
326      * 
327      * }
328      * 
329      * }
330      */
331
332     /*
333      * Conscious choice to not log the filtered out resources because it would dump on every node.
334      * We can always re-visit that choice and put a debug log here if need to / want to.
335      */
336
337     /*
338      * }
339      * 
340      * 
341      * } catch (IOException exc) {
342      * 
343      * System.out.println("Argh an io exception occurred with message = " +
344      * e.getLocalizedMessage());
345      * 
346      * /*LOG.error("An error occurred while converting JSON into POJO = " +
347      * e.getLocalizedMessage());
348      * 
349      * this.setProcessingErrorOccurred(true); this.addErrorCause(
350      * "An error occurred while converting JSON into POJO = " + e.getLocalizedMessage());
351      */
352     // }
353
354   }
355
356
357
358   /**
359    * The main method.
360    *
361    * @param args the arguments
362    * @throws JsonProcessingException the json processing exception
363    * @throws IOException Signals that an I/O exception has occurred.
364    */
365   public static void main(String[] args) throws JsonProcessingException, IOException {
366
367     System.getProperties().setProperty("AJSC_HOME", "d:\\3\\");
368     ActiveInventoryNodeTester tester = new ActiveInventoryNodeTester();
369     // tester.doTest2(true);
370
371     tester.parseDirectSelfLinkJsonResponse(DIRECT_COMPLEX_SELF_LINK_JSON_RESPONSE);
372     System.out.println("---");
373     tester.parseDirectSelfLinkJsonResponse(DIRECT_PSERVER_SELF_LINK_JSON_RESPONSE);
374
375
376
377   }
378
379 }