summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/sparky/viewandinspect/services/VisualizationTransformer.java
blob: d763efe43b1c1745f18b94747ded5cdc4c2cd7b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017-2018 Amdocs
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */
package org.onap.aai.sparky.viewandinspect.services;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.onap.aai.cl.api.Logger;
import org.onap.aai.cl.eelf.LoggerFactory;
import org.onap.aai.sparky.logging.AaiUiMsgs;
import org.onap.aai.sparky.subscription.config.SubscriptionConfig;
import org.onap.aai.sparky.util.ConfigHelper;
import org.onap.aai.sparky.viewandinspect.config.VisualizationConfigs;
import org.onap.aai.sparky.viewandinspect.entity.ActiveInventoryNode;
import org.onap.aai.sparky.viewandinspect.entity.D3VisualizationOutput;
import org.onap.aai.sparky.viewandinspect.entity.GraphMeta;
import org.onap.aai.sparky.viewandinspect.entity.SparkyGraphNode;
import org.onap.aai.sparky.viewandinspect.entity.SparkyGraphLink;
import org.onap.aai.sparky.viewandinspect.entity.NodeDebug;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;

/**
 * The idea here is to receive a collection of graphs and then fold them together (or not) based on
 * configuration. The first goal will be to fold all like-resources together, but the choice of
 * folding could/should be configurable, and will simply change the degree of link based nodes when
 * we generate the Node-Array and Link-Array output.
 * 
 * @author DAVEA
 *
 */

public class VisualizationTransformer {

  private static final Logger LOG = LoggerFactory.getInstance().getLogger(
      VisualizationTransformer.class);

  List<SparkyGraphNode> flatNodeArray = new ArrayList<SparkyGraphNode>();

  /*
   * Maybe this isn't a string but Json-Model objects that we will convert to final string
   * representation when we dump the node-array and link-array collections the post-data blob in the
   * HttpServletResponse.
   */

  List<SparkyGraphLink> linkArrayOutput = new ArrayList<SparkyGraphLink>();
  
  private VisualizationConfigs visualizationConfigs;
  private SubscriptionConfig subConfig;

  /**
   * Instantiates a new visualization transformer.
   *
   * @throws Exception the exception
   */
  public VisualizationTransformer(VisualizationConfigs visualizationConfigs,
      SubscriptionConfig subConfig) throws Exception {
	  this.visualizationConfigs = visualizationConfigs; 
	  this.subConfig = subConfig;
  }


  /**
   * Log optime.
   *
   * @param method the method
   * @param startTimeInMs the start time in ms
   */
  private void logOptime(String method, long startTimeInMs) {
    LOG.info(AaiUiMsgs.OPERATION_TIME, method,
        String.valueOf((System.currentTimeMillis() - startTimeInMs)));
  }

  /**
   * Adds the search target attributes to root node.
   */
  public void addSearchTargetAttributesToRootNode() {

    for (SparkyGraphNode n : flatNodeArray) {
      if (n.isRootNode()) {
        n.getNodeMeta().setSearchTarget(true);
        n.getNodeMeta().setClassName(this.visualizationConfigs.getSelectedSearchedNodeClassName());
      }

    }

  }

  /**
   * Generate visualization output.
   *
   * @param preProcessingOpTimeInMs the pre processing op time in ms
   * @param graphMeta the graph meta
   * @return the d 3 visualization output
   * @throws JsonProcessingException the json processing exception
   * @throws IOException Signals that an I/O exception has occurred.
   */

  public D3VisualizationOutput generateVisualizationOutput(long preProcessingOpTimeInMs,
      GraphMeta graphMeta) throws JsonProcessingException, IOException {

    long opStartTimeInMs = System.currentTimeMillis();

    /*
     * iterate over the flat collection, and only add the graph nodes to the graph node collection
     */

    D3VisualizationOutput output = new D3VisualizationOutput();

    output.setGraphMeta(graphMeta);

    for (SparkyGraphNode n : flatNodeArray) {
      if ( n.getItemType()!= null) {
        output.pegCounter(n.getItemType());
      }
    }

    output.addNodes(flatNodeArray);
    output.addLinks(linkArrayOutput);

    int numNodes = flatNodeArray.size();
    int numLinks = linkArrayOutput.size();

    LOG.info(AaiUiMsgs.VISUALIZATION_GRAPH_OUTPUT, String.valueOf(numNodes),
        String.valueOf(numLinks));

    if (numLinks < (numNodes - 1)) {
      LOG.warn(AaiUiMsgs.DANGLING_NODE_WARNING, String.valueOf(numLinks),
          String.valueOf(numNodes));
    }

    ObjectMapper mapper = new ObjectMapper();

    final String fileContent = ConfigHelper.getFileContents(
        System.getProperty("AJSC_HOME") + this.visualizationConfigs.getAaiEntityNodeDescriptors());
    com.fasterxml.jackson.databind.JsonNode aaiEntityNodeDefinitions = mapper.readTree(fileContent);
    graphMeta.setAaiEntityNodeDescriptors(aaiEntityNodeDefinitions);

    graphMeta.setNumLinks(linkArrayOutput.size());
    graphMeta.setNumNodes(flatNodeArray.size());
    graphMeta.setRenderTimeInMs(preProcessingOpTimeInMs);

    output.setGraphMeta(graphMeta);

    logOptime("generateVisualizationOutput()", opStartTimeInMs);

    return output;
  }

