X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=sparkybe-onap-service%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faai%2Fsparky%2Fdal%2FGizmoAdapter.java;h=c958054ad6b0460a151b071126a08da54b4878fe;hb=206e44d57c0b1800abc2faf313a5c0c1b4945a8a;hp=a888c8b8cbef465096bba1dd7430034632d588b8;hpb=97d1b45603ad3c860ac733810ff3064990433c50;p=aai%2Fsparky-be.git diff --git a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/GizmoAdapter.java b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/GizmoAdapter.java index a888c8b..c958054 100644 --- a/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/GizmoAdapter.java +++ b/sparkybe-onap-service/src/main/java/org/onap/aai/sparky/dal/GizmoAdapter.java @@ -50,229 +50,231 @@ import org.onap.aai.sparky.util.NodeUtils; public class GizmoAdapter { - private static final Logger LOG = LoggerFactory.getInstance().getLogger(GizmoAdapter.class); + private static final Logger LOG = LoggerFactory.getInstance().getLogger(GizmoAdapter.class); - private static final String HEADER_TRANS_ID = "X-TransactionId"; - private static final String HEADER_FROM_APP_ID = "X-FromAppId"; - private static final String HEADER_AUTHORIZATION = "Authorization"; + private static final String HEADER_TRANS_ID = "X-TransactionId"; + private static final String HEADER_FROM_APP_ID = "X-FromAppId"; + private static final String HEADER_AUTHORIZATION = "Authorization"; - private static final String HTTP_SCHEME = "http"; - private static final String HTTPS_SCHEME = "https"; + private static final String HTTP_SCHEME = "http"; + private static final String HTTPS_SCHEME = "https"; - private static final String TRANSACTION_ID_PREFIX = "txnId-"; - private static final String UI_APP_NAME = "AAI-UI"; + private static final String TRANSACTION_ID_PREFIX = "txnId-"; + private static final String UI_APP_NAME = "AAI-UI"; - private OxmModelLoader oxmModelLoader; + private OxmModelLoader oxmModelLoader; - private RestEndpointConfig endpointConfig; + private RestEndpointConfig endpointConfig; - private RestClient restClient; + private RestClient restClient; - private String inventoryBasePath; - private String relationshipsBasePath; + private String inventoryBasePath; + private String relationshipsBasePath; + private String appPartnerName = ""; - /** - * Instantiates a new active inventory adapter. - * - * @throws RestClientConstructionException - * - */ + /** + * Instantiates a new active inventory adapter. + * + * @throws RestClientConstructionException + * + */ - public GizmoAdapter(OxmModelLoader oxmModelLoader, RestEndpointConfig endpointConfig) - throws ElasticSearchOperationException, IOException, RestClientConstructionException { + public GizmoAdapter(OxmModelLoader oxmModelLoader, RestEndpointConfig endpointConfig) + throws ElasticSearchOperationException, IOException, RestClientConstructionException { - this.oxmModelLoader = oxmModelLoader; - this.endpointConfig = endpointConfig; - this.restClient = RestClientFactory.buildClient(endpointConfig); + this.oxmModelLoader = oxmModelLoader; + this.endpointConfig = endpointConfig; + this.restClient = RestClientFactory.buildClient(endpointConfig); - } + } + + public String getAppPartnerName() { + return appPartnerName; + } + + public void setAppPartnerName(String appPartnerName) { + this.appPartnerName = appPartnerName; + } - public String getRelationshipsBasePath() { - return relationshipsBasePath; - } - public void setRelationshipsBasePath(String relationshipsBasePath) { - this.relationshipsBasePath = relationshipsBasePath; - } + public String getRelationshipsBasePath() { + return relationshipsBasePath; + } - public String getInventoryBasePath() { - return inventoryBasePath; - } + public void setRelationshipsBasePath(String relationshipsBasePath) { + this.relationshipsBasePath = relationshipsBasePath; + } - public void setInventoryBasePath(String inventoryBasePath) { - this.inventoryBasePath = inventoryBasePath; - } + public String getInventoryBasePath() { + return inventoryBasePath; + } + + public void setInventoryBasePath(String inventoryBasePath) { + this.inventoryBasePath = inventoryBasePath; + } - public String getFullInventoryUrl(String resourceUrl) throws Exception { - final String host = endpointConfig.getEndpointIpAddress(); - final String port = endpointConfig.getEndpointServerPort(); - final String basePath = getInventoryBasePath(); - return String.format("https://%s:%s%s%s", host, port, basePath, resourceUrl); - } + public String getFullInventoryUrl(String resourceUrl) throws Exception { + final String host = endpointConfig.getEndpointIpAddress(); + final String port = endpointConfig.getEndpointServerPort(); + final String basePath = getInventoryBasePath(); + return String.format("https://%s:%s%s%s", host, port, basePath, resourceUrl); + } - public String addServerDetailsToUrl(String resourceUrl) throws Exception { - final String host = endpointConfig.getEndpointIpAddress(); - final String port = endpointConfig.getEndpointServerPort(); - return String.format("https://%s:%s/%s", host, port, resourceUrl); - } + public String addServerDetailsToUrl(String resourceUrl) throws Exception { + final String host = endpointConfig.getEndpointIpAddress(); + final String port = endpointConfig.getEndpointServerPort(); + return String.format("https://%s:%s/%s", host, port, resourceUrl); + } - public String getFullRelationshipUrl(String resourceUrl) throws Exception { - final String host = endpointConfig.getEndpointIpAddress(); - final String port = endpointConfig.getEndpointServerPort(); - final String basePath = getRelationshipsBasePath(); - return String.format("https://%s:%s%s%s", host, port, basePath, resourceUrl); - } + public String getFullRelationshipUrl(String resourceUrl) throws Exception { + final String host = endpointConfig.getEndpointIpAddress(); + final String port = endpointConfig.getEndpointServerPort(); + final String basePath = getRelationshipsBasePath(); + return String.format("https://%s:%s%s%s", host, port, basePath, resourceUrl); + } - protected Map> getMessageHeaders() { + protected Map> getMessageHeaders() { - Map> headers = new HashMap>(); + Map> headers = new HashMap>(); - headers.putIfAbsent(HEADER_FROM_APP_ID, new ArrayList()); - headers.get(HEADER_FROM_APP_ID).add(UI_APP_NAME); + headers.putIfAbsent(HEADER_FROM_APP_ID, new ArrayList()); + headers.get(HEADER_FROM_APP_ID).add(appPartnerName); - headers.putIfAbsent(HEADER_TRANS_ID, new ArrayList()); - headers.get(HEADER_TRANS_ID).add(TRANSACTION_ID_PREFIX + NodeUtils.getRandomTxnId()); + headers.putIfAbsent(HEADER_TRANS_ID, new ArrayList()); + headers.get(HEADER_TRANS_ID).add(TRANSACTION_ID_PREFIX + NodeUtils.getRandomTxnId()); - if (endpointConfig.getRestAuthenticationMode() == RestAuthenticationMode.SSL_BASIC) { + if (endpointConfig.getRestAuthenticationMode() == RestAuthenticationMode.SSL_BASIC) { - headers.putIfAbsent(HEADER_AUTHORIZATION, new ArrayList()); - headers.get(HEADER_AUTHORIZATION).add(getBasicAuthenticationCredentials()); + headers.putIfAbsent(HEADER_AUTHORIZATION, new ArrayList()); + headers.get(HEADER_AUTHORIZATION).add(getBasicAuthenticationCredentials()); - } + } - return headers; - } + return headers; + } - protected String getBasicAuthenticationCredentials() { - String usernameAndPassword = String.join(":", endpointConfig.getBasicAuthUserName(), - endpointConfig.getBasicAuthPassword()); - return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes()); - } + protected String getBasicAuthenticationCredentials() { + String usernameAndPassword = String.join(":", endpointConfig.getBasicAuthUserName(), + endpointConfig.getBasicAuthPassword()); + return "Basic " + java.util.Base64.getEncoder().encodeToString(usernameAndPassword.getBytes()); + } - /** - * Our retry conditions should be very specific. - * - * @param r - * the r - * @return true, if successful - */ - private boolean shouldRetryRequest(OperationResult r) { + /** + * Our retry conditions should be very specific. + * + * @param r the r + * @return true, if successful + */ + private boolean shouldRetryRequest(OperationResult r) { - if (r == null) { - return true; - } + if (r == null) { + return true; + } - int rc = r.getResultCode(); + int rc = r.getResultCode(); - if (rc == 200) { - return false; - } + if (rc == 200) { + return false; + } - if (rc == 404) { - return false; - } + if (rc == 404) { + return false; + } - return true; + return true; - } + } - /** - * Query active inventory. - * - * @param url - * the url - * @param acceptContentType - * the accept content type - * @return the operation result - */ - OperationResult queryGizmo(String url, String acceptContentType) { + /** + * Query active inventory. + * + * @param url the url + * @param acceptContentType the accept content type + * @return the operation result + */ + OperationResult queryGizmo(String url, String acceptContentType) { - return restClient.get(url, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE); + return restClient.get(url, getMessageHeaders(), MediaType.APPLICATION_JSON_TYPE); - } + } - public RestEndpointConfig getEndpointConfig() { - return endpointConfig; - } + public RestEndpointConfig getEndpointConfig() { + return endpointConfig; + } - public void setEndpointConfig(RestEndpointConfig endpointConfig) { - this.endpointConfig = endpointConfig; - } + public void setEndpointConfig(RestEndpointConfig endpointConfig) { + this.endpointConfig = endpointConfig; + } - public OperationResult queryGizmoWithRetries(String url, String responseType, int numRetries) { + public OperationResult queryGizmoWithRetries(String url, String responseType, int numRetries) { - OperationResult result = null; + OperationResult result = null; - for (int retryCount = 0; retryCount < numRetries; retryCount++) { + for (int retryCount = 0; retryCount < numRetries; retryCount++) { - LOG.debug(AaiUiMsgs.QUERY_AAI_RETRY_SEQ, url, String.valueOf(retryCount + 1)); + LOG.debug(AaiUiMsgs.QUERY_AAI_RETRY_SEQ, url, String.valueOf(retryCount + 1)); - result = queryGizmo(url, responseType); + result = queryGizmo(url, responseType); - /** - * Record number of times we have attempted the request to later - * summarize how many times we are generally retrying over thousands - * of messages in a sync. - * - * If the number of retries is surprisingly high, then we need to - * understand why that is as the number of retries is also causing a - * heavier load on AAI beyond the throttling controls we already - * have in place in term of the transaction rate controller and - * number of parallelized threads per task processor. - */ + /** + * Record number of times we have attempted the request to later summarize how many times we + * are generally retrying over thousands of messages in a sync. + * + * If the number of retries is surprisingly high, then we need to understand why that is as + * the number of retries is also causing a heavier load on AAI beyond the throttling controls + * we already have in place in term of the transaction rate controller and number of + * parallelized threads per task processor. + */ - result.setNumRetries(retryCount); + result.setNumRetries(retryCount); - if (!shouldRetryRequest(result)) { + if (!shouldRetryRequest(result)) { - result.setFromCache(false); - LOG.debug(AaiUiMsgs.QUERY_AAI_RETRY_DONE_SEQ, url, String.valueOf(retryCount + 1)); + result.setFromCache(false); + LOG.debug(AaiUiMsgs.QUERY_AAI_RETRY_DONE_SEQ, url, String.valueOf(retryCount + 1)); - return result; - } + return result; + } - try { - /* - * Sleep between re-tries to be nice to the target system. - */ - Thread.sleep(50); - } catch (InterruptedException exc) { - LOG.error(AaiUiMsgs.QUERY_AAI_WAIT_INTERRUPTION, exc.getLocalizedMessage()); - Thread.currentThread().interrupt(); - break; - } - LOG.error(AaiUiMsgs.QUERY_AAI_RETRY_FAILURE_WITH_SEQ, url, String.valueOf(retryCount + 1)); + try { + /* + * Sleep between re-tries to be nice to the target system. + */ + Thread.sleep(50); + } catch (InterruptedException exc) { + LOG.error(AaiUiMsgs.QUERY_AAI_WAIT_INTERRUPTION, exc.getLocalizedMessage()); + Thread.currentThread().interrupt(); + break; + } + LOG.error(AaiUiMsgs.QUERY_AAI_RETRY_FAILURE_WITH_SEQ, url, String.valueOf(retryCount + 1)); - } + } - LOG.info(AaiUiMsgs.QUERY_AAI_RETRY_MAXED_OUT, url); + LOG.info(AaiUiMsgs.QUERY_AAI_RETRY_MAXED_OUT, url); - return result; + return result; - } + } - /** - * This method adds a scheme, host and port (if missing) to the passed-in - * URI. If these parts of the URI are already present, they will not be - * duplicated. - * - * @param selflink - * The URI to repair - * @param queryParams - * The query parameters as a single string - * @return The corrected URI (i.e. includes a scheme/host/port) - */ + /** + * This method adds a scheme, host and port (if missing) to the passed-in URI. If these parts of + * the URI are already present, they will not be duplicated. + * + * @param selflink The URI to repair + * @param queryParams The query parameters as a single string + * @return The corrected URI (i.e. includes a scheme/host/port) + */ private String repairGizmoSelfLink(String baseUrlPath, String selfLink, String queryParams) { if (selfLink == null) { return selfLink; } - + if (selfLink.startsWith("http") || selfLink.startsWith("https")) { return selfLink; } - + UriBuilder builder = UriBuilder.fromPath(baseUrlPath + "/" + selfLink) .host(endpointConfig.getEndpointIpAddress()) .port(Integer.parseInt(endpointConfig.getEndpointServerPort())); @@ -300,7 +302,7 @@ public class GizmoAdapter { return (builder.build().toString() + (includeQueryParams ? queryParams : "")); } - + public String repairRelationshipSelfLink(String selflink, String queryParams) { return repairGizmoSelfLink(relationshipsBasePath, selflink, queryParams); } @@ -309,25 +311,27 @@ public class GizmoAdapter { return repairGizmoSelfLink(inventoryBasePath, selflink, queryParams); } - public OperationResult getSelfLinksByEntityType(String entityType) throws Exception { - - if (entityType == null) { - throw new NullPointerException("Failed to getSelfLinksByEntityType() because entityType is null"); - } - - String link = getFullInventoryUrl(entityType); - - return queryGizmoWithRetries(link, "application/json", endpointConfig.getNumRequestRetries()); - - } - - public static String extractResourcePath(String selflink) { - try { - return new URI(selflink).getRawPath(); - } catch (URISyntaxException uriSyntaxException) { - LOG.error(AaiUiMsgs.ERROR_EXTRACTING_RESOURCE_PATH_FROM_LINK, uriSyntaxException.getMessage()); - return selflink; - } - } + public OperationResult getSelfLinksByEntityType(String entityType) throws Exception { + + if (entityType == null) { + throw new NullPointerException( + "Failed to getSelfLinksByEntityType() because entityType is null"); + } + + String link = getFullInventoryUrl(entityType); + + return queryGizmoWithRetries(link, "application/json", endpointConfig.getNumRequestRetries()); + + } + + public static String extractResourcePath(String selflink) { + try { + return new URI(selflink).getRawPath(); + } catch (URISyntaxException uriSyntaxException) { + LOG.error(AaiUiMsgs.ERROR_EXTRACTING_RESOURCE_PATH_FROM_LINK, + uriSyntaxException.getMessage()); + return selflink; + } + } }