use of org.onap.aai.parsers.uri.URIToObject in project aai-graphadmin by onap.
the class CommandLineArgs method createNotificationEvent.
public void createNotificationEvent(String transactionId, String sourceOfTruth, URI uri, Introspector obj, Map<String, Introspector> relatedObjects) throws AAIException, UnsupportedEncodingException {
String action = "CREATE";
final Introspector notificationEvent = loader.introspectorFromName("notification-event");
try {
Introspector eventHeader = loader.introspectorFromName("notification-event-header");
URIToObject parser = new URIToObject(loader, uri, (HashMap) relatedObjects);
String entityLink = urlBase + version + uri;
notificationEvent.setValue("cambria-partition", "AAI");
eventHeader.setValue("entity-link", entityLink);
eventHeader.setValue("action", action);
eventHeader.setValue("entity-type", obj.getDbName());
eventHeader.setValue("top-entity-type", parser.getTopEntityName());
eventHeader.setValue("source-name", sourceOfTruth);
eventHeader.setValue("version", version.toString());
eventHeader.setValue("id", transactionId);
eventHeader.setValue("event-type", "AAI-BASELINE");
if (eventHeader.getValue("domain") == null) {
eventHeader.setValue("domain", AAIConfig.get("aai.notificationEvent.default.domain", "UNK"));
}
if (eventHeader.getValue("sequence-number") == null) {
eventHeader.setValue("sequence-number", AAIConfig.get("aai.notificationEvent.default.sequenceNumber", "UNK"));
}
if (eventHeader.getValue("severity") == null) {
eventHeader.setValue("severity", AAIConfig.get("aai.notificationEvent.default.severity", "UNK"));
}
if (eventHeader.getValue("id") == null) {
eventHeader.setValue("id", genDate2() + "-" + UUID.randomUUID().toString());
}
if (eventHeader.getValue("timestamp") == null) {
eventHeader.setValue("timestamp", genDate());
}
List<Object> parentList = parser.getParentList();
parentList.clear();
if (!parser.getTopEntity().equals(parser.getEntity())) {
Introspector child;
String json = obj.marshal(false);
child = parser.getLoader().unmarshal(parser.getEntity().getName(), json);
parentList.add(child.getUnderlyingObject());
}
final Introspector eventObject;
String json = "";
if (parser.getTopEntity().equals(parser.getEntity())) {
json = obj.marshal(false);
eventObject = loader.unmarshal(obj.getName(), json);
} else {
json = parser.getTopEntity().marshal(false);
eventObject = loader.unmarshal(parser.getTopEntity().getName(), json);
}
notificationEvent.setValue("event-header", eventHeader.getUnderlyingObject());
notificationEvent.setValue("entity", eventObject.getUnderlyingObject());
String entityJson = notificationEvent.marshal(false);
bw.newLine();
bw.write(entityJson);
} catch (AAIUnknownObjectException e) {
LOGGER.error("Fatal error - notification-event-header object not found!");
} catch (Exception e) {
LOGGER.error("Unmarshalling error occurred while generating Notification " + LogFormatTools.getStackTop(e));
}
}
use of org.onap.aai.parsers.uri.URIToObject in project aai-aai-common by onap.
the class HttpTestUtil method doPatch.
public Response doPatch(String uri, String payload) throws UnsupportedEncodingException, AAIException {
this.init();
Response response = null;
boolean success = true;
TransactionalGraphEngine dbEngine = null;
try {
if (uri.startsWith("/aai/")) {
uri = uri.substring(5);
}
logger.info("Starting the put request for the uri {} with payload {}", uri, payload);
String[] arr = uri.split("/");
SchemaVersion version = null;
if (arr.length > 1) {
if (arr[0].matches("^v\\d+")) {
version = new SchemaVersion(arr[0]);
uri = uri.replaceAll("^v\\d+", "");
}
}
if (version == null) {
version = schemaVersions.getDefaultVersion();
}
Mockito.when(uriInfo.getPath()).thenReturn(uri);
if (notification != null) {
traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
} else {
traversalHttpEntry.setHttpEntryProperties(version);
}
Loader loader = traversalHttpEntry.getLoader();
dbEngine = traversalHttpEntry.getDbEngine();
URI uriObject = UriBuilder.fromPath(uri).build();
URIToObject uriToObject = new URIToObject(loader, uriObject);
String objType = uriToObject.getEntityName();
QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject);
logger.info("Unmarshalling the payload to this {}", objType);
Introspector obj;
HttpMethod httpMethod;
obj = loader.unmarshal(objType, payload, org.onap.aai.restcore.MediaType.getEnum("application/json"));
httpMethod = HttpMethod.MERGE_PATCH;
this.validateIntrospector(obj, loader, uriObject, httpMethod);
DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION").rawRequestContent(payload).build();
List<DBRequest> dbRequestList = new ArrayList<>();
dbRequestList.add(dbRequest);
Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
response = responsesTuple.getValue1().get(0).getValue1();
} catch (AAIException e) {
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, e);
success = false;
} catch (Exception e) {
AAIException ex = new AAIException("AAI_4000", e);
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.PUT, ex);
success = false;
} finally {
if (success) {
if (response != null) {
if ((response.getStatus() / 100) == 2) {
logger.info("Successfully completed the PUT request with status {} and committing it to DB", response.getStatus());
} else {
logFailure(HttpMethod.PUT, response);
}
}
dbEngine.commit();
} else {
if (response != null) {
logFailure(HttpMethod.PUT, response);
}
dbEngine.rollback();
}
}
return response;
}
use of org.onap.aai.parsers.uri.URIToObject in project aai-aai-common by onap.
the class HttpTestUtil method doDelete.
public Response doDelete(Map<String, Pair<String, String>> deletes) {
this.init();
Response response = null;
boolean success = true;
TransactionalGraphEngine dbEngine = null;
try {
List<DBRequest> dbRequestList = new ArrayList<>();
for (Map.Entry<String, Pair<String, String>> delete : deletes.entrySet()) {
String uri = delete.getKey();
String resourceVersion = delete.getValue().getValue0();
String content = delete.getValue().getValue1();
uri = uri.replaceAll("/aai/", "");
logger.info("Starting the delete request for the uri {} with resource version {}", uri, resourceVersion);
String[] arr = uri.split("/");
SchemaVersion version = null;
if (arr.length > 1) {
if (arr[0].matches("^v\\d+")) {
version = new SchemaVersion(arr[0]);
uri = uri.replaceAll("^v\\d+", "");
}
}
if (version == null) {
version = schemaVersions.getDefaultVersion();
}
Mockito.when(uriInfo.getPath()).thenReturn(uri);
if (notification != null) {
traversalHttpEntry.setHttpEntryProperties(version, notification, notificationDepth);
} else {
traversalHttpEntry.setHttpEntryProperties(version);
}
Loader loader = traversalHttpEntry.getLoader();
dbEngine = traversalHttpEntry.getDbEngine();
URI uriObject = UriBuilder.fromPath(uri).build();
URIToObject uriToObject = new URIToObject(loader, uriObject);
String objType = uriToObject.getEntityName();
queryParameters.add("resource-version", resourceVersion);
QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, queryParameters);
logger.info("Unmarshalling the payload to this {}", objType);
Introspector obj;
HttpMethod httpMethod;
if (uri.contains("/relationship-list/relationship")) {
httpMethod = HttpMethod.DELETE_EDGE;
obj = loader.unmarshal("relationship", content, org.onap.aai.restcore.MediaType.getEnum("application/json"));
} else {
obj = loader.introspectorFromName(objType);
httpMethod = HttpMethod.DELETE;
}
DBRequest dbRequest = new DBRequest.Builder(httpMethod, uriObject, uriQuery, obj, httpHeaders, uriInfo, "JUNIT-TRANSACTION").build();
dbRequestList.add(dbRequest);
}
Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalHttpEntry.process(dbRequestList, "JUNIT");
response = responsesTuple.getValue1().get(0).getValue1();
} catch (AAIException e) {
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.DELETE, e);
success = false;
} catch (Exception e) {
AAIException ex = new AAIException("AAI_4000", e);
response = this.consumerExceptionResponseGenerator(httpHeaders, uriInfo, HttpMethod.DELETE, ex);
success = false;
} finally {
if (success) {
if (response != null) {
if ((response.getStatus() / 100) == 2) {
logger.info("Successfully completed the DELETE request with status {} and committing it to DB", response.getStatus());
} else {
logFailure(HttpMethod.DELETE, response);
}
}
dbEngine.commit();
} else {
logFailure(HttpMethod.DELETE, response);
dbEngine.rollback();
}
}
return response;
}
use of org.onap.aai.parsers.uri.URIToObject in project aai-aai-common by onap.
the class InjectKeysFromURI method resolveIssue.
/**
* {@inheritDoc}
*/
@Override
public boolean resolveIssue(Issue issue) {
boolean result = false;
Introspector obj = issue.getIntrospector();
if (issue.getType().equals(IssueType.MISSING_KEY_PROP)) {
try {
URIToObject toObject = new URIToObject(loader, uri);
Introspector minimumObj = toObject.getEntity();
if (toObject.getEntityName().equals(obj.getDbName())) {
obj.setValue(issue.getPropName(), minimumObj.getValue(issue.getPropName()));
result = true;
}
} catch (Exception e) {
// log something probably
result = false;
}
}
return result;
}
use of org.onap.aai.parsers.uri.URIToObject in project aai-aai-common by onap.
the class DataLinkWriter method processURI.
@Override
protected void processURI(Optional<String> completeUri, Entry<String, String> entry) throws URISyntaxException, UnsupportedEncodingException, AAIException {
if (completeUri.isPresent()) {
URI uri = new URI(completeUri.get());
MultivaluedMap<String, String> map = URITools.getQueryMap(uri);
QueryParser uriQuery = dbEngine.getQueryBuilder(this.latestLoader).createQueryFromURI(uri, map);
List<Vertex> results = uriQuery.getQueryBuilder().toList();
if (results.size() == 1) {
if (results.get(0).<Boolean>property(AAIProperties.LINKED).orElse(false) && obj.getValue(entry.getKey()) == null) {
// delete vertex because property was removed
serializer.delete(results.get(0), "", false);
} else {
// link vertex that already exists
this.addLinkedProperty(results.get(0));
}
} else {
if (results.isEmpty()) {
// locate previously linked vertex
List<Vertex> linkedVertices = uriQuery.getQueryBuilder().getContainerQuery().getVerticesByProperty(AAIProperties.LINKED, true).toList();
if (!linkedVertices.isEmpty()) {
if (linkedVertices.size() > 1) {
throw new AAIMultiplePropertiesException("multiple vertices found for single cardinality propery found when searching " + uri);
} else {
// found one, remove the linked property because it didn't match the uri
linkedVertices.get(0).property(AAIProperties.LINKED).remove();
}
}
if (obj.getValue(entry.getKey()) != null) {
// add new vertex to database if we have values
URIToObject parser = new URIToObject(this.latestLoader, uri);
Introspector resultObj = parser.getEntity();
Vertex newV = serializer.createNewVertex(resultObj);
serializer.serializeToDb(resultObj, newV, uriQuery, completeUri.get(), this.latestLoader.getVersion().toString());
this.addLinkedProperty(newV);
}
} else if (results.size() > 1) {
throw new AAIMultiplePropertiesException("multiple values of " + entry.getKey() + " found when searching " + uri);
}
}
} else {
// skip processing because no required properties were specified
}
}
Aggregations