From 56253dc9561d5b24919bf59f83ccc55934d8ad50 Mon Sep 17 00:00:00 2001 From: Daniel Silverthorn Date: Fri, 15 Mar 2019 14:20:53 -0400 Subject: Create vertex and edge indicies on startup Change-Id: I3ee538796e21a0667df4b4dbbdfec40f5b4ca30d Issue-ID: AAI-2265 Signed-off-by: Daniel Silverthorn --- .../main/java/org/onap/champ/ChampApplication.java | 5 +- .../org/onap/champ/service/ChampDataService.java | 99 +++++++++++++++++++--- .../org/onap/champ/service/ChampUUIDService.java | 10 ++- 3 files changed, 96 insertions(+), 18 deletions(-) (limited to 'champ-service/src/main/java') diff --git a/champ-service/src/main/java/org/onap/champ/ChampApplication.java b/champ-service/src/main/java/org/onap/champ/ChampApplication.java index da4f634..1f1fa3c 100644 --- a/champ-service/src/main/java/org/onap/champ/ChampApplication.java +++ b/champ-service/src/main/java/org/onap/champ/ChampApplication.java @@ -2,8 +2,8 @@ * ============LICENSE_START========================================== * org.onap.aai * =================================================================== - * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. - * Copyright © 2017-2018 Amdocs + * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved. + * Copyright © 2017-2018 Amdocs * =================================================================== * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -63,7 +63,6 @@ public class ChampApplication extends SpringBootServletInitializer { props.put("server.ssl.client-auth",requireClientAuth.equals("true")?"need":"want"); } - new ChampApplication().configure(new SpringApplicationBuilder(ChampApplication.class).properties(props)) .run(args); } diff --git a/champ-service/src/main/java/org/onap/champ/service/ChampDataService.java b/champ-service/src/main/java/org/onap/champ/service/ChampDataService.java index 762b948..6db965f 100644 --- a/champ-service/src/main/java/org/onap/champ/service/ChampDataService.java +++ b/champ-service/src/main/java/org/onap/champ/service/ChampDataService.java @@ -46,6 +46,8 @@ import org.onap.champ.service.logging.ChampMsgs; import org.onap.champ.util.ChampProperties; import org.onap.champ.util.ChampServiceConstants; +import java.io.PrintWriter; +import java.io.StringWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; @@ -61,6 +63,7 @@ public class ChampDataService { private ChampGraph graphImpl; private ChampTransactionCache cache; + private boolean graphInitialized = false; private static final String KEY_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_KEY_NAME); private static final String SOT_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_SOT_NAME); private static final String CREATED_TS_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_CREATED_TS_NAME); @@ -72,19 +75,27 @@ public class ChampDataService { this.champUUIDService = champUUIDService; this.graphImpl = graphImpl; - - ChampField field = new ChampField.Builder(ChampProperties.get("keyName")) - .type(ChampField.Type.STRING) - .build(); - ChampObjectIndex index = new ChampObjectIndex.Builder(ChampProperties.get("keyName"), "STRING", field).build(); - - graphImpl.storeObjectIndex(index); - this.cache = cache; + + try { + initializeGraph(); + } + catch (Exception ex) { + // Swallow exception to prevent application from crashing. Connection will be retried when + // champ processes a new request. + StringWriter writer = new StringWriter(); + PrintWriter printWriter = new PrintWriter(writer); + ex.printStackTrace(printWriter); + logger.error(ChampMsgs.CHAMP_DATA_SERVICE_ERROR, "Unable to initialize graph: " + ex.getLocalizedMessage()); + logger.error(ChampMsgs.CHAMP_DATA_SERVICE_ERROR, writer.toString()); + } } public ChampObject getObject(String id, Optional transaction) throws ChampServiceException { - + if (!graphInitialized) { + initializeGraph(); + } + Optional retrieved = Optional.empty(); try { retrieved = champUUIDService.getObjectbyUUID(id, transaction.orElse(null)); @@ -101,7 +112,10 @@ public class ChampDataService { public ChampObject storeObject(ChampObject object, Optional transaction) throws ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException, ChampTransactionException, ChampServiceException { - + if (!graphInitialized) { + initializeGraph(); + } + if (object.getProperty(KEY_NAME).isPresent() || object.getKey().isPresent()) { throw new ChampServiceException(KEY_NAME + " can't be updated", Status.BAD_REQUEST); } @@ -115,6 +129,10 @@ public class ChampDataService { public ChampObject replaceObject(ChampObject object, String objectId, Optional transaction) throws ChampServiceException, ChampUnmarshallingException, ChampTransactionException, ChampMarshallingException, ChampSchemaViolationException, ChampObjectNotExistsException { + if (!graphInitialized) { + initializeGraph(); + } + if (object.getKey().isPresent() && (!object.getKeyValue().equals(objectId))) { throw new ChampServiceException("Object Id in the URI doesn't match the body.", Status.BAD_REQUEST); } @@ -154,6 +172,10 @@ public class ChampDataService { public void deleteObject(String objectId, Optional transaction) throws ChampServiceException, ChampObjectNotExistsException, ChampTransactionException, ChampUnmarshallingException { + if (!graphInitialized) { + initializeGraph(); + } + Optional retrieved = champUUIDService.getObjectbyUUID(objectId, transaction.orElse(null)); if (!retrieved.isPresent()) { throw new ChampServiceException(objectId + " not found", Status.NOT_FOUND); @@ -172,7 +194,10 @@ public class ChampDataService { throws ChampMarshallingException, ChampObjectNotExistsException, ChampSchemaViolationException, ChampRelationshipNotExistsException, ChampUnmarshallingException, ChampTransactionException, ChampServiceException { - + if (!graphInitialized) { + initializeGraph(); + } + if (r.getSource() == null || !r.getSource().getKey().isPresent() || r.getTarget() == null || !r.getTarget().getKey().isPresent()) { logger.error(ChampMsgs.CHAMP_DATA_SERVICE_ERROR, "Source/Target Object key must be provided"); @@ -207,6 +232,10 @@ public class ChampDataService { public ChampRelationship updateRelationship(ChampRelationship r, String rId, Optional transaction) throws ChampServiceException, ChampUnmarshallingException, ChampTransactionException, ChampMarshallingException, ChampSchemaViolationException, ChampRelationshipNotExistsException { + if (!graphInitialized) { + initializeGraph(); + } + if (r.getKey().isPresent() && (!r.getKeyValue().equals(rId))) { throw new ChampServiceException("Relationship Id in the URI \"" + rId + "\" doesn't match the URI in the body" @@ -257,6 +286,10 @@ public class ChampDataService { public void deleteRelationship(String relationshipId, Optional transaction) throws ChampServiceException, ChampRelationshipNotExistsException, ChampTransactionException, ChampUnmarshallingException { + if (!graphInitialized) { + initializeGraph(); + } + Optional retrieved = champUUIDService.getRelationshipbyUUID(relationshipId, transaction.orElse(null)); if (!retrieved.isPresent()) { @@ -270,6 +303,10 @@ public class ChampDataService { public List getRelationshipsByObject(String objectId, Optional transaction) throws ChampServiceException { + if (!graphInitialized) { + initializeGraph(); + } + try { Optional retrievedObject = champUUIDService.getObjectbyUUID(objectId, transaction.orElse(null)); if (!retrievedObject.isPresent()) { @@ -296,8 +333,11 @@ public class ChampDataService { * @throws ChampServiceException */ public List queryObjects(Map filter, HashSet properties) throws ChampServiceException { + if (!graphInitialized) { + initializeGraph(); + } + try { - Stream retrieved = graphImpl.queryObjects(filter); List objects = champUUIDService.populateUUIDKey(retrieved.collect(Collectors.toList())); @@ -314,6 +354,10 @@ public class ChampDataService { } public List queryRelationships(Map filter) throws ChampServiceException { + if (!graphInitialized) { + initializeGraph(); + } + try { List relations = new ArrayList(); Stream retrieved; @@ -329,7 +373,10 @@ public class ChampDataService { public ChampRelationship getRelationship(String id, Optional transaction) throws ChampServiceException { - + if (!graphInitialized) { + initializeGraph(); + } + Optional retrieved = Optional.empty(); try { retrieved = champUUIDService.getRelationshipbyUUID(id, transaction.orElse(null)); @@ -344,6 +391,10 @@ public class ChampDataService { } public String openTransaction() { + if (!graphInitialized) { + initializeGraph(); + } + ChampTransaction transaction = graphImpl.openTransaction(); String transacId = transaction.id(); cache.put(transacId, transaction); @@ -352,6 +403,10 @@ public class ChampDataService { } public void commitTransaction(String tId) throws ChampServiceException, ChampTransactionException { + if (!graphInitialized) { + initializeGraph(); + } + ChampTransaction transaction = cache.get(tId); if (transaction == null) { throw new ChampServiceException("Transaction Not found: " + tId, Status.NOT_FOUND); @@ -363,6 +418,10 @@ public class ChampDataService { } public void rollbackTransaction(String tId) throws ChampServiceException, ChampTransactionException { + if (!graphInitialized) { + initializeGraph(); + } + ChampTransaction transaction = cache.get(tId); if (transaction == null) { throw new ChampServiceException("Transaction Not found: " + tId, Status.NOT_FOUND); @@ -378,6 +437,10 @@ public class ChampDataService { } public ChampBulkResponse processBulkRequest(ChampBulkPayload bulkPayload) throws ChampServiceException, ChampRelationshipNotExistsException, ChampTransactionException, ChampUnmarshallingException, ChampObjectNotExistsException, ChampMarshallingException, ChampSchemaViolationException { + if (!graphInitialized) { + initializeGraph(); + } + // Open a transaction. If any operations fail, we want to rollback ChampTransaction transaction = graphImpl.openTransaction(); if (transaction == null) { @@ -469,4 +532,14 @@ public class ChampDataService { e.getProperties().put(LAST_MOD_TS_NAME, timestamp); } + + private synchronized void initializeGraph() { + if (graphInitialized) { + return; + } + + graphImpl.createDefaultIndexes(); + + graphInitialized = true; + } } diff --git a/champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java b/champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java index 0001610..259a4bf 100644 --- a/champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java +++ b/champ-service/src/main/java/org/onap/champ/service/ChampUUIDService.java @@ -27,7 +27,11 @@ import org.onap.aai.champcore.exceptions.ChampUnmarshallingException; import org.onap.aai.champcore.model.ChampElement; import org.onap.aai.champcore.model.ChampObject; import org.onap.aai.champcore.model.ChampRelationship; +import org.onap.aai.cl.api.Logger; +import org.onap.aai.cl.eelf.LoggerFactory; +import org.onap.champ.ChampRESTAPI; import org.onap.champ.exception.ChampServiceException; +import org.onap.champ.service.logging.ChampMsgs; import org.onap.champ.util.ChampProperties; import org.onap.champ.util.ChampServiceConstants; @@ -41,7 +45,7 @@ import java.util.stream.Stream; public class ChampUUIDService { private ChampGraph graphImpl; private static final String KEY_NAME = ChampProperties.get(ChampServiceConstants.CHAMP_KEY_NAME); - + private Logger logger = LoggerFactory.getInstance().getLogger(ChampUUIDService.class); public ChampUUIDService(ChampGraph graphImpl) { this.graphImpl = graphImpl; @@ -117,18 +121,20 @@ public class ChampUUIDService { throws ChampUnmarshallingException, ChampTransactionException, ChampServiceException { Optional response = Optional.empty(); - Stream s; Map filter = new HashMap<>(); filter.put(KEY_NAME, uuid); s = graphImpl.queryRelationships(filter, Optional.ofNullable(transaction)); + Object[] objs = s.toArray(); if (objs.length == 0) { return response; } + response = graphImpl.retrieveRelationship(((ChampRelationship) objs[0]).getKey().get(), Optional.ofNullable(transaction)); + return response; } -- cgit 1.2.3-korg