diff options
Diffstat (limited to 'aai-resources/src/main/java')
3 files changed, 229 insertions, 64 deletions
diff --git a/aai-resources/src/main/java/org/onap/aai/IncreaseNodesTool.java b/aai-resources/src/main/java/org/onap/aai/IncreaseNodesTool.java index 5f94141..a4c6ef5 100644 --- a/aai-resources/src/main/java/org/onap/aai/IncreaseNodesTool.java +++ b/aai-resources/src/main/java/org/onap/aai/IncreaseNodesTool.java @@ -23,7 +23,6 @@ import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import java.util.UUID; - import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource; import org.apache.tinkerpop.gremlin.structure.Vertex; import org.janusgraph.core.JanusGraph; @@ -37,18 +36,22 @@ import org.onap.aai.introspection.exceptions.AAIUnknownObjectException; import org.onap.aai.setup.SchemaVersions; import org.onap.aai.util.AAISystemExitUtil; import org.onap.aai.util.PositiveNumValidator; - +import org.onap.aai.serialization.engines.TransactionalGraphEngine; +import org.onap.aai.serialization.db.EdgeSerializer; import com.beust.jcommander.JCommander; import com.beust.jcommander.Parameter; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext; public class IncreaseNodesTool { public static long nodeCount = 0; - + protected EdgeSerializer edgeSerializer; private LoaderFactory loaderFactory; private SchemaVersions schemaVersions; + protected TransactionalGraphEngine engine; + Vertex parentVtx; + + public IncreaseNodesTool(LoaderFactory loaderFactory, SchemaVersions schemaVersions){ this.loaderFactory = loaderFactory; @@ -88,20 +91,18 @@ public class IncreaseNodesTool { nodeCount = Long.parseLong(cArgs.numberOfNodes); addVertex(janusGraph, cArgs.nodeType,propList,cArgs); } - /*** * adds a vertex based on user inputs of node type number of nodes and the node uri * /cloud-infrastructure/pservers/pserver/ * /network/pnfs/pnf/ - * /cloud-infrastructure/pservers/pserver/random-056fd6c4-7313-4fa0-b854-0d9983bdb0ab/DevB/p-interfaces/p-interface/ - * @param nodeType - * @param propList + * /cloud-infrastructure/pservers/pserver/random-056fd6c4-7313-4fa0-b854-0d9983bdb0ab/p-interfaces/p-interface/ + * @param + * @param * @param cArgs */ public void addVertex(JanusGraph janusGraph, String nodeType, List<String> propList,CommandLineArgs cArgs){ long startTime = System.currentTimeMillis(); - try (JanusGraphTransaction transaction = janusGraph.newTransaction()) { boolean success = true; @@ -110,10 +111,27 @@ public class IncreaseNodesTool { for (long i = 1; i <= nodeCount; i++) { String randomId = UUID.randomUUID().toString(); Vertex v = g.addV().next(); + + if(cArgs.child.equals(true)){ + + if(parentVtx == null){ + String[] uriTokens = cArgs.uri.split("//"); + String ParentNodeType = uriTokens[uriTokens.length-4]; //parent node type + String keyVal = uriTokens[uriTokens.length-3]; // parent unique key + parentVtx = g.V().has(ParentNodeType,keyVal).next(); + edgeSerializer.addTreeEdgeIfPossible(g,parentVtx,v); + + } + else{ + edgeSerializer.addTreeEdgeIfPossible(g,parentVtx,v); + } + } + v.property("aai-node-type", nodeType); v.property("source-of-truth", "IncreaseNodesTool"); v.property("aai-uri", cArgs.uri+"random-"+randomId); + for(String propName : propList){ if(propName.equals("in-maint")){ v.property(propName,"false"); @@ -137,17 +155,23 @@ public class IncreaseNodesTool { } } -} + } + + class CommandLineArgs { @Parameter(names = "-numberOfNodes", description = "how many nodes you would like to enter", required = true , validateWith = PositiveNumValidator.class) public String numberOfNodes; - @Parameter(names = "-nodeType", description = "They aai-node-type of the node being entered", required = true) + @Parameter(names = "-nodeType", description = "The aai-node-type of the node being entered", required = true) public String nodeType; - @Parameter(names = "-uri", description = "uri to be passed for the node") + @Parameter(names = "-uri", description = "uri to be passed for the node",required = true) public String uri; + + @Parameter(names = "-child", description = "is this a child node",required = true) + public String child; + } diff --git a/aai-resources/src/main/java/org/onap/aai/ResourcesApp.java b/aai-resources/src/main/java/org/onap/aai/ResourcesApp.java index 988b057..36fcd41 100644 --- a/aai-resources/src/main/java/org/onap/aai/ResourcesApp.java +++ b/aai-resources/src/main/java/org/onap/aai/ResourcesApp.java @@ -25,11 +25,13 @@ import java.util.UUID; import javax.annotation.PostConstruct; import javax.annotation.PreDestroy; +import org.apache.commons.lang3.exception.ExceptionUtils; import org.onap.aai.config.PropertyPasswordConfiguration; import org.onap.aai.config.SpringContextAware; import org.onap.aai.dbmap.AAIGraph; import org.onap.aai.exceptions.AAIException; +import org.onap.aai.logging.ErrorLogHelper; import org.onap.aai.logging.LoggingContext; import org.onap.aai.logging.LoggingContext.StatusCode; import org.onap.aai.util.AAIConfig; @@ -135,13 +137,27 @@ public class ResourcesApp { LoggingContext.serviceName(APP_NAME); LoggingContext.targetServiceName("contextInitialized"); LoggingContext.statusCode(StatusCode.COMPLETE); - - - SpringApplication app = new SpringApplication(ResourcesApp.class); - app.setLogStartupInfo(false); - app.setRegisterShutdownHook(true); - app.addInitializers(new PropertyPasswordConfiguration()); - Environment env = app.run(args).getEnvironment(); + + Environment env =null; + AAIConfig.init(); + try { + SpringApplication app = new SpringApplication(ResourcesApp.class); + app.setLogStartupInfo(false); + app.setRegisterShutdownHook(true); + app.addInitializers(new PropertyPasswordConfiguration()); + env = app.run(args).getEnvironment(); + } + catch(Exception ex){ + AAIException aai = schemaServiceExceptionTranslator(ex); + LoggingContext.statusCode(LoggingContext.StatusCode.ERROR); + LoggingContext.responseCode(LoggingContext.DATA_ERROR); + logger.error("Problems starting ResourcesApp "+aai.getMessage()); + ErrorLogHelper.logException(aai); + ErrorLogHelper.logError(aai.getCode(), ex.getMessage() + ", resolve and restart Resources"); + //ErrorLogHelper.logError(aai.getCode(), aai.getMessage() + aai.getCause().toString()); + throw aai; + } + MDC.setContextMap (contextMap); logger.info( "Application '{}' is running on {}!" , @@ -187,4 +203,21 @@ public class ResourcesApp { } } } + private static AAIException schemaServiceExceptionTranslator(Exception ex) { + AAIException aai = null; + if(ExceptionUtils.getRootCause(ex).getMessage().contains("NodeIngestor")){ + aai = new AAIException("AAI_3026","Error reading OXM from SchemaService - Investigate"); + } + else if(ExceptionUtils.getRootCause(ex).getMessage().contains("EdgeIngestor")){ + aai = new AAIException("AAI_3027","Error reading EdgeRules from SchemaService - Investigate"); + } + else if(ExceptionUtils.getRootCause(ex).getMessage().contains("Connection refused")){ + aai = new AAIException("AAI_3025","Error connecting to SchemaService - Investigate"); + } + else { + aai = new AAIException("AAI_3025","Error connecting to SchemaService - Please Investigate"); + } + + return aai; + } } diff --git a/aai-resources/src/main/java/org/onap/aai/rest/LegacyMoxyConsumer.java b/aai-resources/src/main/java/org/onap/aai/rest/LegacyMoxyConsumer.java index 7f73362..0b544f4 100644 --- a/aai-resources/src/main/java/org/onap/aai/rest/LegacyMoxyConsumer.java +++ b/aai-resources/src/main/java/org/onap/aai/rest/LegacyMoxyConsumer.java @@ -50,6 +50,8 @@ import javax.ws.rs.core.UriInfo; import io.swagger.jaxrs.PATCH; import org.javatuples.Pair; +import org.json.JSONArray; +import org.json.JSONObject; import org.onap.aai.concurrent.AaiCallable; import org.onap.aai.config.SpringContextAware; import org.onap.aai.dbmap.DBConnectionType; @@ -93,6 +95,16 @@ public class LegacyMoxyConsumer extends RESTAPI { } + /** + * + * @param content + * @param versionParam + * @param uri + * @param headers + * @param info + * @param req + * @return + */ @PUT @Path("/{uri: .+}") @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) @@ -128,17 +140,17 @@ public class LegacyMoxyConsumer extends RESTAPI { TransactionalGraphEngine dbEngine = null; boolean success = true; - try { + try { validateRequest(info); - SchemaVersion version = new SchemaVersion(versionParam); - DBConnectionType type = null; - if(AAIConfig.get("aai.use.realtime", "true").equals("true")){ - type = DBConnectionType.REALTIME; + SchemaVersion version = new SchemaVersion(versionParam); + DBConnectionType type = null; + if(AAIConfig.get("aai.use.realtime", "true").equals("true")){ + type = DBConnectionType.REALTIME; } else { type = this.determineConnectionType(sourceOfTruth, realTime); } - HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class); - traversalUriHttpEntry.setHttpEntryProperties(version, type); + HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class); + traversalUriHttpEntry.setHttpEntryProperties(version, type); loader = traversalUriHttpEntry.getLoader(); dbEngine = traversalUriHttpEntry.getDbEngine(); @@ -199,7 +211,7 @@ public class LegacyMoxyConsumer extends RESTAPI { return this.handleWrites(mediaType, HttpMethod.MERGE_PATCH, content, versionParam, uri, headers, info); } - + /** * Gets the legacy. * @@ -230,12 +242,12 @@ public class LegacyMoxyConsumer extends RESTAPI { return getLegacy(content, versionParam, uri, depthParam, cleanUp, headers, info, req, new HashSet<String>(), resultIndex, resultSize); } } - ); + ); } /** * This method exists as a workaround for filtering out undesired query params while routing between REST consumers - * + * * @param content * @param versionParam * @param uri @@ -296,7 +308,7 @@ public class LegacyMoxyConsumer extends RESTAPI { Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalUriHttpEntry.process(requests, sourceOfTruth); response = responsesTuple.getValue1().get(0).getValue1(); - + } catch (AAIException e) { response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET, e); } catch (Exception e ) { @@ -377,7 +389,7 @@ public class LegacyMoxyConsumer extends RESTAPI { QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject); String objType = uriQuery.getResultType(); - Introspector obj = loader.introspectorFromName(objType); + Introspector obj = loader.introspectorFromName(objType); DBRequest request = new DBRequest.Builder(HttpMethod.DELETE, uriObject, uriQuery, obj, headers, info, transId).build(); List<DBRequest> requests = new ArrayList<>(); @@ -462,7 +474,7 @@ public class LegacyMoxyConsumer extends RESTAPI { Introspector wrappedEntity = loader.unmarshal("relationship", content, org.onap.aai.restcore.MediaType.getEnum(this.getInputMediaType(inputMediaType))); - DBRequest request = new DBRequest.Builder(HttpMethod.DELETE_EDGE, uriObject, uriQuery, wrappedEntity, headers, info, transId).build(); + DBRequest request = new DBRequest.Builder(HttpMethod.DELETE_EDGE, uriObject, uriQuery, wrappedEntity, headers, info, transId).build(); List<DBRequest> requests = new ArrayList<>(); requests.add(request); Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalUriHttpEntry.process(requests, sourceOfTruth); @@ -488,7 +500,103 @@ public class LegacyMoxyConsumer extends RESTAPI { return response; } - + + @GET + @Path("/{uri: .+}/relationship-list") + @Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) + public Response getRelationshipList (@DefaultValue("-1") @QueryParam("resultIndex") String resultIndex, @DefaultValue("-1") @QueryParam("resultSize") String resultSize, @PathParam("version")String versionParam, @PathParam("uri") @Encoded String uri, @DefaultValue("false") @QueryParam("cleanup") String cleanUp, @Context HttpHeaders headers, @Context UriInfo info) { + return runner(AAIConstants.AAI_CRUD_TIMEOUT_ENABLED, + AAIConstants.AAI_CRUD_TIMEOUT_APP, + AAIConstants.AAI_CRUD_TIMEOUT_LIMIT, + headers, + info, + HttpMethod.GET, + new AaiCallable<Response>() { + @Override + public Response process() { + return getRelationshipList(versionParam, uri, cleanUp, headers, info, resultIndex, resultSize); + } + } + ); + } + + /** + * + * @param versionParam + * @param uri + * @param cleanUp + * @param headers + * @param info + * @return + */ + public Response getRelationshipList(String versionParam, String uri, String cleanUp, HttpHeaders headers, UriInfo info, String resultIndex, String resultSize) { + String sourceOfTruth = headers.getRequestHeaders().getFirst("X-FromAppId"); + String transId = headers.getRequestHeaders().getFirst("X-TransactionId"); + String realTime = headers.getRequestHeaders().getFirst("Real-Time"); + Response response = null; + TransactionalGraphEngine dbEngine = null; + Loader loader = null; + + try { + validateRequest(info); + SchemaVersion version = new SchemaVersion(versionParam); + DBConnectionType type = null; + if(AAIConfig.get("aai.use.realtime", "true").equals("true")){ + type = DBConnectionType.REALTIME; + } else { + type = this.determineConnectionType(sourceOfTruth, realTime); + } + final HttpEntry traversalUriHttpEntry = SpringContextAware.getBean("traversalUriHttpEntry", HttpEntry.class); + traversalUriHttpEntry.setHttpEntryProperties(version, type); + dbEngine = traversalUriHttpEntry.getDbEngine(); + loader = traversalUriHttpEntry.getLoader(); + MultivaluedMap<String, String> params = info.getQueryParameters(); + + params = removeNonFilterableParams(params); + + uri = uri.split("\\?")[0]; + + URI uriObject = UriBuilder.fromPath(uri).build(); + + QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject, params); + + String objType = ""; + if (!uriQuery.getContainerType().equals("")) { + objType = uriQuery.getContainerType(); + } else { + objType = uriQuery.getResultType(); + } + Introspector obj = loader.introspectorFromName(objType); + DBRequest request = + new DBRequest.Builder(HttpMethod.GET_RELATIONSHIP, uriObject, uriQuery, obj, headers, info, transId).build(); + List<DBRequest> requests = new ArrayList<>(); + requests.add(request); + if (resultIndex != null && resultIndex != "-1" && resultSize != null && resultSize != "-1") { + traversalUriHttpEntry.setPaginationIndex(Integer.parseInt(resultIndex)); + traversalUriHttpEntry.setPaginationBucket(Integer.parseInt(resultSize)); + } + Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalUriHttpEntry.process(requests, sourceOfTruth); + + response = responsesTuple.getValue1().get(0).getValue1(); + } catch (AAIException e) { + response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET_RELATIONSHIP, e); + } catch (Exception e ) { + AAIException ex = new AAIException("AAI_4000", e); + + response = consumerExceptionResponseGenerator(headers, info, HttpMethod.GET_RELATIONSHIP, ex); + } finally { + if (dbEngine != null) { + if (cleanUp.equals("true")) { + dbEngine.commit(); + } else { + dbEngine.rollback(); + } + } + } + return response; + } + /** * Validate request. * @@ -502,7 +610,7 @@ public class LegacyMoxyConsumer extends RESTAPI { throw new AAIException("AAI_3008", "uri=" + getPath(info)); } } - + /** * Gets the path. * @@ -523,11 +631,11 @@ public class LegacyMoxyConsumer extends RESTAPI { if (map.keySet().size() > 0) { path += params + queryParams; } - + return path; - + } - + /** * Handle writes. * @@ -568,32 +676,32 @@ public class LegacyMoxyConsumer extends RESTAPI { URI uriObject = UriBuilder.fromPath(uri).build(); this.validateURI(uriObject); QueryParser uriQuery = dbEngine.getQueryBuilder().createQueryFromURI(uriObject); - String objName = uriQuery.getResultType(); - if (content.length() == 0) { - if (mediaType.toString().contains(MediaType.APPLICATION_JSON)) { - content = "{}"; - } else { - content = "<empty/>"; - } - } - Introspector obj = loader.unmarshal(objName, content, org.onap.aai.restcore.MediaType.getEnum(this.getInputMediaType(mediaType))); - if (obj == null) { - throw new AAIException("AAI_3000", "object could not be unmarshalled:" + content); - } - - if (mediaType.toString().contains(MediaType.APPLICATION_XML) && !content.equals("<empty/>") && isEmptyObject(obj)) { - throw new AAIInvalidXMLNamespace(content); - } - - this.validateIntrospector(obj, loader, uriObject, method); - - DBRequest request = + String objName = uriQuery.getResultType(); + if (content.length() == 0) { + if (mediaType.toString().contains(MediaType.APPLICATION_JSON)) { + content = "{}"; + } else { + content = "<empty/>"; + } + } + Introspector obj = loader.unmarshal(objName, content, org.onap.aai.restcore.MediaType.getEnum(this.getInputMediaType(mediaType))); + if (obj == null) { + throw new AAIException("AAI_3000", "object could not be unmarshalled:" + content); + } + + if (mediaType.toString().contains(MediaType.APPLICATION_XML) && !content.equals("<empty/>") && isEmptyObject(obj)) { + throw new AAIInvalidXMLNamespace(content); + } + + this.validateIntrospector(obj, loader, uriObject, method); + + DBRequest request = new DBRequest.Builder(method, uriObject, uriQuery, obj, headers, info, transId) - .rawRequestContent(content).build(); + .rawRequestContent(content).build(); List<DBRequest> requests = new ArrayList<>(); requests.add(request); Pair<Boolean, List<Pair<URI, Response>>> responsesTuple = traversalUriHttpEntry.process(requests, sourceOfTruth); - + response = responsesTuple.getValue1().get(0).getValue1(); success = responsesTuple.getValue0(); } catch (AAIException e) { @@ -612,21 +720,21 @@ public class LegacyMoxyConsumer extends RESTAPI { } } } - + return response; } - + private void validateURI(URI uri) throws AAIException { if (hasRelatedTo(uri)) { throw new AAIException("AAI_3010"); } } private boolean hasRelatedTo(URI uri) { - + return uri.toString().contains("/" + RestTokens.COUSIN + "/"); } - + protected boolean isEmptyObject(Introspector obj) { - return "{}".equals(obj.marshal(false)); + return "{}".equals(obj.marshal(false)); } } |