Add the logging statements for when
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / serialization / db / DBSerializer.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.serialization.db;
21
22
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.google.common.base.CaseFormat;
26 import org.apache.commons.collections.IteratorUtils;
27 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
29 import org.apache.tinkerpop.gremlin.structure.Direction;
30 import org.apache.tinkerpop.gremlin.structure.Edge;
31 import org.apache.tinkerpop.gremlin.structure.Vertex;
32 import org.apache.tinkerpop.gremlin.structure.VertexProperty;
33 import org.janusgraph.core.SchemaViolationException;
34 import org.javatuples.Triplet;
35 import org.onap.aai.concurrent.AaiCallable;
36 import org.onap.aai.config.SpringContextAware;
37 import org.onap.aai.db.props.AAIProperties;
38 import org.onap.aai.edges.EdgeIngestor;
39 import org.onap.aai.edges.EdgeRule;
40 import org.onap.aai.edges.EdgeRuleQuery;
41 import org.onap.aai.edges.TypeAlphabetizer;
42 import org.onap.aai.edges.enums.AAIDirection;
43 import org.onap.aai.edges.enums.EdgeField;
44 import org.onap.aai.edges.enums.EdgeProperty;
45 import org.onap.aai.edges.enums.EdgeType;
46 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
47 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
48 import org.onap.aai.exceptions.AAIException;
49 import org.onap.aai.introspection.*;
50 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
51 import org.onap.aai.introspection.sideeffect.*;
52 import org.onap.aai.logging.ErrorLogHelper;
53 import org.onap.aai.logging.LogFormatTools;
54 import org.onap.aai.logging.LoggingContext;
55 import org.onap.aai.logging.StopWatch;
56 import org.onap.aai.parsers.query.QueryParser;
57 import org.onap.aai.parsers.uri.URIParser;
58 import org.onap.aai.parsers.uri.URIToRelationshipObject;
59 import org.onap.aai.query.builder.QueryBuilder;
60 import org.onap.aai.schema.enums.ObjectMetadata;
61 import org.onap.aai.schema.enums.PropertyMetadata;
62 import org.onap.aai.serialization.db.exceptions.MultipleEdgeRuleFoundException;
63 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
64 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
65 import org.onap.aai.serialization.engines.query.QueryEngine;
66 import org.onap.aai.setup.SchemaVersion;
67 import org.onap.aai.setup.SchemaVersions;
68 import org.onap.aai.util.AAIConfig;
69 import org.onap.aai.util.AAIConstants;
70 import org.onap.aai.workarounds.NamingExceptions;
71 import org.springframework.context.ApplicationContext;
72
73 import javax.ws.rs.core.UriBuilder;
74 import java.io.UnsupportedEncodingException;
75 import java.lang.reflect.Array;
76 import java.lang.reflect.InvocationTargetException;
77 import java.net.MalformedURLException;
78 import java.net.URI;
79 import java.net.URISyntaxException;
80 import java.util.*;
81 import java.util.concurrent.ExecutionException;
82 import java.util.concurrent.ExecutorService;
83 import java.util.concurrent.Future;
84 import java.util.regex.Matcher;
85 import java.util.regex.Pattern;
86
87 public class DBSerializer {
88
89     private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DBSerializer.class);
90
91     private static final String IMPLICIT_DELETE = "Implicit DELETE";
92
93     private static final String MISSING_REQUIRED_NODE_PROPERTY = "Vertex missing required aai-node-type property";
94
95     private final TransactionalGraphEngine engine;
96     private final String sourceOfTruth;
97     private final ModelType introspectionType;
98     private final SchemaVersion version;
99     private final Loader latestLoader;
100     private EdgeSerializer edgeSer;
101     private EdgeIngestor edgeRules;
102     private final Loader loader;
103     private final String baseURL;
104     private double dbTimeMsecs = 0;
105     private long currentTimeMillis;
106
107     private SchemaVersions schemaVersions;
108     private Set<String> namedPropNodes;
109     /**
110      * Instantiates a new DB serializer.
111      *
112      * @param version the version
113      * @param engine the engine
114      * @param introspectionType the introspection type
115      * @param sourceOfTruth the source of truth
116      * @throws AAIException
117      */
118     public DBSerializer(SchemaVersion version, TransactionalGraphEngine engine, ModelType introspectionType, String sourceOfTruth) throws AAIException {
119         this.engine = engine;
120         this.sourceOfTruth = sourceOfTruth;
121         this.introspectionType = introspectionType;
122         this.schemaVersions = SpringContextAware.getBean(SchemaVersions.class);
123         SchemaVersion LATEST = schemaVersions.getDefaultVersion();
124         this.latestLoader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectionType, LATEST);
125         this.version = version;
126         this.loader = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectionType, version);
127         this.namedPropNodes = this.latestLoader.getNamedPropNodes();
128         this.baseURL = AAIConfig.get(AAIConstants.AAI_SERVER_URL_BASE);
129         this.currentTimeMillis = System.currentTimeMillis();
130         initBeans();
131     }
132
133     private void initBeans() {
134         //TODO proper spring wiring, but that requires a lot of refactoring so for now we have this
135         ApplicationContext ctx = SpringContextAware.getApplicationContext();
136         EdgeIngestor ei = ctx.getBean(EdgeIngestor.class);
137         setEdgeIngestor(ei);
138         EdgeSerializer es = ctx.getBean(EdgeSerializer.class);
139         setEdgeSerializer(es);
140     }
141
142     private void backupESInit() {
143         setEdgeSerializer(new EdgeSerializer(this.edgeRules));
144     }
145
146     public void setEdgeSerializer(EdgeSerializer edgeSer) {
147         this.edgeSer = edgeSer;
148     }
149
150     public EdgeSerializer getEdgeSeriailizer() {
151         return this.edgeSer;
152     }
153
154     public void setEdgeIngestor(EdgeIngestor ei) {
155         this.edgeRules = ei;
156     }
157
158     public EdgeIngestor getEdgeIngestor() {
159         return this.edgeRules;
160     }
161
162     /**
163      * Touch standard vertex properties.
164      *
165      * @param v the v
166      * @param isNewVertex the is new vertex
167      */
168     public void touchStandardVertexProperties(Vertex v, boolean isNewVertex) {
169         String timeNowInSec = Long.toString(currentTimeMillis);
170
171         if (isNewVertex) {
172             v.property(AAIProperties.SOURCE_OF_TRUTH, this.sourceOfTruth);
173             v.property(AAIProperties.CREATED_TS, timeNowInSec);
174             v.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString());
175         }
176         v.property(AAIProperties.RESOURCE_VERSION, timeNowInSec);
177         v.property(AAIProperties.LAST_MOD_TS, timeNowInSec);
178         v.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, this.sourceOfTruth);
179
180     }
181
182     private void touchStandardVertexProperties(String nodeType, Vertex v, boolean isNewVertex) {
183
184         v.property(AAIProperties.NODE_TYPE, nodeType);
185         touchStandardVertexProperties(v, isNewVertex);
186
187     }
188
189
190     /**
191      * Creates the new vertex.
192      *
193      * @param wrappedObject the wrapped object
194      * @return the vertex
195      * @throws UnsupportedEncodingException the unsupported encoding exception
196      * @throws AAIException                 the AAI exception
197      */
198     public Vertex createNewVertex(Introspector wrappedObject) {
199         Vertex v;
200         try {
201             StopWatch.conditionalStart();
202             v = this.engine.tx().addVertex();
203             touchStandardVertexProperties(wrappedObject.getDbName(), v, true);
204         } finally {
205             dbTimeMsecs += StopWatch.stopIfStarted();
206         }
207         return v;
208     }
209
210     /**
211      * Trim class name.
212      *
213      * @param className the class name
214      * @return the string
215      */
216     /*
217      * Removes the classpath from a class name
218      */
219     public String trimClassName(String className) {
220         String returnValue = "";
221
222         if (className.lastIndexOf('.') == -1) {
223             return className;
224         }
225         returnValue = className.substring(className.lastIndexOf('.') + 1, className.length());
226
227         return returnValue;
228     }
229
230     /**
231      * Serialize to db.
232      *
233      * @param obj        the obj
234      * @param v          the v
235      * @param uriQuery   the uri query
236      * @param identifier the identifier
237      * @throws SecurityException            the security exception
238      * @throws IllegalAccessException       the illegal access exception
239      * @throws IllegalArgumentException     the illegal argument exception
240      * @throws InvocationTargetException    the invocation target exception
241      * @throws InstantiationException       the instantiation exception
242      * @throws InterruptedException         the interrupted exception
243      * @throws NoSuchMethodException        the no such method exception
244      * @throws AAIException                 the AAI exception
245      * @throws UnsupportedEncodingException the unsupported encoding exception
246      * @throws AAIUnknownObjectException
247      */
248     public void serializeToDb(Introspector obj, Vertex v, QueryParser uriQuery, String identifier, String requestContext) throws AAIException, UnsupportedEncodingException {
249         StopWatch.conditionalStart();
250         try {
251             if (uriQuery.isDependent()) {
252                 //try to find the parent
253                 List<Vertex> vertices = uriQuery.getQueryBuilder().getParentQuery().toList();
254                 if (!vertices.isEmpty()) {
255                     Vertex parent = vertices.get(0);
256                     this.reflectDependentVertex(parent, v, obj, requestContext);
257                 } else {
258                     dbTimeMsecs += StopWatch.stopIfStarted();
259                     throw new AAIException("AAI_6114", "No parent Node of type " + uriQuery.getParentResultType() + " for " + identifier);
260                 }
261             } else {
262                 serializeSingleVertex(v, obj, requestContext);
263             }
264
265         } catch (SchemaViolationException e) {
266             dbTimeMsecs += StopWatch.stopIfStarted();
267             throw new AAIException("AAI_6117", e);
268         }
269         dbTimeMsecs += StopWatch.stopIfStarted();
270     }
271
272     public void serializeSingleVertex(Vertex v, Introspector obj, String requestContext) throws UnsupportedEncodingException, AAIException {
273         StopWatch.conditionalStart();
274         try {
275             boolean isTopLevel = obj.isTopLevel();
276             if (isTopLevel) {
277                 addUriIfNeeded(v, obj.getURI());
278             }
279
280             processObject(obj, v, requestContext);
281             if (!isTopLevel) {
282                 URI uri = this.getURIForVertex(v);
283                 URIParser parser = new URIParser(this.loader, uri);
284                 if (parser.validate()) {
285                     addUriIfNeeded(v, uri.toString());
286                 }
287             }
288         } catch (SchemaViolationException e) {
289             throw new AAIException("AAI_6117", e);
290         } finally {
291             dbTimeMsecs += StopWatch.stopIfStarted();
292         }
293     }
294
295     private void addUriIfNeeded(Vertex v, String uri) {
296         VertexProperty<String> uriProp = v.property(AAIProperties.AAI_URI);
297         if (!uriProp.isPresent() || (uriProp.isPresent() && !uriProp.value().equals(uri))) {
298             v.property(AAIProperties.AAI_URI, uri);
299         }
300     }
301
302     /**
303      * Process object.
304      *
305      * @param <T> the generic type
306      * @param obj the obj
307      * @param v   the v
308      * @return the list
309      * @throws IllegalAccessException       the illegal access exception
310      * @throws IllegalArgumentException     the illegal argument exception
311      * @throws InvocationTargetException    the invocation target exception
312      * @throws InstantiationException       the instantiation exception
313      * @throws NoSuchMethodException        the no such method exception
314      * @throws SecurityException            the security exception
315      * @throws AAIException                 the AAI exception
316      * @throws UnsupportedEncodingException the unsupported encoding exception
317      * @throws AAIUnknownObjectException
318      */
319     /*
320      * Helper method for reflectToDb
321      * Handles all the property setting
322      */
323     private <T> List<Vertex> processObject(Introspector obj, Vertex v, String requestContext) throws UnsupportedEncodingException, AAIException {
324         Set<String> properties = new LinkedHashSet<>(obj.getProperties());
325         properties.remove(AAIProperties.RESOURCE_VERSION);
326         List<Vertex> dependentVertexes = new ArrayList<>();
327         List<Vertex> processedVertexes = new ArrayList<>();
328         boolean isComplexType = false;
329         boolean isListType = false;
330         if (!obj.isContainer()) {
331             this.touchStandardVertexProperties(obj.getDbName(), v, false);
332         }
333         this.executePreSideEffects(obj, v);
334         for (String property : properties) {
335             Object value = null;
336             final String propertyType;
337             propertyType = obj.getType(property);
338             isComplexType = obj.isComplexType(property);
339             isListType = obj.isListType(property);
340             value = obj.getValue(property);
341
342             if (!(isComplexType || isListType)) {
343                 boolean canModify = this.canModify(obj, property, requestContext);
344
345                 if (canModify) {
346                     final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(property);
347                     String dbProperty = property;
348                     if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
349                         dbProperty = metadata.get(PropertyMetadata.DB_ALIAS);
350                     }
351                     if (metadata.containsKey(PropertyMetadata.DATA_LINK)) {
352                         //data linked properties are ephemeral
353                         //they are populated dynamically on GETs
354                         continue;
355                     }
356                     if (value != null) {
357                         if (!value.equals(v.property(dbProperty).orElse(null))) {
358                             if (propertyType.toLowerCase().contains(".long")) {
359                                 v.property(dbProperty, new Integer(((Long) value).toString()));
360                             } else {
361                                 v.property(dbProperty, value);
362                             }
363                         }
364                     } else {
365                         v.property(dbProperty).remove();
366                     }
367                 }
368             } else if (isListType) {
369                 List<Object> list = (List<Object>) value;
370                 if (obj.isComplexGenericType(property)) {
371                     if (list != null) {
372                         for (Object o : list) {
373                             Introspector child = IntrospectorFactory.newInstance(this.introspectionType, o);
374                             child.setURIChain(obj.getURI());
375                             processedVertexes.add(reflectDependentVertex(v, child, requestContext));
376                         }
377                     }
378                 } else {
379                     //simple list case
380                     engine.setListProperty(v, property, list);
381                 }
382             } else {
383                 //method.getReturnType() is not 'simple' then create a vertex and edge recursively returning an edge back to this method
384                 if (value != null) { //effectively ignore complex properties not included in the object we're processing
385                     if (value.getClass().isArray()) {
386
387                         int length = Array.getLength(value);
388                         for (int i = 0; i < length; i++) {
389                             Object arrayElement = Array.get(value, i);
390                             Introspector child = IntrospectorFactory.newInstance(this.introspectionType, arrayElement);
391                             child.setURIChain(obj.getURI());
392                             processedVertexes.add(reflectDependentVertex(v, child, requestContext));
393
394                         }
395                     } else if (!property.equals("relationship-list")) {
396                         // container case
397                         Introspector introspector = IntrospectorFactory.newInstance(this.introspectionType, value);
398                         if (introspector.isContainer()) {
399                             dependentVertexes.addAll(this.engine.getQueryEngine().findChildrenOfType(v, introspector.getChildDBName()));
400                             introspector.setURIChain(obj.getURI());
401
402                             processedVertexes.addAll(processObject(introspector, v, requestContext));
403
404                         } else {
405                             dependentVertexes.addAll(this.engine.getQueryEngine().findChildrenOfType(v, introspector.getDbName()));
406                             processedVertexes.add(reflectDependentVertex(v, introspector, requestContext));
407
408                         }
409                     } else if (property.equals("relationship-list")) {
410                         handleRelationships(obj, v);
411                     }
412                 }
413             }
414         }
415         this.writeThroughDefaults(v, obj);
416         /* handle those vertexes not touched */
417         for (Vertex toBeRemoved : processedVertexes) {
418             dependentVertexes.remove(toBeRemoved);
419         }
420
421         // If the dependent vertices are not empty, then with
422         // the current behaviour, it should remove the vertices implicitly
423         // We are updating the code to properly log which call
424         // is doing this so the SE can work with the clients making the call to
425         // tell them not to call this API and can hopefully deprecate this
426         // functionality in the future releases
427         if(!dependentVertexes.isEmpty()){
428
429             LoggingContext.responseDescription(IMPLICIT_DELETE);
430
431             // Find all the deletable vertices from the dependent vertices that should be deleted
432             // So for each of the following dependent vertices,
433             // we will use the edge properties and do the cascade delete
434             List<Vertex> impliedDeleteVertices = this.engine.getQueryEngine().findDeletable(dependentVertexes);
435             int impliedDeleteCount = impliedDeleteVertices.size();
436
437             LOGGER.warn(
438                 "For the vertex with id {}, doing an implicit delete on update will delete total of {} vertexes",
439                 v.id(),
440                 impliedDeleteCount
441             );
442
443             String impliedDeleteLogEnabled = AAIConfig.get(AAIConstants.AAI_IMPLIED_DELETE_LOG_ENABLED, "true");
444
445             int impliedDeleteLogLimit = AAIConfig.getInt(AAIConstants.AAI_IMPLIED_DELETE_LOG_LIMIT, "-1");
446
447             if(impliedDeleteLogLimit == -1){
448                 impliedDeleteLogLimit = Integer.MAX_VALUE;
449             }
450
451             // If the logging is enabled for implied delete
452             // then log the payload in the latest format
453             if("true".equals(impliedDeleteLogEnabled) &&
454                 impliedDeleteCount <= impliedDeleteLogLimit){
455                 for(Vertex vertex : impliedDeleteVertices){
456                     Introspector introspector = null;
457                     try {
458                         introspector = getLatestVersionView(vertex);
459                         if(LOGGER.isInfoEnabled()){
460                             LOGGER.info("Implied delete object in json format {}", introspector.marshal(false));
461                         }
462                     } catch(Exception ex){
463                         LOGGER.warn("Encountered an exception during retrieval of vertex properties with vertex-id {} -> {}", v.id(), LogFormatTools.getStackTop(ex));
464                     }
465                 }
466             }
467
468             // After all the appropriate logging, calling the delete to delete the affected vertices
469             this.delete(impliedDeleteVertices);
470         }
471
472         this.executePostSideEffects(obj, v);
473         return processedVertexes;
474     }
475
476     /**
477      * Handle relationships.
478      *
479      * @param obj    the obj
480      * @param vertex the vertex
481      * @throws SecurityException            the security exception
482      * @throws IllegalAccessException       the illegal access exception
483      * @throws IllegalArgumentException     the illegal argument exception
484      * @throws InvocationTargetException    the invocation target exception
485      * @throws UnsupportedEncodingException the unsupported encoding exception
486      * @throws AAIException                 the AAI exception
487      */
488     /*
489      * Handles the explicit relationships defined for an obj
490      */
491     private void handleRelationships(Introspector obj, Vertex vertex) throws UnsupportedEncodingException, AAIException {
492
493
494         Introspector wrappedRl = obj.getWrappedValue("relationship-list");
495         processRelationshipList(wrappedRl, vertex);
496
497
498     }
499
500
501     /**
502      * Process relationship list.
503      *
504      * @param wrapped the wrapped
505      * @param v       the v
506      * @throws UnsupportedEncodingException the unsupported encoding exception
507      * @throws AAIException                 the AAI exception
508      */
509     private void processRelationshipList(Introspector wrapped, Vertex v) throws UnsupportedEncodingException, AAIException {
510
511         List<Object> relationships = (List<Object>) wrapped.getValue("relationship");
512
513         List<Triplet<Vertex, Vertex, String>> addEdges = new ArrayList<>();
514         List<Edge> existingEdges = this.engine.getQueryEngine().findEdgesForVersion(v, wrapped.getLoader());
515
516         for (Object relationship : relationships) {
517             Edge e = null;
518             Vertex cousinVertex = null;
519             String label = null;
520             Introspector wrappedRel = IntrospectorFactory.newInstance(this.introspectionType, relationship);
521             QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(wrappedRel);
522
523             if (wrappedRel.hasProperty("relationship-label")) {
524                 label = wrappedRel.getValue("relationship-label");
525             }
526
527             List<Vertex> results = parser.getQueryBuilder().toList();
528             if (results.isEmpty()) {
529                 final AAIException ex = new AAIException("AAI_6129", "Node of type " + parser.getResultType() + ". Could not find object at: " + parser.getUri());
530                 ex.getTemplateVars().add(parser.getResultType());
531                 ex.getTemplateVars().add(parser.getUri().toString());
532                 throw ex;
533             } else {
534                 //still an issue if there's more than one
535                 cousinVertex = results.get(0);
536             }
537
538             if (cousinVertex != null) {
539                 String vType = (String) v.property(AAIProperties.NODE_TYPE).value();
540                 String cousinType = (String) cousinVertex.property(AAIProperties.NODE_TYPE).value();
541                 EdgeRuleQuery.Builder baseQ = new EdgeRuleQuery.Builder(vType, cousinType).label(label);
542
543
544                 if (!edgeRules.hasRule(baseQ.build())) {
545                     throw new AAIException("AAI_6120", "No EdgeRule found for passed nodeTypes: " + v.property(AAIProperties.NODE_TYPE).value().toString() + ", "
546                         + cousinVertex.property(AAIProperties.NODE_TYPE).value().toString() + (label != null ? (" with label " + label) : "") + ".");
547                 } else if (edgeRules.hasRule(baseQ.edgeType(EdgeType.TREE).build()) && !edgeRules.hasRule(baseQ.edgeType(EdgeType.COUSIN).build())) {
548                     throw new AAIException("AAI_6145");
549                 }
550
551                 e = this.getEdgeBetween(EdgeType.COUSIN, v, cousinVertex, label);
552
553                 if (e == null) {
554                     addEdges.add(new Triplet<>(v, cousinVertex, label));
555                 } else {
556                     existingEdges.remove(e);
557                 }
558             }
559         }
560
561         for (Edge edge : existingEdges) {
562             edge.remove();
563         }
564         for (Triplet<Vertex, Vertex, String> triplet : addEdges) {
565             try {
566                 edgeSer.addEdge(this.engine.asAdmin().getTraversalSource(), triplet.getValue0(), triplet.getValue1(), triplet.getValue2());
567             } catch (NoEdgeRuleFoundException e) {
568                 throw new AAIException("AAI_6129", e);
569             }
570         }
571
572     }
573
574     /**
575      * Write through defaults.
576      *
577      * @param v   the v
578      * @param obj the obj
579      * @throws AAIUnknownObjectException
580      */
581     private void writeThroughDefaults(Vertex v, Introspector obj) throws AAIUnknownObjectException {
582         Introspector latest = this.latestLoader.introspectorFromName(obj.getName());
583         if (latest != null) {
584             Set<String> required = latest.getRequiredProperties();
585
586             for (String field : required) {
587                 String defaultValue = null;
588                 Object vertexProp = null;
589                 defaultValue = latest.getPropertyMetadata(field).get(PropertyMetadata.DEFAULT_VALUE);
590                 if (defaultValue != null) {
591                     vertexProp = v.<Object>property(field).orElse(null);
592                     if (vertexProp == null) {
593                         v.property(field, defaultValue);
594                     }
595                 }
596             }
597         }
598
599     }
600
601
602     /**
603      * Reflect dependent vertex.
604      *
605      * @param v            the v
606      * @param dependentObj the dependent obj
607      * @return the vertex
608      * @throws IllegalAccessException       the illegal access exception
609      * @throws IllegalArgumentException     the illegal argument exception
610      * @throws InvocationTargetException    the invocation target exception
611      * @throws InstantiationException       the instantiation exception
612      * @throws NoSuchMethodException        the no such method exception
613      * @throws SecurityException            the security exception
614      * @throws AAIException                 the AAI exception
615      * @throws UnsupportedEncodingException the unsupported encoding exception
616      * @throws AAIUnknownObjectException
617      */
618     private Vertex reflectDependentVertex(Vertex v, Introspector dependentObj, String requestContext) throws AAIException, UnsupportedEncodingException {
619
620         //QueryParser p = this.engine.getQueryBuilder().createQueryFromURI(obj.getURI());
621         //List<Vertex> items = p.getQuery().toList();
622         QueryBuilder<Vertex> query = this.engine.getQueryBuilder(v);
623         query.createEdgeTraversal(EdgeType.TREE, v, dependentObj);
624         query.createKeyQuery(dependentObj);
625
626         List<Vertex> items = query.toList();
627
628         Vertex dependentVertex = null;
629         if (items.size() == 1) {
630             dependentVertex = items.get(0);
631             this.verifyResourceVersion("update", dependentObj.getDbName(), dependentVertex.<String>property(AAIProperties.RESOURCE_VERSION).orElse(null), (String) dependentObj.getValue(AAIProperties.RESOURCE_VERSION), (String) dependentObj.getURI());
632         } else {
633             this.verifyResourceVersion("create", dependentObj.getDbName(), "", (String) dependentObj.getValue(AAIProperties.RESOURCE_VERSION), (String) dependentObj.getURI());
634             dependentVertex = createNewVertex(dependentObj);
635         }
636
637         return reflectDependentVertex(v, dependentVertex, dependentObj, requestContext);
638
639     }
640
641     /**
642      * Reflect dependent vertex.
643      *
644      * @param parent the parent
645      * @param child  the child
646      * @param obj    the obj
647      * @return the vertex
648      * @throws IllegalAccessException       the illegal access exception
649      * @throws IllegalArgumentException     the illegal argument exception
650      * @throws InvocationTargetException    the invocation target exception
651      * @throws InstantiationException       the instantiation exception
652      * @throws NoSuchMethodException        the no such method exception
653      * @throws SecurityException            the security exception
654      * @throws AAIException                 the AAI exception
655      * @throws UnsupportedEncodingException the unsupported encoding exception
656      * @throws AAIUnknownObjectException
657      */
658     private Vertex reflectDependentVertex(Vertex parent, Vertex child, Introspector obj, String requestContext) throws AAIException, UnsupportedEncodingException {
659
660         String parentUri = parent.<String>property(AAIProperties.AAI_URI).orElse(null);
661         if (parentUri != null) {
662             String uri;
663             uri = obj.getURI();
664             addUriIfNeeded(child, parentUri + uri);
665         }
666         processObject(obj, child, requestContext);
667
668         Edge e;
669         e = this.getEdgeBetween(EdgeType.TREE, parent, child, null);
670
671         if (e == null) {
672             String canBeLinked = obj.getMetadata(ObjectMetadata.CAN_BE_LINKED);
673             if (canBeLinked != null && canBeLinked.equals("true")) {
674                 Loader ldrForCntxt = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectionType, getVerForContext(requestContext));
675                 boolean isFirst = !this.engine.getQueryBuilder(ldrForCntxt, parent).createEdgeTraversal(EdgeType.TREE, parent, obj).hasNext();
676                 if (isFirst) {
677                     child.property(AAIProperties.LINKED, true);
678                 }
679             }
680             edgeSer.addTreeEdge(this.engine.asAdmin().getTraversalSource(), parent, child);
681         }
682         return child;
683
684     }
685
686     private SchemaVersion getVerForContext(String requestContext) {
687         Pattern pattern = Pattern.compile("v[0-9]+");
688         Matcher m = pattern.matcher(requestContext);
689         if (!m.find()) {
690             return this.version;
691         } else {
692             return new SchemaVersion(requestContext);
693         }
694     }
695
696     /**
697      * Db to object.
698      *
699      * @param vertices the vertices
700      * @param obj      the obj
701      * @param depth    the depth
702      * @param cleanUp  the clean up
703      * @return the introspector
704      * @throws AAIException                 the AAI exception
705      * @throws IllegalAccessException       the illegal access exception
706      * @throws IllegalArgumentException     the illegal argument exception
707      * @throws InvocationTargetException    the invocation target exception
708      * @throws SecurityException            the security exception
709      * @throws InstantiationException       the instantiation exception
710      * @throws NoSuchMethodException        the no such method exception
711      * @throws UnsupportedEncodingException the unsupported encoding exception
712      * @throws MalformedURLException        the malformed URL exception
713      * @throws AAIUnknownObjectException
714      * @throws URISyntaxException
715      */
716     public Introspector dbToObject(List<Vertex> vertices, final Introspector obj, int depth, boolean nodeOnly, String cleanUp) throws UnsupportedEncodingException, AAIException {
717         final int internalDepth;
718         if (depth == Integer.MAX_VALUE) {
719             internalDepth = depth--;
720         } else {
721             internalDepth = depth;
722         }
723         StopWatch.conditionalStart();
724         if (vertices.size() > 1 && !obj.isContainer()) {
725             dbTimeMsecs += StopWatch.stopIfStarted();
726             throw new AAIException("AAI_6136", "query object mismatch: this object cannot hold multiple items." + obj.getDbName());
727         } else if (obj.isContainer()) {
728             final List getList;
729             String listProperty = null;
730             for (String property : obj.getProperties()) {
731                 if (obj.isListType(property) && obj.isComplexGenericType(property)) {
732                     listProperty = property;
733                     break;
734                 }
735             }
736             final String propertyName = listProperty;
737             getList = (List) obj.getValue(listProperty);
738
739             /* This is an experimental multithreading experiment
740              * on get alls.
741              */
742             ExecutorService pool = GetAllPool.getInstance().getPool();
743
744             List<Future<Object>> futures = new ArrayList<>();
745
746             QueryEngine tgEngine = this.engine.getQueryEngine();
747             for (Vertex v : vertices) {
748
749                 AaiCallable<Object> task = new AaiCallable<Object>() {
750                     @Override
751                     public Object process() throws UnsupportedEncodingException, AAIException {
752                         Set<Vertex> seen = new HashSet<>();
753                         Introspector childObject;
754                         try {
755                             childObject = obj.newIntrospectorInstanceOfNestedProperty(propertyName);
756                         } catch (AAIUnknownObjectException e) {
757                             throw e;
758                         }
759                         try {
760                             dbToObject(childObject, v, seen, internalDepth, nodeOnly, cleanUp);
761                         } catch (UnsupportedEncodingException e) {
762                             throw e;
763                         } catch (AAIException e) {
764                             throw e;
765                         }
766                         return childObject.getUnderlyingObject();
767                         //getList.add(childObject.getUnderlyingObject());
768                     }
769                 };
770                 futures.add(pool.submit(task));
771             }
772
773             for (Future<Object> future : futures) {
774                 try {
775                     getList.add(future.get());
776                 } catch (ExecutionException e) {
777                     dbTimeMsecs += StopWatch.stopIfStarted();
778                     throw new AAIException("AAI_4000", e);
779                 } catch (InterruptedException e) {
780                     dbTimeMsecs += StopWatch.stopIfStarted();
781                     throw new AAIException("AAI_4000", e);
782                 }
783             }
784         } else if (vertices.size() == 1) {
785             Set<Vertex> seen = new HashSet<>();
786             dbToObject(obj, vertices.get(0), seen, depth, nodeOnly, cleanUp);
787         } else {
788             //obj = null;
789         }
790
791         dbTimeMsecs += StopWatch.stopIfStarted();
792         return obj;
793     }
794
795     /**
796      * Db to object.
797      *
798      * @param obj     the obj
799      * @param v       the v
800      * @param seen    the seen
801      * @param depth   the depth
802      * @param cleanUp the clean up
803      * @return the introspector
804      * @throws IllegalAccessException       the illegal access exception
805      * @throws IllegalArgumentException     the illegal argument exception
806      * @throws InvocationTargetException    the invocation target exception
807      * @throws SecurityException            the security exception
808      * @throws InstantiationException       the instantiation exception
809      * @throws NoSuchMethodException        the no such method exception
810      * @throws UnsupportedEncodingException the unsupported encoding exception
811      * @throws AAIException                 the AAI exception
812      * @throws MalformedURLException        the malformed URL exception
813      * @throws AAIUnknownObjectException
814      * @throws URISyntaxException
815      */
816     private Introspector dbToObject(Introspector obj, Vertex v, Set<Vertex> seen, int depth, boolean nodeOnly, String cleanUp) throws AAIException, UnsupportedEncodingException {
817
818         if (depth < 0) {
819             return null;
820         }
821         depth--;
822         seen.add(v);
823
824         boolean modified = false;
825         for (String property : obj.getProperties(PropertyPredicates.isVisible())) {
826             List<Object> getList = null;
827             Vertex[] vertices = null;
828
829             if (!(obj.isComplexType(property) || obj.isListType(property))) {
830                 this.copySimpleProperty(property, obj, v);
831                 modified = true;
832             } else {
833                 if (obj.isComplexType(property)) {
834                     /* container case */
835
836                     if (!property.equals("relationship-list") && depth >= 0) {
837                         Introspector argumentObject = obj.newIntrospectorInstanceOfProperty(property);
838                         Object result = dbToObject(argumentObject, v, seen, depth + 1, nodeOnly, cleanUp);
839                         if (result != null) {
840                             obj.setValue(property, argumentObject.getUnderlyingObject());
841                             modified = true;
842                         }
843                     } else if (property.equals("relationship-list") && !nodeOnly) {
844                         /* relationships need to be handled correctly */
845                         Introspector relationshipList = obj.newIntrospectorInstanceOfProperty(property);
846                         relationshipList = createRelationshipList(v, relationshipList, cleanUp);
847                         if (relationshipList != null) {
848                             modified = true;
849                             obj.setValue(property, relationshipList.getUnderlyingObject());
850                             modified = true;
851                         }
852
853                     }
854                 } else if (obj.isListType(property)) {
855
856                     if (property.equals("any")) {
857                         continue;
858                     }
859                     String genericType = obj.getGenericTypeClass(property).getSimpleName();
860                     if (obj.isComplexGenericType(property) && depth >= 0) {
861                         final String childDbName = convertFromCamelCase(genericType);
862                         String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
863                         EdgeRule rule;
864
865                         try {
866                             rule = edgeRules.getRule(new EdgeRuleQuery.Builder(vType, childDbName).edgeType(EdgeType.TREE).build());
867                         } catch (EdgeRuleNotFoundException e) {
868                             throw new NoEdgeRuleFoundException(e);
869                         } catch (AmbiguousRuleChoiceException e) {
870                             throw new MultipleEdgeRuleFoundException(e);
871                         }
872                         if (!rule.getContains().equals(AAIDirection.NONE.toString())) {
873                             //vertices = this.queryEngine.findRelatedVertices(v, Direction.OUT, rule.getLabel(), childDbName);
874                             Direction ruleDirection = rule.getDirection();
875                             Iterator<Vertex> itr = v.vertices(ruleDirection, rule.getLabel());
876                             List<Vertex> verticesList = (List<Vertex>) IteratorUtils.toList(itr);
877                             itr = verticesList.stream().filter(item -> {
878                                 return item.property(AAIProperties.NODE_TYPE).orElse("").equals(childDbName);
879                             }).iterator();
880                             if (itr.hasNext()) {
881                                 getList = (List<Object>) obj.getValue(property);
882                             }
883                             int processed = 0;
884                             int removed = 0;
885                             while (itr.hasNext()) {
886                                 Vertex childVertex = itr.next();
887                                 if (!seen.contains(childVertex)) {
888                                     Introspector argumentObject = obj.newIntrospectorInstanceOfNestedProperty(property);
889
890                                     Object result = dbToObject(argumentObject, childVertex, seen, depth, nodeOnly, cleanUp);
891                                     if (result != null) {
892                                         getList.add(argumentObject.getUnderlyingObject());
893                                     }
894
895                                     processed++;
896                                 } else {
897                                     removed++;
898                                     LOGGER.warn("Cycle found while serializing vertex id={}", childVertex.id().toString());
899                                 }
900                             }
901                             if (processed == 0) {
902                                 //vertices were all seen, reset the list
903                                 getList = null;
904                             }
905                             if (processed > 0) {
906                                 modified = true;
907                             }
908                         }
909                     } else if (obj.isSimpleGenericType(property)) {
910                         List<Object> temp = this.engine.getListProperty(v, property);
911                         if (temp != null) {
912                             getList = (List<Object>) obj.getValue(property);
913                             getList.addAll(temp);
914                             modified = true;
915                         }
916
917                     }
918
919                 }
920
921             }
922         }
923
924         //no changes were made to this obj, discard the instance
925         if (!modified) {
926             return null;
927         }
928         this.enrichData(obj, v);
929         return obj;
930
931     }
932
933
934     public Introspector getVertexProperties(Vertex v) throws AAIException, UnsupportedEncodingException {
935         String nodeType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
936         if (nodeType == null) {
937             throw new AAIException("AAI_6143");
938         }
939
940         Introspector obj = this.latestLoader.introspectorFromName(nodeType);
941         Set<Vertex> seen = new HashSet<>();
942         int depth = 0;
943         String cleanUp = "false";
944         boolean nodeOnly = true;
945         StopWatch.conditionalStart();
946         this.dbToObject(obj, v, seen, depth, nodeOnly, cleanUp);
947         dbTimeMsecs += StopWatch.stopIfStarted();
948         return obj;
949
950     }
951
952     public Introspector getLatestVersionView(Vertex v) throws AAIException, UnsupportedEncodingException {
953         String nodeType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
954         if (nodeType == null) {
955             throw new AAIException("AAI_6143");
956         }
957         Introspector obj = this.latestLoader.introspectorFromName(nodeType);
958         Set<Vertex> seen = new HashSet<>();
959         int depth = AAIProperties.MAXIMUM_DEPTH;
960         String cleanUp = "false";
961         boolean nodeOnly = false;
962         StopWatch.conditionalStart();
963         this.dbToObject(obj, v, seen, depth, nodeOnly, cleanUp);
964         dbTimeMsecs += StopWatch.stopIfStarted();
965         return obj;
966     }
967
968     /**
969      * Copy simple property.
970      *
971      * @param property the property
972      * @param obj      the obj
973      * @param v        the v
974      * @throws InstantiationException    the instantiation exception
975      * @throws IllegalAccessException    the illegal access exception
976      * @throws IllegalArgumentException  the illegal argument exception
977      * @throws InvocationTargetException the invocation target exception
978      * @throws NoSuchMethodException     the no such method exception
979      * @throws SecurityException         the security exception
980      */
981     private void copySimpleProperty(String property, Introspector obj, Vertex v) {
982         final Object temp = getProperty(obj, property, v);
983         if (temp != null) {
984             obj.setValue(property, temp);
985         }
986     }
987
988
989     /**
990      * Load the introspector from the hashmap for the given property key
991      *
992      * @param property - vertex property
993      * @param obj      - introspector object representing the vertex
994      * @param hashMap  - Containing a list of pre-fetched properties for a given vertex
995      */
996     private void copySimplePropertyFromHashMap(String property, Introspector obj, Map<String, Object> hashMap){
997
998         final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(property);
999         String dbPropertyName = property;
1000
1001         if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
1002             dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
1003         }
1004
1005         final Object temp = hashMap.getOrDefault(dbPropertyName, null);
1006
1007         if (temp != null) {
1008             obj.setValue(property, temp);
1009         }
1010     }
1011
1012     /**
1013      * Simple db to object.
1014      *
1015      * @param obj the obj
1016      * @param v   the v
1017      * @throws InstantiationException    the instantiation exception
1018      * @throws IllegalAccessException    the illegal access exception
1019      * @throws IllegalArgumentException  the illegal argument exception
1020      * @throws InvocationTargetException the invocation target exception
1021      * @throws NoSuchMethodException     the no such method exception
1022      * @throws SecurityException         the security exception
1023      */
1024     private void simpleDbToObject(Introspector obj, Vertex v) {
1025         for(String key : obj.getProperties()){
1026             this.copySimpleProperty(key, obj, v);
1027         }
1028     }
1029
1030
1031     public Map<String, Object> convertVertexToHashMap(Introspector obj, Vertex v){
1032
1033         long startTime = System.currentTimeMillis();
1034
1035         Set<String> simpleProperties = obj.getSimpleProperties(PropertyPredicates.isVisible());
1036         String[] simplePropsArray    = new String[simpleProperties.size()];
1037         simplePropsArray             = simpleProperties.toArray(simplePropsArray);
1038
1039         Map<String, Object> simplePropsHashMap = new HashMap<>(simplePropsArray.length * 2);
1040
1041         v.properties(simplePropsArray).forEachRemaining((vp) -> simplePropsHashMap.put(vp.key(), vp.value()));
1042
1043         return simplePropsHashMap;
1044     }
1045
1046     public Introspector dbToRelationshipObject(Vertex v) throws UnsupportedEncodingException, AAIException {
1047         Introspector relationshipList = this.latestLoader.introspectorFromName("relationship-list");
1048         relationshipList =  createRelationshipList(v, relationshipList, "false");
1049         return relationshipList;
1050     }
1051     /**
1052      * Creates the relationship list.
1053      *
1054      * @param v       the v
1055      * @param obj     the obj
1056      * @param cleanUp the clean up
1057      * @return the object
1058      * @throws InstantiationException       the instantiation exception
1059      * @throws IllegalAccessException       the illegal access exception
1060      * @throws IllegalArgumentException     the illegal argument exception
1061      * @throws InvocationTargetException    the invocation target exception
1062      * @throws NoSuchMethodException        the no such method exception
1063      * @throws SecurityException            the security exception
1064      * @throws UnsupportedEncodingException the unsupported encoding exception
1065      * @throws AAIException                 the AAI exception
1066      * @throws MalformedURLException        the malformed URL exception
1067      * @throws URISyntaxException
1068      */
1069     private Introspector createRelationshipList(Vertex v, Introspector obj, String cleanUp) throws UnsupportedEncodingException, AAIException {
1070
1071         String[] cousinRules = new String[0];
1072
1073         try {
1074             cousinRules = edgeRules.retrieveCachedCousinLabels(obj.getDbName());
1075         } catch (ExecutionException e) {
1076             LOGGER.warn("Encountered an execution exception while retrieving labels for the node type {} using cached", obj.getDbName(), e);
1077         }
1078
1079         List<Vertex> cousins = null;
1080         if(cousinRules != null && cousinRules.length != 0){
1081             cousins = this.engine.getQueryEngine().findCousinVertices(v, cousinRules);
1082         } else {
1083             cousins = this.engine.getQueryEngine().findCousinVertices(v);
1084         }
1085
1086         List<Object> relationshipObjList = obj.getValue("relationship");
1087         VertexProperty nodeTypeProperty = v.property(AAIProperties.NODE_TYPE);
1088
1089         if(!nodeTypeProperty.isPresent()){
1090             LoggingContext.responseDescription(MISSING_REQUIRED_NODE_PROPERTY);
1091             LOGGER.warn("Not processing the vertex {} because its missing required property aai-node-type", v.id());
1092             LoggingContext.remove(LoggingContext.LoggingField.RESPONSE_DESCRIPTION.toString());
1093             return null;
1094         }
1095
1096         String aNodeType = nodeTypeProperty.value().toString();
1097
1098         TypeAlphabetizer alphabetizer = new TypeAlphabetizer();
1099
1100         EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
1101         Set<String> keysWithMultipleLabels = edgeIngestor.getMultipleLabelKeys();
1102
1103         // For the given vertex, find all the cousins
1104         // For each cousin retrieve the node type and then
1105         // check if the version is greater than the edge label version
1106         // meaning is the current version equal to greater than the version
1107         // where we introduced the edge labels into the relationship payload
1108         // If it is, then we check if the edge key there are multiple labels
1109         // If there are multiple labels, then we need to go to the database
1110         // to retrieve the labels between itself and cousin vertex
1111         // If there is only single label between the edge a and b, then
1112         // we can retrieve what that is without going to the database
1113         // from using the edge rules json and get the edge rule out of it
1114         EdgeRuleQuery.Builder queryBuilder = new EdgeRuleQuery.Builder(aNodeType);
1115         for (Vertex cousin : cousins) {
1116             VertexProperty vertexProperty = cousin.property(AAIProperties.NODE_TYPE);
1117             String bNodeType = null;
1118             if(vertexProperty.isPresent()){
1119                 bNodeType = cousin.property(AAIProperties.NODE_TYPE).value().toString();
1120             } else {
1121                 // If the vertex is missing the aai-node-type
1122                 // Then its either a bad vertex or its in the process
1123                 // of getting deleted so we should ignore these vertexes
1124                 LoggingContext.responseDescription(MISSING_REQUIRED_NODE_PROPERTY);
1125                 if(LOGGER.isDebugEnabled()){
1126                     LOGGER.debug("For the vertex {}, unable to retrieve the aai-node-type", v.id().toString());
1127                 } else {
1128                     LOGGER.info("Unable to retrieve the aai-node-type for vertex, for more info enable debug log");
1129                 }
1130                 LoggingContext.remove(LoggingContext.LoggingField.RESPONSE_DESCRIPTION.toString());
1131                 continue;
1132             }
1133             if (obj.getVersion().compareTo(schemaVersions.getEdgeLabelVersion()) >= 0) {
1134                 String edgeKey = alphabetizer.buildAlphabetizedKey(aNodeType, bNodeType);
1135                 if(keysWithMultipleLabels.contains(edgeKey)){
1136                     List<String> edgeLabels = this.getEdgeLabelsBetween(EdgeType.COUSIN, v, cousin);
1137                     for(String edgeLabel: edgeLabels){
1138                         Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
1139                         Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, edgeLabel);
1140                         if (result != null) {
1141                             relationshipObjList.add(result);
1142                         }
1143                     }
1144                 } else {
1145
1146                     EdgeRule edgeRule = null;
1147
1148                     // Create a query based on the a nodetype and b nodetype
1149                     // which is also a cousin edge and ensure the version
1150                     // is used properly so for example in order to be backwards
1151                     // compatible if we had allowed a edge between a and b
1152                     // in a previous release and we decided to remove it from
1153                     // the edge rules in the future we can display the edge
1154                     // only for the older apis and the new apis if the edge rule
1155                     // is removed will not be seen in the newer version of the API
1156
1157                     EdgeRuleQuery ruleQuery = queryBuilder
1158                         .to(bNodeType)
1159                         .edgeType(EdgeType.COUSIN)
1160                         .version(obj.getVersion())
1161                         .build();
1162
1163                     try {
1164                         edgeRule = edgeIngestor.getRule(ruleQuery);
1165                     } catch (EdgeRuleNotFoundException e) {
1166                         LOGGER.warn("Caught an edge rule not found exception for query {}, {}," +
1167                             " it could be the edge rule is no longer valid for the existing edge in db",
1168                             ruleQuery, LogFormatTools.getStackTop(e));
1169                         continue;
1170                     } catch (AmbiguousRuleChoiceException e) {
1171                         LOGGER.error("Caught an ambiguous rule not found exception for query {}, {}",
1172                             ruleQuery, LogFormatTools.getStackTop(e));
1173                         continue;
1174                     }
1175
1176                     Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
1177                     Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp,edgeRule.getLabel());
1178                     if (result != null) {
1179                         relationshipObjList.add(result);
1180                     }
1181                 }
1182             } else {
1183                 Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
1184                 Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, null);
1185                 if (result != null) {
1186                     relationshipObjList.add(result);
1187                 }
1188             }
1189
1190         }
1191
1192         if (relationshipObjList.isEmpty()) {
1193             return null;
1194         } else {
1195             return obj;
1196         }
1197     }
1198
1199     /**
1200      * Process edge relationship.
1201      *
1202      * @param relationshipObj the relationship obj
1203      * @param edge            the edge
1204      * @param cleanUp         the clean up
1205      * @return the object
1206      * @throws InstantiationException       the instantiation exception
1207      * @throws IllegalAccessException       the illegal access exception
1208      * @throws IllegalArgumentException     the illegal argument exception
1209      * @throws InvocationTargetException    the invocation target exception
1210      * @throws NoSuchMethodException        the no such method exception
1211      * @throws SecurityException            the security exception
1212      * @throws UnsupportedEncodingException the unsupported encoding exception
1213      * @throws AAIException                 the AAI exception
1214      * @throws MalformedURLException        the malformed URL exception
1215      * @throws AAIUnknownObjectException
1216      * @throws URISyntaxException
1217      */
1218     private Object processEdgeRelationship(Introspector relationshipObj, Vertex cousin, String cleanUp, String edgeLabel) throws UnsupportedEncodingException, AAIUnknownObjectException {
1219
1220         VertexProperty aaiUriProperty = cousin.property("aai-uri");
1221
1222         if(!aaiUriProperty.isPresent()){
1223             return null;
1224         }
1225
1226         URI uri = UriBuilder.fromUri(aaiUriProperty.value().toString()).build();
1227
1228         URIToRelationshipObject uriParser = null;
1229         Introspector result = null;
1230         try {
1231             uriParser = new URIToRelationshipObject(relationshipObj.getLoader(), uri, this.baseURL);
1232             result = uriParser.getResult();
1233         } catch (AAIException | URISyntaxException e) {
1234             LOGGER.error("Error while processing edge relationship in version " + relationshipObj.getVersion() + " (bad vertex ID=" + ": "
1235                 + e.getMessage() + " " + LogFormatTools.getStackTop(e));
1236             return null;
1237         }
1238
1239         VertexProperty cousinVertexNodeType = cousin.property(AAIProperties.NODE_TYPE);
1240
1241         if(cousinVertexNodeType.isPresent()){
1242             String cousinType = cousinVertexNodeType.value().toString();
1243             if(namedPropNodes.contains(cousinType)){
1244                 this.addRelatedToProperty(result, cousin, cousinType);
1245             }
1246         }
1247
1248         if (edgeLabel != null && result.hasProperty("relationship-label")) {
1249             result.setValue("relationship-label", edgeLabel);
1250         }
1251
1252         return result.getUnderlyingObject();
1253     }
1254
1255     /**
1256      * Gets the URI for vertex.
1257      *
1258      * @param v the v
1259      * @return the URI for vertex
1260      * @throws InstantiationException       the instantiation exception
1261      * @throws IllegalAccessException       the illegal access exception
1262      * @throws IllegalArgumentException     the illegal argument exception
1263      * @throws InvocationTargetException    the invocation target exception
1264      * @throws NoSuchMethodException        the no such method exception
1265      * @throws SecurityException            the security exception
1266      * @throws UnsupportedEncodingException the unsupported encoding exception
1267      * @throws AAIUnknownObjectException
1268      */
1269     public URI getURIForVertex(Vertex v) throws UnsupportedEncodingException {
1270
1271         return getURIForVertex(v, false);
1272     }
1273
1274     public URI getURIForVertex(Vertex v, boolean overwrite) throws UnsupportedEncodingException {
1275         URI uri = UriBuilder.fromPath("/unknown-uri").build();
1276
1277         String aaiUri = v.<String>property(AAIProperties.AAI_URI).orElse(null);
1278
1279         if (aaiUri != null && !overwrite) {
1280             uri = UriBuilder.fromPath(aaiUri).build();
1281         }
1282
1283         return uri;
1284     }
1285
1286     /**
1287      * Gets the URI from list.
1288      *
1289      * @param list the list
1290      * @return the URI from list
1291      * @throws UnsupportedEncodingException the unsupported encoding exception
1292      */
1293     private URI getURIFromList(List<Introspector> list) throws UnsupportedEncodingException {
1294         String uri = "";
1295         StringBuilder sb = new StringBuilder();
1296         for (Introspector i : list) {
1297             sb.insert(0, i.getURI());
1298         }
1299
1300         uri = sb.toString();
1301         return UriBuilder.fromPath(uri).build();
1302     }
1303
1304     public void addRelatedToProperty(Introspector relationship, Vertex cousinVertex, String cousinType) throws AAIUnknownObjectException {
1305         Introspector obj = this.latestLoader.introspectorFromName(cousinType);
1306         String nameProps = obj.getMetadata(ObjectMetadata.NAME_PROPS);
1307         List<Introspector> relatedToProperties = new ArrayList<>();
1308
1309         if (nameProps != null) {
1310             String[] props = nameProps.split(",");
1311             for (String prop : props) {
1312                 final Object temp = getProperty(obj, prop, cousinVertex);
1313                 Introspector relatedTo = relationship.newIntrospectorInstanceOfNestedProperty("related-to-property");
1314                 relatedTo.setValue("property-key", cousinType + "." + prop);
1315                 relatedTo.setValue("property-value", temp);
1316                 relatedToProperties.add(relatedTo);
1317             }
1318         }
1319
1320         if (!relatedToProperties.isEmpty()) {
1321             List relatedToList = (List) relationship.getValue("related-to-property");
1322             for (Introspector introspector : relatedToProperties) {
1323                 relatedToList.add(introspector.getUnderlyingObject());
1324             }
1325         }
1326
1327     }
1328
1329     private Object getProperty(Introspector obj, String prop, Vertex vertex){
1330
1331         final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(prop);
1332         String dbPropertyName = prop;
1333
1334         if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
1335             dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
1336         }
1337
1338         return vertex.<Object>property(dbPropertyName).orElse(null);
1339     }
1340
1341     /**
1342      * Creates the edge.
1343      *
1344      * @param relationship the relationship
1345      * @param inputVertex  the input vertex
1346      * @return true, if successful
1347      * @throws UnsupportedEncodingException the unsupported encoding exception
1348      * @throws AAIException                 the AAI exception
1349      */
1350     public boolean createEdge(Introspector relationship, Vertex inputVertex) throws UnsupportedEncodingException, AAIException {
1351
1352         Vertex relatedVertex = null;
1353         StopWatch.conditionalStart();
1354         QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(relationship);
1355
1356         String label = null;
1357         if (relationship.hasProperty("relationship-label")) {
1358             label = relationship.getValue("relationship-label");
1359         }
1360
1361         List<Vertex> results = parser.getQueryBuilder().toList();
1362         if (results.isEmpty()) {
1363             dbTimeMsecs += StopWatch.stopIfStarted();
1364             AAIException e = new AAIException("AAI_6129", "Node of type " + parser.getResultType() + ". Could not find object at: " + parser.getUri());
1365             e.getTemplateVars().add(parser.getResultType());
1366             e.getTemplateVars().add(parser.getUri().toString());
1367             throw e;
1368         } else {
1369             //still an issue if there's more than one
1370             relatedVertex = results.get(0);
1371         }
1372
1373         if (relatedVertex != null) {
1374
1375             Edge e;
1376             try {
1377                 e = this.getEdgeBetween(EdgeType.COUSIN, inputVertex, relatedVertex, label);
1378                 if (e == null) {
1379                     edgeSer.addEdge(this.engine.asAdmin().getTraversalSource(), inputVertex, relatedVertex, label);
1380                 } else {
1381                     //attempted to link two vertexes already linked
1382                 }
1383             } finally {
1384                 dbTimeMsecs += StopWatch.stopIfStarted();
1385             }
1386         }
1387
1388         dbTimeMsecs += StopWatch.stopIfStarted();
1389         return true;
1390     }
1391
1392     /**
1393      * Gets all the edges between of the type with the specified label.
1394      *
1395      * @param aVertex the out vertex
1396      * @param bVertex the in vertex
1397      * @return the edges between
1398      * @throws AAIException             the AAI exception
1399      * @throws NoEdgeRuleFoundException
1400      */
1401     private Edge getEdgeBetweenWithLabel(EdgeType type, Vertex aVertex, Vertex bVertex, EdgeRule edgeRule) {
1402
1403         Edge result = null;
1404
1405         if (bVertex != null) {
1406             GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1407             if (EdgeType.TREE.equals(type)) {
1408                 GraphTraversal<Vertex,Vertex> findVertex = this.engine.asAdmin().getTraversalSource().V(bVertex);
1409                 if(edgeRule.getDirection().equals(Direction.IN)){
1410                     findEdgesBetween = findVertex.outE(edgeRule.getLabel())
1411                         .has(EdgeProperty.CONTAINS.toString(), edgeRule.getContains())
1412                         .not(
1413                             __.has(EdgeField.PRIVATE.toString(), true)
1414                         );
1415                 } else {
1416                     findEdgesBetween = findVertex.inE(edgeRule.getLabel())
1417                         .has(EdgeProperty.CONTAINS.toString(), edgeRule.getContains())
1418                         .not(
1419                             __.has(EdgeField.PRIVATE.toString(), true)
1420                         );
1421                 }
1422                 findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(aVertex.id())).limit(1);
1423             } else {
1424                 findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE(edgeRule.getLabel());
1425                 findEdgesBetween = findEdgesBetween
1426                     .has(EdgeProperty.CONTAINS.toString(), "NONE")
1427                     .not(
1428                         __.has(EdgeField.PRIVATE.toString(), true)
1429                     );
1430                 findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id())).limit(1);
1431             }
1432             List<Edge> list = findEdgesBetween.toList();
1433             if(!list.isEmpty()){
1434                 result = list.get(0);
1435             }
1436         }
1437
1438         return result;
1439     }
1440
1441     /**
1442      * Gets all the edges between of the type.
1443      *
1444      * @param aVertex the out vertex
1445      * @param bVertex the in vertex
1446      * @return the edges between
1447      * @throws AAIException             the AAI exception
1448      * @throws NoEdgeRuleFoundException
1449      */
1450     private List<Edge> getEdgesBetween(EdgeType type, Vertex aVertex, Vertex bVertex) {
1451
1452         List<Edge> result = new ArrayList<>();
1453
1454         if (bVertex != null) {
1455             GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1456             findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE();
1457             if (EdgeType.TREE.equals(type)) {
1458                 findEdgesBetween = findEdgesBetween
1459                     .not(
1460                         __.or(
1461                             __.has(EdgeProperty.CONTAINS.toString(), "NONE"),
1462                             __.has(EdgeField.PRIVATE.toString(), true)
1463                         )
1464                     );
1465             } else {
1466                 findEdgesBetween = findEdgesBetween
1467                     .has(EdgeProperty.CONTAINS.toString(), "NONE")
1468                     .not(
1469                         __.has(EdgeField.PRIVATE.toString(), true)
1470                     );
1471             }
1472             findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id()));
1473             result = findEdgesBetween.toList();
1474         }
1475
1476         return result;
1477     }
1478
1479     /**
1480      * Gets all the edges string between of the type.
1481      *
1482      * @param aVertex the out vertex
1483      * @param bVertex the in vertex
1484      * @return the edges between
1485      * @throws AAIException the AAI exception
1486      * @throws NoEdgeRuleFoundException
1487      */
1488     private List<String> getEdgeLabelsBetween(EdgeType type, Vertex aVertex, Vertex bVertex) {
1489
1490         List<String> result = new ArrayList<>();
1491
1492         if (bVertex != null) {
1493             GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1494             findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE();
1495             if (EdgeType.TREE.equals(type)) {
1496                 findEdgesBetween = findEdgesBetween
1497                         .not(
1498                                 __.or(
1499                                         __.has(EdgeProperty.CONTAINS.toString(), "NONE"),
1500                                         __.has(EdgeField.PRIVATE.toString(), true)
1501                                 )
1502                         );
1503             } else {
1504                 findEdgesBetween = findEdgesBetween
1505                         .has(EdgeProperty.CONTAINS.toString(), "NONE")
1506                         .not(
1507                                 __.has(EdgeField.PRIVATE.toString(), true)
1508                         );
1509             }
1510             findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id()));
1511             result = findEdgesBetween.label().toList();
1512         }
1513         return result;
1514     }
1515
1516     /**
1517      * Gets all the edges string between of the type.
1518      *
1519      * @param aVertex the out vertex
1520      * @param bVertex the in vertex
1521      * @return the edges between
1522      * @throws AAIException the AAI exception
1523      * @throws NoEdgeRuleFoundException
1524      */
1525     private Long getEdgeLabelsCount(Vertex aVertex, Vertex bVertex) {
1526
1527         Long result = null;
1528
1529         if (bVertex != null) {
1530             GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1531             findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE();
1532             findEdgesBetween = findEdgesBetween
1533                     .has(EdgeProperty.CONTAINS.toString(), "NONE")
1534                     .not(
1535                             __.has(EdgeField.PRIVATE.toString(), true)
1536                     );
1537             findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id()));
1538             result = findEdgesBetween.count().next();
1539         }
1540         return result;
1541     }
1542     /**
1543      * Gets all the edges between the vertexes with the label and type.
1544      *
1545      * @param aVertex the out vertex
1546      * @param bVertex the in vertex
1547      * @param label
1548      * @return the edges between
1549      * @throws AAIException the AAI exception
1550      */
1551     private Edge getEdgesBetween(EdgeType type, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
1552
1553         Edge edge = null;
1554
1555         if (bVertex != null) {
1556             String aType = aVertex.<String>property(AAIProperties.NODE_TYPE).value();
1557             String bType = bVertex.<String>property(AAIProperties.NODE_TYPE).value();
1558             EdgeRuleQuery q = new EdgeRuleQuery.Builder(aType, bType).edgeType(type).label(label).build();
1559             EdgeRule rule;
1560             try {
1561                 rule = edgeRules.getRule(q);
1562             } catch (EdgeRuleNotFoundException e) {
1563                 throw new NoEdgeRuleFoundException(e);
1564             } catch (AmbiguousRuleChoiceException e) {
1565                 throw new MultipleEdgeRuleFoundException(e);
1566             }
1567             edge = this.getEdgeBetweenWithLabel(type, aVertex, bVertex, rule);
1568         }
1569
1570         return edge;
1571     }
1572
1573     /**
1574      * Gets the edge between with the label and edge type.
1575      *
1576      * @param aVertex the out vertex
1577      * @param bVertex the in vertex
1578      * @param label
1579      * @return the edge between
1580      * @throws AAIException             the AAI exception
1581      * @throws NoEdgeRuleFoundException
1582      */
1583     public Edge getEdgeBetween(EdgeType type, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
1584
1585         StopWatch.conditionalStart();
1586         if (bVertex != null) {
1587
1588             Edge edge = this.getEdgesBetween(type, aVertex, bVertex, label);
1589             if (edge != null) {
1590                 dbTimeMsecs += StopWatch.stopIfStarted();
1591                 return edge;
1592             }
1593
1594         }
1595         dbTimeMsecs += StopWatch.stopIfStarted();
1596         return null;
1597     }
1598
1599     public Edge getEdgeBetween(EdgeType type, Vertex aVertex, Vertex bVertex) throws AAIException {
1600         return this.getEdgeBetween(type, aVertex, bVertex, null);
1601     }
1602
1603
1604     /**
1605      * Delete edge.
1606      *
1607      * @param relationship the relationship
1608      * @param inputVertex  the input vertex
1609      * @return true, if successful
1610      * @throws UnsupportedEncodingException the unsupported encoding exception
1611      * @throws AAIException                 the AAI exception
1612      */
1613     public boolean deleteEdge(Introspector relationship, Vertex inputVertex) throws UnsupportedEncodingException, AAIException {
1614
1615         Vertex relatedVertex = null;
1616         StopWatch.conditionalStart();
1617         QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(relationship);
1618
1619         List<Vertex> results = parser.getQueryBuilder().toList();
1620
1621         String label = null;
1622         if (relationship.hasProperty("relationship-label")) {
1623             label = relationship.getValue("relationship-label");
1624         }
1625
1626         if (results.isEmpty()) {
1627             dbTimeMsecs += StopWatch.stopIfStarted();
1628             return false;
1629         }
1630
1631         relatedVertex = results.get(0);
1632         Edge edge;
1633         try {
1634             edge = this.getEdgeBetween(EdgeType.COUSIN, inputVertex, relatedVertex, label);
1635         } catch (NoEdgeRuleFoundException e) {
1636             dbTimeMsecs += StopWatch.stopIfStarted();
1637             throw new AAIException("AAI_6129", e);
1638         }
1639         if (edge != null) {
1640             edge.remove();
1641             dbTimeMsecs += StopWatch.stopIfStarted();
1642             return true;
1643         } else {
1644             dbTimeMsecs += StopWatch.stopIfStarted();
1645             return false;
1646         }
1647
1648     }
1649
1650     /**
1651      * Delete items with traversal.
1652      *
1653      * @param vertexes the vertexes
1654      * @throws IllegalStateException the illegal state exception
1655      */
1656     public void deleteItemsWithTraversal(List<Vertex> vertexes) throws IllegalStateException {
1657
1658         for (Vertex v : vertexes) {
1659             deleteWithTraversal(v);
1660         }
1661
1662     }
1663
1664     /**
1665      * Delete with traversal.
1666      *
1667      * @param startVertex the start vertex
1668      */
1669     public void deleteWithTraversal(Vertex startVertex) {
1670         StopWatch.conditionalStart();
1671         List<Vertex> results = this.engine.getQueryEngine().findDeletable(startVertex);
1672
1673         for (Vertex v : results) {
1674             LOGGER.debug("Removing vertex {} with label {}", v.id(), v.label());
1675             v.remove();
1676         }
1677         dbTimeMsecs += StopWatch.stopIfStarted();
1678     }
1679
1680     /**
1681      * Removes the list of vertexes from the graph
1682      * <p>
1683      * Current the vertex label will just be vertex but
1684      * in the future the aai-node-type property will be replaced
1685      * by using the vertex label as when retrieving an vertex
1686      * and retrieving an single property on an vertex will pre-fetch
1687      * all the properties of that vertex and this is due to the following property
1688      * <p>
1689      * <code>
1690      * query.fast-property=true
1691      * </code>
1692      * <p>
1693      * JanusGraph doesn't provide the capability to override that
1694      * at a transaction level and there is a plan to move to vertex label
1695      * so it is best to utilize this for now and when the change is applied
1696      *
1697      * @param vertices - list of vertices to delete from the graph
1698      */
1699     void delete(List<Vertex> vertices){
1700         StopWatch.conditionalStart();
1701
1702         for (Vertex v : vertices) {
1703             LOGGER.debug("Removing vertex {} with label {}", v.id(), v.label());
1704             v.remove();
1705         }
1706
1707         dbTimeMsecs += StopWatch.stopIfStarted();
1708     }
1709
1710     /**
1711      * Delete.
1712      *
1713      * @param v               the v
1714      * @param resourceVersion the resource version
1715      * @throws IllegalArgumentException the illegal argument exception
1716      * @throws AAIException             the AAI exception
1717      * @throws InterruptedException     the interrupted exception
1718      */
1719     public void delete(Vertex v, List<Vertex> deletableVertices, String resourceVersion, boolean enableResourceVersion) throws IllegalArgumentException, AAIException {
1720
1721         boolean result = verifyDeleteSemantics(v, resourceVersion, enableResourceVersion);
1722         /*
1723          * The reason why I want to call PreventDeleteSemantics second time is to catch the prevent-deletes in a chain
1724          * These are far-fewer than seeing a prevnt-delete on the vertex to be deleted
1725          * So its better to make these in 2 steps
1726          */
1727         if (result && !deletableVertices.isEmpty()) {
1728             result = verifyPreventDeleteSemantics(deletableVertices);
1729         }
1730         if (result) {
1731
1732             try {
1733                 deleteWithTraversal(v);
1734             } catch (IllegalStateException e) {
1735                 throw new AAIException("AAI_6110", e);
1736             }
1737
1738         }
1739
1740     }
1741
1742
1743     /**
1744      * Delete.
1745      *
1746      * @param v               the v
1747      * @param resourceVersion the resource version
1748      * @throws IllegalArgumentException the illegal argument exception
1749      * @throws AAIException             the AAI exception
1750      * @throws InterruptedException     the interrupted exception
1751      */
1752     public void delete(Vertex v, String resourceVersion, boolean enableResourceVersion) throws IllegalArgumentException, AAIException {
1753
1754         boolean result = verifyDeleteSemantics(v, resourceVersion, enableResourceVersion);
1755
1756         if (result) {
1757
1758             try {
1759                 deleteWithTraversal(v);
1760             } catch (IllegalStateException e) {
1761                 throw new AAIException("AAI_6110", e);
1762             }
1763
1764         }
1765
1766     }
1767
1768     /**
1769      * Verify delete semantics.
1770      *
1771      * @param vertex          the vertex
1772      * @param resourceVersion the resource version
1773      * @return true, if successful
1774      * @throws AAIException the AAI exception
1775      */
1776     private boolean verifyDeleteSemantics(Vertex vertex, String resourceVersion, boolean enableResourceVersion) throws AAIException {
1777         boolean result = true;
1778         String nodeType = "";
1779         String errorDetail = " unknown delete semantic found";
1780         String aaiExceptionCode = "";
1781         nodeType = vertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
1782         if (enableResourceVersion && !this.verifyResourceVersion("delete", nodeType, vertex.<String>property(AAIProperties.RESOURCE_VERSION).orElse(null), resourceVersion, nodeType)) {
1783         }
1784         List<Vertex> vertices = new ArrayList<Vertex>();
1785         vertices.add(vertex);
1786         result = verifyPreventDeleteSemantics(vertices);
1787
1788         return result;
1789     }
1790
1791     /**
1792      * Verify Prevent delete semantics.
1793      *
1794      * @param vertices the list of vertices
1795      * @return true, if successful
1796      * @throws AAIException the AAI exception
1797      */
1798     private boolean verifyPreventDeleteSemantics(List<Vertex> vertices) throws AAIException {
1799         boolean result = true;
1800         String nodeType = "";
1801         String errorDetail = " unknown delete semantic found";
1802         String aaiExceptionCode = "";
1803
1804         StopWatch.conditionalStart();
1805         /*
1806          * This takes in all the vertices in a cascade-delete-chain and checks if there is any edge with a "prevent-delete" condition
1807          * If yes - that should prevent the deletion of the vertex
1808          * Dedup makes sure we dont capture the prevent-delete vertices twice
1809          * The prevent-delete vertices are stored so that the error message displays what prevents the delete
1810          */
1811
1812         List<Object> preventDeleteVertices = this.engine.asAdmin().getReadOnlyTraversalSource().V(vertices).
1813             union(__.inE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.IN.toString()).outV().values(AAIProperties.NODE_TYPE),
1814                 __.outE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.OUT.toString()).inV().values(AAIProperties.NODE_TYPE))
1815             .dedup().toList();
1816
1817         dbTimeMsecs += StopWatch.stopIfStarted();
1818         if (!preventDeleteVertices.isEmpty()) {
1819             aaiExceptionCode = "AAI_6110";
1820             errorDetail = String.format("Object is being reference by additional objects preventing it from being deleted. Please clean up references from the following types %s", preventDeleteVertices);
1821             result = false;
1822         }
1823         if (!result) {
1824             throw new AAIException(aaiExceptionCode, errorDetail);
1825         }
1826         return result;
1827     }
1828
1829     /**
1830      * Verify resource version.
1831      *
1832      * @param action                 the action
1833      * @param nodeType               the node type
1834      * @param currentResourceVersion the current resource version
1835      * @param resourceVersion        the resource version
1836      * @param uri                    the uri
1837      * @return true, if successful
1838      * @throws AAIException the AAI exception
1839      */
1840     public boolean verifyResourceVersion(String action, String nodeType, String currentResourceVersion, String resourceVersion, String uri) throws AAIException {
1841         String enabled = "";
1842         String errorDetail = "";
1843         String aaiExceptionCode = "";
1844         boolean isDeleteResourceVersionOk = true;
1845         if (currentResourceVersion == null) {
1846             currentResourceVersion = "";
1847         }
1848
1849         if (resourceVersion == null) {
1850             resourceVersion = "";
1851         }
1852         try {
1853             enabled = AAIConfig.get(AAIConstants.AAI_RESVERSION_ENABLEFLAG);
1854
1855         } catch (AAIException e) {
1856             ErrorLogHelper.logException(e);
1857         }
1858         if (enabled.equals("true")) {
1859             if ("delete".equals(action)) {
1860                 isDeleteResourceVersionOk = verifyResourceVersionForDelete(currentResourceVersion, resourceVersion);
1861             }
1862             if ((!isDeleteResourceVersionOk) || ((!"delete".equals(action)) && (!currentResourceVersion.equals(resourceVersion)))) {
1863                 if ("create".equals(action) && !resourceVersion.equals("")) {
1864                     errorDetail = "resource-version passed for " + action + " of " + uri;
1865                     aaiExceptionCode = "AAI_6135";
1866                 } else if (resourceVersion.equals("")) {
1867                     errorDetail = "resource-version not passed for " + action + " of " + uri;
1868                     aaiExceptionCode = "AAI_6130";
1869                 } else {
1870                     errorDetail = "resource-version MISMATCH for " + action + " of " + uri;
1871                     aaiExceptionCode = "AAI_6131";
1872                 }
1873
1874                 throw new AAIException(aaiExceptionCode, errorDetail);
1875
1876             }
1877         }
1878         return true;
1879     }
1880
1881     /**
1882      * Verify resource version for delete.
1883      *
1884      * @param currentResourceVersion the current resource version
1885      * @param resourceVersion        the resource version
1886      * @return true, if successful or false if there is a mismatch
1887      */
1888     private boolean verifyResourceVersionForDelete(String currentResourceVersion, String resourceVersion) {
1889
1890         boolean isDeleteResourceVersionOk = true;
1891         String resourceVersionDisabledUuid = AAIConfig.get(AAIConstants.AAI_RESVERSION_DISABLED_UUID,
1892             AAIConstants.AAI_RESVERSION_DISABLED_UUID_DEFAULT);
1893
1894         if ((!currentResourceVersion.equals(resourceVersion)) && (!resourceVersion.equals(resourceVersionDisabledUuid))) {
1895             isDeleteResourceVersionOk = false;
1896         }
1897         return isDeleteResourceVersionOk;
1898     }
1899
1900     /**
1901      * Convert from camel case.
1902      *
1903      * @param name the name
1904      * @return the string
1905      */
1906     private String convertFromCamelCase(String name) {
1907         String result = "";
1908         result = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, name);
1909
1910         NamingExceptions exceptions = NamingExceptions.getInstance();
1911         result = exceptions.getDBName(result);
1912
1913         return result;
1914     }
1915
1916     private boolean canModify(Introspector obj, String propName, String requestContext) {
1917         final String readOnly = obj.getPropertyMetadata(propName).get(PropertyMetadata.READ_ONLY);
1918         if (readOnly != null) {
1919             final String[] items = readOnly.split(",");
1920             for (String item : items) {
1921                 if (requestContext.equals(item)) {
1922                     return false;
1923                 }
1924             }
1925         }
1926         return true;
1927     }
1928
1929     private void executePreSideEffects(Introspector obj, Vertex self) throws AAIException {
1930
1931         SideEffectRunner runner = new SideEffectRunner
1932             .Builder(this.engine, this).addSideEffect(DataCopy.class).addSideEffect(PrivateEdge.class).build();
1933
1934         runner.execute(obj, self);
1935     }
1936
1937     private void executePostSideEffects(Introspector obj, Vertex self) throws AAIException {
1938
1939         SideEffectRunner runner = new SideEffectRunner
1940             .Builder(this.engine, this).addSideEffect(DataLinkWriter.class).build();
1941
1942         runner.execute(obj, self);
1943     }
1944
1945     private void enrichData(Introspector obj, Vertex self) throws AAIException {
1946
1947         SideEffectRunner runner = new SideEffectRunner
1948             .Builder(this.engine, this).addSideEffect(DataLinkReader.class).build();
1949
1950         runner.execute(obj, self);
1951     }
1952
1953     public double getDBTimeMsecs() {
1954         return (dbTimeMsecs);
1955     }
1956
1957     /**
1958      * Db to object With Filters
1959      * This is for a one-time run with Tenant Isloation to only filter relationships
1960      * TODO: Chnage the original dbToObject to take filter parent/cousins
1961      *
1962      * @param obj               the obj
1963      * @param v                 the vertex from the graph
1964      * @param depth             the depth
1965      * @param nodeOnly          specify if to exclude relationships or not
1966      * @param filterCousinNodes
1967      * @return the introspector
1968      * @throws AAIException                 the AAI exception
1969      * @throws IllegalAccessException       the illegal access exception
1970      * @throws IllegalArgumentException     the illegal argument exception
1971      * @throws InvocationTargetException    the invocation target exception
1972      * @throws SecurityException            the security exception
1973      * @throws InstantiationException       the instantiation exception
1974      * @throws NoSuchMethodException        the no such method exception
1975      * @throws UnsupportedEncodingException the unsupported encoding exception
1976      * @throws MalformedURLException        the malformed URL exception
1977      * @throws AAIUnknownObjectException
1978      * @throws URISyntaxException
1979      */
1980     //TODO - See if you can merge the 2 dbToObjectWithFilters
1981     public Introspector dbToObjectWithFilters(Introspector obj, Vertex v, Set<Vertex> seen, int depth, boolean nodeOnly, List<String> filterCousinNodes, List<String> filterParentNodes) throws AAIException, UnsupportedEncodingException {
1982         String cleanUp = "false";
1983         if (depth < 0) {
1984             return null;
1985         }
1986         depth--;
1987         seen.add(v);
1988         boolean modified = false;
1989         for (String property : obj.getProperties(PropertyPredicates.isVisible())) {
1990             List<Object> getList = null;
1991             Vertex[] vertices = null;
1992
1993             if (!(obj.isComplexType(property) || obj.isListType(property))) {
1994                 this.copySimpleProperty(property, obj, v);
1995                 modified = true;
1996             } else {
1997                 if (obj.isComplexType(property)) {
1998                     /* container case */
1999
2000                     if (!property.equals("relationship-list") && depth >= 0) {
2001                         Introspector argumentObject = obj.newIntrospectorInstanceOfProperty(property);
2002                         Object result = dbToObjectWithFilters(argumentObject, v, seen, depth + 1, nodeOnly, filterCousinNodes, filterParentNodes);
2003                         if (result != null) {
2004                             obj.setValue(property, argumentObject.getUnderlyingObject());
2005                             modified = true;
2006                         }
2007                     } else if (property.equals("relationship-list") && !nodeOnly) {
2008                         /* relationships need to be handled correctly */
2009                         Introspector relationshipList = obj.newIntrospectorInstanceOfProperty(property);
2010                         relationshipList = createFilteredRelationshipList(v, relationshipList, cleanUp, filterCousinNodes);
2011                         if (relationshipList != null) {
2012                             modified = true;
2013                             obj.setValue(property, relationshipList.getUnderlyingObject());
2014                             modified = true;
2015                         }
2016
2017                     }
2018                 } else if (obj.isListType(property)) {
2019
2020                     if (property.equals("any")) {
2021                         continue;
2022                     }
2023                     String genericType = obj.getGenericTypeClass(property).getSimpleName();
2024                     if (obj.isComplexGenericType(property) && depth >= 0) {
2025                         final String childDbName = convertFromCamelCase(genericType);
2026                         String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
2027                         EdgeRule rule;
2028
2029                         boolean isthisParentRequired = filterParentNodes.parallelStream().anyMatch(childDbName::contains);
2030
2031                         EdgeRuleQuery q = new EdgeRuleQuery.Builder(vType, childDbName).edgeType(EdgeType.TREE).build();
2032
2033                         try {
2034                             rule = edgeRules.getRule(q);
2035                         } catch (EdgeRuleNotFoundException e) {
2036                             throw new NoEdgeRuleFoundException(e);
2037                         } catch (AmbiguousRuleChoiceException e) {
2038                             throw new MultipleEdgeRuleFoundException(e);
2039                         }
2040                         if (!rule.getContains().equals(AAIDirection.NONE.toString()) && isthisParentRequired) {
2041                             //vertices = this.queryEngine.findRelatedVertices(v, Direction.OUT, rule.getLabel(), childDbName);
2042                             Direction ruleDirection = rule.getDirection();
2043                             Iterator<Vertex> itr = v.vertices(ruleDirection, rule.getLabel());
2044                             List<Vertex> verticesList = (List<Vertex>) IteratorUtils.toList(itr);
2045                             itr = verticesList.stream().filter(item -> {
2046                                 return item.property(AAIProperties.NODE_TYPE).orElse("").equals(childDbName);
2047                             }).iterator();
2048                             if (itr.hasNext()) {
2049                                 getList = (List<Object>) obj.getValue(property);
2050                             }
2051                             int processed = 0;
2052                             int removed = 0;
2053                             while (itr.hasNext()) {
2054                                 Vertex childVertex = itr.next();
2055                                 if (!seen.contains(childVertex)) {
2056                                     Introspector argumentObject = obj.newIntrospectorInstanceOfNestedProperty(property);
2057
2058                                     Object result = dbToObjectWithFilters(argumentObject, childVertex, seen, depth, nodeOnly, filterCousinNodes, filterParentNodes);
2059                                     if (result != null) {
2060                                         getList.add(argumentObject.getUnderlyingObject());
2061                                     }
2062
2063                                     processed++;
2064                                 } else {
2065                                     removed++;
2066                                     LOGGER.warn("Cycle found while serializing vertex id={}", childVertex.id().toString());
2067                                 }
2068                             }
2069                             if (processed == 0) {
2070                                 //vertices were all seen, reset the list
2071                                 getList = null;
2072                             }
2073                             if (processed > 0) {
2074                                 modified = true;
2075                             }
2076                         }
2077                     } else if (obj.isSimpleGenericType(property)) {
2078                         List<Object> temp = this.engine.getListProperty(v, property);
2079                         if (temp != null) {
2080                             getList = (List<Object>) obj.getValue(property);
2081                             getList.addAll(temp);
2082                             modified = true;
2083                         }
2084
2085                     }
2086
2087                 }
2088
2089             }
2090         }
2091
2092         //no changes were made to this obj, discard the instance
2093         if (!modified) {
2094             return null;
2095         }
2096         this.enrichData(obj, v);
2097         return obj;
2098
2099     }
2100
2101     /**
2102      * Creates the relationship list with the filtered node types.
2103      *
2104      * @param v       the v
2105      * @param obj     the obj
2106      * @param cleanUp the clean up
2107      * @return the object
2108      * @throws InstantiationException       the instantiation exception
2109      * @throws IllegalAccessException       the illegal access exception
2110      * @throws IllegalArgumentException     the illegal argument exception
2111      * @throws InvocationTargetException    the invocation target exception
2112      * @throws NoSuchMethodException        the no such method exception
2113      * @throws SecurityException            the security exception
2114      * @throws UnsupportedEncodingException the unsupported encoding exception
2115      * @throws AAIException                 the AAI exception
2116      * @throws MalformedURLException        the malformed URL exception
2117      * @throws URISyntaxException
2118      */
2119     private Introspector createFilteredRelationshipList(Vertex v, Introspector obj, String cleanUp, List<String> filterNodes) throws UnsupportedEncodingException, AAIException {
2120         List<Vertex> allCousins = this.engine.getQueryEngine().findCousinVertices(v);
2121
2122         Iterator<Vertex> cousinVertices = allCousins.stream().filter(item -> {
2123             String node = (String) item.property(AAIProperties.NODE_TYPE).orElse("");
2124             return filterNodes.parallelStream().anyMatch(node::contains);
2125         }).iterator();
2126
2127
2128         List<Vertex> cousins = (List<Vertex>) IteratorUtils.toList(cousinVertices);
2129
2130         //items.parallelStream().anyMatch(inputStr::contains)
2131         List<Object> relationshipObjList = obj.getValue("relationship");
2132         for (Vertex cousin : cousins) {
2133
2134             Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
2135             Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, null);
2136             if (result != null) {
2137                 relationshipObjList.add(result);
2138             }
2139
2140
2141         }
2142
2143         if (relationshipObjList.isEmpty()) {
2144             return null;
2145         } else {
2146             return obj;
2147         }
2148     }
2149
2150 }