2 * ============LICENSE_START=======================================================
3 * SPARKY (AAI UI service)
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 * ECOMP and OpenECOMP are trademarks
23 * and service marks of AT&T Intellectual Property.
26 package org.openecomp.sparky.viewandinspect;
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;
34 import java.io.IOException;
35 import java.util.Iterator;
37 import org.openecomp.sparky.viewandinspect.config.VisualizationConfig;
38 import org.openecomp.sparky.viewandinspect.entity.ActiveInventoryNode;
41 * The Class ActiveInventoryNodeTester.
43 public class ActiveInventoryNodeTester {
48 * @return the active inventory node
50 public ActiveInventoryNode buildTree1() {
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");
58 createChildNode("C", nodeA, "c1", "c2", "c3");
59 createChildNode("D", nodeA, "d1", "d2", "d3");
60 createChildNode("E", nodeA, "e1", "e2", "e3");
63 * Assume key uniqueness within a single tree. Safe?? Can we say that every nodeId is unique?
74 * @return the active inventory node
76 public ActiveInventoryNode buildTree2() {
78 ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
79 nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
80 nodeA.addProperty("a4", "a4");
82 ActiveInventoryNode nodeD = createChildNode("D", nodeA, "d7", "d8");
83 ActiveInventoryNode nodeW = createChildNode("W", nodeD, "w1", "w2", "w3");
85 createChildNode("H", nodeA, "h2", "h4", "h6");
90 private String selfLinkFormat =
91 "https://aai-hostname:9292/aai/v7/network/generic-vnfs/%s/%s";
95 * Creates the child node.
98 * @param parent the parent
99 * @param propertyNames the property names
100 * @return the active inventory node
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));
107 * if (propertyNames != null) { for (String p : propertyNames) { ain.addProperty(p, p); } }
110 ActiveInventoryNode ain = new ActiveInventoryNode();
119 * @return the active inventory node
121 public ActiveInventoryNode buildTree3() {
123 ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
124 nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
125 nodeA.addProperty("a1", "a1");
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");
140 * @return the active inventory node
142 public ActiveInventoryNode buildTree4() {
144 ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
145 nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
146 nodeA.addProperty("a2", "a2");
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");
161 public void doTest1() {
163 ActiveInventoryNode one = buildTree1();
164 ActiveInventoryNode two = buildTree2();
166 one.dumpNodeTree(true);
167 System.out.println("---");
168 two.dumpNodeTree(true);
170 System.out.println("---");
172 one.dumpNodeTree(true);
179 * @param showProps the show props
181 public void doTest2(boolean showProps) {
183 VisualizationConfig.getConfig().setVisualizationDebugEnabled(false);
185 ActiveInventoryNode one = buildTree3();
186 ActiveInventoryNode two = buildTree4();
188 System.out.println(one.dumpNodeTree(showProps));
189 System.out.println("---");
190 System.out.println(two.dumpNodeTree(showProps));
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));
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-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\"}]}]}}}";
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-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\"}]}]}}]}}}";
205 * Parses the direct self link json response.
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.
211 public void parseDirectSelfLinkJsonResponse(String selfLinkJsonResponse)
212 throws JsonProcessingException, IOException {
214 ObjectMapper mapper = new ObjectMapper();
215 mapper.setSerializationInclusion(Include.NON_EMPTY);
216 mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.KebabCaseStrategy());
220 JsonNode jsonNodeArray = mapper.readTree(selfLinkJsonResponse);
222 Iterator<String> iterator = jsonNodeArray.fieldNames();
223 JsonNode entityNode = null;
224 String entityTypeStr = null;
225 String entityNodeFieldName = null;
227 while (iterator.hasNext()) {
228 entityTypeStr = iterator.next();
229 entityNode = jsonNodeArray.get(entityTypeStr);
231 Iterator<String> entityNodeFields = entityNode.fieldNames();
233 while (entityNodeFields.hasNext()) {
234 entityNodeFieldName = entityNodeFields.next();
235 System.out.println(String.format("%s.%s", entityTypeStr, entityNodeFieldName));
240 * Iterator<Entry<String, JsonNode>> fieldNames = jsonNode.fields(); Entry<String,JsonNode>
241 * field = null; List<String> entitiesToFilter = null;
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; }
253 * JsonNode entityNode = jsonNode.
255 * /*String entityType = entityNode.textValue(); fieldNames = entityNode.fields();
257 * while ( fieldNames.hasNext() ) {
259 * field = fieldNames.next();
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
266 * String fieldName = field.getKey();
268 * System.out.println(
269 * "processDirectSelfLinkResponse(), fieldName for current node with entityType = " + entityType
270 * + " and field name " + fieldName);
273 * /*if ( "relationship-list".equals( fieldName ) ) {
275 * /* Parse the relationship list like we were doing before, or at least navigate it so we can
276 * extract the relationship data
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).
285 * if ( !entitiesToFilter.contains(entityType) ) {
287 * // if the current depth >= maxTraversal depth, stop analyzing relationships RelationshipList
288 * relationships = null;
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.
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 = " +
302 * } else { LOG.warn(String.format(
303 * "Ignoring relationship-list attribute for '%s' based on configuration", entityType)); }
307 * JsonNode nodeValue = field.getValue();
309 * if ( nodeValue.isValueNode() ) {
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
315 * if ( LOG.isDebugEnabled()) { LOG.debug("Complex field discovered = " + fieldName); }
317 * Iterator<String> childFields = nodeValue.fieldNames(); StringBuilder sb = new
318 * StringBuilder(128);
320 * while ( childFields.hasNext() ) { String f= childFields.next();
322 * if ( LOG.isDebugEnabled()) { LOG.debug("found field = " + f + " for parent field = " +
323 * fieldName); } sb.append(fieldName + "=" + nodeValue.get(f).asText()); }
325 * addProperty(fieldName, sb.toString());
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.
341 * } catch (IOException exc) {
343 * System.out.println("Argh an io exception occurred with message = " +
344 * e.getLocalizedMessage());
346 * /*LOG.error("An error occurred while converting JSON into POJO = " +
347 * e.getLocalizedMessage());
349 * this.setProcessingErrorOccurred(true); this.addErrorCause(
350 * "An error occurred while converting JSON into POJO = " + e.getLocalizedMessage());
361 * @param args the arguments
362 * @throws JsonProcessingException the json processing exception
363 * @throws IOException Signals that an I/O exception has occurred.
365 public static void main(String[] args) throws JsonProcessingException, IOException {
367 System.getProperties().setProperty("AJSC_HOME", "d:\\3\\");
368 ActiveInventoryNodeTester tester = new ActiveInventoryNodeTester();
369 // tester.doTest2(true);
371 tester.parseDirectSelfLinkJsonResponse(DIRECT_COMPLEX_SELF_LINK_JSON_RESPONSE);
372 System.out.println("---");
373 tester.parseDirectSelfLinkJsonResponse(DIRECT_PSERVER_SELF_LINK_JSON_RESPONSE);