2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 package org.onap.aai.sparky.viewandinspect.services;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
30 import java.util.UUID;
32 import org.onap.aai.cl.api.Logger;
33 import org.onap.aai.cl.eelf.LoggerFactory;
34 import org.onap.aai.sparky.logging.AaiUiMsgs;
35 import org.onap.aai.sparky.subscription.config.SubscriptionConfig;
36 import org.onap.aai.sparky.util.ConfigHelper;
37 import org.onap.aai.sparky.viewandinspect.config.VisualizationConfigs;
38 import org.onap.aai.sparky.viewandinspect.entity.ActiveInventoryNode;
39 import org.onap.aai.sparky.viewandinspect.entity.D3VisualizationOutput;
40 import org.onap.aai.sparky.viewandinspect.entity.GraphMeta;
41 import org.onap.aai.sparky.viewandinspect.entity.SparkyGraphNode;
42 import org.onap.aai.sparky.viewandinspect.entity.SparkyGraphLink;
43 import org.onap.aai.sparky.viewandinspect.entity.NodeDebug;
45 import com.fasterxml.jackson.core.JsonProcessingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.ObjectWriter;
50 * The idea here is to receive a collection of graphs and then fold them together (or not) based on
51 * configuration. The first goal will be to fold all like-resources together, but the choice of
52 * folding could/should be configurable, and will simply change the degree of link based nodes when
53 * we generate the Node-Array and Link-Array output.
59 public class VisualizationTransformer {
61 private static final Logger LOG = LoggerFactory.getInstance().getLogger(
62 VisualizationTransformer.class);
64 List<SparkyGraphNode> flatNodeArray = new ArrayList<SparkyGraphNode>();
67 * Maybe this isn't a string but Json-Model objects that we will convert to final string
68 * representation when we dump the node-array and link-array collections the post-data blob in the
69 * HttpServletResponse.
72 List<SparkyGraphLink> linkArrayOutput = new ArrayList<SparkyGraphLink>();
74 private VisualizationConfigs visualizationConfigs;
75 private SubscriptionConfig subConfig;
78 * Instantiates a new visualization transformer.
80 * @throws Exception the exception
82 public VisualizationTransformer(VisualizationConfigs visualizationConfigs,
83 SubscriptionConfig subConfig) throws Exception {
84 this.visualizationConfigs = visualizationConfigs;
85 this.subConfig = subConfig;
92 * @param method the method
93 * @param startTimeInMs the start time in ms
95 private void logOptime(String method, long startTimeInMs) {
96 LOG.info(AaiUiMsgs.OPERATION_TIME, method,
97 String.valueOf((System.currentTimeMillis() - startTimeInMs)));
101 * Adds the search target attributes to root node.
103 public void addSearchTargetAttributesToRootNode() {
105 for (SparkyGraphNode n : flatNodeArray) {
106 if (n.isRootNode()) {
107 n.getNodeMeta().setSearchTarget(true);
108 n.getNodeMeta().setClassName(this.visualizationConfigs.getSelectedSearchedNodeClassName());
116 * Generate visualization output.
118 * @param preProcessingOpTimeInMs the pre processing op time in ms
119 * @param graphMeta the graph meta
120 * @return the d 3 visualization output
121 * @throws JsonProcessingException the json processing exception
122 * @throws IOException Signals that an I/O exception has occurred.
125 public D3VisualizationOutput generateVisualizationOutput(long preProcessingOpTimeInMs,
126 GraphMeta graphMeta) throws JsonProcessingException, IOException {
128 long opStartTimeInMs = System.currentTimeMillis();
131 * iterate over the flat collection, and only add the graph nodes to the graph node collection
134 D3VisualizationOutput output = new D3VisualizationOutput();
136 output.setGraphMeta(graphMeta);
138 for (SparkyGraphNode n : flatNodeArray) {
139 if ( n.getItemType()!= null) {
140 output.pegCounter(n.getItemType());
144 output.addNodes(flatNodeArray);
145 output.addLinks(linkArrayOutput);
147 int numNodes = flatNodeArray.size();
148 int numLinks = linkArrayOutput.size();
150 LOG.info(AaiUiMsgs.VISUALIZATION_GRAPH_OUTPUT, String.valueOf(numNodes),
151 String.valueOf(numLinks));
153 if (numLinks < (numNodes - 1)) {
154 LOG.warn(AaiUiMsgs.DANGLING_NODE_WARNING, String.valueOf(numLinks),
155 String.valueOf(numNodes));
158 ObjectMapper mapper = new ObjectMapper();
160 final String fileContent = ConfigHelper.getFileContents(
161 System.getProperty("AJSC_HOME") + this.visualizationConfigs.getAaiEntityNodeDescriptors());
162 com.fasterxml.jackson.databind.JsonNode aaiEntityNodeDefinitions = mapper.readTree(fileContent);
163 graphMeta.setAaiEntityNodeDescriptors(aaiEntityNodeDefinitions);
165 graphMeta.setNumLinks(linkArrayOutput.size());
166 graphMeta.setNumNodes(flatNodeArray.size());
167 graphMeta.setRenderTimeInMs(preProcessingOpTimeInMs);
169 output.setGraphMeta(graphMeta);
171 logOptime("generateVisualizationOutput()", opStartTimeInMs);
177 * Convert visualization output to json.
179 * @param output the output
181 * @throws JsonProcessingException the json processing exception
183 public String convertVisualizationOutputToJson(D3VisualizationOutput output)
184 throws JsonProcessingException {
186 if (output == null) {
190 ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
192 return ow.writeValueAsString(output);
197 * Builds the links from graph collection.
199 * @param nodeMap the node map
201 public void buildLinksFromGraphCollection(Map<String, ActiveInventoryNode> nodeMap) {
203 for (ActiveInventoryNode ain : nodeMap.values()) {
206 * This one is a little bit different, when we iterate over the collection we only want to
207 * draw the links for node that are less than the max traversal depth. We want to only draw
208 * links at a depth of n-1 because we are basing the links on the outbound neighbors from the
212 if (ain.getNodeDepth() < this.visualizationConfigs.getMaxSelfLinkTraversalDepth()) {
214 Collection<String> outboundNeighbors = ain.getOutboundNeighbors();
216 for (String outboundNeighbor : outboundNeighbors) {
218 SparkyGraphLink nodeLink = new SparkyGraphLink();
220 nodeLink.setId(UUID.randomUUID().toString());
221 nodeLink.setSource(ain.getNodeId());
222 nodeLink.setTarget(outboundNeighbor);
224 linkArrayOutput.add(nodeLink);
228 Collection<String> inboundNeighbors = ain.getInboundNeighbors();
230 for (String inboundNeighbor : inboundNeighbors) {
232 SparkyGraphLink nodeLink = new SparkyGraphLink();
234 nodeLink.setId(UUID.randomUUID().toString());
235 nodeLink.setSource(ain.getNodeId());
236 nodeLink.setTarget(inboundNeighbor);
238 linkArrayOutput.add(nodeLink);
244 if (LOG.isDebugEnabled()) {
245 LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "buildLinks(),"
246 + " Filtering node = " + ain.getNodeId() + " @ depth = "
247 + ain.getNodeDepth());
256 * Builds the flat node array from graph collection.
258 * @param nodeMap the node map
261 * Recursive function to walk multi-graph nodes and children to build a folded resource target
264 public void buildFlatNodeArrayFromGraphCollection(Map<String, ActiveInventoryNode> nodeMap) {
266 for (ActiveInventoryNode n : nodeMap.values()) {
268 if (n.getNodeDepth() <= this.visualizationConfigs.getMaxSelfLinkTraversalDepth()) {
270 SparkyGraphNode jsonNode = new SparkyGraphNode(n, this.visualizationConfigs, this.subConfig);
272 jsonNode.getNodeMeta().setClassName(this.visualizationConfigs.getGeneralNodeClassName());
274 if (this.visualizationConfigs.isVisualizationDebugEnabled()) {
276 NodeDebug nodeDebug = jsonNode.getNodeMeta().getNodeDebug();
278 if (nodeDebug != null) {
279 nodeDebug.setProcessingError(n.isProcessingErrorOccurred());
280 nodeDebug.setProcessingErrorCauses(n.getProcessingErrorCauses());
283 flatNodeArray.add(jsonNode);
285 if (LOG.isDebugEnabled()) {
286 LOG.debug(AaiUiMsgs.DEBUG_GENERIC,
287 "Filtering node from visualization: " + n.getNodeId() + " @ depth = "