Update from titan to using janusgraph
[aai/aai-common.git] / aai-core / src / main / java / org / onap / aai / rest / db / HttpEntry.java
1 /**
2  * ============LICENSE_START=======================================================
3  * org.onap.aai
4  * ================================================================================
5  * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *    http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20 package org.onap.aai.rest.db;
21
22 import java.io.IOException;
23 import java.io.UnsupportedEncodingException;
24 import java.lang.reflect.InvocationTargetException;
25 import java.net.MalformedURLException;
26 import java.net.URI;
27 import java.net.URISyntaxException;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.Set;
33 import java.util.Map.Entry;
34 import java.util.concurrent.TimeUnit;
35
36 import javax.ws.rs.core.HttpHeaders;
37 import javax.ws.rs.core.MediaType;
38 import javax.ws.rs.core.MultivaluedMap;
39 import javax.ws.rs.core.Response;
40 import javax.ws.rs.core.Response.Status;
41 import javax.ws.rs.core.UriBuilder;
42
43 import org.apache.commons.lang.StringUtils;
44 import org.apache.tinkerpop.gremlin.structure.Graph;
45 import org.apache.tinkerpop.gremlin.structure.Vertex;
46 import org.javatuples.Pair;
47 import org.onap.aai.db.props.AAIProperties;
48 import org.onap.aai.dbmap.DBConnectionType;
49 import org.onap.aai.domain.responseMessage.AAIResponseMessage;
50 import org.onap.aai.domain.responseMessage.AAIResponseMessageDatum;
51 import org.onap.aai.exceptions.AAIException;
52 import org.onap.aai.extensions.AAIExtensionMap;
53 import org.onap.aai.extensions.ExtensionController;
54 import org.onap.aai.introspection.Introspector;
55 import org.onap.aai.introspection.Loader;
56 import org.onap.aai.introspection.LoaderFactory;
57 import org.onap.aai.introspection.MarshallerProperties;
58 import org.onap.aai.introspection.ModelInjestor;
59 import org.onap.aai.introspection.ModelType;
60 import org.onap.aai.introspection.Version;
61 import org.onap.aai.introspection.exceptions.AAIUnknownObjectException;
62 import org.onap.aai.logging.ErrorLogHelper;
63 import org.onap.aai.logging.LoggingContext;
64 import org.onap.aai.parsers.query.QueryParser;
65 import org.onap.aai.parsers.uri.URIToExtensionInformation;
66 import org.onap.aai.rest.ueb.UEBNotification;
67 import org.onap.aai.restcore.HttpMethod;
68 import org.onap.aai.schema.enums.ObjectMetadata;
69 import org.onap.aai.serialization.db.DBSerializer;
70 import org.onap.aai.serialization.engines.QueryStyle;
71 import org.onap.aai.serialization.engines.JanusGraphDBEngine;
72 import org.onap.aai.serialization.engines.TransactionalGraphEngine;
73 import org.onap.aai.serialization.engines.query.QueryEngine;
74
75 import com.att.eelf.configuration.EELFLogger;
76 import com.att.eelf.configuration.EELFManager;
77 import com.fasterxml.jackson.databind.JsonNode;
78 import com.fasterxml.jackson.databind.ObjectMapper;
79 import com.github.fge.jsonpatch.JsonPatchException;
80 import com.github.fge.jsonpatch.mergepatch.JsonMergePatch;
81 import org.janusgraph.core.JanusGraphException;
82 import org.onap.aai.serialization.queryformats.Format;
83 import org.onap.aai.serialization.queryformats.FormatFactory;
84 import org.onap.aai.serialization.queryformats.Formatter;
85
86 /**
87  * The Class HttpEntry.
88  */
89 public class HttpEntry {
90
91         private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(HttpEntry.class);
92         private static final String TARGET_ENTITY = "DB";
93
94         private final ModelType introspectorFactoryType;
95         
96         private final QueryStyle queryStyle;
97         
98         private final Version version;
99         
100         private final Loader loader;
101         
102         private final TransactionalGraphEngine dbEngine;
103                 
104         private boolean processSingle = true;
105
106         /**
107          * Instantiates a new http entry.
108          *
109          * @param version the version
110          * @param modelType the model type
111          * @param queryStyle the query style
112          * @param llBuilder the ll builder
113          */
114         public HttpEntry(Version version, ModelType modelType, QueryStyle queryStyle, DBConnectionType connectionType) {
115                 this.introspectorFactoryType = modelType;
116                 this.queryStyle = queryStyle;
117                 this.version = version;
118                 this.loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
119                 this.dbEngine = new JanusGraphDBEngine(
120                                 queryStyle,
121                                 connectionType,
122                                 loader);
123                 //start transaction on creation
124                 dbEngine.startTransaction();
125
126         }
127         
128         /**
129          * Gets the introspector factory type.
130          *
131          * @return the introspector factory type
132          */
133         public ModelType getIntrospectorFactoryType() {
134                 return introspectorFactoryType;
135         }
136
137         /**
138          * Gets the query style.
139          *
140          * @return the query style
141          */
142         public QueryStyle getQueryStyle() {
143                 return queryStyle;
144         }
145
146         /**
147          * Gets the version.
148          *
149          * @return the version
150          */
151         public Version getVersion() {
152                 return version;
153         }
154
155         /**
156          * Gets the loader.
157          *
158          * @return the loader
159          */
160         public Loader getLoader() {
161                 return loader;
162         }
163
164         /**
165          * Gets the db engine.
166          *
167          * @return the db engine
168          */
169         public TransactionalGraphEngine getDbEngine() {
170                 return dbEngine;
171         }
172
173         public Pair<Boolean, List<Pair<URI, Response>>> process (List<DBRequest> requests, String sourceOfTruth) throws AAIException {
174                 return this.process(requests, sourceOfTruth, true);
175         }
176         /**
177          * Process.
178          * @param requests the requests
179          * @param sourceOfTruth the source of truth
180          *
181          * @return the pair
182          * @throws AAIException the AAI exception
183          */
184         public Pair<Boolean, List<Pair<URI, Response>>> process (List<DBRequest> requests, String sourceOfTruth, boolean enableResourceVersion) throws AAIException {
185                 DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth);
186                 String methodName = "process";
187                 Response response = null;
188                 Status status = Status.NOT_FOUND;
189                 Introspector obj = null;
190                 QueryParser query = null;
191                 URI uri = null;
192                 String transactionId = null;
193                 UEBNotification notification = new UEBNotification(loader);
194                 int depth = AAIProperties.MAXIMUM_DEPTH;
195                 List<Pair<URI,Response>> responses = new ArrayList<>();
196                 MultivaluedMap<String, String> params = null;
197                 HttpMethod method = null;
198                 String uriTemp = "";
199                 Boolean success = true;
200                 QueryEngine queryEngine = dbEngine.getQueryEngine();
201                 int maxRetries = 10;
202                 int retry = 0;
203
204                 LoggingContext.save();
205                 for (DBRequest request : requests) {
206                         try {
207                                 for (retry = 0; retry < maxRetries; ++retry) {
208                                         try {
209                                                 method = request.getMethod();
210
211                                                 LoggingContext.targetEntity(TARGET_ENTITY);
212                                                 LoggingContext.targetServiceName(methodName + " " + method);
213
214                                                 obj = request.getIntrospector();
215                                                 query = request.getParser();
216                                                 transactionId = request.getTransactionId();
217                                                 uriTemp = request.getUri().getRawPath().replaceFirst("^v\\d+/", "");
218                                                 uri = UriBuilder.fromPath(uriTemp).build();
219
220                                                 LoggingContext.startTime();
221
222                                                 List<Vertex> vertices = query.getQueryBuilder().toList();
223                                                 boolean isNewVertex = false;
224                                                 String outputMediaType = getMediaType(request.getHeaders().getAcceptableMediaTypes());
225                                                 String result = null;
226                                                 params = request.getInfo().getQueryParameters(false);
227                                                 depth = setDepth(obj, params.getFirst("depth"));
228                                                 String cleanUp = params.getFirst("cleanup");
229                                                 String requestContext = "";
230                                                 List<String> requestContextList = request.getHeaders().getRequestHeader("aai-request-context");
231                                                 if (requestContextList != null) {
232                                                         requestContext = requestContextList.get(0);
233                                                 }
234                                         
235                                                 if (cleanUp == null) {
236                                                         cleanUp = "false";
237                                                 }
238                                                 if (vertices.size() > 1 && processSingle && !method.equals(HttpMethod.GET)) {
239                                                         if (method.equals(HttpMethod.DELETE)) {
240                                                                 LoggingContext.restoreIfPossible();
241                                                                 throw new AAIException("AAI_6138");
242                                                         } else {
243                                                                 LoggingContext.restoreIfPossible();
244                                                                 throw new AAIException("AAI_6137");
245                                                         }
246                                                 }
247                                                 if (method.equals(HttpMethod.PUT)) {
248                                                         String resourceVersion = (String)obj.getValue("resource-version");
249                                                         if (vertices.isEmpty()) {
250                                                                 if (enableResourceVersion) {
251                                                                         serializer.verifyResourceVersion("create", query.getResultType(), "", resourceVersion, obj.getURI());
252                                                                 }
253                                                                 isNewVertex = true;
254                                                         } else {
255                                                                 if (enableResourceVersion) {
256                                                                         serializer.verifyResourceVersion("update", query.getResultType(), (String)vertices.get(0).<String>property("resource-version").orElse(null), resourceVersion, obj.getURI());
257                                                                 }
258                                                                 isNewVertex = false;
259                                                         }
260                                                 } else {
261                                                         if (vertices.isEmpty()) {
262                                                                 String msg = createNotFoundMessage(query.getResultType(), request.getUri());
263                                                                 throw new AAIException("AAI_6114", msg);
264                                                         } else {
265                                                                 isNewVertex = false;
266                                                         }
267                                                 }
268                                                 Vertex v = null;
269                                                 if (!isNewVertex) {
270                                                         v = vertices.get(0);
271                                                 }
272                                                 HashMap<String, Introspector> relatedObjects = new HashMap<>();
273                                                 switch (method) {
274                                                         case GET:
275                                                                 String nodeOnly = params.getFirst("nodes-only");
276                                                                 boolean isNodeOnly = nodeOnly != null;
277                                                                 obj = this.getObjectFromDb(vertices, serializer, query, obj, request.getUri(), depth, isNodeOnly, cleanUp);
278
279                                                                 LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs(),TimeUnit.MILLISECONDS);
280                                                                 LOGGER.info ("Completed");
281                                                                 LoggingContext.restoreIfPossible();
282
283                                                                 if (obj != null) {
284                                                                         status = Status.OK;
285                                                                         MarshallerProperties properties;
286                                                                         if (!request.getMarshallerProperties().isPresent()) {
287                                                                                 properties =
288                                                                                                 new MarshallerProperties.Builder(org.onap.aai.restcore.MediaType.getEnum(outputMediaType)).build();
289                                                                         } else {
290                                                                                 properties = request.getMarshallerProperties().get();
291                                                                         }
292                                                                         result = obj.marshal(properties);
293                                                                 }
294                                                                 
295                                                                 break;
296                                                         case PUT:
297                                                                 if (isNewVertex) {
298                                                                         v = serializer.createNewVertex(obj);
299                                                                 }
300                                                                 serializer.serializeToDb(obj, v, query, uri.getRawPath(), requestContext);
301                                                                 status = Status.OK;
302                                                                 if (isNewVertex) {
303                                                                         status = Status.CREATED;
304                                                                 }
305                                                                 obj = serializer.getLatestVersionView(v);
306                                                                 if (query.isDependent()) {
307                                                                         relatedObjects = this.getRelatedObjects(serializer, queryEngine, v);
308                                                                 }
309                                                                 LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() +
310                                                                                 (long)queryEngine.getDBTimeMsecs(), TimeUnit.MILLISECONDS);
311                                                                 LOGGER.info ("Completed");
312                                                                 LoggingContext.restoreIfPossible();
313                                                                 notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects);
314
315                                                                 break;
316                                                         case PUT_EDGE:
317                                                                 serializer.touchStandardVertexProperties(v, false);
318                                                                 serializer.createEdge(obj, v);
319
320                                                                 LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs(),TimeUnit.MILLISECONDS);
321                                                                 LOGGER.info ("Completed");
322                                                                 LoggingContext.restoreIfPossible();
323                                                                 status = Status.OK;
324                                                                 break;
325                                                         case MERGE_PATCH:
326                                                                 Introspector existingObj = (Introspector) obj.clone();
327                                                                 existingObj = this.getObjectFromDb(vertices, serializer, query, existingObj, request.getUri(), 0, false, cleanUp);
328                                                                 String existingJson = existingObj.marshal(false);
329                                                                 String newJson;
330                                                                 
331                                                                 if (request.getRawRequestContent().isPresent()) {
332                                                                         newJson = request.getRawRequestContent().get();
333                                                                 } else {
334                                                                         newJson = "";
335                                                                 }
336                                                                 Object relationshipList = request.getIntrospector().getValue("relationship-list");
337                                                                 ObjectMapper mapper = new ObjectMapper();
338                                                                 try {
339                                                                         JsonNode existingNode = mapper.readTree(existingJson);
340                                                                         JsonNode newNode = mapper.readTree(newJson);
341                                                                         JsonMergePatch patch = JsonMergePatch.fromJson(newNode);
342                                                                         JsonNode completed = patch.apply(existingNode);
343                                                                         String patched = mapper.writeValueAsString(completed);
344                                                                         Introspector patchedObj = loader.unmarshal(existingObj.getName(), patched);
345                                                                         if (relationshipList == null) {
346                                                                                 //if the caller didn't touch the relationship-list, we shouldn't either
347                                                                                 patchedObj.setValue("relationship-list", null);
348                                                                         }
349                                                                         serializer.serializeToDb(patchedObj, v, query, uri.getRawPath(), requestContext);
350                                                                         status = Status.OK;
351                                                                         patchedObj = serializer.getLatestVersionView(v);
352                                                                         if (query.isDependent()) {
353                                                                                 relatedObjects = this.getRelatedObjects(serializer, queryEngine, v);
354                                                                         }
355                                                                         LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() +
356                                                                                         (long)queryEngine.getDBTimeMsecs(), TimeUnit.MILLISECONDS);
357                                                                         LOGGER.info ("Completed");
358                                                                         LoggingContext.restoreIfPossible();
359                                                                         notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, patchedObj, relatedObjects);
360                                                                 } catch (IOException | JsonPatchException e) {
361
362                                                                         LOGGER.info ("Caught exception: " + e.getMessage());
363                                                                         LoggingContext.restoreIfPossible();
364                                                                         throw new AAIException("AAI_3000", "could not perform patch operation");
365                                                                 }
366                                                                 break;
367                                                         case DELETE:
368                                                                 String resourceVersion = params.getFirst("resource-version");
369                                                                 obj = serializer.getLatestVersionView(v);
370                                                                 if (query.isDependent()) {
371                                                                         relatedObjects = this.getRelatedObjects(serializer, queryEngine, v);
372                                                                 }
373                                                                 /*
374                                                                  * Find all Delete-other-vertex vertices and create structure for notify
375                                                                  * findDeleatble also returns the startVertex v and we dont want to create
376                                                                  * duplicate notification events for the same
377                                                                  * So remove the startvertex first
378                                                                  */
379
380                                                                 List<Vertex> deletableVertices = dbEngine.getQueryEngine().findDeletable(v);
381                                                                 Long vId = (Long) v.id();
382
383                                                                 /*
384                                                                  * I am assuming vertexId cant be null
385                                                                  */
386                                                                 deletableVertices.removeIf(s -> vId.equals(s.id()));
387                                                                 boolean isDelVerticesPresent = !deletableVertices.isEmpty();
388                                                                 Map<Vertex, Introspector> deleteObjects = new HashMap<>();
389                                                                 Map<String, URI> uriMap = new HashMap<>();
390                                                                 Map<String, HashMap<String, Introspector>> deleteRelatedObjects = new HashMap<>();
391
392                                                                 if(isDelVerticesPresent){
393                                                                         deleteObjects = this.buildIntrospectorObjects(serializer, deletableVertices);
394
395                                                                         uriMap = this.buildURIMap(serializer, deleteObjects);
396                                                                         deleteRelatedObjects = this.buildRelatedObjects(serializer, queryEngine, deleteObjects);
397                                                                 }
398
399                                                                 serializer.delete(v, resourceVersion, enableResourceVersion);
400
401                                                                 LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs() +
402                                                                                 (long)queryEngine.getDBTimeMsecs(), TimeUnit.MILLISECONDS);
403                                                                 LOGGER.info ("Completed");
404                                                                 LoggingContext.restoreIfPossible();
405                                                                 status = Status.NO_CONTENT;
406                                                                 notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects);
407
408                                                                 /*
409                                                                  * Notify delete-other-v candidates
410                                                                  */
411
412                                                                 if(isDelVerticesPresent){
413                                                                         this.buildNotificationEvent(sourceOfTruth, status, transactionId, notification, deleteObjects,
414                                                                         uriMap, deleteRelatedObjects);
415                                                                 }
416
417                                                                 break;
418                                                         case DELETE_EDGE:
419                                                                 serializer.touchStandardVertexProperties(v, false);
420                                                                 serializer.deleteEdge(obj, v);
421
422                                                                 LoggingContext.elapsedTime((long)serializer.getDBTimeMsecs(),TimeUnit.MILLISECONDS);
423                                                                 LOGGER.info ("Completed");
424                                                                 LoggingContext.restoreIfPossible();
425                                                                 status = Status.NO_CONTENT;
426                                                                 break;
427                                                         default:
428                                                                 break;
429                                                 }
430                                                 
431                                                 
432                                                 /* temporarily adding vertex id to the headers
433                                                  * to be able to use for testing the vertex id endpoint functionality
434                                                  * since we presently have no other way of generating those id urls
435                                                 */
436                                                 if (response == null && v != null && (
437                                                         method.equals(HttpMethod.PUT)
438                                                         || method.equals(HttpMethod.GET)
439                                                         || method.equals(HttpMethod.MERGE_PATCH))
440                                                 ) {
441                                                         String myvertid = v.id().toString();
442                                                         response = Response.status(status)
443                                                                         .header("vertex-id", myvertid)
444                                                                         .entity(result)
445                                                                         .type(outputMediaType).build();
446                                                 } else if (response == null) {
447                                                         response = Response.status(status)
448                                                                         .type(outputMediaType).build();
449                                                 } else {
450                                                         //response already set to something
451                                                 }
452                                                 Pair<URI,Response> pairedResp = Pair.with(request.getUri(), response);
453                                                 responses.add(pairedResp);
454                                                 //break out of retry loop
455                                                 break;
456                                         } catch (JanusGraphException e) {
457                                                 this.dbEngine.rollback();
458
459                                                 LOGGER.info ("Caught exception: " + e.getMessage());
460                                                 LoggingContext.restoreIfPossible();
461                                                 AAIException ex = new AAIException("AAI_6142", e);
462                                                 ErrorLogHelper.logException(ex);
463                                                 Thread.sleep((retry + 1) * 20L);
464                                                 this.dbEngine.startTransaction();
465                                                 queryEngine = dbEngine.getQueryEngine();
466                                                 serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth);
467                                         }
468                                         if (retry == maxRetries) {
469                                                 throw new AAIException("AAI_6134");
470                                         }
471                                 }
472                         } catch (AAIException e) {
473                                 success = false;
474                                 ArrayList<String> templateVars = new ArrayList<>();
475                                 templateVars.add(request.getMethod().toString()); //GET, PUT, etc
476                                 templateVars.add(request.getUri().getPath());
477                                 templateVars.addAll(e.getTemplateVars());
478                                 ErrorLogHelper.logException(e);
479                                 response = Response
480                                                 .status(e.getErrorObject().getHTTPResponseCode())
481                                                 .entity(ErrorLogHelper.getRESTAPIErrorResponse(request.getHeaders().getAcceptableMediaTypes(), e, templateVars))
482                                                 .build();
483                                 Pair<URI,Response> pairedResp = Pair.with(request.getUri(), response);
484                                 responses.add(pairedResp);
485                                 continue;
486                         } catch (Exception e) {
487                                 success = false;
488                                 e.printStackTrace();
489                                 AAIException ex = new AAIException("AAI_4000", e);
490                                 ArrayList<String> templateVars = new ArrayList<String>();
491                                 templateVars.add(request.getMethod().toString()); //GET, PUT, etc
492                                 templateVars.add(request.getUri().getPath().toString());
493                                 ErrorLogHelper.logException(ex);
494                                 response = Response
495                                                 .status(ex.getErrorObject().getHTTPResponseCode())
496                                                 .entity(ErrorLogHelper.getRESTAPIErrorResponse(request.getHeaders().getAcceptableMediaTypes(), ex, templateVars))
497                                                 .build();
498                                 Pair<URI, Response> pairedResp = Pair.with(request.getUri(), response);
499                                 responses.add(pairedResp);
500                                 continue;
501                         }
502                 }
503                 
504                 notification.triggerEvents();
505                 return Pair.with(success, responses);
506         }
507
508
509         /**
510          * Gets the media type.
511          *
512          * @param mediaTypeList the media type list
513          * @return the media type
514          */
515         private String getMediaType(List <MediaType> mediaTypeList) {
516                 String mediaType = MediaType.APPLICATION_JSON;  // json is the default    
517                 for (MediaType mt : mediaTypeList) {
518                         if (MediaType.APPLICATION_XML_TYPE.isCompatible(mt)) {
519                                 mediaType = MediaType.APPLICATION_XML;
520                         } 
521                 }
522                 return mediaType;
523         }
524         
525         /**
526          * Gets the object from db.
527          *
528          * @param serializer the serializer
529          * @param g the g
530          * @param query the query
531          * @param obj the obj
532          * @param uri the uri
533          * @param depth the depth
534          * @param cleanUp the clean up
535          * @return the object from db
536          * @throws AAIException the AAI exception
537          * @throws IllegalAccessException the illegal access exception
538          * @throws IllegalArgumentException the illegal argument exception
539          * @throws InvocationTargetException the invocation target exception
540          * @throws SecurityException the security exception
541          * @throws InstantiationException the instantiation exception
542          * @throws NoSuchMethodException the no such method exception
543          * @throws UnsupportedEncodingException the unsupported encoding exception
544          * @throws MalformedURLException the malformed URL exception
545          * @throws AAIUnknownObjectException 
546          * @throws URISyntaxException 
547          */
548         private Introspector getObjectFromDb(List<Vertex> results, DBSerializer serializer, QueryParser query, Introspector obj, URI uri, int depth, boolean nodeOnly, String cleanUp) throws AAIException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, UnsupportedEncodingException, AAIUnknownObjectException, URISyntaxException {
549         
550         //nothing found
551         if (results.isEmpty()) {
552                 String msg = createNotFoundMessage(query.getResultType(), uri);
553                         throw new AAIException("AAI_6114", msg);
554         }
555
556         return serializer.dbToObject(results, obj, depth, nodeOnly, cleanUp);
557         
558         }
559
560         
561         /**
562          * Creates the not found message.
563          *
564          * @param resultType the result type
565          * @param uri the uri
566          * @return the string
567          */
568         private String createNotFoundMessage(String resultType, URI uri) {
569                 
570         String msg = "No Node of type " + resultType + " found at: " + uri.getPath();
571
572         return msg;
573         }
574         
575         /**
576          * Sets the depth.
577          *
578          * @param depthParam the depth param
579          * @return the int
580          * @throws AAIException the AAI exception
581          */
582         protected int setDepth(Introspector obj, String depthParam) throws AAIException {
583                         int depth = AAIProperties.MAXIMUM_DEPTH;
584
585         if(depthParam == null){
586                         if(this.version.compareTo(Version.v9) >= 0){
587                                 depth = 0;
588                         } else {
589                 depth = AAIProperties.MAXIMUM_DEPTH;
590                         }
591                 } else {
592                         if (!depthParam.isEmpty() && !"all".equals(depthParam)){
593                                 try {
594                                         depth = Integer.parseInt(depthParam);
595                                 } catch (Exception e) {
596                                         throw new AAIException("AAI_4016");
597                                 }
598
599                         }
600                 }
601         String maxDepth = obj.getMetadata(ObjectMetadata.MAXIMUM_DEPTH);
602         
603                 int maximumDepth = AAIProperties.MAXIMUM_DEPTH;
604
605                 if(maxDepth != null){
606             try {
607                 maximumDepth = Integer.parseInt(maxDepth);
608             } catch(Exception ex){
609                 throw new AAIException("AAI_4018");
610             }
611                 }
612
613                 if(depth > maximumDepth){
614                         throw new AAIException("AAI_3303");
615                 }
616
617                 return depth;
618         }
619         
620         /**
621          * Checks if is modification method.
622          *
623          * @param method the method
624          * @return true, if is modification method
625          */
626         private boolean isModificationMethod(HttpMethod method) {
627                 boolean result = false;
628                 
629                 if (method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PUT_EDGE) || method.equals(HttpMethod.DELETE_EDGE) || method.equals(HttpMethod.MERGE_PATCH)) {
630                         result = true;
631                 }
632                 
633                 return result;
634                 
635         }
636         
637         private HashMap<String, Introspector> getRelatedObjects(DBSerializer serializer, QueryEngine queryEngine, Vertex v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, UnsupportedEncodingException, AAIException, URISyntaxException {
638                 HashMap<String, Introspector> relatedVertices = new HashMap<>();
639                 List<Vertex> vertexChain = queryEngine.findParents(v);
640                 for (Vertex vertex : vertexChain) {
641                         try {
642                                 final Introspector vertexObj = serializer.getVertexProperties(vertex);
643                                 relatedVertices.put(vertexObj.getObjectId(), vertexObj);
644                         } catch (AAIUnknownObjectException e) {
645                                 LOGGER.warn("Unable to get vertex properties, partial list of related vertices returned");
646                         }
647                         
648                 }
649                 
650                 return relatedVertices;
651         }
652
653         private Map<Vertex, Introspector> buildIntrospectorObjects(DBSerializer serializer, Iterable<Vertex> vertices) {
654                 Map<Vertex, Introspector> deleteObjectMap = new HashMap<>();
655                 for (Vertex vertex : vertices) {
656                         try {
657                                 // deleteObjectMap.computeIfAbsent(vertex, s ->
658                                 // serializer.getLatestVersionView(vertex));
659                                 Introspector deleteObj = serializer.getLatestVersionView(vertex);
660                                 deleteObjectMap.put(vertex, deleteObj);
661                         } catch (UnsupportedEncodingException | AAIException e) {
662                                 LOGGER.warn("Unable to get Introspctor Objects, Just continue");
663                                 continue;
664                         }
665
666                 }
667
668                 return deleteObjectMap;
669
670         }
671
672         private Map<String, URI> buildURIMap(DBSerializer serializer, Map<Vertex, Introspector> introSpector) {
673                 Map<String, URI> uriMap = new HashMap<>();
674                 for (Map.Entry<Vertex, Introspector> entry : introSpector.entrySet()) {
675                         URI uri;
676                         try {
677                                 uri = serializer.getURIForVertex(entry.getKey());
678                                 if (null != entry.getValue())
679                                         uriMap.put(entry.getValue().getObjectId(), uri);
680                         } catch (UnsupportedEncodingException e) {
681                                 LOGGER.warn("Unable to get URIs, Just continue");
682                                 continue;
683                         }
684
685                 }
686
687                 return uriMap;
688
689         }
690
691         private Map<String, HashMap<String, Introspector>> buildRelatedObjects(DBSerializer serializer,
692                         QueryEngine queryEngine, Map<Vertex, Introspector> introSpector) {
693
694                 Map<String, HashMap<String, Introspector>> relatedObjectsMap = new HashMap<>();
695                 for (Map.Entry<Vertex, Introspector> entry : introSpector.entrySet()) {
696                         try {
697                                 HashMap<String, Introspector> relatedObjects = this.getRelatedObjects(serializer, queryEngine,
698                                                 entry.getKey());
699                                 if (null != entry.getValue())
700                                         relatedObjectsMap.put(entry.getValue().getObjectId(), relatedObjects);
701                         } catch (IllegalAccessException | IllegalArgumentException
702                                         | InvocationTargetException | SecurityException | InstantiationException | NoSuchMethodException
703                                         | UnsupportedEncodingException | AAIException | URISyntaxException e) {
704                                 LOGGER.warn("Unable to get realted Objects, Just continue");
705                                 continue;
706                         }
707
708                 }
709
710                 return relatedObjectsMap;
711
712         }
713
714         private void buildNotificationEvent(String sourceOfTruth, Status status, String transactionId,
715                         UEBNotification notification, Map<Vertex, Introspector> deleteObjects, Map<String, URI> uriMap,
716                         Map<String, HashMap<String, Introspector>> deleteRelatedObjects) {
717                 for (Map.Entry<Vertex, Introspector> entry : deleteObjects.entrySet()) {
718                         try {
719                                 String vertexObjectId = "";
720
721                                 if (null != entry.getValue()) {
722                                         vertexObjectId = entry.getValue().getObjectId();
723
724                                         if (uriMap.containsKey(vertexObjectId) && deleteRelatedObjects.containsKey(vertexObjectId))
725                                                 notification.createNotificationEvent(transactionId, sourceOfTruth, status,
726                                                                 uriMap.get(vertexObjectId), entry.getValue(), deleteRelatedObjects.get(vertexObjectId));
727                                 }
728                         } catch (UnsupportedEncodingException | AAIException e) {
729
730                                 LOGGER.warn("Error in sending otification");
731                         }
732                 }
733         }
734
735         
736 }