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.onap.aai.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.onap.aai.sparky.viewandinspect.config.VisualizationConfig;
38 import org.onap.aai.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 = "https://aai-hostname:9292/aai/v7/network/generic-vnfs/%s/%s";
94 * Creates the child node.
97 * @param parent the parent
98 * @param propertyNames the property names
99 * @return the active inventory node
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));
106 * if (propertyNames != null) { for (String p : propertyNames) { ain.addProperty(p, p); } }
109 ActiveInventoryNode ain = new ActiveInventoryNode();
118 * @return the active inventory node
120 public ActiveInventoryNode buildTree3() {
122 ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
123 nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
124 nodeA.addProperty("a1", "a1");
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");
139 * @return the active inventory node
141 public ActiveInventoryNode buildTree4() {
143 ActiveInventoryNode nodeA = new ActiveInventoryNode("A");
144 nodeA.setSelfLink(String.format(selfLinkFormat, "A", "A"));
145 nodeA.addProperty("a2", "a2");
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");
160 public void doTest1() {
162 ActiveInventoryNode one = buildTree1();
163 ActiveInventoryNode two = buildTree2();
165 one.dumpNodeTree(true);
166 System.out.println("---");
167 two.dumpNodeTree(true);
169 System.out.println("---");
171 one.dumpNodeTree(true);
178 * @param showProps the show props
180 public void doTest2(boolean showProps) {
182 VisualizationConfig.getConfig().setVisualizationDebugEnabled(false);
184 ActiveInventoryNode one = buildTree3();
185 ActiveInventoryNode two = buildTree4();
187 System.out.println(one.dumpNodeTree(showProps));
188 System.out.println("---");
189 System.out.println(two.dumpNodeTree(showProps));
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));
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\"}]}]}}]}}}";
204 * Parses the direct self link json response.
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.
210 public void parseDirectSelfLinkJsonResponse(String selfLinkJsonResponse)
211 throws JsonProcessingException, IOException {
213 ObjectMapper mapper = new ObjectMapper();
214 mapper.setSerializationInclusion(Include.NON_EMPTY);
215 mapper.setPropertyNamingStrategy(new PropertyNamingStrategy.KebabCaseStrategy());
219 JsonNode jsonNodeArray = mapper.readTree(selfLinkJsonResponse);
221 Iterator<String> iterator = jsonNodeArray.fieldNames();
222 JsonNode entityNode = null;
223 String entityTypeStr = null;
224 String entityNodeFieldName = null;
226 while (iterator.hasNext()) {
227 entityTypeStr = iterator.next();
228 entityNode = jsonNodeArray.get(entityTypeStr);
230 Iterator<String> entityNodeFields = entityNode.fieldNames();
232 while (entityNodeFields.hasNext()) {
233 entityNodeFieldName = entityNodeFields.next();
234 System.out.println(String.format("%s.%s", entityTypeStr, entityNodeFieldName));
239 * Iterator<Entry<String, JsonNode>> fieldNames = jsonNode.fields(); Entry<String,JsonNode>
240 * field = null; List<String> entitiesToFilter = null;
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; }
252 * JsonNode entityNode = jsonNode.
254 * /*String entityType = entityNode.textValue(); fieldNames = entityNode.fields();
256 * while ( fieldNames.hasNext() ) {
258 * field = fieldNames.next();
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
265 * String fieldName = field.getKey();
267 * System.out.println(
268 * "processDirectSelfLinkResponse(), fieldName for current node with entityType = " + entityType
269 * + " and field name " + fieldName);
272 * /*if ( "relationship-list".equals( fieldName ) ) {
274 * /* Parse the relationship list like we were doing before, or at least navigate it so we can
275 * extract the relationship data
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).
284 * if ( !entitiesToFilter.contains(entityType) ) {
286 * // if the current depth >= maxTraversal depth, stop analyzing relationships RelationshipList
287 * relationships = null;
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.
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 = " +
301 * } else { LOG.warn(String.format(
302 * "Ignoring relationship-list attribute for '%s' based on configuration", entityType)); }
306 * JsonNode nodeValue = field.getValue();
308 * if ( nodeValue.isValueNode() ) {
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
314 * if ( LOG.isDebugEnabled()) { LOG.debug("Complex field discovered = " + fieldName); }
316 * Iterator<String> childFields = nodeValue.fieldNames(); StringBuilder sb = new
317 * StringBuilder(128);
319 * while ( childFields.hasNext() ) { String f= childFields.next();
321 * if ( LOG.isDebugEnabled()) { LOG.debug("found field = " + f + " for parent field = " +
322 * fieldName); } sb.append(fieldName + "=" + nodeValue.get(f).asText()); }
324 * addProperty(fieldName, sb.toString());
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.
340 * } catch (IOException exc) {
342 * System.out.println("Argh an io exception occurred with message = " +
343 * e.getLocalizedMessage());
345 * /*LOG.error("An error occurred while converting JSON into POJO = " +
346 * e.getLocalizedMessage());
348 * this.setProcessingErrorOccurred(true); this.addErrorCause(
349 * "An error occurred while converting JSON into POJO = " + e.getLocalizedMessage());