aboutsummaryrefslogtreecommitdiffstats
path: root/ajsc-aai/src/main/java/org/openecomp/aai/dbgen/DbSearchWithTags.java
blob: 5ab8134062a84e2ac75537e9939c10f0b3217776 (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
/*-
 * ============LICENSE_START=======================================================
 * org.openecomp.aai
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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.openecomp.aai.dbgen;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;

import org.apache.tinkerpop.gremlin.structure.Direction;
import org.apache.tinkerpop.gremlin.structure.VertexProperty;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.logging.AAILogger;
import org.openecomp.aai.logging.LogLine;
import org.openecomp.aai.util.AAIConfig;

import com.thinkaurelius.titan.core.TitanTransaction;
import com.thinkaurelius.titan.core.TitanVertex;



/**
 * Database-level Search-Utility class that uses edge-tags to help it navigate the graph.   
 */
public class DbSearchWithTags{

	private static AAILogger aaiLogger = new AAILogger(DbSearchWithTags.class.getName());


	/**
	 * Identify top node set.
	 *
	 * @param transId the trans id
	 * @param fromAppId the from app id
	 * @param graph the graph
	 * @param edgeTag the edge tag
	 * @param topNodeType the top node type
	 * @param initialFilterHash the initial filter hash
	 * @param maxLevels the max levels
	 * @return List<titanVertex>
	 * @throws AAIException the AAI exception
	 */
	  public static HashMap <String, TitanVertex> identifyTopNodeSet( String transId, String fromAppId, TitanTransaction graph,
			  String edgeTag, String topNodeType, HashMap <String,Object> initialFilterHash, int maxLevels )   
					  throws AAIException {
			  
		  
		  LogLine logline = new LogLine();
		  logline.init("aaidbgen", transId, fromAppId, "identifyInitialNodeSet");
	    	
		  HashMap <String, TitanVertex> topVertHash = new HashMap <String, TitanVertex>();
		  if( graph == null ){
			  String emsg = "null graph object passed to identifyInitialNodeSet()\n";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6101");
			  throw new AAIException("AAI_6101", emsg); 
		  }
	  
		  
		  // Given the filter, we want to select all the nodes of the type the filter tells us that have the
		  // 	property they gave us.
		  // Then looping through those start points, we will look "up" and then "down" to find the set of target/top nodes.
		 
		  if( initialFilterHash == null || initialFilterHash.isEmpty() ){
			  String emsg = " initialFilterHash is required for identifyInitialNodeSet() call. \n";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6118");
			  throw new AAIException("AAI_6118", emsg); 
		  }
		  
		  // NOTE: we're expecting the filter to have a format like this: "nodeType.parameterName:parameterValue"
		  Iterator <?> it = initialFilterHash.entrySet().iterator();
		  // -- DEBUG -- for now we only deal with ONE initial filter parameter
		  //     it would be easy enough to deal with multiple parameters if they all
		  //     applied to the same nodeType.
		  String propNodeTypeDotName = "";
		  String initNodeType = "";
		  String initPropName = "";
		  HashMap <String,Object> initFilterTweakedHash = new HashMap <String,Object>();

		  HashMap <String,Object> initFilterHashUpper = new HashMap <String,Object>();
		  HashMap <String,Object> initFilterHashLower = new HashMap <String,Object>();
		  Boolean isKludgeCase = false;		 
		  String extraChecks = "";
		  
		  String propVal = "";
		  if( it.hasNext() ){
			  Map.Entry<?,?> propEntry = (Map.Entry<?,?>) it.next();
			  propNodeTypeDotName = (propEntry.getKey()).toString();
			  propVal = (propEntry.getValue()).toString();
		  }
		  
		  int periodLoc = propNodeTypeDotName.indexOf(".");
		  if( periodLoc <= 0 ){
			  String emsg = "Bad filter param key passed in: [" + propNodeTypeDotName + "].  Expected format = [nodeName.paramName]\n";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6120");
			  throw new AAIException("AAI_6120", emsg); 
		  }
		  else {
			  initNodeType = propNodeTypeDotName.substring(0,periodLoc);
			  initPropName = propNodeTypeDotName.substring(periodLoc + 1);
			  initFilterTweakedHash.put(initPropName, propVal);
			  
			  if( (initNodeType.equals("generic-vnf") && initPropName.equals("vnf-name"))
					  || (initNodeType.equals("vserver") && initPropName.equals("vserver-name")) ){
				  isKludgeCase = true;
				  String propValUpper = propVal.toUpperCase();
				  initFilterHashUpper.put(initPropName, propValUpper);
				  String propValLower = propVal.toLowerCase();
				  initFilterHashLower.put(initPropName, propValLower);
				  extraChecks = ", " + initFilterHashUpper.toString() + ", " + initFilterHashLower.toString();
			  }		  
		  }
	
		  ArrayList <TitanVertex> startVList = DbMeth.getNodes( transId, fromAppId, graph, initNodeType, initFilterTweakedHash, true, "v?"); 
		  if( startVList.isEmpty() && isKludgeCase ){
			  // We couldn't find any start nodes, but for two kludged cases, we will look again with all upper case 
			  // and then again with all Lower case if needed.
			  startVList = DbMeth.getNodes( transId, fromAppId, graph, initNodeType, initFilterHashUpper, true, "v?"); 
			  if( startVList.isEmpty() ){
				  // Still have no results, but since we're in the kludge case, we'll try again.
				  startVList = DbMeth.getNodes( transId, fromAppId, graph, initNodeType, initFilterHashLower, true, "v?");
			  }
		  }
		  
		  // To make sure we don't have duplicates, create a hash of the resulting vertices using their vertexId as the key
		  HashMap<String, TitanVertex> startVertHash = new HashMap<String, TitanVertex>();
		  Iterator<TitanVertex> iter = startVList.iterator(); 
		  while( iter.hasNext() ){ 
			  TitanVertex tvx = iter.next(); 
			  String vid = tvx.id().toString();
			  startVertHash.put(vid,tvx);
		  }
			  
		  if( startVertHash.isEmpty() ){
			   System.out.println("Probably want to log this -- but, No vertices found for inital search conditions: [ " +
					   initialFilterHash.toString() + "]" + extraChecks );
		  }
		  else {
			  for( Map.Entry<String, TitanVertex> entry : startVertHash.entrySet() ){
					// For each starting point vertex found, we need to look for 'top-level' vertices that correspond to it
					TitanVertex tv = entry.getValue();  
					HashMap <String,TitanVertex> targVHash = lookForTargetsUsingStartNodes( transId, fromAppId, graph, 
							edgeTag, topNodeType, tv, maxLevels );
					
					Iterator <?> vit = targVHash.entrySet().iterator();
					while( vit.hasNext() ){
						Map.Entry<?,?> propEntry = (Map.Entry<?,?>) vit.next();
						String foundVid = (propEntry.getKey()).toString();
						TitanVertex foundVtx = (TitanVertex) (propEntry.getValue());
						topVertHash.put(foundVid, foundVtx);
					}		
			  }
		  }
		  
		  if( topVertHash.isEmpty() ){
				// No Vertex was found  - throw a not-found exception
				String msg = "No Node of type " + topNodeType + " found for properties: " + initialFilterHash.toString() + extraChecks;
				logline.add("msg", msg);
				aaiLogger.info(logline, false, "AAI_6114");
				throw new AAIException("AAI_6114", msg);
		  }
		  else {
			  return topVertHash;
		  }
		  
	  }// End identifyInitialNodeSet()
  	  
		
	/**
	 * Look for targets using start nodes.
	 *
	 * @param transId the trans id
	 * @param fromAppId the from app id
	 * @param graph the graph
	 * @param edgeTag the edge tag
	 * @param topNodeType the top node type
	 * @param startVtx the start vtx
	 * @param maxLevels the max levels
	 * @return HashMap<vtxId,titanVertex>
	 * @throws AAIException the AAI exception
	 */
	  public static HashMap <String, TitanVertex> lookForTargetsUsingStartNodes( String transId, String fromAppId, TitanTransaction graph,
			  String edgeTag, String topNodeType, TitanVertex startVtx, int maxLevels )   
					  throws AAIException {
			
		  LogLine logline = new LogLine();
		  logline.init("aaidbgen", transId, fromAppId, "lookForTargetsUsingStartNodes");
	    	
		  if( graph == null ){
			  String emsg = "null graph object passed to lookForTargetsUsingStartNodes()\n";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6101");
			  throw new AAIException("AAI_6101", emsg); 
		  }
		  
		  // Walk the graph using tagged "IN" edges to try to find topNodeType vertices
		  HashMap <String, TitanVertex> targetVertHash = lookForTargetsInOneDirection( transId, fromAppId, graph,
				  edgeTag, topNodeType, startVtx, Direction.IN, 0, maxLevels );
		    
		  // Walk the graph using tagged "OUT" edges to find vertices of the topNodeType
		  HashMap <String, TitanVertex> foundOutVtxHash = lookForTargetsInOneDirection( transId, fromAppId, graph,
				  edgeTag, topNodeType, startVtx, Direction.OUT, 0, maxLevels );
		  
		  // Add the targetVertHash that was found for IN edges with the foundOutVtxHash 
  		  if( !foundOutVtxHash.isEmpty() ){
  			  Iterator <?> vit = foundOutVtxHash.entrySet().iterator();
  			  while( vit.hasNext() ){
  				  Map.Entry<?,?> propEntry = (Map.Entry<?,?>) vit.next();
  				  String foundVid = (propEntry.getKey()).toString();
  				  TitanVertex foundVtx = (TitanVertex) (propEntry.getValue());
  				  targetVertHash.put(foundVid, foundVtx);
  			  }
  		  }
		  
		  return targetVertHash;
		  
	  } // End of lookForTargetsUsingStartNodes()
	
	  
	/**
	 * Look for targets in one direction.
	 *
	 * @param transId the trans id
	 * @param fromAppId the from app id
	 * @param graph the graph
	 * @param edgeTag the edge tag
	 * @param topNodeType the top node type
	 * @param startVtx the start vtx
	 * @param dir the dir
	 * @param levelCounter the level counter
	 * @param maxLevels the max levels
	 * @return HashMap<vtxId,titanVertex>
	 * @throws AAIException the AAI exception
	 */
	  public static HashMap <String, TitanVertex> lookForTargetsInOneDirection( String transId, String fromAppId, TitanTransaction graph,
			  String edgeTag, String topNodeType, TitanVertex startVtx, Direction dir, int levelCounter, int maxLevels )   
					  throws AAIException {
		  
		  LogLine logline = new LogLine();
		  logline.init("aaidbgen", transId, fromAppId, "lookForTargetsInOneDirection");
		  levelCounter++;

		  //System.out.println(" DEBUG -- levelcount in lookForTargetsInOneDirection = " + levelCounter);

      	  if( levelCounter > maxLevels ) {
			  String emsg = "lookForTargetsInOneDirection() has looped across more levels than allowed: " + maxLevels + ". ";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6125");
			  throw new AAIException("AAI_6125", emsg); 
      	  }
      
		  HashMap <String, TitanVertex> targetVertHash = new HashMap <String, TitanVertex>();
		  
		  if( startVtx.property("aai-node-type").orElse(null).equals(topNodeType) ){
			  // We're starting on the kind of node we were looking for - so just return it, don't keep looking
			  // NOTE - this assumes that we will not find layers of the target-type node -- 
			  String vid = startVtx.id().toString();
			  targetVertHash.put(vid, startVtx);
			  return targetVertHash;
		  }
		
		  //System.out.println("DEBUG -- about to run the actual edge query (dir = " + dir + ", tag = " + edgeTag +  ", level = " + levelCounter );
		 
		  // Look for target nodes using results we got looking at normal edges in the "normal" direction
		  Iterable <?> verts = startVtx.query().direction(dir).has(edgeTag,true).vertices();
		  Iterator <?> vertI = verts.iterator();
		  while( vertI != null && vertI.hasNext() ){
        	  TitanVertex tmpVert = (TitanVertex) vertI.next();
        	  String vid = tmpVert.id().toString();
        	  String nType = tmpVert.<String>property("aai-node-type").orElse(null);
        	  //System.out.println("DEBUG -- going " + dir + ", found a node [" + nType + "], id = " + vid + " -- ");
          	  if( nType.equals(topNodeType) ){
          		  // We found a vertex that meets the input criteria - put it on our output hash.
          		  targetVertHash.put(vid, tmpVert);
          	  }
          	  else {
          		  // keep looking in the same direction
          		  //System.out.println("DEBUG1 -- do the actual recursive call staring from this node (vid = " + vid + "), direction = " + dir);
          		  HashMap <String, TitanVertex> tmpHash = lookForTargetsInOneDirection( transId, fromAppId, graph,
          				  edgeTag, topNodeType, tmpVert, dir, levelCounter, maxLevels );
          		  if( !tmpHash.isEmpty() ){
          			  // We found more vertexes to add to the return Hash
          			  Iterator <?> vit = tmpHash.entrySet().iterator();
          			  while( vit.hasNext() ){
          				  Map.Entry<?,?> propEntry = (Map.Entry<?,?>) vit.next();
          				  String foundVid = (propEntry.getKey()).toString();
          				  TitanVertex foundVtx = (TitanVertex) (propEntry.getValue());
          				  targetVertHash.put(foundVid, foundVtx);
          			  }
          		  }
          	  }
          }
		  
		  // Also walk the graph with "-REV" tags for edges that are opposite of the direction asked for
		  String revEdgeTag = edgeTag + "-REV";
		  Direction revDir = Direction.IN;
		  if( dir.equals(Direction.IN) ){
			  revDir = Direction.OUT;
		  }
		  //System.out.println("DEBUG -- about to run the REVERSE edge query (dir = " + revDir + ", tag = " + revEdgeTag +  ", level = " + levelCounter );
		  Iterable <?> vertsRev = startVtx.query().direction(revDir).has(revEdgeTag,true).vertices();
		  Iterator <?> vertIRev = vertsRev.iterator();
		  while( vertIRev != null && vertIRev.hasNext() ){
        	  TitanVertex tmpVertRev = (TitanVertex) vertIRev.next();
        	  String vid = tmpVertRev.id().toString();
        	  String nType = tmpVertRev.<String>property("aai-node-type").orElse(null);
        	  //System.out.println("DEBUG -- going REV: " + revDir + ", found a node [" + nType + "], id = " + vid + ")");
        	  
          	  if( nType.equals(topNodeType) ){
          		  // We found a vertex that meets the input criteria - put it on our output hash.
          		  targetVertHash.put(vid, tmpVertRev);
          	  }
          	  else {
          		  // keep looking in the same direction -- Ie. the same direction that this method was called with
          		  //System.out.println("DEBUG2 -- do the actual recursive call staring from this node (vid = " + vid + "), direction = " + dir + "----");
          		  HashMap <String, TitanVertex> tmpHash = lookForTargetsInOneDirection( transId, fromAppId, graph,
          				  edgeTag, topNodeType, tmpVertRev, dir, levelCounter, maxLevels );
          		  if( !tmpHash.isEmpty() ){
          			  // We found more vertexes to add to the return Hash
          			  Iterator <?> vit = tmpHash.entrySet().iterator();
          			  while( vit.hasNext() ){
          				  Map.Entry<?,?> propEntry = (Map.Entry<?,?>) vit.next();
          				  String foundVid = (propEntry.getKey()).toString();
          				  TitanVertex foundVtx = (TitanVertex) (propEntry.getValue());
          				  targetVertHash.put(foundVid, foundVtx);
          			  }
          		  }
          	  }
          }
  
		  return targetVertHash;
	}// End lookForTargetsInOneDirection()
		  
		    
	/**
	 * Collect result set.
	 *
	 * @param transId the trans id
	 * @param fromAppId the from app id
	 * @param graph the graph
	 * @param edgeTag the edge tag
	 * @param thisLevelVertex the this level vertex
	 * @param secFilterHash the sec filter hash
	 * @param retNodeType the ret node type
	 * @param levelCounter the level counter
	 * @param maxLevels the max levels
	 * @return resultSet
	 * @throws AAIException the AAI exception
	 */
	  public static ResultSet collectResultSet( String transId, String fromAppId, TitanTransaction graph,
			  String edgeTag, TitanVertex thisLevelVertex, HashMap <String,Object> secFilterHash, 
			  String retNodeType, int levelCounter, int maxLevels )   
					  throws AAIException {

		  // Note:  our return data set is everything on the OUT-edge side of the topVertex - or following
		  //        an IN-edge but with a "-REV" tagged edge
		  levelCounter++;
		  LogLine logline = new LogLine();
		  logline.init("aaidbgen", transId, fromAppId, "collectResultSet");
		  
		  if( graph == null ){
			  String emsg = "null graph object passed to collectResultSet()\n";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6101");
			  throw new AAIException("AAI_6101", emsg); 
		  }
		  
		  //String thisVid = thisLevelVertex.id().toString();
		  //String thisNt = thisLevelVertex.<String>property("aai-node-type").orElse(null);
		  //System.out.println(" DEBUG -- level counter in collectResultSet = " + levelCounter + " thisNodeType = " + thisNt + " this vid = " + thisVid);

      	  if( levelCounter > maxLevels ) {
			  String emsg = "collectResultSet() has looped across more levels than allowed: " + maxLevels + ". ";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6125");
			  throw new AAIException("AAI_6125", emsg); 
      	  }
		  
      	  ResultSet rs = new ResultSet();
		  rs.vert = thisLevelVertex;
		
		  // Look at the tagged "OUT" edges for this node	
		  //System.out.println("DEBUG -- about to run the collectResult query at level = " + levelCounter );
		  Iterable <?> verts = thisLevelVertex.query().direction(Direction.OUT).has(edgeTag,true).vertices();
		  Iterator <?> vertI = verts.iterator();
		  
		  ArrayList <TitanVertex> nodeList = new <TitanVertex> ArrayList ();
		  while( vertI != null && vertI.hasNext() ){
        	  TitanVertex tmpVert = (TitanVertex) vertI.next();
        	  nodeList.add(tmpVert);
		  }
		  
		  // Look at the reverse-tagged "IN" edges for this node
		  String reverseEdgeTag = edgeTag + "-REV";
		  Iterable <?> vertsRev = thisLevelVertex.query().direction(Direction.IN).has(reverseEdgeTag,true).vertices();
		  Iterator <?> vertIRev = vertsRev.iterator();
		  while( vertIRev != null && vertIRev.hasNext() ){
        	  TitanVertex tmpVert = (TitanVertex) vertIRev.next();
        	  nodeList.add(tmpVert);
		  }
		  
		  if( nodeList.isEmpty() ){
			  // There were no sub-vertices, so we can return this result set
			  //System.out.println("DEBUG -- no subVert found here - we will return the resultSet with no sub dudes. ");
		  }
		  else {
			  // For each sub-vertex found, need to get it's result set (recursively)
			  Iterator<?> nodeIter = nodeList.iterator();
			  while( nodeIter.hasNext() ){
	        	  TitanVertex tmpVert = (TitanVertex) nodeIter.next();
	          	  ResultSet tmpResSet = collectResultSet( transId, fromAppId, graph, edgeTag, tmpVert, 
	            		  secFilterHash, retNodeType, levelCounter, maxLevels );
	          	  rs.subResultSet.add(tmpResSet);
	          	  //System.out.println("DEBUG -- found this guy at level " + levelCounter + ", nodeType = " + tmpVert.property("aai-node-type").orElse(null));
			  }
		  }
		  
		  //System.out.println("DEBUG -- returning from a call to collectResultSet() ");
		  return rs;
		  
	  } // End of collectResultSet()
	
  
	/**
	 * Collect result set.
	 *
	 * @param transId the trans id
	 * @param fromAppId the from app id
	 * @param graph the graph
	 * @param edgeTag the edge tag
	 * @param thisLevelVertex the this level vertex
	 * @param secFilterHash the sec filter hash
	 * @param retNodeType the ret node type
	 * @param levelCounter the level counter
	 * @param fwdSearchDirection the fwd search direction
	 * @param revSearchDirection the rev search direction
	 * @param doPruning the do pruning
	 * @param pruneNodeType -- nodeType of nodes we want to stop collecting at
	 * @param pruneKeepId -- vertexId of a pruneType node that we DO want to keep
	 * @param trimList the trim list
	 * @param maxLevels the max levels
	 * @return resultSet
	 * @throws AAIException the AAI exception
	 */
	  public static ResultSet collectResultSet( String transId, String fromAppId, TitanTransaction graph,
			  String edgeTag, 
			  TitanVertex thisLevelVertex, 
			  HashMap <String,Object> secFilterHash, 
			  String retNodeType, 
			  int levelCounter,
			  Direction fwdSearchDirection,
			  Direction revSearchDirection,
			  Boolean doPruning, 
			  String pruneNodeType,
			  String pruneKeepId,
			  ArrayList trimList,
			  int maxLevels )   
					  throws AAIException {

		  // Note - our return data set is everything found starting at this vertex and found 
		  //     following edges that are either a) in the search-direction, or b) tagged the opposite of the 
		  //     search-Direction, but tagged to be followed in "-REV" direction.
		  
		  // Note - when pruning, if we hit a node of type pruneNodeType that does Not have the ID matching
		  //        pruneKeepId, then we do NOT add that vertex to our result set
		  
		  levelCounter++;
		  LogLine logline = new LogLine();
		  logline.init("aaidbgen", transId, fromAppId, "collectResultSet");
		  
		  boolean needToDoPruning = false;
		  if( doPruning && pruneKeepId != null && !pruneKeepId.equals("") && pruneNodeType != null && !pruneNodeType.equals("") ){
			  needToDoPruning = true;
		  }
		  
		  if( graph == null ){
			  String emsg = "null graph object passed to collectResultSet()\n";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6101");
			  throw new AAIException("AAI_6101", emsg); 
		  }
		  
		  //String thisVid = thisLevelVertex.id().toString();
		  //String thisNt = thisLevelVertex.<String>property("aai-node-type").orElse(null);
		  //System.out.println(" DEBUG -- level counter in collectResultSet = " + levelCounter + " thisNodeType = " + thisNt + " this vid = " + thisVid);

      	  if( levelCounter > maxLevels ) {
			  String emsg = "collectResultSet() has looped across more levels than allowed: " + maxLevels + ". ";
			  logline.add("emsg", emsg);         	 
			  aaiLogger.info(logline, false, "AAI_6125");
			  throw new AAIException("AAI_6125", emsg); 
      	  }
      	  
		  ResultSet rs = new ResultSet();
		  rs.vert = thisLevelVertex;
		
		  // Look at the FWD edges for this node	
		  //System.out.println("DEBUG -- about to run the collectResult query at level = " + levelCounter );
		  Iterable <?> verts = thisLevelVertex.query().direction(fwdSearchDirection).has(edgeTag,true).vertices();
		  Iterator <?> vertI = verts.iterator();
		  
		  //int levelNodeCount = 0;
		  ArrayList <TitanVertex> nodeList = new <TitanVertex> ArrayList ();
		  while( vertI != null && vertI.hasNext() ){
			  TitanVertex tmpVert = (TitanVertex) vertI.next();
			  String nodeType = tmpVert.<String>property("aai-node-type").orElse(null);
			  if( needToDoPruning ){
				  if( nodeType.equals(pruneNodeType) && 
						  !tmpVert.id().toString().equals(pruneKeepId) ){
					  // This node is the type we're pruning and it is not the ONE that we want to keep - so
					  // do not put it into the resultSet
				  }
				  else {
					  // Don't need to prune this one 
					  if( ! trimList.contains(nodeType) ){
						  // They're not trimming or pruning this kind of thing - so keep it.
						  nodeList.add(tmpVert);
					  }
				  }
			  }
			  else {
				  if( ! trimList.contains(nodeType) ){
					  // They're not trimming or pruning this kind of thing - so keep it.
					  nodeList.add(tmpVert);
				  }
			  }
		  }
		  
		  // Look at the reverse-tagged edges that are in the rev-direction for this node
		  String reverseEdgeTag = edgeTag + "-REV";
		  Iterable <?> vertsRev = thisLevelVertex.query().direction(revSearchDirection).has(reverseEdgeTag,true).vertices();
		  Iterator <?> vertIRev = vertsRev.iterator();
		  while( vertIRev != null && vertIRev.hasNext() ){
        	  TitanVertex tmpVert = (TitanVertex) vertIRev.next();
        	  String nodeType = tmpVert.<String>property("aai-node-type").orElse(null);
        	  if( needToDoPruning ){
				  if( nodeType.equals(pruneNodeType) && 
						  !tmpVert.id().toString().equals(pruneKeepId) ){
					  // This node is the type we're pruning and it is not the ONE that we want to keep - so
					  // do not put it into the resultSet
				  }
				  else {
					  if( ! trimList.contains(nodeType) ){
						  // They're not trimming or pruning this kind of thing - so keep it.
						  nodeList.add(tmpVert);
					  }
				  }
			  }
			  else {
				  if( ! trimList.contains(nodeType) ){
					  // They're not trimming or pruning this kind of thing - so keep it.
					  nodeList.add(tmpVert);
				  }
			  }
		  }
		  
		  if( nodeList.isEmpty() ){
			  // There were no sub-vertices, so we can return this result set
			  //System.out.println("DEBUG -- no subVert found here - we will return the resultSet with no sub dudes. ");
		  }
		  else {
			  // For each sub-vertex found, need to get it's result set (recursively)
			  Iterator<?> nodeIter = nodeList.iterator();
			  while( nodeIter.hasNext() ){
	        	  TitanVertex tmpVert = (TitanVertex) nodeIter.next();
	          	  ResultSet tmpResSet = collectResultSet( transId, fromAppId, graph, edgeTag, tmpVert, 
	            		  secFilterHash, retNodeType, levelCounter,
	            		  fwdSearchDirection, revSearchDirection, doPruning, 
	        			  pruneNodeType, pruneKeepId, trimList, maxLevels );
	          	  
	          	  rs.subResultSet.add(tmpResSet);
	          	  //System.out.println("DEBUG -- found this guy at level " + levelCounter + ", nodeType = " + tmpVert.property("aai-node-type").orElse(null));
			  }
		  }
		  
		  //System.out.println("DEBUG -- returning from a call to collectResultSet() ");
		  return rs;
		  
	  } // End of collectResultSet()
		
	  
	  
	  
	  
	  
	  /**
  	 * Prints the out result set.
  	 *
  	 * @param resSet the res set
  	 * @param levelCount the level count
  	 */
  	public static void printOutResultSet( ResultSet resSet, int levelCount ) {
            
		  levelCount++;
		  for( int i= 1; i <= levelCount; i++ ){
			  System.out.print("-");
		  }
		  String nt = resSet.vert.<String>property("aai-node-type").orElse(null);
		  
		  Iterator<VertexProperty<Object>> pI = resSet.vert.properties();
		  String propsStr = "";
		  while( pI.hasNext() ){
				VertexProperty<Object> tp = pI.next();
				String pkTop = tp.key();
				/***
				if( ! pkTop.toString().startsWith("aai") 
						&& ! pkTop.toString().equals("source-of-truth")
						&& ! pkTop.toString().equals("resource-version")
						&& ! pkTop.toString().startsWith("last-mod")
						){
				****/
				// For my testing - just want to see these:
				if( pkTop.equals("vserver-id")
						|| pkTop.toString().equals("vnf-name")
						|| pkTop.toString().equals("vnf-id")
						|| pkTop.toString().equals("vserver-name")
						|| pkTop.toString().equals("hostname")
					){
					propsStr = propsStr + " [" + tp.key() + " = " + tp.value() + "]";
				}
		  }
		 
		  System.out.println( levelCount + " " + nt + ", " + propsStr );
		  
		  if( !resSet.subResultSet.isEmpty() ){
			  ListIterator<ResultSet> listItr = resSet.subResultSet.listIterator();
			  while( listItr.hasNext() ){
				  printOutResultSet( listItr.next(), levelCount );
			  }
		  }
		  
	  }// end of printOutResultSet()
	  
	  
	  /**
  	 * Satisfies hash of filters.
  	 *
  	 * @param transId the trans id
  	 * @param fromAppId the from app id
  	 * @param resSet the res set
  	 * @param filterHash the filter hash
  	 * @return true, if successful
  	 * @throws AAIException the AAI exception
  	 */
  	public static boolean satisfiesHashOfFilters( String transId, String fromAppId, 
			  ResultSet resSet, HashMap <String,Object> filterHash )  throws AAIException {
		  
		  LogLine logline = new LogLine();
		  logline.init("aaidbgen", transId, fromAppId, "satisfiesHashOfFilters");
		  
		  if( filterHash.isEmpty() ){
			  // Nothing to match - so we're OK
			  //System.out.println("DEBUG ----- nothing to match for sec. filters - so we're ok ");
			  return true;
		  }
		  
		  Iterator <?> it = filterHash.entrySet().iterator();
		  while( it.hasNext() ){
			  Map.Entry<?,?> filtEntry = (Map.Entry<?,?>) it.next();
			  String propNodeTypeDotName = (filtEntry.getKey()).toString();
			  String fpv = (filtEntry.getValue()).toString();
			  
			  int periodLoc = propNodeTypeDotName.indexOf(".");
			  if( periodLoc <= 0 ){
				  String emsg = "Bad filter param key passed in: [" + propNodeTypeDotName + "].  Expected format = [nodeName.paramName]\n";
				  logline.add("emsg", emsg);         	 
				  aaiLogger.info(logline, false, "AAI_6120");
				  throw new AAIException("AAI_6120", emsg); 
			  }
			  else {
				  String fnt = propNodeTypeDotName.substring(0,periodLoc);
				  String fpn = propNodeTypeDotName.substring(periodLoc + 1);
				
				  if( !filterMetByThisSet( resSet, fnt, fpn, fpv ) ){
					  //System.out.println(" DEBUG -- FAILED to satisfy filter: [" + fnt + "|" + fpn + "|" + fpv + "].");
					  return false;
				  }
			  }
		  }
		  
		  //System.out.println("DEBUG ----- Made it PAST all sec. filters - so we're ok ");
		  // Made it through all the filters -- must be good to go.
		  return true;
		  
	  }// end of satisfiesHashOfFilters()
	  
	  
	  /**
  	 * Filter met by this set.
  	 *
  	 * @param resSet the res set
  	 * @param filtNodeType the filt node type
  	 * @param filtPropName the filt prop name
  	 * @param filtPropVal the filt prop val
  	 * @return true, if successful
  	 */
  	public static boolean filterMetByThisSet( ResultSet resSet, String filtNodeType, String filtPropName, String filtPropVal ) {
          // Note - we are just looking for a positive match for one filter for this resultSet
		  // NOTE: we're expecting the filter to have a format like this: "nodeType.parameterName:parameterValue"
	
		  TitanVertex vert = resSet.vert;
		  if( vert == null ){
			  return false;
		  }
		  else {
			  String nt = resSet.vert.<String>property("aai-node-type").orElse(null);
			  if( nt.equals( filtNodeType ) ){
				  if( filtPropName.equals("vertex-id") ){
					  // vertex-id can't be gotten the same way as other properties
					  String thisVtxId = vert.id().toString();
					  if( thisVtxId.equals(filtPropVal) ){
						  //System.out.println(" DEBUG -- filter [" + filtNodeType + "|" + filtPropName + "|" + filtPropVal + "] has been met.");
						  return true;
					  }
				  }
				  else {
					  Object thisValObj = vert.property(filtPropName).orElse(null);
					  if( thisValObj != null ){
						  String thisVal = thisValObj.toString();
						  if( thisVal.equals(filtPropVal) ){
							  //System.out.println(" DEBUG -- filter [" + filtNodeType + "|" + filtPropName + "|" + filtPropVal + "] has been met.");
							  return true;
						  }
					  }
				  }
			  }
		  }
			  
		  // Didn't find a match at the this level, so check the sets below it meet the criteria
		  if( !resSet.subResultSet.isEmpty() ){
			  ListIterator<ResultSet> listItr = resSet.subResultSet.listIterator();
			  while( listItr.hasNext() ){
				  if( filterMetByThisSet(listItr.next(), filtNodeType, filtPropName, filtPropVal) ){
					  return true;
				  }
			  }
		  }
		  
		  return false;
		  
	  }// end of filterMetByThisSet()
	  
		  
}