  /**
   * Convert visualization output to json.
   *
   * @param output the output
   * @return the string
   * @throws JsonProcessingException the json processing exception
   */
  public String convertVisualizationOutputToJson(D3VisualizationOutput output)
      throws JsonProcessingException {

    if (output == null) {
      return null;
    }

    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();

    return ow.writeValueAsString(output);

  }

  /**
   * Builds the links from graph collection.
   *
   * @param nodeMap the node map
   */
  public void buildLinksFromGraphCollection(Map<String, ActiveInventoryNode> nodeMap) {

    for (ActiveInventoryNode ain : nodeMap.values()) {

      /*
       * This one is a little bit different, when we iterate over the collection we only want to
       * draw the links for node that are less than the max traversal depth. We want to only draw
       * links at a depth of n-1 because we are basing the links on the outbound neighbors from the
       * current node.
       */

      if (ain.getNodeDepth() < this.visualizationConfigs.getMaxSelfLinkTraversalDepth()) {

        Collection<String> outboundNeighbors = ain.getOutboundNeighbors();

        for (String outboundNeighbor : outboundNeighbors) {

          SparkyGraphLink nodeLink = new SparkyGraphLink();

          nodeLink.setId(UUID.randomUUID().toString());
          nodeLink.setSource(ain.getNodeId());
          nodeLink.setTarget(outboundNeighbor);

          linkArrayOutput.add(nodeLink);

        }

        Collection<String> inboundNeighbors = ain.getInboundNeighbors();

        for (String inboundNeighbor : inboundNeighbors) {

          SparkyGraphLink nodeLink = new SparkyGraphLink();

          nodeLink.setId(UUID.randomUUID().toString());
          nodeLink.setSource(ain.getNodeId());
          nodeLink.setTarget(inboundNeighbor);

          linkArrayOutput.add(nodeLink);

        }


      } else {
        if (LOG.isDebugEnabled()) {
          LOG.debug(AaiUiMsgs.DEBUG_GENERIC, "buildLinks(),"
              + " Filtering node = " + ain.getNodeId() + " @ depth = "
              + ain.getNodeDepth());
        }

      }
    }

  }

  /**
   * Builds the flat node array from graph collection.
   *
   * @param nodeMap the node map
   */
  /*
   * Recursive function to walk multi-graph nodes and children to build a folded resource target
   * graph.
   */
  public void buildFlatNodeArrayFromGraphCollection(Map<String, ActiveInventoryNode> nodeMap) {

    for (ActiveInventoryNode n : nodeMap.values()) {

      if (n.getNodeDepth() <= this.visualizationConfigs.getMaxSelfLinkTraversalDepth()) {

        SparkyGraphNode jsonNode = new SparkyGraphNode(n, this.visualizationConfigs, this.subConfig);

        jsonNode.getNodeMeta().setClassName(this.visualizationConfigs.getGeneralNodeClassName());

        if (this.visualizationConfigs.isVisualizationDebugEnabled()) {

          NodeDebug nodeDebug = jsonNode.getNodeMeta().getNodeDebug();

          if (nodeDebug != null) {
            nodeDebug.setProcessingError(n.isProcessingErrorOccurred());
            nodeDebug.setProcessingErrorCauses(n.getProcessingErrorCauses());
          }
        }
        flatNodeArray.add(jsonNode);
      } else {
        if (LOG.isDebugEnabled()) {
          LOG.debug(AaiUiMsgs.DEBUG_GENERIC, 
              "Filtering node from visualization: " + n.getNodeId() + " @ depth = "
              + n.getNodeDepth());
        }
      }
    }
  }

}