1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
|
/*-
* ============LICENSE_START=======================================================
* org.openecomp.aai
* ================================================================================
* Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ============LICENSE_END=========================================================
*/
package org.openecomp.aai.rest.db;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Set;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.core.UriBuilder;
import org.apache.commons.lang.StringUtils;
import org.apache.tinkerpop.gremlin.structure.Graph;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.javatuples.Pair;
import org.openecomp.aai.db.props.AAIProperties;
import org.openecomp.aai.dbmap.DBConnectionType;
import org.openecomp.aai.domain.responseMessage.AAIResponseMessage;
import org.openecomp.aai.domain.responseMessage.AAIResponseMessageDatum;
import org.openecomp.aai.exceptions.AAIException;
import org.openecomp.aai.introspection.Introspector;
import org.openecomp.aai.introspection.Loader;
import org.openecomp.aai.introspection.LoaderFactory;
import org.openecomp.aai.introspection.MarshallerProperties;
import org.openecomp.aai.introspection.ModelInjestor;
import org.openecomp.aai.introspection.ModelType;
import org.openecomp.aai.introspection.Version;
import org.openecomp.aai.introspection.exceptions.AAIUnknownObjectException;
import org.openecomp.aai.logging.ErrorLogHelper;
import org.openecomp.aai.parsers.query.QueryParser;
import org.openecomp.aai.parsers.uri.URIToExtensionInformation;
import org.openecomp.aai.rest.ueb.UEBNotification;
import org.openecomp.aai.restcore.HttpMethod;
import org.openecomp.aai.schema.enums.ObjectMetadata;
import org.openecomp.aai.serialization.db.DBSerializer;
import org.openecomp.aai.serialization.engines.QueryStyle;
import org.openecomp.aai.serialization.engines.TitanDBEngine;
import org.openecomp.aai.serialization.engines.TransactionalGraphEngine;
import org.openecomp.aai.serialization.engines.query.QueryEngine;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.fge.jsonpatch.JsonPatchException;
import com.github.fge.jsonpatch.mergepatch.JsonMergePatch;
import com.thinkaurelius.titan.core.TitanException;
/**
* The Class HttpEntry.
*/
public class HttpEntry {
private static final EELFLogger LOGGER = EELFManager.getInstance().getLogger(HttpEntry.class);
private final ModelType introspectorFactoryType;
private final QueryStyle queryStyle;
private final Version version;
private final Loader loader;
private final TransactionalGraphEngine dbEngine;
private boolean processSingle = true;
/**
* Instantiates a new http entry.
*
* @param version the version
* @param modelType the model type
* @param queryStyle the query style
* @param llBuilder the ll builder
*/
public HttpEntry(Version version, ModelType modelType, QueryStyle queryStyle, DBConnectionType connectionType) {
this.introspectorFactoryType = modelType;
this.queryStyle = queryStyle;
this.version = version;
this.loader = LoaderFactory.createLoaderForVersion(introspectorFactoryType, version);
this.dbEngine = new TitanDBEngine(
queryStyle,
connectionType,
loader);
//start transaction on creation
dbEngine.startTransaction();
}
/**
* Gets the introspector factory type.
*
* @return the introspector factory type
*/
public ModelType getIntrospectorFactoryType() {
return introspectorFactoryType;
}
/**
* Gets the query style.
*
* @return the query style
*/
public QueryStyle getQueryStyle() {
return queryStyle;
}
/**
* Gets the version.
*
* @return the version
*/
public Version getVersion() {
return version;
}
/**
* Gets the loader.
*
* @return the loader
*/
public Loader getLoader() {
return loader;
}
/**
* Gets the db engine.
*
* @return the db engine
*/
public TransactionalGraphEngine getDbEngine() {
return dbEngine;
}
public Pair<Boolean, List<Pair<URI, Response>>> process (List<DBRequest> requests, String sourceOfTruth) throws AAIException {
return this.process(requests, sourceOfTruth, true);
}
/**
* Process.
* @param requests the requests
* @param sourceOfTruth the source of truth
*
* @return the pair
* @throws AAIException the AAI exception
*/
public Pair<Boolean, List<Pair<URI, Response>>> process (List<DBRequest> requests, String sourceOfTruth, boolean enableResourceVersion) throws AAIException {
DBSerializer serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth);
Response response = null;
Status status = Status.NOT_FOUND;
Introspector obj = null;
QueryParser query = null;
URI uri = null;
String transactionId = null;
UEBNotification notification = new UEBNotification(loader);
int depth = AAIProperties.MAXIMUM_DEPTH;
List<Pair<URI,Response>> responses = new ArrayList<>();
MultivaluedMap<String, String> params = null;
HttpMethod method = null;
String uriTemp = "";
Boolean success = true;
QueryEngine queryEngine = dbEngine.getQueryEngine();
int maxRetries = 10;
int retry = 0;
for (DBRequest request : requests) {
try {
for (retry = 0; retry < maxRetries; ++retry) {
try {
method = request.getMethod();
obj = request.getIntrospector();
query = request.getParser();
transactionId = request.getTransactionId();
uriTemp = request.getUri().getRawPath().replaceFirst("^v\\d+/", "");
uri = UriBuilder.fromPath(uriTemp).build();
List<Vertex> vertices = query.getQueryBuilder().toList();
boolean isNewVertex = false;
String outputMediaType = getMediaType(request.getHeaders().getAcceptableMediaTypes());
String result = null;
params = request.getInfo().getQueryParameters(false);
depth = setDepth(obj, params.getFirst("depth"));
String cleanUp = params.getFirst("cleanup");
String requestContext = "";
List<String> requestContextList = request.getHeaders().getRequestHeader("aai-request-context");
if (requestContextList != null) {
requestContext = requestContextList.get(0);
}
if (cleanUp == null) {
cleanUp = "false";
}
if (vertices.size() > 1 && processSingle && !method.equals(HttpMethod.GET)) {
if (method.equals(HttpMethod.DELETE)) {
throw new AAIException("AAI_6138");
} else {
throw new AAIException("AAI_6137");
}
}
if (method.equals(HttpMethod.PUT)) {
String resourceVersion = (String)obj.getValue("resource-version");
if (vertices.isEmpty()) {
if (enableResourceVersion) {
serializer.verifyResourceVersion("create", query.getResultType(), "", resourceVersion, obj.getURI());
}
isNewVertex = true;
} else {
if (enableResourceVersion) {
serializer.verifyResourceVersion("update", query.getResultType(), (String)vertices.get(0).<String>property("resource-version").orElse(null), resourceVersion, obj.getURI());
}
isNewVertex = false;
}
} else {
if (vertices.isEmpty()) {
String msg = createNotFoundMessage(query.getResultType(), request.getUri());
throw new AAIException("AAI_6114", msg);
} else {
isNewVertex = false;
}
}
Vertex v = null;
if (!isNewVertex) {
v = vertices.get(0);
}
HashMap<String, Introspector> relatedObjects = new HashMap<>();
switch (method) {
case GET:
String nodeOnly = params.getFirst("nodes-only");
boolean isNodeOnly = nodeOnly != null;
obj = this.getObjectFromDb(vertices, serializer, query, obj, request.getUri(), depth, isNodeOnly, cleanUp);
if (obj != null) {
status = Status.OK;
MarshallerProperties properties;
if (!request.getMarshallerProperties().isPresent()) {
properties =
new MarshallerProperties.Builder(org.openecomp.aai.restcore.MediaType.getEnum(outputMediaType)).build();
} else {
properties = request.getMarshallerProperties().get();
}
result = obj.marshal(properties);
}
break;
case PUT:
if (isNewVertex) {
v = serializer.createNewVertex(obj);
}
serializer.serializeToDb(obj, v, query, uri.getRawPath(), requestContext);
status = Status.OK;
if (isNewVertex) {
status = Status.CREATED;
}
obj = serializer.getLatestVersionView(v);
if (query.isDependent()) {
relatedObjects = this.getRelatedObjects(serializer, queryEngine, v);
}
notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects);
break;
case PUT_EDGE:
serializer.touchStandardVertexProperties(v, false);
serializer.createEdge(obj, v);
status = Status.OK;
break;
case MERGE_PATCH:
Introspector existingObj = (Introspector) obj.clone();
existingObj = this.getObjectFromDb(vertices, serializer, query, existingObj, request.getUri(), 0, false, cleanUp);
String existingJson = existingObj.marshal(false);
String newJson;
if (request.getRawRequestContent().isPresent()) {
newJson = request.getRawRequestContent().get();
} else {
newJson = "";
}
Object relationshipList = request.getIntrospector().getValue("relationship-list");
ObjectMapper mapper = new ObjectMapper();
try {
JsonNode existingNode = mapper.readTree(existingJson);
JsonNode newNode = mapper.readTree(newJson);
JsonMergePatch patch = JsonMergePatch.fromJson(newNode);
JsonNode completed = patch.apply(existingNode);
String patched = mapper.writeValueAsString(completed);
Introspector patchedObj = loader.unmarshal(existingObj.getName(), patched);
if (relationshipList == null) {
//if the caller didn't touch the relationship-list, we shouldn't either
patchedObj.setValue("relationship-list", null);
}
serializer.serializeToDb(patchedObj, v, query, uri.getRawPath(), requestContext);
status = Status.OK;
patchedObj = serializer.getLatestVersionView(v);
if (query.isDependent()) {
relatedObjects = this.getRelatedObjects(serializer, queryEngine, v);
}
notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, patchedObj, relatedObjects);
} catch (IOException | JsonPatchException e) {
throw new AAIException("AAI_3000", "could not perform patch operation");
}
break;
case DELETE:
String resourceVersion = params.getFirst("resource-version");
obj = serializer.getLatestVersionView(v);
if (query.isDependent()) {
relatedObjects = this.getRelatedObjects(serializer, queryEngine, v);
}
serializer.delete(v, resourceVersion, enableResourceVersion);
status = Status.NO_CONTENT;
notification.createNotificationEvent(transactionId, sourceOfTruth, status, uri, obj, relatedObjects);
break;
case DELETE_EDGE:
serializer.touchStandardVertexProperties(v, false);
serializer.deleteEdge(obj, v);
status = Status.NO_CONTENT;
break;
default:
break;
}
/* temporarily adding vertex id to the headers
* to be able to use for testing the vertex id endpoint functionality
* since we presently have no other way of generating those id urls
*/
if (response == null && v != null && (
method.equals(HttpMethod.PUT)
|| method.equals(HttpMethod.GET)
|| method.equals(HttpMethod.MERGE_PATCH))
) {
String myvertid = v.id().toString();
response = Response.status(status)
.header("vertex-id", myvertid)
.entity(result)
.type(outputMediaType).build();
} else if (response == null) {
response = Response.status(status)
.type(outputMediaType).build();
} else {
//response already set to something
}
Pair<URI,Response> pairedResp = Pair.with(request.getUri(), response);
responses.add(pairedResp);
//break out of retry loop
break;
} catch (TitanException e) {
this.dbEngine.rollback();
AAIException ex = new AAIException("AAI_6142", e);
ErrorLogHelper.logException(ex);
Thread.sleep((retry + 1) * 20);
this.dbEngine.startTransaction();
queryEngine = dbEngine.getQueryEngine();
serializer = new DBSerializer(version, dbEngine, introspectorFactoryType, sourceOfTruth);
}
if (retry == maxRetries) {
throw new AAIException("AAI_6134");
}
}
} catch (AAIException e) {
success = false;
ArrayList<String> templateVars = new ArrayList<String>();
templateVars.add(request.getMethod().toString()); //GET, PUT, etc
templateVars.add(request.getUri().getPath().toString());
templateVars.addAll(e.getTemplateVars());
response = Response
.status(e.getErrorObject().getHTTPResponseCode())
.entity(ErrorLogHelper.getRESTAPIErrorResponse(request.getHeaders().getAcceptableMediaTypes(), e, templateVars))
.build();
Pair<URI,Response> pairedResp = Pair.with(request.getUri(), response);
responses.add(pairedResp);
continue;
} catch (Exception e) {
success = false;
e.printStackTrace();
AAIException ex = new AAIException("AAI_4000", e);
ArrayList<String> templateVars = new ArrayList<String>();
templateVars.add(request.getMethod().toString()); //GET, PUT, etc
templateVars.add(request.getUri().getPath().toString());
response = Response
.status(ex.getErrorObject().getHTTPResponseCode())
.entity(ErrorLogHelper.getRESTAPIErrorResponse(request.getHeaders().getAcceptableMediaTypes(), ex, templateVars))
.build();
Pair<URI, Response> pairedResp = Pair.with(request.getUri(), response);
responses.add(pairedResp);
continue;
}
}
notification.triggerEvents();
Pair<Boolean, List<Pair<URI, Response>>> tuple = Pair.with(success, responses);
return tuple;
}
/**
* Gets the media type.
*
* @param mediaTypeList the media type list
* @return the media type
*/
private String getMediaType(List <MediaType> mediaTypeList) {
String mediaType = MediaType.APPLICATION_JSON; // json is the default
for (MediaType mt : mediaTypeList) {
if (MediaType.APPLICATION_XML_TYPE.isCompatible(mt)) {
mediaType = MediaType.APPLICATION_XML;
}
}
return mediaType;
}
/**
* Gets the object from db.
*
* @param serializer the serializer
* @param g the g
* @param query the query
* @param obj the obj
* @param uri the uri
* @param depth the depth
* @param cleanUp the clean up
* @return the object from db
* @throws AAIException the AAI exception
* @throws IllegalAccessException the illegal access exception
* @throws IllegalArgumentException the illegal argument exception
* @throws InvocationTargetException the invocation target exception
* @throws SecurityException the security exception
* @throws InstantiationException the instantiation exception
* @throws NoSuchMethodException the no such method exception
* @throws UnsupportedEncodingException the unsupported encoding exception
* @throws MalformedURLException the malformed URL exception
* @throws AAIUnknownObjectException
* @throws URISyntaxException
*/
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 {
//nothing found
if (results.size() == 0) {
String msg = createNotFoundMessage(query.getResultType(), uri);
throw new AAIException("AAI_6114", msg);
}
obj = serializer.dbToObject(results, obj, depth, nodeOnly, cleanUp);
return obj;
}
/**
* Creates the not found message.
*
* @param resultType the result type
* @param uri the uri
* @return the string
*/
private String createNotFoundMessage(String resultType, URI uri) {
String msg = "No Node of type " + resultType + " found at: " + uri.getPath();
return msg;
}
/**
* Sets the depth.
*
* @param depthParam the depth param
* @return the int
* @throws AAIException the AAI exception
*/
protected int setDepth(Introspector obj, String depthParam) throws AAIException {
int depth = AAIProperties.MAXIMUM_DEPTH;
if(depthParam == null){
if(this.version.compareTo(Version.v9) >= 0){
depth = 0;
} else {
depth = AAIProperties.MAXIMUM_DEPTH;
}
} else {
if (depthParam.length() > 0 && !depthParam.equals("all")){
try {
depth = Integer.valueOf(depthParam);
} catch (Exception e) {
throw new AAIException("AAI_4016");
}
}
}
String maxDepth = obj.getMetadata(ObjectMetadata.MAXIMUM_DEPTH);
int maximumDepth = AAIProperties.MAXIMUM_DEPTH;
if(maxDepth != null){
try {
maximumDepth = Integer.parseInt(maxDepth);
} catch(Exception ex){
throw new AAIException("AAI_4018");
}
}
if(depth > maximumDepth){
throw new AAIException("AAI_3303");
}
return depth;
}
/**
* Checks if is modification method.
*
* @param method the method
* @return true, if is modification method
*/
private boolean isModificationMethod(HttpMethod method) {
boolean result = false;
if (method.equals(HttpMethod.PUT) || method.equals(HttpMethod.PUT_EDGE) || method.equals(HttpMethod.DELETE_EDGE) || method.equals(HttpMethod.MERGE_PATCH)) {
result = true;
}
return result;
}
private HashMap<String, Introspector> getRelatedObjects(DBSerializer serializer, QueryEngine queryEngine, Vertex v) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, SecurityException, InstantiationException, NoSuchMethodException, UnsupportedEncodingException, AAIException, URISyntaxException {
HashMap<String, Introspector> relatedVertices = new HashMap<>();
List<Vertex> vertexChain = queryEngine.findParents(v);
for (Vertex vertex : vertexChain) {
try {
final Introspector vertexObj = serializer.getVertexProperties(vertex);
relatedVertices.put(vertexObj.getObjectId(), vertexObj);
} catch (AAIUnknownObjectException e) {
LOGGER.warn("Unable to get vertex properties, partial list of related vertices returned");
}
}
return relatedVertices;
}
}
|