Routes and beans for subscription
[aai/sparky-be.git] / src / main / java / org / onap / aai / sparky / viewandinspect / services / VisualizationTransformer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
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
11  *
12  *       http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  *
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  */
23 package org.onap.aai.sparky.viewandinspect.services;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.UUID;
31
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;
44
45 import com.fasterxml.jackson.core.JsonProcessingException;
46 import com.fasterxml.jackson.databind.ObjectMapper;
47 import com.fasterxml.jackson.databind.ObjectWriter;
48
49 /**
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.
54  * 
55  * @author DAVEA
56  *
57  */
58
59 public class VisualizationTransformer {
60
61   private static final Logger LOG = LoggerFactory.getInstance().getLogger(
62       VisualizationTransformer.class);
63
64   List<SparkyGraphNode> flatNodeArray = new ArrayList<SparkyGraphNode>();
65
66   /*
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.
70    */
71
72   List<SparkyGraphLink> linkArrayOutput = new ArrayList<SparkyGraphLink>();
73   
74   private VisualizationConfigs visualizationConfigs;
75   private SubscriptionConfig subConfig;
76
77   /**
78    * Instantiates a new visualization transformer.
79    *
80    * @throws Exception the exception
81    */
82   public VisualizationTransformer(VisualizationConfigs visualizationConfigs,
83       SubscriptionConfig subConfig) throws Exception {
84           this.visualizationConfigs = visualizationConfigs; 
85           this.subConfig = subConfig;
86   }
87
88
89   /**
90    * Log optime.
91    *
92    * @param method the method
93    * @param startTimeInMs the start time in ms
94    */
95   private void logOptime(String method, long startTimeInMs) {
96     LOG.info(AaiUiMsgs.OPERATION_TIME, method,
97         String.valueOf((System.currentTimeMillis() - startTimeInMs)));
98   }
99
100   /**
101    * Adds the search target attributes to root node.
102    */
103   public void addSearchTargetAttributesToRootNode() {
104
105     for (SparkyGraphNode n : flatNodeArray) {
106       if (n.isRootNode()) {
107         n.getNodeMeta().setSearchTarget(true);
108         n.getNodeMeta().setClassName(this.visualizationConfigs.getSelectedSearchedNodeClassName());
109       }
110
111     }
112
113   }
114
115   /**
116    * Generate visualization output.
117    *
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.
123    */
124
125   public D3VisualizationOutput generateVisualizationOutput(long preProcessingOpTimeInMs,
126       GraphMeta graphMeta) throws JsonProcessingException, IOException {
127
128     long opStartTimeInMs = System.currentTimeMillis();
129
130     /*
131      * iterate over the flat collection, and only add the graph nodes to the graph node collection
132      */
133
134     D3VisualizationOutput output = new D3VisualizationOutput();
135
136     output.setGraphMeta(graphMeta);
137
138     for (SparkyGraphNode n : flatNodeArray) {
139       if ( n.getItemType()!= null) {
140         output.pegCounter(n.getItemType());
141       }
142     }
143
144     output.addNodes(flatNodeArray);
145     output.addLinks(linkArrayOutput);
146
147     int numNodes = flatNodeArray.size();
148     int numLinks = linkArrayOutput.size();
149
150     LOG.info(AaiUiMsgs.VISUALIZATION_GRAPH_OUTPUT, String.valueOf(numNodes),
151         String.valueOf(numLinks));
152
153     if (numLinks < (numNodes - 1)) {
154       LOG.warn(AaiUiMsgs.DANGLING_NODE_WARNING, String.valueOf(numLinks),
155           String.valueOf(numNodes));
156     }
157
158     ObjectMapper mapper = new ObjectMapper();
159
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);
164
165     graphMeta.setNumLinks(linkArrayOutput.size());
166     graphMeta.setNumNodes(flatNodeArray.size());
167     graphMeta.setRenderTimeInMs(preProcessingOpTimeInMs);
168
169     output.setGraphMeta(graphMeta);
170
171     logOptime("generateVisualizationOutput()", opStartTimeInMs);
172
173     return output;
174   }
175
176   /**
177    * Convert visualization output to json.
178    *
179    * @param output the output
180    * @return the string
181    * @throws JsonProcessingException the json processing exception
182    */
183   public String convertVisualizationOutputToJson(D3VisualizationOutput output)
184       throws JsonProcessingException {
185
186     if (output == null) {
187       return null;
188     }
189
190     ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
191
192     return ow.writeValueAsString(output);
193
194   }
195
196   /**
197    * Builds the links from graph collection.
198    *
199    * @param nodeMap the node map
200    */
201   public void buildLinksFromGraphCollection(Map<String, ActiveInventoryNode> nodeMap) {
202
203     for (ActiveInventoryNode ain : nodeMap.values()) {
204
205       /*
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
209        * current node.
210        */
211
212       if (ain.getNodeDepth() < this.visualizationConfigs.getMaxSelfLinkTraversalDepth()) {
213
214         Collection<String> outboundNeighbors = ain.getOutboundNeighbors();
215
216         for (String outboundNeighbor : outboundNeighbors) {
217
218           SparkyGraphLink nodeLink = new SparkyGraphLink();
219
220           nodeLink.setId(UUID.randomUUID().toString());
221           nodeLink.setSource(ain.getNodeId());
222           nodeLink.setTarget(outboundNeighbor);
223
224           linkArrayOutput.add(nodeLink);
225
226         }
227
228         Collection<String> inboundNeighbors = ain.getInboundNeighbors();
229
230         for (String inboundNeighbor : inboundNeighbors) {
231
232           SparkyGraphLink nodeLink = new SparkyGraphLink();
233
234           nodeLink.setId(UUID.randomUUID().toString());
235           nodeLink.setSource(ain.getNodeId());
236           nodeLink.setTarget(inboundNeighbor);
237
238           linkArrayOutput.add(nodeLink);
239
240         }
241
242
243       } else {
244         if (LOG.isDebugEnabled()) {
245           LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "buildLinks(),"
246               + " Filtering node = " + ain.getNodeId() + " @ depth = "
247               + ain.getNodeDepth());
248         }
249
250       }
251     }
252
253   }
254
255   /**
256    * Builds the flat node array from graph collection.
257    *
258    * @param nodeMap the node map
259    */
260   /*
261    * Recursive function to walk multi-graph nodes and children to build a folded resource target
262    * graph.
263    */
264   public void buildFlatNodeArrayFromGraphCollection(Map<String, ActiveInventoryNode> nodeMap) {
265
266     for (ActiveInventoryNode n : nodeMap.values()) {
267
268       if (n.getNodeDepth() <= this.visualizationConfigs.getMaxSelfLinkTraversalDepth()) {
269
270         SparkyGraphNode jsonNode = new SparkyGraphNode(n, this.visualizationConfigs, this.subConfig);
271
272         jsonNode.getNodeMeta().setClassName(this.visualizationConfigs.getGeneralNodeClassName());
273
274         if (this.visualizationConfigs.isVisualizationDebugEnabled()) {
275
276           NodeDebug nodeDebug = jsonNode.getNodeMeta().getNodeDebug();
277
278           if (nodeDebug != null) {
279             nodeDebug.setProcessingError(n.isProcessingErrorOccurred());
280             nodeDebug.setProcessingErrorCauses(n.getProcessingErrorCauses());
281           }
282         }
283         flatNodeArray.add(jsonNode);
284       } else {
285         if (LOG.isDebugEnabled()) {
286           LOG.debug(AaiUiMsgs.DEBUG_GENERIC, 
287               "Filtering node from visualization: " + n.getNodeId() + " @ depth = "
288               + n.getNodeDepth());
289         }
290       }
291     }
292   }
293
294 }