2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
20 package org.onap.aai.serialization.db;
23 import com.att.eelf.configuration.EELFLogger;
24 import com.att.eelf.configuration.EELFManager;
25 import com.google.common.base.CaseFormat;
26 import com.google.common.collect.Multimap;
27 import org.apache.commons.collections.IteratorUtils;
28 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversal;
29 import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
30 import org.apache.tinkerpop.gremlin.process.traversal.step.util.Tree;
31 import org.apache.tinkerpop.gremlin.structure.*;
32 import org.janusgraph.core.SchemaViolationException;
33 import org.javatuples.Triplet;
34 import org.onap.aai.concurrent.AaiCallable;
35 import org.onap.aai.config.SpringContextAware;
36 import org.onap.aai.db.props.AAIProperties;
37 import org.onap.aai.edges.EdgeIngestor;
38 import org.onap.aai.edges.EdgeRule;
39 import org.onap.aai.edges.EdgeRuleQuery;
40 import org.onap.aai.edges.TypeAlphabetizer;
41 import org.onap.aai.edges.enums.AAIDirection;
42 import org.onap.aai.edges.enums.EdgeField;
43 import org.onap.aai.edges.enums.EdgeProperty;
44 import org.onap.aai.edges.enums.EdgeType;
45 import org.onap.aai.edges.exceptions.AmbiguousRuleChoiceException;
46 import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
47 import org.onap.aai.exceptions.AAIException;
48 import org.onap.aai.introspection.*;
49 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
50 import org.onap.aai.introspection.sideeffect.*;
51 import org.onap.aai.logging.ErrorLogHelper;
52 import org.onap.aai.logging.LogFormatTools;
53 import org.onap.aai.logging.LoggingContext;
54 import org.onap.aai.logging.StopWatch;
55 import org.onap.aai.parsers.query.QueryParser;
56 import org.onap.aai.parsers.uri.URIParser;
57 import org.onap.aai.parsers.uri.URIToRelationshipObject;
58 import org.onap.aai.query.builder.QueryBuilder;
59 import org.onap.aai.schema.enums.ObjectMetadata;
60 import org.onap.aai.schema.enums.PropertyMetadata;
61 import org.onap.aai.serialization.db.exceptions.MultipleEdgeRuleFoundException;
62 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
63 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
64 import org.onap.aai.serialization.engines.query.QueryEngine;
65 import org.onap.aai.serialization.tinkerpop.TreeBackedVertex;
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;
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;
79 import java.net.URISyntaxException;
81 import java.util.concurrent.ExecutionException;
82 import java.util.concurrent.ExecutorService;
83 import java.util.concurrent.Future;
84 import java.util.function.Function;
85 import java.util.regex.Matcher;
86 import java.util.regex.Pattern;
87 import java.util.stream.Collectors;
89 public class DBSerializer {
91 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DBSerializer.class);
93 private static final String MISSING_REQUIRED_NODE_PROPERTY = "Vertex missing required aai-node-type property";
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;
107 private SchemaVersions schemaVersions;
108 private Set<String> namedPropNodes;
110 * Instantiates a new DB serializer.
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
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();
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);
138 EdgeSerializer es = ctx.getBean(EdgeSerializer.class);
139 setEdgeSerializer(es);
142 private void backupESInit() {
143 setEdgeSerializer(new EdgeSerializer(this.edgeRules));
146 public void setEdgeSerializer(EdgeSerializer edgeSer) {
147 this.edgeSer = edgeSer;
150 public EdgeSerializer getEdgeSeriailizer() {
154 public void setEdgeIngestor(EdgeIngestor ei) {
158 public EdgeIngestor getEdgeIngestor() {
159 return this.edgeRules;
163 * Touch standard vertex properties.
166 * @param isNewVertex the is new vertex
168 public void touchStandardVertexProperties(Vertex v, boolean isNewVertex) {
169 String timeNowInSec = Long.toString(currentTimeMillis);
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());
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);
182 private void touchStandardVertexProperties(String nodeType, Vertex v, boolean isNewVertex) {
184 v.property(AAIProperties.NODE_TYPE, nodeType);
185 touchStandardVertexProperties(v, isNewVertex);
191 * Creates the new vertex.
193 * @param wrappedObject the wrapped object
195 * @throws UnsupportedEncodingException the unsupported encoding exception
196 * @throws AAIException the AAI exception
198 public Vertex createNewVertex(Introspector wrappedObject) {
201 StopWatch.conditionalStart();
202 v = this.engine.tx().addVertex();
203 touchStandardVertexProperties(wrappedObject.getDbName(), v, true);
205 dbTimeMsecs += StopWatch.stopIfStarted();
213 * @param className the class name
217 * Removes the classpath from a class name
219 public String trimClassName(String className) {
220 String returnValue = "";
222 if (className.lastIndexOf('.') == -1) {
225 returnValue = className.substring(className.lastIndexOf('.') + 1, className.length());
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
248 public void serializeToDb(Introspector obj, Vertex v, QueryParser uriQuery, String identifier, String requestContext) throws AAIException, UnsupportedEncodingException {
249 StopWatch.conditionalStart();
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);
258 dbTimeMsecs += StopWatch.stopIfStarted();
259 throw new AAIException("AAI_6114", "No parent Node of type " + uriQuery.getParentResultType() + " for " + identifier);
262 serializeSingleVertex(v, obj, requestContext);
265 } catch (SchemaViolationException e) {
266 dbTimeMsecs += StopWatch.stopIfStarted();
267 throw new AAIException("AAI_6117", e);
269 dbTimeMsecs += StopWatch.stopIfStarted();
272 public void serializeSingleVertex(Vertex v, Introspector obj, String requestContext) throws UnsupportedEncodingException, AAIException {
273 StopWatch.conditionalStart();
275 boolean isTopLevel = obj.isTopLevel();
277 addUriIfNeeded(v, obj.getURI());
280 processObject(obj, v, requestContext);
282 URI uri = this.getURIForVertex(v);
283 URIParser parser = new URIParser(this.loader, uri);
284 if (parser.validate()) {
285 addUriIfNeeded(v, uri.toString());
288 } catch (SchemaViolationException e) {
289 throw new AAIException("AAI_6117", e);
291 dbTimeMsecs += StopWatch.stopIfStarted();
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);
305 * @param <T> the generic type
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
320 * Helper method for reflectToDb
321 * Handles all the property setting
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);
333 this.executePreSideEffects(obj, v);
334 for (String property : properties) {
336 final String propertyType;
337 propertyType = obj.getType(property);
338 isComplexType = obj.isComplexType(property);
339 isListType = obj.isListType(property);
340 value = obj.getValue(property);
342 if (!(isComplexType || isListType)) {
343 boolean canModify = this.canModify(obj, property, requestContext);
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);
351 if (metadata.containsKey(PropertyMetadata.DATA_LINK)) {
352 //data linked properties are ephemeral
353 //they are populated dynamically on GETs
357 if (!value.equals(v.property(dbProperty).orElse(null))) {
358 if (propertyType.toLowerCase().contains(".long")) {
359 v.property(dbProperty, new Integer(((Long) value).toString()));
361 v.property(dbProperty, value);
365 v.property(dbProperty).remove();
368 } else if (isListType) {
369 List<Object> list = (List<Object>) value;
370 if (obj.isComplexGenericType(property)) {
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));
380 engine.setListProperty(v, property, list);
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()) {
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));
395 } else if (!property.equals("relationship-list")) {
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());
402 processedVertexes.addAll(processObject(introspector, v, requestContext));
405 dependentVertexes.addAll(this.engine.getQueryEngine().findChildrenOfType(v, introspector.getDbName()));
406 processedVertexes.add(reflectDependentVertex(v, introspector, requestContext));
409 } else if (property.equals("relationship-list")) {
410 handleRelationships(obj, v);
415 this.writeThroughDefaults(v, obj);
416 /* handle those vertexes not touched */
417 for (Vertex toBeRemoved : processedVertexes) {
418 dependentVertexes.remove(toBeRemoved);
420 this.deleteItemsWithTraversal(dependentVertexes);
422 this.executePostSideEffects(obj, v);
423 return processedVertexes;
427 * Handle relationships.
430 * @param vertex the vertex
431 * @throws SecurityException the security exception
432 * @throws IllegalAccessException the illegal access exception
433 * @throws IllegalArgumentException the illegal argument exception
434 * @throws InvocationTargetException the invocation target exception
435 * @throws UnsupportedEncodingException the unsupported encoding exception
436 * @throws AAIException the AAI exception
439 * Handles the explicit relationships defined for an obj
441 private void handleRelationships(Introspector obj, Vertex vertex) throws UnsupportedEncodingException, AAIException {
444 Introspector wrappedRl = obj.getWrappedValue("relationship-list");
445 processRelationshipList(wrappedRl, vertex);
452 * Process relationship list.
454 * @param wrapped the wrapped
456 * @throws UnsupportedEncodingException the unsupported encoding exception
457 * @throws AAIException the AAI exception
459 private void processRelationshipList(Introspector wrapped, Vertex v) throws UnsupportedEncodingException, AAIException {
461 List<Object> relationships = (List<Object>) wrapped.getValue("relationship");
463 List<Triplet<Vertex, Vertex, String>> addEdges = new ArrayList<>();
464 List<Edge> existingEdges = this.engine.getQueryEngine().findEdgesForVersion(v, wrapped.getLoader());
466 for (Object relationship : relationships) {
468 Vertex cousinVertex = null;
470 Introspector wrappedRel = IntrospectorFactory.newInstance(this.introspectionType, relationship);
471 QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(wrappedRel);
473 if (wrappedRel.hasProperty("relationship-label")) {
474 label = wrappedRel.getValue("relationship-label");
477 List<Vertex> results = parser.getQueryBuilder().toList();
478 if (results.isEmpty()) {
479 final AAIException ex = new AAIException("AAI_6129", "Node of type " + parser.getResultType() + ". Could not find object at: " + parser.getUri());
480 ex.getTemplateVars().add(parser.getResultType());
481 ex.getTemplateVars().add(parser.getUri().toString());
484 //still an issue if there's more than one
485 cousinVertex = results.get(0);
488 if (cousinVertex != null) {
489 String vType = (String) v.property(AAIProperties.NODE_TYPE).value();
490 String cousinType = (String) cousinVertex.property(AAIProperties.NODE_TYPE).value();
491 EdgeRuleQuery.Builder baseQ = new EdgeRuleQuery.Builder(vType, cousinType).label(label);
494 if (!edgeRules.hasRule(baseQ.build())) {
495 throw new AAIException("AAI_6120", "No EdgeRule found for passed nodeTypes: " + v.property(AAIProperties.NODE_TYPE).value().toString() + ", "
496 + cousinVertex.property(AAIProperties.NODE_TYPE).value().toString() + (label != null ? (" with label " + label) : "") + ".");
497 } else if (edgeRules.hasRule(baseQ.edgeType(EdgeType.TREE).build()) && !edgeRules.hasRule(baseQ.edgeType(EdgeType.COUSIN).build())) {
498 throw new AAIException("AAI_6145");
501 e = this.getEdgeBetween(EdgeType.COUSIN, v, cousinVertex, label);
504 addEdges.add(new Triplet<>(v, cousinVertex, label));
506 existingEdges.remove(e);
511 for (Edge edge : existingEdges) {
514 for (Triplet<Vertex, Vertex, String> triplet : addEdges) {
516 edgeSer.addEdge(this.engine.asAdmin().getTraversalSource(), triplet.getValue0(), triplet.getValue1(), triplet.getValue2());
517 } catch (NoEdgeRuleFoundException e) {
518 throw new AAIException("AAI_6129", e);
525 * Write through defaults.
529 * @throws AAIUnknownObjectException
531 private void writeThroughDefaults(Vertex v, Introspector obj) throws AAIUnknownObjectException {
532 Introspector latest = this.latestLoader.introspectorFromName(obj.getName());
533 if (latest != null) {
534 Set<String> required = latest.getRequiredProperties();
536 for (String field : required) {
537 String defaultValue = null;
538 Object vertexProp = null;
539 defaultValue = latest.getPropertyMetadata(field).get(PropertyMetadata.DEFAULT_VALUE);
540 if (defaultValue != null) {
541 vertexProp = v.<Object>property(field).orElse(null);
542 if (vertexProp == null) {
543 v.property(field, defaultValue);
553 * Reflect dependent vertex.
556 * @param dependentObj the dependent obj
558 * @throws IllegalAccessException the illegal access exception
559 * @throws IllegalArgumentException the illegal argument exception
560 * @throws InvocationTargetException the invocation target exception
561 * @throws InstantiationException the instantiation exception
562 * @throws NoSuchMethodException the no such method exception
563 * @throws SecurityException the security exception
564 * @throws AAIException the AAI exception
565 * @throws UnsupportedEncodingException the unsupported encoding exception
566 * @throws AAIUnknownObjectException
568 private Vertex reflectDependentVertex(Vertex v, Introspector dependentObj, String requestContext) throws AAIException, UnsupportedEncodingException {
570 //QueryParser p = this.engine.getQueryBuilder().createQueryFromURI(obj.getURI());
571 //List<Vertex> items = p.getQuery().toList();
572 QueryBuilder<Vertex> query = this.engine.getQueryBuilder(v);
573 query.createEdgeTraversal(EdgeType.TREE, v, dependentObj);
574 query.createKeyQuery(dependentObj);
576 List<Vertex> items = query.toList();
578 Vertex dependentVertex = null;
579 if (items.size() == 1) {
580 dependentVertex = items.get(0);
581 this.verifyResourceVersion("update", dependentObj.getDbName(), dependentVertex.<String>property(AAIProperties.RESOURCE_VERSION).orElse(null), (String) dependentObj.getValue(AAIProperties.RESOURCE_VERSION), (String) dependentObj.getURI());
583 this.verifyResourceVersion("create", dependentObj.getDbName(), "", (String) dependentObj.getValue(AAIProperties.RESOURCE_VERSION), (String) dependentObj.getURI());
584 dependentVertex = createNewVertex(dependentObj);
587 return reflectDependentVertex(v, dependentVertex, dependentObj, requestContext);
592 * Reflect dependent vertex.
594 * @param parent the parent
595 * @param child the child
598 * @throws IllegalAccessException the illegal access exception
599 * @throws IllegalArgumentException the illegal argument exception
600 * @throws InvocationTargetException the invocation target exception
601 * @throws InstantiationException the instantiation exception
602 * @throws NoSuchMethodException the no such method exception
603 * @throws SecurityException the security exception
604 * @throws AAIException the AAI exception
605 * @throws UnsupportedEncodingException the unsupported encoding exception
606 * @throws AAIUnknownObjectException
608 private Vertex reflectDependentVertex(Vertex parent, Vertex child, Introspector obj, String requestContext) throws AAIException, UnsupportedEncodingException {
610 String parentUri = parent.<String>property(AAIProperties.AAI_URI).orElse(null);
611 if (parentUri != null) {
614 addUriIfNeeded(child, parentUri + uri);
616 processObject(obj, child, requestContext);
619 e = this.getEdgeBetween(EdgeType.TREE, parent, child, null);
622 String canBeLinked = obj.getMetadata(ObjectMetadata.CAN_BE_LINKED);
623 if (canBeLinked != null && canBeLinked.equals("true")) {
624 Loader ldrForCntxt = SpringContextAware.getBean(LoaderFactory.class).createLoaderForVersion(introspectionType, getVerForContext(requestContext));
625 boolean isFirst = !this.engine.getQueryBuilder(ldrForCntxt, parent).createEdgeTraversal(EdgeType.TREE, parent, obj).hasNext();
627 child.property(AAIProperties.LINKED, true);
630 edgeSer.addTreeEdge(this.engine.asAdmin().getTraversalSource(), parent, child);
636 private SchemaVersion getVerForContext(String requestContext) {
637 Pattern pattern = Pattern.compile("v[0-9]+");
638 Matcher m = pattern.matcher(requestContext);
642 return new SchemaVersion(requestContext);
649 * @param vertices the vertices
651 * @param depth the depth
652 * @param cleanUp the clean up
653 * @return the introspector
654 * @throws AAIException the AAI exception
655 * @throws IllegalAccessException the illegal access exception
656 * @throws IllegalArgumentException the illegal argument exception
657 * @throws InvocationTargetException the invocation target exception
658 * @throws SecurityException the security exception
659 * @throws InstantiationException the instantiation exception
660 * @throws NoSuchMethodException the no such method exception
661 * @throws UnsupportedEncodingException the unsupported encoding exception
662 * @throws MalformedURLException the malformed URL exception
663 * @throws AAIUnknownObjectException
664 * @throws URISyntaxException
666 public Introspector dbToObject(List<Vertex> vertices, final Introspector obj, int depth, boolean nodeOnly, String cleanUp) throws UnsupportedEncodingException, AAIException {
667 final int internalDepth;
668 if (depth == Integer.MAX_VALUE) {
669 internalDepth = depth--;
671 internalDepth = depth;
673 StopWatch.conditionalStart();
674 if (vertices.size() > 1 && !obj.isContainer()) {
675 dbTimeMsecs += StopWatch.stopIfStarted();
676 throw new AAIException("AAI_6136", "query object mismatch: this object cannot hold multiple items." + obj.getDbName());
677 } else if (obj.isContainer()) {
679 String listProperty = null;
680 for (String property : obj.getProperties()) {
681 if (obj.isListType(property) && obj.isComplexGenericType(property)) {
682 listProperty = property;
686 final String propertyName = listProperty;
687 getList = (List) obj.getValue(listProperty);
689 /* This is an experimental multithreading experiment
692 ExecutorService pool = GetAllPool.getInstance().getPool();
694 List<Future<Object>> futures = new ArrayList<>();
696 QueryEngine tgEngine = this.engine.getQueryEngine();
697 for (Vertex v : vertices) {
699 AaiCallable<Object> task = new AaiCallable<Object>() {
701 public Object process() throws UnsupportedEncodingException, AAIException {
702 Set<Vertex> seen = new HashSet<>();
703 Introspector childObject;
705 childObject = obj.newIntrospectorInstanceOfNestedProperty(propertyName);
706 } catch (AAIUnknownObjectException e) {
710 dbToObject(childObject, v, seen, internalDepth, nodeOnly, cleanUp);
711 } catch (UnsupportedEncodingException e) {
713 } catch (AAIException e) {
716 return childObject.getUnderlyingObject();
717 //getList.add(childObject.getUnderlyingObject());
720 futures.add(pool.submit(task));
723 for (Future<Object> future : futures) {
725 getList.add(future.get());
726 } catch (ExecutionException e) {
727 dbTimeMsecs += StopWatch.stopIfStarted();
728 throw new AAIException("AAI_4000", e);
729 } catch (InterruptedException e) {
730 dbTimeMsecs += StopWatch.stopIfStarted();
731 throw new AAIException("AAI_4000", e);
734 } else if (vertices.size() == 1) {
735 Set<Vertex> seen = new HashSet<>();
736 dbToObject(obj, vertices.get(0), seen, depth, nodeOnly, cleanUp);
741 dbTimeMsecs += StopWatch.stopIfStarted();
750 * @param seen the seen
751 * @param depth the depth
752 * @param cleanUp the clean up
753 * @return the introspector
754 * @throws IllegalAccessException the illegal access exception
755 * @throws IllegalArgumentException the illegal argument exception
756 * @throws InvocationTargetException the invocation target exception
757 * @throws SecurityException the security exception
758 * @throws InstantiationException the instantiation exception
759 * @throws NoSuchMethodException the no such method exception
760 * @throws UnsupportedEncodingException the unsupported encoding exception
761 * @throws AAIException the AAI exception
762 * @throws MalformedURLException the malformed URL exception
763 * @throws AAIUnknownObjectException
764 * @throws URISyntaxException
766 private Introspector dbToObject(Introspector obj, Vertex v, Set<Vertex> seen, int depth, boolean nodeOnly, String cleanUp) throws AAIException, UnsupportedEncodingException {
774 boolean modified = false;
775 for (String property : obj.getProperties(PropertyPredicates.isVisible())) {
776 List<Object> getList = null;
777 Vertex[] vertices = null;
779 if (!(obj.isComplexType(property) || obj.isListType(property))) {
780 this.copySimpleProperty(property, obj, v);
783 if (obj.isComplexType(property)) {
786 if (!property.equals("relationship-list") && depth >= 0) {
787 Introspector argumentObject = obj.newIntrospectorInstanceOfProperty(property);
788 Object result = dbToObject(argumentObject, v, seen, depth + 1, nodeOnly, cleanUp);
789 if (result != null) {
790 obj.setValue(property, argumentObject.getUnderlyingObject());
793 } else if (property.equals("relationship-list") && !nodeOnly) {
794 /* relationships need to be handled correctly */
795 Introspector relationshipList = obj.newIntrospectorInstanceOfProperty(property);
796 relationshipList = createRelationshipList(v, relationshipList, cleanUp);
797 if (relationshipList != null) {
799 obj.setValue(property, relationshipList.getUnderlyingObject());
804 } else if (obj.isListType(property)) {
806 if (property.equals("any")) {
809 String genericType = obj.getGenericTypeClass(property).getSimpleName();
810 if (obj.isComplexGenericType(property) && depth >= 0) {
811 final String childDbName = convertFromCamelCase(genericType);
812 String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
816 rule = edgeRules.getRule(new EdgeRuleQuery.Builder(vType, childDbName).edgeType(EdgeType.TREE).build());
817 } catch (EdgeRuleNotFoundException e) {
818 throw new NoEdgeRuleFoundException(e);
819 } catch (AmbiguousRuleChoiceException e) {
820 throw new MultipleEdgeRuleFoundException(e);
822 if (!rule.getContains().equals(AAIDirection.NONE.toString())) {
823 //vertices = this.queryEngine.findRelatedVertices(v, Direction.OUT, rule.getLabel(), childDbName);
824 Direction ruleDirection = rule.getDirection();
825 Iterator<Vertex> itr = v.vertices(ruleDirection, rule.getLabel());
826 List<Vertex> verticesList = (List<Vertex>) IteratorUtils.toList(itr);
827 itr = verticesList.stream().filter(item -> {
828 return item.property(AAIProperties.NODE_TYPE).orElse("").equals(childDbName);
831 getList = (List<Object>) obj.getValue(property);
835 while (itr.hasNext()) {
836 Vertex childVertex = itr.next();
837 if (!seen.contains(childVertex)) {
838 Introspector argumentObject = obj.newIntrospectorInstanceOfNestedProperty(property);
840 Object result = dbToObject(argumentObject, childVertex, seen, depth, nodeOnly, cleanUp);
841 if (result != null) {
842 getList.add(argumentObject.getUnderlyingObject());
848 LOGGER.warn("Cycle found while serializing vertex id={}", childVertex.id().toString());
851 if (processed == 0) {
852 //vertices were all seen, reset the list
859 } else if (obj.isSimpleGenericType(property)) {
860 List<Object> temp = this.engine.getListProperty(v, property);
862 getList = (List<Object>) obj.getValue(property);
863 getList.addAll(temp);
874 //no changes were made to this obj, discard the instance
878 this.enrichData(obj, v);
884 public Introspector getVertexProperties(Vertex v) throws AAIException, UnsupportedEncodingException {
885 String nodeType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
886 if (nodeType == null) {
887 throw new AAIException("AAI_6143");
890 Introspector obj = this.latestLoader.introspectorFromName(nodeType);
891 Set<Vertex> seen = new HashSet<>();
893 String cleanUp = "false";
894 boolean nodeOnly = true;
895 StopWatch.conditionalStart();
896 this.dbToObject(obj, v, seen, depth, nodeOnly, cleanUp);
897 dbTimeMsecs += StopWatch.stopIfStarted();
902 public Introspector getLatestVersionView(Vertex v) throws AAIException, UnsupportedEncodingException {
903 String nodeType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
904 if (nodeType == null) {
905 throw new AAIException("AAI_6143");
907 Introspector obj = this.latestLoader.introspectorFromName(nodeType);
908 Set<Vertex> seen = new HashSet<>();
909 int depth = AAIProperties.MAXIMUM_DEPTH;
910 String cleanUp = "false";
911 boolean nodeOnly = false;
912 StopWatch.conditionalStart();
913 Tree<Element> tree = this.engine.getQueryEngine().findSubGraph(v, depth, nodeOnly);
914 TreeBackedVertex treeVertex = new TreeBackedVertex(v, tree);
915 this.dbToObject(obj, treeVertex, seen, depth, nodeOnly, cleanUp);
916 dbTimeMsecs += StopWatch.stopIfStarted();
921 * Copy simple property.
923 * @param property the property
926 * @throws InstantiationException the instantiation exception
927 * @throws IllegalAccessException the illegal access exception
928 * @throws IllegalArgumentException the illegal argument exception
929 * @throws InvocationTargetException the invocation target exception
930 * @throws NoSuchMethodException the no such method exception
931 * @throws SecurityException the security exception
933 private void copySimpleProperty(String property, Introspector obj, Vertex v) {
934 final Object temp = getProperty(obj, property, v);
936 obj.setValue(property, temp);
942 * Load the introspector from the hashmap for the given property key
944 * @param property - vertex property
945 * @param obj - introspector object representing the vertex
946 * @param hashMap - Containing a list of pre-fetched properties for a given vertex
948 private void copySimplePropertyFromHashMap(String property, Introspector obj, Map<String, Object> hashMap){
950 final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(property);
951 String dbPropertyName = property;
953 if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
954 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
957 final Object temp = hashMap.getOrDefault(dbPropertyName, null);
960 obj.setValue(property, temp);
965 * Simple db to object.
969 * @throws InstantiationException the instantiation exception
970 * @throws IllegalAccessException the illegal access exception
971 * @throws IllegalArgumentException the illegal argument exception
972 * @throws InvocationTargetException the invocation target exception
973 * @throws NoSuchMethodException the no such method exception
974 * @throws SecurityException the security exception
976 private void simpleDbToObject(Introspector obj, Vertex v) {
977 for(String key : obj.getProperties()){
978 this.copySimpleProperty(key, obj, v);
983 public Map<String, Object> convertVertexToHashMap(Introspector obj, Vertex v){
985 long startTime = System.currentTimeMillis();
987 Set<String> simpleProperties = obj.getSimpleProperties(PropertyPredicates.isVisible());
988 String[] simplePropsArray = new String[simpleProperties.size()];
989 simplePropsArray = simpleProperties.toArray(simplePropsArray);
991 Map<String, Object> simplePropsHashMap = new HashMap<>(simplePropsArray.length * 2);
993 v.properties(simplePropsArray).forEachRemaining((vp) -> simplePropsHashMap.put(vp.key(), vp.value()));
995 return simplePropsHashMap;
998 public Introspector dbToRelationshipObject(Vertex v) throws UnsupportedEncodingException, AAIException {
999 Introspector relationshipList = this.latestLoader.introspectorFromName("relationship-list");
1000 relationshipList = createRelationshipList(v, relationshipList, "false");
1001 return relationshipList;
1004 * Creates the relationship list.
1007 * @param obj the obj
1008 * @param cleanUp the clean up
1009 * @return the object
1010 * @throws InstantiationException the instantiation exception
1011 * @throws IllegalAccessException the illegal access exception
1012 * @throws IllegalArgumentException the illegal argument exception
1013 * @throws InvocationTargetException the invocation target exception
1014 * @throws NoSuchMethodException the no such method exception
1015 * @throws SecurityException the security exception
1016 * @throws UnsupportedEncodingException the unsupported encoding exception
1017 * @throws AAIException the AAI exception
1018 * @throws MalformedURLException the malformed URL exception
1019 * @throws URISyntaxException
1021 private Introspector createRelationshipList(Vertex v, Introspector obj, String cleanUp) throws UnsupportedEncodingException, AAIException {
1023 String[] cousinRules = new String[0];
1026 cousinRules = edgeRules.retrieveCachedCousinLabels(obj.getDbName());
1027 } catch (ExecutionException e) {
1028 LOGGER.warn("Encountered an execution exception while retrieving labels for the node type {} using cached", obj.getDbName(), e);
1031 List<Vertex> cousins = null;
1032 if(cousinRules != null && cousinRules.length != 0){
1033 cousins = this.engine.getQueryEngine().findCousinVertices(v, cousinRules);
1035 cousins = this.engine.getQueryEngine().findCousinVertices(v);
1038 List<Object> relationshipObjList = obj.getValue("relationship");
1039 VertexProperty nodeTypeProperty = v.property(AAIProperties.NODE_TYPE);
1041 if(!nodeTypeProperty.isPresent()){
1042 LoggingContext.responseDescription(MISSING_REQUIRED_NODE_PROPERTY);
1043 LOGGER.warn("Not processing the vertex {} because its missing required property aai-node-type", v.id());
1044 LoggingContext.remove(LoggingContext.LoggingField.RESPONSE_DESCRIPTION.toString());
1048 String aNodeType = nodeTypeProperty.value().toString();
1050 TypeAlphabetizer alphabetizer = new TypeAlphabetizer();
1052 EdgeIngestor edgeIngestor = SpringContextAware.getBean(EdgeIngestor.class);
1053 Set<String> keysWithMultipleLabels = edgeIngestor.getMultipleLabelKeys();
1055 // For the given vertex, find all the cousins
1056 // For each cousin retrieve the node type and then
1057 // check if the version is greater than the edge label version
1058 // meaning is the current version equal to greater than the version
1059 // where we introduced the edge labels into the relationship payload
1060 // If it is, then we check if the edge key there are multiple labels
1061 // If there are multiple labels, then we need to go to the database
1062 // to retrieve the labels between itself and cousin vertex
1063 // If there is only single label between the edge a and b, then
1064 // we can retrieve what that is without going to the database
1065 // from using the edge rules json and get the edge rule out of it
1066 EdgeRuleQuery.Builder queryBuilder = new EdgeRuleQuery.Builder(aNodeType);
1067 for (Vertex cousin : cousins) {
1068 VertexProperty vertexProperty = cousin.property(AAIProperties.NODE_TYPE);
1069 String bNodeType = null;
1070 if(vertexProperty.isPresent()){
1071 bNodeType = cousin.property(AAIProperties.NODE_TYPE).value().toString();
1073 // If the vertex is missing the aai-node-type
1074 // Then its either a bad vertex or its in the process
1075 // of getting deleted so we should ignore these vertexes
1076 LoggingContext.responseDescription(MISSING_REQUIRED_NODE_PROPERTY);
1077 if(LOGGER.isDebugEnabled()){
1078 LOGGER.debug("For the vertex {}, unable to retrieve the aai-node-type", v.id().toString());
1080 LOGGER.info("Unable to retrieve the aai-node-type for vertex, for more info enable debug log");
1082 LoggingContext.remove(LoggingContext.LoggingField.RESPONSE_DESCRIPTION.toString());
1085 if (obj.getVersion().compareTo(schemaVersions.getEdgeLabelVersion()) >= 0) {
1086 String edgeKey = alphabetizer.buildAlphabetizedKey(aNodeType, bNodeType);
1087 if(keysWithMultipleLabels.contains(edgeKey)){
1088 List<String> edgeLabels = this.getEdgeLabelsBetween(EdgeType.COUSIN, v, cousin);
1089 for(String edgeLabel: edgeLabels){
1090 Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
1091 Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, edgeLabel);
1092 if (result != null) {
1093 relationshipObjList.add(result);
1098 EdgeRule edgeRule = null;
1100 // Create a query based on the a nodetype and b nodetype
1101 // which is also a cousin edge and ensure the version
1102 // is used properly so for example in order to be backwards
1103 // compatible if we had allowed a edge between a and b
1104 // in a previous release and we decided to remove it from
1105 // the edge rules in the future we can display the edge
1106 // only for the older apis and the new apis if the edge rule
1107 // is removed will not be seen in the newer version of the API
1109 EdgeRuleQuery ruleQuery = queryBuilder
1111 .edgeType(EdgeType.COUSIN)
1112 .version(obj.getVersion())
1116 edgeRule = edgeIngestor.getRule(ruleQuery);
1117 } catch (EdgeRuleNotFoundException e) {
1118 LOGGER.warn("Caught an edge rule not found exception for query {}, {}," +
1119 " it could be the edge rule is no longer valid for the existing edge in db",
1120 ruleQuery, LogFormatTools.getStackTop(e));
1122 } catch (AmbiguousRuleChoiceException e) {
1123 LOGGER.error("Caught an ambiguous rule not found exception for query {}, {}",
1124 ruleQuery, LogFormatTools.getStackTop(e));
1128 Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
1129 Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp,edgeRule.getLabel());
1130 if (result != null) {
1131 relationshipObjList.add(result);
1135 Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
1136 Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, null);
1137 if (result != null) {
1138 relationshipObjList.add(result);
1144 if (relationshipObjList.isEmpty()) {
1152 * Process edge relationship.
1154 * @param relationshipObj the relationship obj
1155 * @param edge the edge
1156 * @param cleanUp the clean up
1157 * @return the object
1158 * @throws InstantiationException the instantiation exception
1159 * @throws IllegalAccessException the illegal access exception
1160 * @throws IllegalArgumentException the illegal argument exception
1161 * @throws InvocationTargetException the invocation target exception
1162 * @throws NoSuchMethodException the no such method exception
1163 * @throws SecurityException the security exception
1164 * @throws UnsupportedEncodingException the unsupported encoding exception
1165 * @throws AAIException the AAI exception
1166 * @throws MalformedURLException the malformed URL exception
1167 * @throws AAIUnknownObjectException
1168 * @throws URISyntaxException
1170 private Object processEdgeRelationship(Introspector relationshipObj, Vertex cousin, String cleanUp, String edgeLabel) throws UnsupportedEncodingException, AAIUnknownObjectException {
1172 VertexProperty aaiUriProperty = cousin.property("aai-uri");
1174 if(!aaiUriProperty.isPresent()){
1178 URI uri = UriBuilder.fromUri(aaiUriProperty.value().toString()).build();
1180 URIToRelationshipObject uriParser = null;
1181 Introspector result = null;
1183 uriParser = new URIToRelationshipObject(relationshipObj.getLoader(), uri, this.baseURL);
1184 result = uriParser.getResult();
1185 } catch (AAIException | URISyntaxException e) {
1186 LOGGER.error("Error while processing edge relationship in version " + relationshipObj.getVersion() + " (bad vertex ID=" + ": "
1187 + e.getMessage() + " " + LogFormatTools.getStackTop(e));
1191 VertexProperty cousinVertexNodeType = cousin.property(AAIProperties.NODE_TYPE);
1193 if(cousinVertexNodeType.isPresent()){
1194 String cousinType = cousinVertexNodeType.value().toString();
1195 if(namedPropNodes.contains(cousinType)){
1196 this.addRelatedToProperty(result, cousin, cousinType);
1200 if (edgeLabel != null && result.hasProperty("relationship-label")) {
1201 result.setValue("relationship-label", edgeLabel);
1204 return result.getUnderlyingObject();
1208 * Gets the URI for vertex.
1211 * @return the URI for vertex
1212 * @throws InstantiationException the instantiation exception
1213 * @throws IllegalAccessException the illegal access exception
1214 * @throws IllegalArgumentException the illegal argument exception
1215 * @throws InvocationTargetException the invocation target exception
1216 * @throws NoSuchMethodException the no such method exception
1217 * @throws SecurityException the security exception
1218 * @throws UnsupportedEncodingException the unsupported encoding exception
1219 * @throws AAIUnknownObjectException
1221 public URI getURIForVertex(Vertex v) throws UnsupportedEncodingException {
1223 return getURIForVertex(v, false);
1226 public URI getURIForVertex(Vertex v, boolean overwrite) throws UnsupportedEncodingException {
1227 URI uri = UriBuilder.fromPath("/unknown-uri").build();
1229 String aaiUri = v.<String>property(AAIProperties.AAI_URI).orElse(null);
1231 if (aaiUri != null && !overwrite) {
1232 uri = UriBuilder.fromPath(aaiUri).build();
1239 * Gets the URI from list.
1241 * @param list the list
1242 * @return the URI from list
1243 * @throws UnsupportedEncodingException the unsupported encoding exception
1245 private URI getURIFromList(List<Introspector> list) throws UnsupportedEncodingException {
1247 StringBuilder sb = new StringBuilder();
1248 for (Introspector i : list) {
1249 sb.insert(0, i.getURI());
1252 uri = sb.toString();
1253 return UriBuilder.fromPath(uri).build();
1256 public void addRelatedToProperty(Introspector relationship, Vertex cousinVertex, String cousinType) throws AAIUnknownObjectException {
1257 Introspector obj = this.latestLoader.introspectorFromName(cousinType);
1258 String nameProps = obj.getMetadata(ObjectMetadata.NAME_PROPS);
1259 List<Introspector> relatedToProperties = new ArrayList<>();
1261 if (nameProps != null) {
1262 String[] props = nameProps.split(",");
1263 for (String prop : props) {
1264 final Object temp = getProperty(obj, prop, cousinVertex);
1265 Introspector relatedTo = relationship.newIntrospectorInstanceOfNestedProperty("related-to-property");
1266 relatedTo.setValue("property-key", cousinType + "." + prop);
1267 relatedTo.setValue("property-value", temp);
1268 relatedToProperties.add(relatedTo);
1272 if (!relatedToProperties.isEmpty()) {
1273 List relatedToList = (List) relationship.getValue("related-to-property");
1274 for (Introspector introspector : relatedToProperties) {
1275 relatedToList.add(introspector.getUnderlyingObject());
1281 private Object getProperty(Introspector obj, String prop, Vertex vertex){
1283 final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(prop);
1284 String dbPropertyName = prop;
1286 if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
1287 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
1290 return vertex.<Object>property(dbPropertyName).orElse(null);
1296 * @param relationship the relationship
1297 * @param inputVertex the input vertex
1298 * @return true, if successful
1299 * @throws UnsupportedEncodingException the unsupported encoding exception
1300 * @throws AAIException the AAI exception
1302 public boolean createEdge(Introspector relationship, Vertex inputVertex) throws UnsupportedEncodingException, AAIException {
1304 Vertex relatedVertex = null;
1305 StopWatch.conditionalStart();
1306 QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(relationship);
1308 String label = null;
1309 if (relationship.hasProperty("relationship-label")) {
1310 label = relationship.getValue("relationship-label");
1313 List<Vertex> results = parser.getQueryBuilder().toList();
1314 if (results.isEmpty()) {
1315 dbTimeMsecs += StopWatch.stopIfStarted();
1316 AAIException e = new AAIException("AAI_6129", "Node of type " + parser.getResultType() + ". Could not find object at: " + parser.getUri());
1317 e.getTemplateVars().add(parser.getResultType());
1318 e.getTemplateVars().add(parser.getUri().toString());
1321 //still an issue if there's more than one
1322 relatedVertex = results.get(0);
1325 if (relatedVertex != null) {
1329 e = this.getEdgeBetween(EdgeType.COUSIN, inputVertex, relatedVertex, label);
1331 edgeSer.addEdge(this.engine.asAdmin().getTraversalSource(), inputVertex, relatedVertex, label);
1333 //attempted to link two vertexes already linked
1336 dbTimeMsecs += StopWatch.stopIfStarted();
1340 dbTimeMsecs += StopWatch.stopIfStarted();
1345 * Gets all the edges between of the type with the specified label.
1347 * @param aVertex the out vertex
1348 * @param bVertex the in vertex
1349 * @return the edges between
1350 * @throws AAIException the AAI exception
1351 * @throws NoEdgeRuleFoundException
1353 private Edge getEdgeBetweenWithLabel(EdgeType type, Vertex aVertex, Vertex bVertex, EdgeRule edgeRule) {
1357 if (bVertex != null) {
1358 GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1359 if (EdgeType.TREE.equals(type)) {
1360 GraphTraversal<Vertex,Vertex> findVertex = this.engine.asAdmin().getTraversalSource().V(bVertex);
1361 if(edgeRule.getDirection().equals(Direction.IN)){
1362 findEdgesBetween = findVertex.outE(edgeRule.getLabel())
1363 .has(EdgeProperty.CONTAINS.toString(), edgeRule.getContains())
1365 __.has(EdgeField.PRIVATE.toString(), true)
1368 findEdgesBetween = findVertex.inE(edgeRule.getLabel())
1369 .has(EdgeProperty.CONTAINS.toString(), edgeRule.getContains())
1371 __.has(EdgeField.PRIVATE.toString(), true)
1374 findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(aVertex.id())).limit(1);
1376 findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE(edgeRule.getLabel());
1377 findEdgesBetween = findEdgesBetween
1378 .has(EdgeProperty.CONTAINS.toString(), "NONE")
1380 __.has(EdgeField.PRIVATE.toString(), true)
1382 findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id())).limit(1);
1384 List<Edge> list = findEdgesBetween.toList();
1385 if(!list.isEmpty()){
1386 result = list.get(0);
1394 * Gets all the edges between of the type.
1396 * @param aVertex the out vertex
1397 * @param bVertex the in vertex
1398 * @return the edges between
1399 * @throws AAIException the AAI exception
1400 * @throws NoEdgeRuleFoundException
1402 private List<Edge> getEdgesBetween(EdgeType type, Vertex aVertex, Vertex bVertex) {
1404 List<Edge> result = new ArrayList<>();
1406 if (bVertex != null) {
1407 GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1408 findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE();
1409 if (EdgeType.TREE.equals(type)) {
1410 findEdgesBetween = findEdgesBetween
1413 __.has(EdgeProperty.CONTAINS.toString(), "NONE"),
1414 __.has(EdgeField.PRIVATE.toString(), true)
1418 findEdgesBetween = findEdgesBetween
1419 .has(EdgeProperty.CONTAINS.toString(), "NONE")
1421 __.has(EdgeField.PRIVATE.toString(), true)
1424 findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id()));
1425 result = findEdgesBetween.toList();
1432 * Gets all the edges string between of the type.
1434 * @param aVertex the out vertex
1435 * @param bVertex the in vertex
1436 * @return the edges between
1437 * @throws AAIException the AAI exception
1438 * @throws NoEdgeRuleFoundException
1440 private List<String> getEdgeLabelsBetween(EdgeType type, Vertex aVertex, Vertex bVertex) {
1442 List<String> result = new ArrayList<>();
1444 if (bVertex != null) {
1445 GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1446 findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE();
1447 if (EdgeType.TREE.equals(type)) {
1448 findEdgesBetween = findEdgesBetween
1451 __.has(EdgeProperty.CONTAINS.toString(), "NONE"),
1452 __.has(EdgeField.PRIVATE.toString(), true)
1456 findEdgesBetween = findEdgesBetween
1457 .has(EdgeProperty.CONTAINS.toString(), "NONE")
1459 __.has(EdgeField.PRIVATE.toString(), true)
1462 findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id()));
1463 result = findEdgesBetween.label().toList();
1469 * Gets all the edges string between of the type.
1471 * @param aVertex the out vertex
1472 * @param bVertex the in vertex
1473 * @return the edges between
1474 * @throws AAIException the AAI exception
1475 * @throws NoEdgeRuleFoundException
1477 private Long getEdgeLabelsCount(Vertex aVertex, Vertex bVertex) {
1481 if (bVertex != null) {
1482 GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1483 findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE();
1484 findEdgesBetween = findEdgesBetween
1485 .has(EdgeProperty.CONTAINS.toString(), "NONE")
1487 __.has(EdgeField.PRIVATE.toString(), true)
1489 findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id()));
1490 result = findEdgesBetween.count().next();
1495 * Gets all the edges between the vertexes with the label and type.
1497 * @param aVertex the out vertex
1498 * @param bVertex the in vertex
1500 * @return the edges between
1501 * @throws AAIException the AAI exception
1503 private Edge getEdgesBetween(EdgeType type, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
1507 if (bVertex != null) {
1508 String aType = aVertex.<String>property(AAIProperties.NODE_TYPE).value();
1509 String bType = bVertex.<String>property(AAIProperties.NODE_TYPE).value();
1510 EdgeRuleQuery q = new EdgeRuleQuery.Builder(aType, bType).edgeType(type).label(label).build();
1513 rule = edgeRules.getRule(q);
1514 } catch (EdgeRuleNotFoundException e) {
1515 throw new NoEdgeRuleFoundException(e);
1516 } catch (AmbiguousRuleChoiceException e) {
1517 throw new MultipleEdgeRuleFoundException(e);
1519 edge = this.getEdgeBetweenWithLabel(type, aVertex, bVertex, rule);
1526 * Gets the edge between with the label and edge type.
1528 * @param aVertex the out vertex
1529 * @param bVertex the in vertex
1531 * @return the edge between
1532 * @throws AAIException the AAI exception
1533 * @throws NoEdgeRuleFoundException
1535 public Edge getEdgeBetween(EdgeType type, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
1537 StopWatch.conditionalStart();
1538 if (bVertex != null) {
1540 Edge edge = this.getEdgesBetween(type, aVertex, bVertex, label);
1542 dbTimeMsecs += StopWatch.stopIfStarted();
1547 dbTimeMsecs += StopWatch.stopIfStarted();
1551 public Edge getEdgeBetween(EdgeType type, Vertex aVertex, Vertex bVertex) throws AAIException {
1552 return this.getEdgeBetween(type, aVertex, bVertex, null);
1559 * @param relationship the relationship
1560 * @param inputVertex the input vertex
1561 * @return true, if successful
1562 * @throws UnsupportedEncodingException the unsupported encoding exception
1563 * @throws AAIException the AAI exception
1565 public boolean deleteEdge(Introspector relationship, Vertex inputVertex) throws UnsupportedEncodingException, AAIException {
1567 Vertex relatedVertex = null;
1568 StopWatch.conditionalStart();
1569 QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(relationship);
1571 List<Vertex> results = parser.getQueryBuilder().toList();
1573 String label = null;
1574 if (relationship.hasProperty("relationship-label")) {
1575 label = relationship.getValue("relationship-label");
1578 if (results.isEmpty()) {
1579 dbTimeMsecs += StopWatch.stopIfStarted();
1583 relatedVertex = results.get(0);
1586 edge = this.getEdgeBetween(EdgeType.COUSIN, inputVertex, relatedVertex, label);
1587 } catch (NoEdgeRuleFoundException e) {
1588 dbTimeMsecs += StopWatch.stopIfStarted();
1589 throw new AAIException("AAI_6129", e);
1593 dbTimeMsecs += StopWatch.stopIfStarted();
1596 dbTimeMsecs += StopWatch.stopIfStarted();
1603 * Delete items with traversal.
1605 * @param vertexes the vertexes
1606 * @throws IllegalStateException the illegal state exception
1608 public void deleteItemsWithTraversal(List<Vertex> vertexes) throws IllegalStateException {
1610 for (Vertex v : vertexes) {
1611 LOGGER.debug("About to delete the vertex with id: " + v.id());
1612 deleteWithTraversal(v);
1618 * Delete with traversal.
1620 * @param startVertex the start vertex
1622 public void deleteWithTraversal(Vertex startVertex) {
1623 StopWatch.conditionalStart();
1624 List<Vertex> results = this.engine.getQueryEngine().findDeletable(startVertex);
1626 for (Vertex v : results) {
1627 LOGGER.warn("Removing vertex " + v.id().toString());
1631 dbTimeMsecs += StopWatch.stopIfStarted();
1638 * @param resourceVersion the resource version
1639 * @throws IllegalArgumentException the illegal argument exception
1640 * @throws AAIException the AAI exception
1641 * @throws InterruptedException the interrupted exception
1643 public void delete(Vertex v, List<Vertex> deletableVertices, String resourceVersion, boolean enableResourceVersion) throws IllegalArgumentException, AAIException {
1645 boolean result = verifyDeleteSemantics(v, resourceVersion, enableResourceVersion);
1647 * The reason why I want to call PreventDeleteSemantics second time is to catch the prevent-deletes in a chain
1648 * These are far-fewer than seeing a prevnt-delete on the vertex to be deleted
1649 * So its better to make these in 2 steps
1651 if (result && !deletableVertices.isEmpty()) {
1652 result = verifyPreventDeleteSemantics(deletableVertices);
1657 deleteWithTraversal(v);
1658 } catch (IllegalStateException e) {
1659 throw new AAIException("AAI_6110", e);
1671 * @param resourceVersion the resource version
1672 * @throws IllegalArgumentException the illegal argument exception
1673 * @throws AAIException the AAI exception
1674 * @throws InterruptedException the interrupted exception
1676 public void delete(Vertex v, String resourceVersion, boolean enableResourceVersion) throws IllegalArgumentException, AAIException {
1678 boolean result = verifyDeleteSemantics(v, resourceVersion, enableResourceVersion);
1683 deleteWithTraversal(v);
1684 } catch (IllegalStateException e) {
1685 throw new AAIException("AAI_6110", e);
1693 * Verify delete semantics.
1695 * @param vertex the vertex
1696 * @param resourceVersion the resource version
1697 * @return true, if successful
1698 * @throws AAIException the AAI exception
1700 private boolean verifyDeleteSemantics(Vertex vertex, String resourceVersion, boolean enableResourceVersion) throws AAIException {
1701 boolean result = true;
1702 String nodeType = "";
1703 String errorDetail = " unknown delete semantic found";
1704 String aaiExceptionCode = "";
1705 nodeType = vertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
1706 if (enableResourceVersion && !this.verifyResourceVersion("delete", nodeType, vertex.<String>property(AAIProperties.RESOURCE_VERSION).orElse(null), resourceVersion, nodeType)) {
1708 List<Vertex> vertices = new ArrayList<Vertex>();
1709 vertices.add(vertex);
1710 result = verifyPreventDeleteSemantics(vertices);
1716 * Verify Prevent delete semantics.
1718 * @param vertices the list of vertices
1719 * @return true, if successful
1720 * @throws AAIException the AAI exception
1722 private boolean verifyPreventDeleteSemantics(List<Vertex> vertices) throws AAIException {
1723 boolean result = true;
1724 String nodeType = "";
1725 String errorDetail = " unknown delete semantic found";
1726 String aaiExceptionCode = "";
1728 StopWatch.conditionalStart();
1730 * This takes in all the vertices in a cascade-delete-chain and checks if there is any edge with a "prevent-delete" condition
1731 * If yes - that should prevent the deletion of the vertex
1732 * Dedup makes sure we dont capture the prevent-delete vertices twice
1733 * The prevent-delete vertices are stored so that the error message displays what prevents the delete
1736 List<Object> preventDeleteVertices = this.engine.asAdmin().getReadOnlyTraversalSource().V(vertices).
1737 union(__.inE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.IN.toString()).outV().values(AAIProperties.NODE_TYPE),
1738 __.outE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.OUT.toString()).inV().values(AAIProperties.NODE_TYPE))
1741 dbTimeMsecs += StopWatch.stopIfStarted();
1742 if (!preventDeleteVertices.isEmpty()) {
1743 aaiExceptionCode = "AAI_6110";
1744 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);
1748 throw new AAIException(aaiExceptionCode, errorDetail);
1754 * Verify resource version.
1756 * @param action the action
1757 * @param nodeType the node type
1758 * @param currentResourceVersion the current resource version
1759 * @param resourceVersion the resource version
1760 * @param uri the uri
1761 * @return true, if successful
1762 * @throws AAIException the AAI exception
1764 public boolean verifyResourceVersion(String action, String nodeType, String currentResourceVersion, String resourceVersion, String uri) throws AAIException {
1765 String enabled = "";
1766 String errorDetail = "";
1767 String aaiExceptionCode = "";
1768 boolean isDeleteResourceVersionOk = true;
1769 if (currentResourceVersion == null) {
1770 currentResourceVersion = "";
1773 if (resourceVersion == null) {
1774 resourceVersion = "";
1777 enabled = AAIConfig.get(AAIConstants.AAI_RESVERSION_ENABLEFLAG);
1779 } catch (AAIException e) {
1780 ErrorLogHelper.logException(e);
1782 if (enabled.equals("true")) {
1783 if ("delete".equals(action)) {
1784 isDeleteResourceVersionOk = verifyResourceVersionForDelete(currentResourceVersion, resourceVersion);
1786 if ((!isDeleteResourceVersionOk) || ((!"delete".equals(action)) && (!currentResourceVersion.equals(resourceVersion)))) {
1787 if ("create".equals(action) && !resourceVersion.equals("")) {
1788 errorDetail = "resource-version passed for " + action + " of " + uri;
1789 aaiExceptionCode = "AAI_6135";
1790 } else if (resourceVersion.equals("")) {
1791 errorDetail = "resource-version not passed for " + action + " of " + uri;
1792 aaiExceptionCode = "AAI_6130";
1794 errorDetail = "resource-version MISMATCH for " + action + " of " + uri;
1795 aaiExceptionCode = "AAI_6131";
1798 throw new AAIException(aaiExceptionCode, errorDetail);
1806 * Verify resource version for delete.
1808 * @param currentResourceVersion the current resource version
1809 * @param resourceVersion the resource version
1810 * @return true, if successful or false if there is a mismatch
1812 private boolean verifyResourceVersionForDelete(String currentResourceVersion, String resourceVersion) {
1814 boolean isDeleteResourceVersionOk = true;
1815 String resourceVersionDisabledUuid = AAIConfig.get(AAIConstants.AAI_RESVERSION_DISABLED_UUID,
1816 AAIConstants.AAI_RESVERSION_DISABLED_UUID_DEFAULT);
1818 if ((!currentResourceVersion.equals(resourceVersion)) && (!resourceVersion.equals(resourceVersionDisabledUuid))) {
1819 isDeleteResourceVersionOk = false;
1821 return isDeleteResourceVersionOk;
1825 * Convert from camel case.
1827 * @param name the name
1828 * @return the string
1830 private String convertFromCamelCase(String name) {
1832 result = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, name);
1834 NamingExceptions exceptions = NamingExceptions.getInstance();
1835 result = exceptions.getDBName(result);
1840 private boolean canModify(Introspector obj, String propName, String requestContext) {
1841 final String readOnly = obj.getPropertyMetadata(propName).get(PropertyMetadata.READ_ONLY);
1842 if (readOnly != null) {
1843 final String[] items = readOnly.split(",");
1844 for (String item : items) {
1845 if (requestContext.equals(item)) {
1853 private void executePreSideEffects(Introspector obj, Vertex self) throws AAIException {
1855 SideEffectRunner runner = new SideEffectRunner
1856 .Builder(this.engine, this).addSideEffect(DataCopy.class).addSideEffect(PrivateEdge.class).build();
1858 runner.execute(obj, self);
1861 private void executePostSideEffects(Introspector obj, Vertex self) throws AAIException {
1863 SideEffectRunner runner = new SideEffectRunner
1864 .Builder(this.engine, this).addSideEffect(DataLinkWriter.class).build();
1866 runner.execute(obj, self);
1869 private void enrichData(Introspector obj, Vertex self) throws AAIException {
1871 SideEffectRunner runner = new SideEffectRunner
1872 .Builder(this.engine, this).addSideEffect(DataLinkReader.class).build();
1874 runner.execute(obj, self);
1877 public double getDBTimeMsecs() {
1878 return (dbTimeMsecs);
1882 * Db to object With Filters
1883 * This is for a one-time run with Tenant Isloation to only filter relationships
1884 * TODO: Chnage the original dbToObject to take filter parent/cousins
1886 * @param obj the obj
1887 * @param v the vertex from the graph
1888 * @param depth the depth
1889 * @param nodeOnly specify if to exclude relationships or not
1890 * @param filterCousinNodes
1891 * @return the introspector
1892 * @throws AAIException the AAI exception
1893 * @throws IllegalAccessException the illegal access exception
1894 * @throws IllegalArgumentException the illegal argument exception
1895 * @throws InvocationTargetException the invocation target exception
1896 * @throws SecurityException the security exception
1897 * @throws InstantiationException the instantiation exception
1898 * @throws NoSuchMethodException the no such method exception
1899 * @throws UnsupportedEncodingException the unsupported encoding exception
1900 * @throws MalformedURLException the malformed URL exception
1901 * @throws AAIUnknownObjectException
1902 * @throws URISyntaxException
1904 //TODO - See if you can merge the 2 dbToObjectWithFilters
1905 public Introspector dbToObjectWithFilters(Introspector obj, Vertex v, Set<Vertex> seen, int depth, boolean nodeOnly, List<String> filterCousinNodes, List<String> filterParentNodes) throws AAIException, UnsupportedEncodingException {
1906 String cleanUp = "false";
1912 boolean modified = false;
1913 for (String property : obj.getProperties(PropertyPredicates.isVisible())) {
1914 List<Object> getList = null;
1915 Vertex[] vertices = null;
1917 if (!(obj.isComplexType(property) || obj.isListType(property))) {
1918 this.copySimpleProperty(property, obj, v);
1921 if (obj.isComplexType(property)) {
1922 /* container case */
1924 if (!property.equals("relationship-list") && depth >= 0) {
1925 Introspector argumentObject = obj.newIntrospectorInstanceOfProperty(property);
1926 Object result = dbToObjectWithFilters(argumentObject, v, seen, depth + 1, nodeOnly, filterCousinNodes, filterParentNodes);
1927 if (result != null) {
1928 obj.setValue(property, argumentObject.getUnderlyingObject());
1931 } else if (property.equals("relationship-list") && !nodeOnly) {
1932 /* relationships need to be handled correctly */
1933 Introspector relationshipList = obj.newIntrospectorInstanceOfProperty(property);
1934 relationshipList = createFilteredRelationshipList(v, relationshipList, cleanUp, filterCousinNodes);
1935 if (relationshipList != null) {
1937 obj.setValue(property, relationshipList.getUnderlyingObject());
1942 } else if (obj.isListType(property)) {
1944 if (property.equals("any")) {
1947 String genericType = obj.getGenericTypeClass(property).getSimpleName();
1948 if (obj.isComplexGenericType(property) && depth >= 0) {
1949 final String childDbName = convertFromCamelCase(genericType);
1950 String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
1953 boolean isthisParentRequired = filterParentNodes.parallelStream().anyMatch(childDbName::contains);
1955 EdgeRuleQuery q = new EdgeRuleQuery.Builder(vType, childDbName).edgeType(EdgeType.TREE).build();
1958 rule = edgeRules.getRule(q);
1959 } catch (EdgeRuleNotFoundException e) {
1960 throw new NoEdgeRuleFoundException(e);
1961 } catch (AmbiguousRuleChoiceException e) {
1962 throw new MultipleEdgeRuleFoundException(e);
1964 if (!rule.getContains().equals(AAIDirection.NONE.toString()) && isthisParentRequired) {
1965 //vertices = this.queryEngine.findRelatedVertices(v, Direction.OUT, rule.getLabel(), childDbName);
1966 Direction ruleDirection = rule.getDirection();
1967 Iterator<Vertex> itr = v.vertices(ruleDirection, rule.getLabel());
1968 List<Vertex> verticesList = (List<Vertex>) IteratorUtils.toList(itr);
1969 itr = verticesList.stream().filter(item -> {
1970 return item.property(AAIProperties.NODE_TYPE).orElse("").equals(childDbName);
1972 if (itr.hasNext()) {
1973 getList = (List<Object>) obj.getValue(property);
1977 while (itr.hasNext()) {
1978 Vertex childVertex = itr.next();
1979 if (!seen.contains(childVertex)) {
1980 Introspector argumentObject = obj.newIntrospectorInstanceOfNestedProperty(property);
1982 Object result = dbToObjectWithFilters(argumentObject, childVertex, seen, depth, nodeOnly, filterCousinNodes, filterParentNodes);
1983 if (result != null) {
1984 getList.add(argumentObject.getUnderlyingObject());
1990 LOGGER.warn("Cycle found while serializing vertex id={}", childVertex.id().toString());
1993 if (processed == 0) {
1994 //vertices were all seen, reset the list
1997 if (processed > 0) {
2001 } else if (obj.isSimpleGenericType(property)) {
2002 List<Object> temp = this.engine.getListProperty(v, property);
2004 getList = (List<Object>) obj.getValue(property);
2005 getList.addAll(temp);
2016 //no changes were made to this obj, discard the instance
2020 this.enrichData(obj, v);
2026 * Creates the relationship list with the filtered node types.
2029 * @param obj the obj
2030 * @param cleanUp the clean up
2031 * @return the object
2032 * @throws InstantiationException the instantiation exception
2033 * @throws IllegalAccessException the illegal access exception
2034 * @throws IllegalArgumentException the illegal argument exception
2035 * @throws InvocationTargetException the invocation target exception
2036 * @throws NoSuchMethodException the no such method exception
2037 * @throws SecurityException the security exception
2038 * @throws UnsupportedEncodingException the unsupported encoding exception
2039 * @throws AAIException the AAI exception
2040 * @throws MalformedURLException the malformed URL exception
2041 * @throws URISyntaxException
2043 private Introspector createFilteredRelationshipList(Vertex v, Introspector obj, String cleanUp, List<String> filterNodes) throws UnsupportedEncodingException, AAIException {
2044 List<Vertex> allCousins = this.engine.getQueryEngine().findCousinVertices(v);
2046 Iterator<Vertex> cousinVertices = allCousins.stream().filter(item -> {
2047 String node = (String) item.property(AAIProperties.NODE_TYPE).orElse("");
2048 return filterNodes.parallelStream().anyMatch(node::contains);
2052 List<Vertex> cousins = (List<Vertex>) IteratorUtils.toList(cousinVertices);
2054 //items.parallelStream().anyMatch(inputStr::contains)
2055 List<Object> relationshipObjList = obj.getValue("relationship");
2056 for (Vertex cousin : cousins) {
2058 Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
2059 Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, null);
2060 if (result != null) {
2061 relationshipObjList.add(result);
2067 if (relationshipObjList.isEmpty()) {