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.thinkaurelius.titan.core.SchemaViolationException;
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.javatuples.Pair;
33 import org.javatuples.Triplet;
34 import org.onap.aai.db.props.AAIProperties;
35 import org.onap.aai.exceptions.AAIException;
36 import org.onap.aai.introspection.*;
37 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
38 import org.onap.aai.introspection.sideeffect.DataCopy;
39 import org.onap.aai.introspection.sideeffect.DataLinkReader;
40 import org.onap.aai.introspection.sideeffect.DataLinkWriter;
41 import org.onap.aai.introspection.sideeffect.SideEffectRunner;
42 import org.onap.aai.logging.ErrorLogHelper;
43 import org.onap.aai.logging.LogFormatTools;
44 import org.onap.aai.parsers.query.QueryParser;
45 import org.onap.aai.parsers.uri.URIParser;
46 import org.onap.aai.parsers.uri.URIToRelationshipObject;
47 import org.onap.aai.query.builder.QueryBuilder;
48 import org.onap.aai.schema.enums.ObjectMetadata;
49 import org.onap.aai.schema.enums.PropertyMetadata;
50 import org.onap.aai.serialization.db.exceptions.NoEdgeRuleFoundException;
51 import org.onap.aai.serialization.db.util.VersionChecker;
52 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
53 import org.onap.aai.serialization.tinkerpop.TreeBackedVertex;
54 import org.onap.aai.util.AAIApiServerURLBase;
55 import org.onap.aai.util.AAIConfig;
56 import org.onap.aai.util.AAIConstants;
57 import org.onap.aai.workarounds.NamingExceptions;
58 import org.onap.aai.logging.StopWatch;
60 import javax.ws.rs.core.UriBuilder;
61 import java.io.UnsupportedEncodingException;
62 import java.lang.reflect.Array;
63 import java.lang.reflect.InvocationTargetException;
64 import java.net.MalformedURLException;
66 import java.net.URISyntaxException;
68 import java.util.concurrent.Callable;
69 import java.util.concurrent.ExecutionException;
70 import java.util.concurrent.ExecutorService;
71 import java.util.concurrent.Future;
72 import java.util.regex.Matcher;
73 import java.util.regex.Pattern;
75 public class DBSerializer {
77 private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(DBSerializer.class);
79 private final TransactionalGraphEngine engine;
80 private final String sourceOfTruth;
81 private final ModelType introspectionType;
82 private final Version version;
83 private final Loader latestLoader;
84 private final EdgeRules edgeRules = EdgeRules.getInstance();
85 private final Loader loader;
86 private final String baseURL;
87 private double dbTimeMsecs = 0;
89 * Instantiates a new DB serializer.
91 * @param version the version
92 * @param engine the engine
94 * @param introspectionType the introspection type
95 * @param sourceOfTruth the source of truth
96 * @param llBuilder the ll builder
97 * @throws AAIException
99 public DBSerializer(Version version, TransactionalGraphEngine engine, ModelType introspectionType, String sourceOfTruth) throws AAIException {
100 this.engine = engine;
101 this.sourceOfTruth = sourceOfTruth;
102 this.introspectionType = introspectionType;
103 this.latestLoader = LoaderFactory.createLoaderForVersion(introspectionType, AAIProperties.LATEST);
104 this.version = version;
105 this.loader = LoaderFactory.createLoaderForVersion(introspectionType, version);
106 this.baseURL = AAIApiServerURLBase.get(version);
110 * Touch standard vertex properties.
113 * @param isNewVertex the is new vertex
115 public void touchStandardVertexProperties(Vertex v, boolean isNewVertex) {
117 String timeNowInSec = Long.toString(System.currentTimeMillis());
119 v.property(AAIProperties.SOURCE_OF_TRUTH, this.sourceOfTruth);
120 v.property(AAIProperties.CREATED_TS, timeNowInSec);
121 v.property(AAIProperties.AAI_UUID, UUID.randomUUID().toString());
123 v.property(AAIProperties.RESOURCE_VERSION, timeNowInSec );
124 v.property(AAIProperties.LAST_MOD_TS, timeNowInSec);
125 v.property(AAIProperties.LAST_MOD_SOURCE_OF_TRUTH, this.sourceOfTruth);
129 private void touchStandardVertexProperties(String nodeType, Vertex v, boolean isNewVertex) {
131 v.property(AAIProperties.NODE_TYPE, nodeType);
132 touchStandardVertexProperties(v, isNewVertex);
139 * Creates the new vertex.
141 * @param wrappedObject the wrapped object
143 * @throws UnsupportedEncodingException the unsupported encoding exception
144 * @throws AAIException the AAI exception
146 public Vertex createNewVertex(Introspector wrappedObject) {
149 StopWatch.conditionalStart();
150 v = this.engine.tx().addVertex();
151 touchStandardVertexProperties(wrappedObject.getDbName(), v, true);
154 dbTimeMsecs += StopWatch.stopIfStarted();
162 * @param className the class name
166 * Removes the classpath from a class name
168 public String trimClassName (String className) {
169 String returnValue = "";
171 if (className.lastIndexOf('.') == -1) {
174 returnValue = className.substring(className.lastIndexOf('.') + 1, className.length());
184 * @param uriQuery the uri query
185 * @param identifier the identifier
186 * @throws SecurityException the security exception
187 * @throws IllegalAccessException the illegal access exception
188 * @throws IllegalArgumentException the illegal argument exception
189 * @throws InvocationTargetException the invocation target exception
190 * @throws InstantiationException the instantiation exception
191 * @throws InterruptedException the interrupted exception
192 * @throws NoSuchMethodException the no such method exception
193 * @throws AAIException the AAI exception
194 * @throws UnsupportedEncodingException the unsupported encoding exception
195 * @throws AAIUnknownObjectException
197 public void serializeToDb(Introspector obj, Vertex v, QueryParser uriQuery, String identifier, String requestContext) throws AAIException, UnsupportedEncodingException {
198 StopWatch.conditionalStart();
200 if (uriQuery.isDependent()) {
201 //try to find the parent
202 List<Vertex> vertices = uriQuery.getQueryBuilder().getParentQuery().toList();
203 if (!vertices.isEmpty()) {
204 Vertex parent = vertices.get(0);
205 this.reflectDependentVertex(parent, v, obj, requestContext);
207 dbTimeMsecs += StopWatch.stopIfStarted();
208 throw new AAIException("AAI_6114", "No parent Node of type " + uriQuery.getParentResultType() + " for " + identifier);
211 serializeSingleVertex(v, obj, requestContext);
214 } catch (SchemaViolationException e) {
215 dbTimeMsecs += StopWatch.stopIfStarted();
216 throw new AAIException("AAI_6117", e);
218 dbTimeMsecs += StopWatch.stopIfStarted();
221 public void serializeSingleVertex(Vertex v, Introspector obj, String requestContext) throws UnsupportedEncodingException, AAIException {
222 StopWatch.conditionalStart();
224 boolean isTopLevel = obj.isTopLevel();
226 v.property(AAIProperties.AAI_URI, obj.getURI());
229 processObject(obj, v, requestContext);
231 URI uri = this.getURIForVertex(v);
232 URIParser parser = new URIParser(this.loader, uri);
233 if (parser.validate()) {
234 VertexProperty<String> uriProp = v.property(AAIProperties.AAI_URI);
235 if (!uriProp.isPresent() || uriProp.isPresent() && !uriProp.value().equals(uri.toString())) {
236 v.property(AAIProperties.AAI_URI, uri.toString());
240 } catch (SchemaViolationException e) {
241 throw new AAIException("AAI_6117", e);
244 dbTimeMsecs += StopWatch.stopIfStarted();
251 * @param <T> the generic type
255 * @throws IllegalAccessException the illegal access exception
256 * @throws IllegalArgumentException the illegal argument exception
257 * @throws InvocationTargetException the invocation target exception
258 * @throws InstantiationException the instantiation exception
259 * @throws NoSuchMethodException the no such method exception
260 * @throws SecurityException the security exception
261 * @throws AAIException the AAI exception
262 * @throws UnsupportedEncodingException the unsupported encoding exception
263 * @throws AAIUnknownObjectException
266 * Helper method for reflectToDb
267 * Handles all the property setting
269 private <T> List<Vertex> processObject (Introspector obj, Vertex v, String requestContext) throws UnsupportedEncodingException, AAIException {
270 Set<String> properties = new LinkedHashSet<>(obj.getProperties());
271 properties.remove(AAIProperties.RESOURCE_VERSION);
272 List<Vertex> dependentVertexes = new ArrayList<>();
273 List<Vertex> processedVertexes = new ArrayList<>();
274 boolean isComplexType = false;
275 boolean isListType = false;
276 if (!obj.isContainer()) {
277 this.touchStandardVertexProperties(obj.getDbName(), v, false);
279 this.executePreSideEffects(obj, v);
280 for (String property : properties) {
282 final String propertyType;
283 propertyType = obj.getType(property);
284 isComplexType = obj.isComplexType(property);
285 isListType = obj.isListType(property);
286 value = obj.getValue(property);
288 if (!(isComplexType || isListType)) {
289 boolean canModify = this.canModify(obj, property, requestContext);
292 final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(property);
293 String dbProperty = property;
294 if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
295 dbProperty = metadata.get(PropertyMetadata.DB_ALIAS);
297 if (metadata.containsKey(PropertyMetadata.DATA_LINK)) {
298 //data linked properties are ephemeral
299 //they are populated dynamically on GETs
303 if (!value.equals(v.property(dbProperty).orElse(null))) {
304 if (propertyType.toLowerCase().contains(".long")) {
305 v.property(dbProperty, new Integer(((Long)value).toString()));
307 v.property(dbProperty, value);
311 v.property(dbProperty).remove();
314 } else if (isListType) {
315 List<Object> list = (List<Object>)value;
316 if (obj.isComplexGenericType(property)) {
318 for (Object o : list) {
319 Introspector child = IntrospectorFactory.newInstance(this.introspectionType, o);
320 child.setURIChain(obj.getURI());
321 processedVertexes.add(reflectDependentVertex(v, child, requestContext));
326 engine.setListProperty(v, property, list);
329 //method.getReturnType() is not 'simple' then create a vertex and edge recursively returning an edge back to this method
330 if (value != null) { //effectively ignore complex properties not included in the object we're processing
331 if (value.getClass().isArray()) {
333 int length = Array.getLength(value);
334 for (int i = 0; i < length; i ++) {
335 Object arrayElement = Array.get(value, i);
336 Introspector child = IntrospectorFactory.newInstance(this.introspectionType, arrayElement);
337 child.setURIChain(obj.getURI());
338 processedVertexes.add(reflectDependentVertex(v, child, requestContext));
341 } else if (!property.equals("relationship-list")) {
343 Introspector introspector = IntrospectorFactory.newInstance(this.introspectionType, value);
344 if (introspector.isContainer()) {
345 dependentVertexes.addAll(this.engine.getQueryEngine().findChildrenOfType(v, introspector.getChildDBName()));
346 introspector.setURIChain(obj.getURI());
348 processedVertexes.addAll(processObject(introspector, v, requestContext));
351 dependentVertexes.addAll(this.engine.getQueryEngine().findChildrenOfType(v, introspector.getDbName()));
352 processedVertexes.add(reflectDependentVertex(v, introspector, requestContext));
355 } else if (property.equals("relationship-list")) {
356 handleRelationships(obj, v);
361 this.writeThroughDefaults(v, obj);
362 /* handle those vertexes not touched */
363 for (Vertex toBeRemoved : processedVertexes) {
364 dependentVertexes.remove(toBeRemoved);
366 this.deleteItemsWithTraversal(dependentVertexes);
368 this.executePostSideEffects(obj, v);
369 return processedVertexes;
373 * Handle relationships.
376 * @param vertex the vertex
377 * @throws SecurityException the security exception
378 * @throws IllegalAccessException the illegal access exception
379 * @throws IllegalArgumentException the illegal argument exception
380 * @throws InvocationTargetException the invocation target exception
381 * @throws UnsupportedEncodingException the unsupported encoding exception
382 * @throws AAIException the AAI exception
385 * Handles the explicit relationships defined for an obj
387 private void handleRelationships(Introspector obj, Vertex vertex) throws UnsupportedEncodingException, AAIException {
391 Introspector wrappedRl = obj.getWrappedValue("relationship-list");
392 processRelationshipList(wrappedRl, vertex);
399 * Process relationship list.
401 * @param wrapped the wrapped
403 * @throws UnsupportedEncodingException the unsupported encoding exception
404 * @throws AAIException the AAI exception
406 private void processRelationshipList(Introspector wrapped, Vertex v) throws UnsupportedEncodingException, AAIException {
408 List<Object> relationships = (List<Object>)wrapped.getValue("relationship");
410 List<Triplet<Vertex, Vertex, String>> addEdges = new ArrayList<>();
411 List<Edge> existingEdges = this.engine.getQueryEngine().findEdgesForVersion(v, wrapped.getLoader());
413 for (Object relationship : relationships) {
415 Vertex cousinVertex = null;
417 Introspector wrappedRel = IntrospectorFactory.newInstance(this.introspectionType, relationship);
418 QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(wrappedRel);
420 if (wrappedRel.hasProperty("relationship-label")) {
421 label = wrappedRel.getValue("relationship-label");
424 List<Vertex> results = parser.getQueryBuilder().toList();
425 if (results.isEmpty()) {
426 final AAIException ex = new AAIException("AAI_6129", "Node of type " + parser.getResultType() + ". Could not find object at: " + parser.getUri());
427 ex.getTemplateVars().add(parser.getResultType());
428 ex.getTemplateVars().add(parser.getUri().toString());
431 //still an issue if there's more than one
432 cousinVertex = results.get(0);
435 if (cousinVertex != null) {
437 if (!edgeRules.hasEdgeRule(v, cousinVertex, label)) {
438 throw new AAIException("AAI_6120", "No EdgeRule found for passed nodeTypes: " + v.property(AAIProperties.NODE_TYPE).value().toString() + ", "
439 + cousinVertex.property(AAIProperties.NODE_TYPE).value().toString() + (label != null ? (" with label " + label):"") +".");
440 } else if (edgeRules.hasTreeEdgeRule(v, cousinVertex) && !edgeRules.hasCousinEdgeRule(v, cousinVertex, label)) {
441 throw new AAIException("AAI_6145");
444 e = this.getEdgeBetween(EdgeType.COUSIN, v, cousinVertex, label);
447 addEdges.add(new Triplet<>(v, cousinVertex, label));
449 existingEdges.remove(e);
454 for (Edge edge : existingEdges) {
457 for (Triplet<Vertex, Vertex, String> triplet : addEdges) {
459 edgeRules.addEdge(this.engine.asAdmin().getTraversalSource(), triplet.getValue0(), triplet.getValue1(), triplet.getValue2());
460 } catch (NoEdgeRuleFoundException e) {
461 throw new AAIException("AAI_6129", e);
468 * Write through defaults.
472 * @throws AAIUnknownObjectException
474 private void writeThroughDefaults(Vertex v, Introspector obj) throws AAIUnknownObjectException {
475 Introspector latest = this.latestLoader.introspectorFromName(obj.getName());
476 if (latest != null) {
477 Set<String> required = latest.getRequiredProperties();
479 for (String field : required) {
480 String defaultValue = null;
481 Object vertexProp = null;
482 defaultValue = latest.getPropertyMetadata(field).get(PropertyMetadata.DEFAULT_VALUE);
483 if (defaultValue != null) {
484 vertexProp = v.<Object>property(field).orElse(null);
485 if (vertexProp == null) {
486 v.property(field, defaultValue);
496 * Reflect dependent vertex.
499 * @param dependentObj the dependent obj
501 * @throws IllegalAccessException the illegal access exception
502 * @throws IllegalArgumentException the illegal argument exception
503 * @throws InvocationTargetException the invocation target exception
504 * @throws InstantiationException the instantiation exception
505 * @throws NoSuchMethodException the no such method exception
506 * @throws SecurityException the security exception
507 * @throws AAIException the AAI exception
508 * @throws UnsupportedEncodingException the unsupported encoding exception
509 * @throws AAIUnknownObjectException
511 private Vertex reflectDependentVertex(Vertex v, Introspector dependentObj, String requestContext) throws AAIException, UnsupportedEncodingException {
513 //QueryParser p = this.engine.getQueryBuilder().createQueryFromURI(obj.getURI());
514 //List<Vertex> items = p.getQuery().toList();
515 QueryBuilder<Vertex> query = this.engine.getQueryBuilder(v);
516 query.createEdgeTraversal(EdgeType.TREE, v, dependentObj);
517 query.createKeyQuery(dependentObj);
519 List<Vertex> items = query.toList();
521 Vertex dependentVertex = null;
522 if (items.size() == 1) {
523 dependentVertex = items.get(0);
524 this.verifyResourceVersion("update", dependentObj.getDbName(), dependentVertex.<String>property(AAIProperties.RESOURCE_VERSION).orElse(null), (String)dependentObj.getValue(AAIProperties.RESOURCE_VERSION), (String)dependentObj.getURI());
526 this.verifyResourceVersion("create", dependentObj.getDbName(), "", (String)dependentObj.getValue(AAIProperties.RESOURCE_VERSION), (String)dependentObj.getURI());
527 dependentVertex = createNewVertex(dependentObj);
530 return reflectDependentVertex(v, dependentVertex, dependentObj, requestContext);
535 * Reflect dependent vertex.
537 * @param parent the parent
538 * @param child the child
541 * @throws IllegalAccessException the illegal access exception
542 * @throws IllegalArgumentException the illegal argument exception
543 * @throws InvocationTargetException the invocation target exception
544 * @throws InstantiationException the instantiation exception
545 * @throws NoSuchMethodException the no such method exception
546 * @throws SecurityException the security exception
547 * @throws AAIException the AAI exception
548 * @throws UnsupportedEncodingException the unsupported encoding exception
549 * @throws AAIUnknownObjectException
551 private Vertex reflectDependentVertex(Vertex parent, Vertex child, Introspector obj, String requestContext) throws AAIException, UnsupportedEncodingException {
553 String parentUri = parent.<String>property(AAIProperties.AAI_URI).orElse(null);
554 if (parentUri != null) {
557 child.property(AAIProperties.AAI_URI, parentUri + uri);
559 processObject(obj, child, requestContext);
562 e = this.getEdgeBetween(EdgeType.TREE, parent, child, null);
564 String canBeLinked = obj.getMetadata(ObjectMetadata.CAN_BE_LINKED);
565 if (canBeLinked != null && canBeLinked.equals("true")) {
566 Loader ldrForCntxt = LoaderFactory.createLoaderForVersion(introspectionType, getVerForContext(requestContext));
567 boolean isFirst = !this.engine.getQueryBuilder(ldrForCntxt, parent).createEdgeTraversal(EdgeType.TREE, parent, obj).hasNext();
569 child.property(AAIProperties.LINKED, true);
572 edgeRules.addTreeEdge(this.engine.asAdmin().getTraversalSource(), parent, child);
578 private Version getVerForContext(String requestContext) {
579 Pattern pattern = Pattern.compile("v[0-9]+");
580 Matcher m = pattern.matcher(requestContext);
584 return Version.valueOf(requestContext);
591 * @param vertices the vertices
593 * @param depth the depth
594 * @param cleanUp the clean up
595 * @return the introspector
596 * @throws AAIException the AAI exception
597 * @throws IllegalAccessException the illegal access exception
598 * @throws IllegalArgumentException the illegal argument exception
599 * @throws InvocationTargetException the invocation target exception
600 * @throws SecurityException the security exception
601 * @throws InstantiationException the instantiation exception
602 * @throws NoSuchMethodException the no such method exception
603 * @throws UnsupportedEncodingException the unsupported encoding exception
604 * @throws MalformedURLException the malformed URL exception
605 * @throws AAIUnknownObjectException
606 * @throws URISyntaxException
608 public Introspector dbToObject(List<Vertex> vertices, final Introspector obj, int depth, boolean nodeOnly, String cleanUp) throws UnsupportedEncodingException, AAIException {
609 final int internalDepth;
610 if (depth == Integer.MAX_VALUE) {
611 internalDepth = depth--;
613 internalDepth = depth;
615 StopWatch.conditionalStart();
616 if (vertices.size() > 1 && !obj.isContainer()) {
617 dbTimeMsecs += StopWatch.stopIfStarted();
618 throw new AAIException("AAI_6136", "query object mismatch: this object cannot hold multiple items." + obj.getDbName());
619 } else if (obj.isContainer()) {
621 String listProperty = null;
622 for (String property : obj.getProperties()) {
623 if (obj.isListType(property) && obj.isComplexGenericType(property)) {
624 listProperty = property;
628 final String propertyName = listProperty;
629 getList = (List)obj.getValue(listProperty);
631 /* This is an experimental multithreading experiment
634 ExecutorService pool = GetAllPool.getInstance().getPool();
636 List<Future<Object>> futures = new ArrayList<>();
639 for (Vertex v : vertices) {
640 Callable<Object> task = () -> {
641 Set<Vertex> seen = new HashSet<>();
642 Introspector childObject = obj.newIntrospectorInstanceOfNestedProperty(propertyName);
643 Tree<Element> tree = this.engine.getQueryEngine().findSubGraph(v, internalDepth, nodeOnly);
644 TreeBackedVertex treeVertex = new TreeBackedVertex(v, tree);
645 dbToObject(childObject, treeVertex, seen, internalDepth, nodeOnly, cleanUp);
646 return childObject.getUnderlyingObject();
647 //getList.add(childObject.getUnderlyingObject());
649 futures.add(pool.submit(task));
652 for (Future<Object> future : futures) {
654 getList.add(future.get());
655 } catch (ExecutionException e) {
656 dbTimeMsecs += StopWatch.stopIfStarted();
657 throw new AAIException("AAI_4000", e);
658 } catch (InterruptedException e) {
659 dbTimeMsecs += StopWatch.stopIfStarted();
660 throw new AAIException("AAI_4000", e);
663 } else if (vertices.size() == 1) {
664 Set<Vertex> seen = new HashSet<>();
665 Tree<Element> tree = this.engine.getQueryEngine().findSubGraph(vertices.get(0), depth, nodeOnly);
666 TreeBackedVertex treeVertex = new TreeBackedVertex(vertices.get(0), tree);
667 dbToObject(obj, treeVertex, seen, depth, nodeOnly, cleanUp);
672 dbTimeMsecs += StopWatch.stopIfStarted();
681 * @param seen the seen
682 * @param depth the depth
683 * @param cleanUp the clean up
684 * @return the introspector
685 * @throws IllegalAccessException the illegal access exception
686 * @throws IllegalArgumentException the illegal argument exception
687 * @throws InvocationTargetException the invocation target exception
688 * @throws SecurityException the security exception
689 * @throws InstantiationException the instantiation exception
690 * @throws NoSuchMethodException the no such method exception
691 * @throws UnsupportedEncodingException the unsupported encoding exception
692 * @throws AAIException the AAI exception
693 * @throws MalformedURLException the malformed URL exception
694 * @throws AAIUnknownObjectException
695 * @throws URISyntaxException
697 private Introspector dbToObject(Introspector obj, Vertex v, Set<Vertex> seen, int depth, boolean nodeOnly, String cleanUp) throws AAIException, UnsupportedEncodingException {
705 boolean modified = false;
706 for (String property : obj.getProperties(PropertyPredicates.isVisible())) {
707 List<Object> getList = null;
708 Vertex[] vertices = null;
710 if (!(obj.isComplexType(property) || obj.isListType(property))) {
711 this.copySimpleProperty(property, obj, v);
714 if (obj.isComplexType(property)) {
717 if (!property.equals("relationship-list") && depth >= 0) {
718 Introspector argumentObject = obj.newIntrospectorInstanceOfProperty(property);
719 Object result = dbToObject(argumentObject, v, seen, depth+1, nodeOnly, cleanUp);
720 if (result != null) {
721 obj.setValue(property, argumentObject.getUnderlyingObject());
724 } else if (property.equals("relationship-list") && !nodeOnly){
725 /* relationships need to be handled correctly */
726 Introspector relationshipList = obj.newIntrospectorInstanceOfProperty(property);
727 relationshipList = createRelationshipList(v, relationshipList, cleanUp);
728 if (relationshipList != null) {
730 obj.setValue(property, relationshipList.getUnderlyingObject());
735 } else if (obj.isListType(property)) {
737 if (property.equals("any")) {
740 String genericType = obj.getGenericTypeClass(property).getSimpleName();
741 if (obj.isComplexGenericType(property) && depth >= 0) {
742 final String childDbName = convertFromCamelCase(genericType);
743 String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
746 rule = edgeRules.getEdgeRule(EdgeType.TREE, vType, childDbName);
747 if (!rule.getContains().equals(AAIDirection.NONE.toString())) {
748 //vertices = this.queryEngine.findRelatedVertices(v, Direction.OUT, rule.getLabel(), childDbName);
749 Direction ruleDirection = rule.getDirection();
750 Iterator<Vertex> itr = v.vertices(ruleDirection, rule.getLabel());
751 List<Vertex> verticesList = (List<Vertex>)IteratorUtils.toList(itr);
752 itr = verticesList.stream().filter(item -> {
753 return item.property(AAIProperties.NODE_TYPE).orElse("").equals(childDbName);
756 getList = (List<Object>)obj.getValue(property);
760 while (itr.hasNext()) {
761 Vertex childVertex = itr.next();
762 if (!seen.contains(childVertex)) {
763 Introspector argumentObject = obj.newIntrospectorInstanceOfNestedProperty(property);
765 Object result = dbToObject(argumentObject, childVertex, seen, depth, nodeOnly, cleanUp);
766 if (result != null) {
767 getList.add(argumentObject.getUnderlyingObject());
773 LOGGER.warn("Cycle found while serializing vertex id={}", childVertex.id().toString());
776 if (processed == 0) {
777 //vertices were all seen, reset the list
784 } else if (obj.isSimpleGenericType(property)) {
785 List<Object> temp = this.engine.getListProperty(v, property);
787 getList = (List<Object>)obj.getValue(property);
788 getList.addAll(temp);
799 //no changes were made to this obj, discard the instance
803 this.enrichData(obj, v);
809 public Introspector getVertexProperties(Vertex v) throws AAIException, UnsupportedEncodingException {
810 String nodeType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
811 if (nodeType == null) {
812 throw new AAIException("AAI_6143");
815 Introspector obj = this.latestLoader.introspectorFromName(nodeType);
816 Set<Vertex> seen = new HashSet<>();
818 String cleanUp = "false";
819 boolean nodeOnly = true;
820 StopWatch.conditionalStart();
821 this.dbToObject(obj, v, seen, depth, nodeOnly, cleanUp);
822 dbTimeMsecs += StopWatch.stopIfStarted();
826 public Introspector getLatestVersionView(Vertex v) throws AAIException, UnsupportedEncodingException {
827 String nodeType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
828 if (nodeType == null) {
829 throw new AAIException("AAI_6143");
831 Introspector obj = this.latestLoader.introspectorFromName(nodeType);
832 Set<Vertex> seen = new HashSet<>();
833 int depth = AAIProperties.MAXIMUM_DEPTH;
834 String cleanUp = "false";
835 boolean nodeOnly = false;
836 StopWatch.conditionalStart();
837 Tree<Element> tree = this.engine.getQueryEngine().findSubGraph(v, depth, nodeOnly);
838 TreeBackedVertex treeVertex = new TreeBackedVertex(v, tree);
839 this.dbToObject(obj, treeVertex, seen, depth, nodeOnly, cleanUp);
840 dbTimeMsecs += StopWatch.stopIfStarted();
844 * Copy simple property.
846 * @param property the property
849 * @throws InstantiationException the instantiation exception
850 * @throws IllegalAccessException the illegal access exception
851 * @throws IllegalArgumentException the illegal argument exception
852 * @throws InvocationTargetException the invocation target exception
853 * @throws NoSuchMethodException the no such method exception
854 * @throws SecurityException the security exception
856 private void copySimpleProperty(String property, Introspector obj, Vertex v) {
857 final Map<PropertyMetadata, String> metadata = obj.getPropertyMetadata(property);
858 String dbPropertyName = property;
859 if (metadata.containsKey(PropertyMetadata.DB_ALIAS)) {
860 dbPropertyName = metadata.get(PropertyMetadata.DB_ALIAS);
862 final Object temp = v.<Object>property(dbPropertyName).orElse(null);
865 obj.setValue(property, temp);
870 * Simple db to object.
874 * @throws InstantiationException the instantiation exception
875 * @throws IllegalAccessException the illegal access exception
876 * @throws IllegalArgumentException the illegal argument exception
877 * @throws InvocationTargetException the invocation target exception
878 * @throws NoSuchMethodException the no such method exception
879 * @throws SecurityException the security exception
881 private void simpleDbToObject (Introspector obj, Vertex v) {
882 for (String property : obj.getProperties()) {
885 if (!(obj.isComplexType(property) || obj.isListType(property))) {
886 this.copySimpleProperty(property, obj, v);
892 * Creates the relationship list.
896 * @param cleanUp the clean up
898 * @throws InstantiationException the instantiation exception
899 * @throws IllegalAccessException the illegal access exception
900 * @throws IllegalArgumentException the illegal argument exception
901 * @throws InvocationTargetException the invocation target exception
902 * @throws NoSuchMethodException the no such method exception
903 * @throws SecurityException the security exception
904 * @throws UnsupportedEncodingException the unsupported encoding exception
905 * @throws AAIException the AAI exception
906 * @throws MalformedURLException the malformed URL exception
907 * @throws URISyntaxException
909 private Introspector createRelationshipList(Vertex v, Introspector obj, String cleanUp) throws UnsupportedEncodingException, AAIException {
911 List<Vertex> cousins = this.engine.getQueryEngine().findCousinVertices(v);
913 List<Object> relationshipObjList = obj.getValue("relationship");
915 for (Vertex cousin : cousins) {
916 if (VersionChecker.apiVersionNeedsEdgeLabel(obj.getVersion())) {
917 List<Edge> edges = this.getEdgesBetween(EdgeType.COUSIN, v, cousin);
918 for (Edge e : edges) {
919 Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
920 Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, e);
921 if (result != null) {
922 relationshipObjList.add(result);
926 Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
927 Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, null);
928 if (result != null) {
929 relationshipObjList.add(result);
935 if (relationshipObjList.isEmpty()) {
943 * Process edge relationship.
945 * @param relationshipObj the relationship obj
946 * @param edge the edge
947 * @param direction the direction
948 * @param cleanUp the clean up
950 * @throws InstantiationException the instantiation exception
951 * @throws IllegalAccessException the illegal access exception
952 * @throws IllegalArgumentException the illegal argument exception
953 * @throws InvocationTargetException the invocation target exception
954 * @throws NoSuchMethodException the no such method exception
955 * @throws SecurityException the security exception
956 * @throws UnsupportedEncodingException the unsupported encoding exception
957 * @throws AAIException the AAI exception
958 * @throws MalformedURLException the malformed URL exception
959 * @throws AAIUnknownObjectException
960 * @throws URISyntaxException
962 private Object processEdgeRelationship(Introspector relationshipObj, Vertex cousin, String cleanUp, Edge edge) throws UnsupportedEncodingException, AAIUnknownObjectException {
965 //we must look up all parents in this case because we need to compute name-properties
966 //we cannot used the cached aaiUri to perform this action currently
967 Optional<Pair<Vertex, List<Introspector>>> tuple = this.getParents(relationshipObj.getLoader(), cousin, "true".equals(cleanUp));
968 //damaged vertex found, ignore
969 if (!tuple.isPresent()) {
972 List<Introspector> list = tuple.get().getValue1();
973 URI uri = this.getURIFromList(list);
975 URIToRelationshipObject uriParser = null;
976 Introspector result = null;
978 uriParser = new URIToRelationshipObject(relationshipObj.getLoader(), uri, this.baseURL);
979 result = uriParser.getResult();
980 } catch (AAIException | URISyntaxException e) {
981 LOGGER.error("Error while processing edge relationship in version " + relationshipObj.getVersion() + " (bad vertex ID=" + tuple.get().getValue0().id().toString() + ": "
982 + e.getMessage() + " " + LogFormatTools.getStackTop(e));
983 if ("true".equals(cleanUp)) {
984 this.deleteWithTraversal(tuple.get().getValue0());
988 if (!list.isEmpty()) {
989 this.addRelatedToProperty(result, list.get(0));
992 if (edge != null && result.hasProperty("relationship-label")) {
993 result.setValue("relationship-label", edge.label());
996 return result.getUnderlyingObject();
1000 * Gets the URI for vertex.
1003 * @return the URI for vertex
1004 * @throws InstantiationException the instantiation exception
1005 * @throws IllegalAccessException the illegal access exception
1006 * @throws IllegalArgumentException the illegal argument exception
1007 * @throws InvocationTargetException the invocation target exception
1008 * @throws NoSuchMethodException the no such method exception
1009 * @throws SecurityException the security exception
1010 * @throws UnsupportedEncodingException the unsupported encoding exception
1011 * @throws AAIUnknownObjectException
1013 public URI getURIForVertex(Vertex v) throws UnsupportedEncodingException {
1015 return getURIForVertex(v, false);
1018 public URI getURIForVertex(Vertex v, boolean overwrite) throws UnsupportedEncodingException {
1019 URI uri = UriBuilder.fromPath("/unknown-uri").build();
1021 String aaiUri = v.<String>property(AAIProperties.AAI_URI).orElse(null);
1023 if (aaiUri != null && !overwrite) {
1024 uri = UriBuilder.fromPath(aaiUri).build();
1026 StopWatch.conditionalStart();
1027 Optional<Pair<Vertex, List<Introspector>>> tuple = this.getParents(this.loader, v, false);
1028 dbTimeMsecs += StopWatch.stopIfStarted();
1029 if (tuple.isPresent()) {
1030 List<Introspector> list = tuple.get().getValue1();
1031 uri = this.getURIFromList(list);
1039 * Gets the URI from list.
1041 * @param list the list
1042 * @return the URI from list
1043 * @throws UnsupportedEncodingException the unsupported encoding exception
1045 private URI getURIFromList(List<Introspector> list) throws UnsupportedEncodingException {
1047 StringBuilder sb = new StringBuilder();
1048 for (Introspector i : list) {
1049 sb.insert(0, i.getURI());
1052 uri = sb.toString();
1053 return UriBuilder.fromPath(uri).build();
1059 * @param start the start
1060 * @param removeDamaged the remove damaged
1061 * @return the parents
1062 * @throws InstantiationException the instantiation exception
1063 * @throws IllegalAccessException the illegal access exception
1064 * @throws IllegalArgumentException the illegal argument exception
1065 * @throws InvocationTargetException the invocation target exception
1066 * @throws NoSuchMethodException the no such method exception
1067 * @throws SecurityException the security exception
1068 * @throws AAIUnknownObjectException
1070 private Optional<Pair<Vertex, List<Introspector>>> getParents(Loader loader, Vertex start, boolean removeDamaged) {
1072 List<Vertex> results = this.engine.getQueryEngine().findParents(start);
1073 List<Introspector> objs = new ArrayList<>();
1074 boolean shortCircuit = false;
1075 for (Vertex v : results) {
1076 String nodeType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
1077 Introspector obj = null;
1078 //vertex on the other end of this edge is bad
1079 if (nodeType == null) {
1080 //log something here about what was found and that it was removed
1081 ErrorLogHelper.logError("AAI-6143", "Found a damaged parent vertex " + v.id().toString());
1082 if (removeDamaged) {
1083 this.deleteWithTraversal(v);
1085 shortCircuit = true;
1088 obj = loader.introspectorFromName(nodeType);
1089 } catch (AAIUnknownObjectException e) {
1090 LOGGER.info("attempted to create node type " + nodeType + " but we do not understand it for version: " + loader.getVersion());
1096 //can't make a valid path because we don't understand this object
1099 this.simpleDbToObject(obj, v);
1104 //stop processing and don't return anything for this bad vertex
1106 return Optional.empty();
1109 return Optional.of(new Pair<>(results.get(results.size()-1), objs));
1112 * Takes a list of vertices and a list of objs and assumes they are in
1113 * the order you want the URIs to be nested.
1114 * [A,B,C] creates uris [A, AB, ABC]
1117 * @throws UnsupportedEncodingException
1118 * @throws URISyntaxException
1120 public void setCachedURIs(List<Vertex> vertices, List<Introspector> objs) throws UnsupportedEncodingException, URISyntaxException {
1122 StringBuilder uriChain = new StringBuilder();
1123 for (int i = 0; i < vertices.size(); i++) {
1126 v = vertices.get(i);
1127 aaiUri = v.<String>property(AAIProperties.AAI_URI).orElse(null);
1128 if (aaiUri != null) {
1129 uriChain.append(aaiUri);
1131 URI uri = UriBuilder.fromPath(objs.get(i).getURI()).build();
1132 aaiUri = uri.toString();
1133 uriChain.append(aaiUri);
1134 v.property(AAIProperties.AAI_URI, uriChain.toString());
1145 * @throws AAIUnknownObjectException
1146 * @throws IllegalArgumentException elated to property.
1148 * @param relationship the relationship
1149 * @param child the throws IllegalArgumentException, AAIUnknownObjectException child
1151 public void addRelatedToProperty(Introspector relationship, Introspector child) throws AAIUnknownObjectException {
1152 String nameProps = child.getMetadata(ObjectMetadata.NAME_PROPS);
1153 List<Introspector> relatedToProperties = new ArrayList<>();
1155 if (nameProps != null) {
1156 String[] props = nameProps.split(",");
1157 for (String prop : props) {
1158 Introspector relatedTo = relationship.newIntrospectorInstanceOfNestedProperty("related-to-property");
1159 relatedTo.setValue("property-key", child.getDbName() + "." + prop);
1160 relatedTo.setValue("property-value", child.getValue(prop));
1161 relatedToProperties.add(relatedTo);
1165 if (!relatedToProperties.isEmpty()) {
1166 List relatedToList = (List)relationship.getValue("related-to-property");
1167 for (Introspector obj : relatedToProperties) {
1168 relatedToList.add(obj.getUnderlyingObject());
1177 * @param relationship the relationship
1178 * @param inputVertex the input vertex
1179 * @return true, if successful
1180 * @throws UnsupportedEncodingException the unsupported encoding exception
1181 * @throws AAIException the AAI exception
1183 public boolean createEdge(Introspector relationship, Vertex inputVertex) throws UnsupportedEncodingException, AAIException {
1185 Vertex relatedVertex = null;
1186 StopWatch.conditionalStart();
1187 QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(relationship);
1189 String label = null;
1190 if (relationship.hasProperty("relationship-label")) {
1191 label = relationship.getValue("relationship-label");
1194 List<Vertex> results = parser.getQueryBuilder().toList();
1195 if (results.isEmpty()) {
1196 dbTimeMsecs += StopWatch.stopIfStarted();
1197 AAIException e = new AAIException("AAI_6129", "Node of type " + parser.getResultType() + ". Could not find object at: " + parser.getUri());
1198 e.getTemplateVars().add(parser.getResultType());
1199 e.getTemplateVars().add(parser.getUri().toString());
1202 //still an issue if there's more than one
1203 relatedVertex = results.get(0);
1206 if (relatedVertex != null) {
1210 e = this.getEdgeBetween(EdgeType.COUSIN, inputVertex, relatedVertex, label);
1212 edgeRules.addEdge(this.engine.asAdmin().getTraversalSource(), inputVertex, relatedVertex, label);
1214 //attempted to link two vertexes already linked
1217 dbTimeMsecs += StopWatch.stopIfStarted();
1221 dbTimeMsecs += StopWatch.stopIfStarted();
1226 * Gets all the edges between of the type.
1228 * @param aVertex the out vertex
1229 * @param bVertex the in vertex
1230 * @return the edges between
1231 * @throws AAIException the AAI exception
1232 * @throws NoEdgeRuleFoundException
1234 private List<Edge> getEdgesBetween(EdgeType type, Vertex aVertex, Vertex bVertex) {
1236 List<Edge> result = new ArrayList<>();
1238 if (bVertex != null) {
1239 GraphTraversal<Vertex, Edge> findEdgesBetween = null;
1240 findEdgesBetween = this.engine.asAdmin().getTraversalSource().V(aVertex).bothE();
1241 if (EdgeType.TREE.equals(type)) {
1242 findEdgesBetween = findEdgesBetween.not(__.has(EdgeProperty.CONTAINS.toString(), "NONE"));
1244 findEdgesBetween = findEdgesBetween.has(EdgeProperty.CONTAINS.toString(), "NONE");
1246 findEdgesBetween = findEdgesBetween.filter(__.otherV().hasId(bVertex.id()));
1247 result = findEdgesBetween.toList();
1253 * Gets all the edges between the vertexes with the label and type.
1255 * @param aVertex the out vertex
1256 * @param bVertex the in vertex
1258 * @return the edges between
1259 * @throws AAIException the AAI exception
1261 private List<Edge> getEdgesBetween(EdgeType type, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
1263 List<Edge> result = new ArrayList<>();
1265 if (bVertex != null) {
1266 EdgeRule rule = edgeRules.getEdgeRule(type, aVertex, bVertex, label);
1267 List<Edge> edges = this.getEdgesBetween(type, aVertex, bVertex);
1268 for (Edge edge : edges) {
1269 if (edge.label().equals(rule.getLabel())) {
1279 * Gets the edge between with the label and edge type.
1281 * @param aVertex the out vertex
1282 * @param bVertex the in vertex
1284 * @return the edge between
1285 * @throws AAIException the AAI exception
1286 * @throws NoEdgeRuleFoundException
1288 public Edge getEdgeBetween(EdgeType type, Vertex aVertex, Vertex bVertex, String label) throws AAIException {
1290 StopWatch.conditionalStart();
1291 if (bVertex != null) {
1293 List<Edge> edges = this.getEdgesBetween(type, aVertex, bVertex, label);
1295 if (!edges.isEmpty()) {
1296 dbTimeMsecs += StopWatch.stopIfStarted();
1297 return edges.get(0);
1301 dbTimeMsecs += StopWatch.stopIfStarted();
1304 public Edge getEdgeBetween(EdgeType type, Vertex aVertex, Vertex bVertex) throws AAIException {
1305 return this.getEdgeBetween(type, aVertex, bVertex, null);
1312 * @param relationship the relationship
1313 * @param inputVertex the input vertex
1314 * @return true, if successful
1315 * @throws UnsupportedEncodingException the unsupported encoding exception
1316 * @throws AAIException the AAI exception
1318 public boolean deleteEdge(Introspector relationship, Vertex inputVertex) throws UnsupportedEncodingException, AAIException {
1320 Vertex relatedVertex = null;
1321 StopWatch.conditionalStart();
1322 QueryParser parser = engine.getQueryBuilder().createQueryFromRelationship(relationship);
1324 List<Vertex> results = parser.getQueryBuilder().toList();
1326 String label = null;
1327 if (relationship.hasProperty("relationship-label")) {
1328 label = relationship.getValue("relationship-label");
1331 if (results.isEmpty()) {
1332 dbTimeMsecs += StopWatch.stopIfStarted();
1336 relatedVertex = results.get(0);
1339 edge = this.getEdgeBetween(EdgeType.COUSIN, inputVertex, relatedVertex, label);
1340 } catch (NoEdgeRuleFoundException e) {
1341 dbTimeMsecs += StopWatch.stopIfStarted();
1342 throw new AAIException("AAI_6129", e);
1346 dbTimeMsecs += StopWatch.stopIfStarted();
1349 dbTimeMsecs += StopWatch.stopIfStarted();
1356 * Delete items with traversal.
1358 * @param vertexes the vertexes
1359 * @throws IllegalStateException the illegal state exception
1361 public void deleteItemsWithTraversal(List<Vertex> vertexes) throws IllegalStateException {
1363 for (Vertex v : vertexes) {
1364 LOGGER.debug("About to delete the vertex with id: " + v.id());
1365 deleteWithTraversal(v);
1371 * Delete with traversal.
1373 * @param startVertex the start vertex
1375 public void deleteWithTraversal(Vertex startVertex) {
1376 StopWatch.conditionalStart();
1377 List<Vertex> results = this.engine.getQueryEngine().findDeletable(startVertex);
1379 for (Vertex v : results) {
1380 LOGGER.warn("Removing vertex " + v.id().toString());
1384 dbTimeMsecs += StopWatch.stopIfStarted();
1391 * @param resourceVersion the resource version
1392 * @throws IllegalArgumentException the illegal argument exception
1393 * @throws AAIException the AAI exception
1394 * @throws InterruptedException the interrupted exception
1396 public void delete(Vertex v, String resourceVersion, boolean enableResourceVersion) throws IllegalArgumentException, AAIException {
1398 boolean result = verifyDeleteSemantics(v, resourceVersion, enableResourceVersion);
1401 deleteWithTraversal(v);
1402 } catch (IllegalStateException e) {
1403 throw new AAIException("AAI_6110", e);
1412 * Verify delete semantics.
1414 * @param vertex the vertex
1415 * @param resourceVersion the resource version
1416 * @return true, if successful
1417 * @throws AAIException the AAI exception
1419 private boolean verifyDeleteSemantics(Vertex vertex, String resourceVersion, boolean enableResourceVersion) throws AAIException {
1420 boolean result = true;
1421 String nodeType = "";
1422 String errorDetail = " unknown delete semantic found";
1423 String aaiExceptionCode = "";
1424 nodeType = vertex.<String>property(AAIProperties.NODE_TYPE).orElse(null);
1425 if (enableResourceVersion && !this.verifyResourceVersion("delete", nodeType, vertex.<String>property(AAIProperties.RESOURCE_VERSION).orElse(null), resourceVersion, nodeType)) {
1427 StopWatch.conditionalStart();
1428 List<Object> preventDeleteVertices = this.engine.asAdmin().getReadOnlyTraversalSource().V(vertex).union(__.inE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.IN.toString()).outV().values(AAIProperties.NODE_TYPE), __.outE().has(EdgeProperty.PREVENT_DELETE.toString(), AAIDirection.OUT.toString()).inV().values(AAIProperties.NODE_TYPE)).dedup().toList();
1430 dbTimeMsecs += StopWatch.stopIfStarted();
1431 if (!preventDeleteVertices.isEmpty()) {
1432 aaiExceptionCode = "AAI_6110";
1433 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);
1437 throw new AAIException(aaiExceptionCode, errorDetail);
1443 * Verify resource version.
1445 * @param action the action
1446 * @param nodeType the node type
1447 * @param currentResourceVersion the current resource version
1448 * @param resourceVersion the resource version
1449 * @param uri the uri
1450 * @return true, if successful
1451 * @throws AAIException the AAI exception
1453 public boolean verifyResourceVersion(String action, String nodeType, String currentResourceVersion, String resourceVersion, String uri) throws AAIException {
1454 String enabled = "";
1455 String errorDetail = "";
1456 String aaiExceptionCode = "";
1457 if (currentResourceVersion == null) {
1458 currentResourceVersion = "";
1461 if (resourceVersion == null) {
1462 resourceVersion = "";
1465 enabled = AAIConfig.get(AAIConstants.AAI_RESVERSION_ENABLEFLAG);
1466 } catch (AAIException e) {
1467 ErrorLogHelper.logException(e);
1469 // We're only doing the resource version checks for v5 and later
1470 if (enabled.equals("true")) {
1471 if (!currentResourceVersion.equals(resourceVersion)) {
1472 if (action.equals("create") && !resourceVersion.equals("")) {
1473 errorDetail = "resource-version passed for " + action + " of " + uri;
1474 aaiExceptionCode = "AAI_6135";
1475 } else if (resourceVersion.equals("")) {
1476 errorDetail = "resource-version not passed for " + action + " of " + uri;
1477 aaiExceptionCode = "AAI_6130";
1479 errorDetail = "resource-version MISMATCH for " + action + " of " + uri;
1480 aaiExceptionCode = "AAI_6131";
1483 throw new AAIException(aaiExceptionCode, errorDetail);
1491 * Convert from camel case.
1493 * @param name the name
1494 * @return the string
1496 private String convertFromCamelCase (String name) {
1498 result = CaseFormat.UPPER_CAMEL.to(CaseFormat.LOWER_HYPHEN, name);
1500 NamingExceptions exceptions = NamingExceptions.getInstance();
1501 result = exceptions.getDBName(result);
1506 private boolean canModify(Introspector obj, String propName, String requestContext) {
1507 final String readOnly = obj.getPropertyMetadata(propName).get(PropertyMetadata.READ_ONLY);
1508 if (readOnly != null) {
1509 final String[] items = readOnly.split(",");
1510 for (String item : items) {
1511 if (requestContext.equals(item)) {
1519 private void executePreSideEffects(Introspector obj, Vertex self) throws AAIException {
1521 SideEffectRunner runner = new SideEffectRunner
1522 .Builder(this.engine, this).addSideEffect(DataCopy.class).build();
1524 runner.execute(obj, self);
1527 private void executePostSideEffects(Introspector obj, Vertex self) throws AAIException {
1529 SideEffectRunner runner = new SideEffectRunner
1530 .Builder(this.engine, this).addSideEffect(DataLinkWriter.class).build();
1532 runner.execute(obj, self);
1535 private void enrichData(Introspector obj, Vertex self) throws AAIException {
1537 SideEffectRunner runner = new SideEffectRunner
1538 .Builder(this.engine, this).addSideEffect(DataLinkReader.class).build();
1540 runner.execute(obj, self);
1543 public double getDBTimeMsecs() {
1544 return (dbTimeMsecs);
1548 * Db to object With Filters
1549 * This is for a one-time run with Tenant Isloation to only filter relationships
1550 * TODO: Chnage the original dbToObject to take filter parent/cousins
1552 * @param vertices the vertices
1553 * @param obj the obj
1554 * @param depth the depth
1555 * @param cleanUp the clean up
1556 * @return the introspector
1557 * @throws AAIException the AAI exception
1558 * @throws IllegalAccessException the illegal access exception
1559 * @throws IllegalArgumentException the illegal argument exception
1560 * @throws InvocationTargetException the invocation target exception
1561 * @throws SecurityException the security exception
1562 * @throws InstantiationException the instantiation exception
1563 * @throws NoSuchMethodException the no such method exception
1564 * @throws UnsupportedEncodingException the unsupported encoding exception
1565 * @throws MalformedURLException the malformed URL exception
1566 * @throws AAIUnknownObjectException
1567 * @throws URISyntaxException
1569 //TODO - See if you can merge the 2 dbToObjectWithFilters
1570 public Introspector dbToObjectWithFilters(Introspector obj, Vertex v, Set<Vertex> seen, int depth, boolean nodeOnly, List<String> filterCousinNodes, List<String> filterParentNodes) throws AAIException, UnsupportedEncodingException {
1571 String cleanUp = "false";
1577 boolean modified = false;
1578 for (String property : obj.getProperties(PropertyPredicates.isVisible())) {
1579 List<Object> getList = null;
1580 Vertex[] vertices = null;
1582 if (!(obj.isComplexType(property) || obj.isListType(property))) {
1583 this.copySimpleProperty(property, obj, v);
1586 if (obj.isComplexType(property)) {
1587 /* container case */
1589 if (!property.equals("relationship-list") && depth >= 0) {
1590 Introspector argumentObject = obj.newIntrospectorInstanceOfProperty(property);
1591 Object result = dbToObjectWithFilters(argumentObject, v, seen, depth+1, nodeOnly, filterCousinNodes, filterParentNodes);
1592 if (result != null) {
1593 obj.setValue(property, argumentObject.getUnderlyingObject());
1596 } else if (property.equals("relationship-list") && !nodeOnly){
1597 /* relationships need to be handled correctly */
1598 Introspector relationshipList = obj.newIntrospectorInstanceOfProperty(property);
1599 relationshipList = createFilteredRelationshipList(v, relationshipList, cleanUp, filterCousinNodes);
1600 if (relationshipList != null) {
1602 obj.setValue(property, relationshipList.getUnderlyingObject());
1607 } else if (obj.isListType(property)) {
1609 if (property.equals("any")) {
1612 String genericType = obj.getGenericTypeClass(property).getSimpleName();
1613 if (obj.isComplexGenericType(property) && depth >= 0) {
1614 final String childDbName = convertFromCamelCase(genericType);
1615 String vType = v.<String>property(AAIProperties.NODE_TYPE).orElse(null);
1618 boolean isthisParentRequired = filterParentNodes.parallelStream().anyMatch(childDbName::contains);
1622 rule = edgeRules.getEdgeRule(EdgeType.TREE, vType, childDbName);
1623 if (!rule.getContains().equals(AAIDirection.NONE.toString()) && isthisParentRequired) {
1624 //vertices = this.queryEngine.findRelatedVertices(v, Direction.OUT, rule.getLabel(), childDbName);
1625 Direction ruleDirection = rule.getDirection();
1626 Iterator<Vertex> itr = v.vertices(ruleDirection, rule.getLabel());
1627 List<Vertex> verticesList = (List<Vertex>)IteratorUtils.toList(itr);
1628 itr = verticesList.stream().filter(item -> {
1629 return item.property(AAIProperties.NODE_TYPE).orElse("").equals(childDbName);
1631 if (itr.hasNext()) {
1632 getList = (List<Object>)obj.getValue(property);
1636 while (itr.hasNext()) {
1637 Vertex childVertex = itr.next();
1638 if (!seen.contains(childVertex)) {
1639 Introspector argumentObject = obj.newIntrospectorInstanceOfNestedProperty(property);
1641 Object result = dbToObjectWithFilters(argumentObject, childVertex, seen, depth, nodeOnly, filterCousinNodes, filterParentNodes);
1642 if (result != null) {
1643 getList.add(argumentObject.getUnderlyingObject());
1649 LOGGER.warn("Cycle found while serializing vertex id={}", childVertex.id().toString());
1652 if (processed == 0) {
1653 //vertices were all seen, reset the list
1656 if (processed > 0) {
1660 } else if (obj.isSimpleGenericType(property)) {
1661 List<Object> temp = this.engine.getListProperty(v, property);
1663 getList = (List<Object>)obj.getValue(property);
1664 getList.addAll(temp);
1675 //no changes were made to this obj, discard the instance
1679 this.enrichData(obj, v);
1685 * Creates the relationship list with the filtered node types.
1688 * @param obj the obj
1689 * @param cleanUp the clean up
1690 * @return the object
1691 * @throws InstantiationException the instantiation exception
1692 * @throws IllegalAccessException the illegal access exception
1693 * @throws IllegalArgumentException the illegal argument exception
1694 * @throws InvocationTargetException the invocation target exception
1695 * @throws NoSuchMethodException the no such method exception
1696 * @throws SecurityException the security exception
1697 * @throws UnsupportedEncodingException the unsupported encoding exception
1698 * @throws AAIException the AAI exception
1699 * @throws MalformedURLException the malformed URL exception
1700 * @throws URISyntaxException
1702 private Introspector createFilteredRelationshipList(Vertex v, Introspector obj, String cleanUp, List<String> filterNodes) throws UnsupportedEncodingException, AAIException {
1703 List<Vertex> allCousins = this.engine.getQueryEngine().findCousinVertices(v);
1705 Iterator<Vertex> cousinVertices = allCousins.stream().filter(item -> {
1706 String node = (String)item.property(AAIProperties.NODE_TYPE).orElse("");
1707 return filterNodes.parallelStream().anyMatch(node::contains);
1711 List<Vertex> cousins = (List<Vertex>)IteratorUtils.toList(cousinVertices);
1713 //items.parallelStream().anyMatch(inputStr::contains)
1714 List<Object> relationshipObjList = obj.getValue("relationship");
1715 for (Vertex cousin : cousins) {
1717 Introspector relationshipObj = obj.newIntrospectorInstanceOfNestedProperty("relationship");
1718 Object result = processEdgeRelationship(relationshipObj, cousin, cleanUp, null);
1719 if (result != null) {
1720 relationshipObjList.add(result);
1726 if (relationshipObjList.isEmpty()) {