diff options
author | Ittay Stern <ittay.stern@att.com> | 2018-12-31 17:21:27 +0200 |
---|---|---|
committer | Ittay Stern <ittay.stern@att.com> | 2019-01-09 20:19:55 +0200 |
commit | 6ad41e3ccd398a2721f41ad61c80b7bb03f7d127 (patch) | |
tree | 3bd672dff83e3218232cd8665680416b7fc26a5d /vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java | |
parent | 5ec29ff5e3864f1ba6ecac71f8bffbefa400cf27 (diff) |
Merge from ECOMP's repository
Main Features
--------------
- Async-Instantiation jobs mechanism major update; still WIP (package
`org.onap.vid.job`)
- New features in View/Edit: Activate fabric configuration; show
related networks; soft delete
- Support AAI service-tree traversal (`AAIServiceTree`)
- In-memory cache for SDC models and certain A&AI queries
(`CacheProviderWithLoadingCache`)
- Upgrade TOSCA Parser and add parsing options; fix malformed TOSCA
models
- Resolve Cloud-Owner values for MSO
- Pass X-ONAP headers to MSO
Infrastructure
--------------
- Remove codehaus' jackson mapper; use soley fasterxml 2.9.7
- Surefire invokes both TestNG and JUnit tests
- Support Kotlin source files
- AaiController2 which handles errors in a "Spring manner"
- Inline generated-sources and remove jsonschema2pojo
Quality
--------
- Cumulative bug fixes (A&AI API, UI timeouts, and many more)
- Many Sonar issues cleaned-up
- Some unused classes removed
- Minor changes in vid-automation project, allowing some API
verification to run
Hard Merges
------------
- HTTP Clients (MSO, A&AI, WebConfig, OutgoingRequestHeadersTest)
- Moved `package org.onap.vid.controllers` to `controller`, without
plural -- just to keep semantic sync with ECOMP.
Reference commit in ECOMP: 3d1141625
Issue-ID: VID-378
Change-Id: I9c8d1e74caa41815891d441fc0760bb5f29c5788
Signed-off-by: Ittay Stern <ittay.stern@att.com>
Diffstat (limited to 'vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java')
-rw-r--r-- | vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java | 108 |
1 files changed, 49 insertions, 59 deletions
diff --git a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java index f5625ce21..f4401ab3a 100644 --- a/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java +++ b/vid-app-common/src/main/java/org/onap/vid/aai/util/AAIRestInterface.java @@ -22,12 +22,14 @@ package org.onap.vid.aai.util; import com.att.eelf.configuration.EELFLogger; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.exception.ExceptionUtils; import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; import org.onap.vid.aai.ExceptionWithRequestInfo; import org.onap.vid.aai.ResponseWithRequestInfo; import org.onap.vid.aai.exceptions.InvalidPropertyException; import org.onap.vid.utils.Logging; +import org.onap.vid.utils.Unchecked; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpMethod; @@ -37,6 +39,7 @@ import javax.ws.rs.client.Invocation; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import java.io.UnsupportedEncodingException; +import java.net.URI; import java.net.URLEncoder; import java.util.Optional; import java.util.UUID; @@ -55,7 +58,6 @@ public class AAIRestInterface { protected final EELFLogger outgoingRequestsLogger = Logging.getRequestsLogger("aai"); - /** The client. */ private Client client = null; @@ -105,9 +107,9 @@ public class AAIRestInterface { return URLEncoder.encode(nodeKey, "UTF-8").replaceAll("\\+", "%20"); } - private void initRestClient() { - initRestClient(false); - } + protected void initRestClient() { + initRestClient(false); + } private void initRestClient(boolean propagateExceptions) { @@ -160,47 +162,61 @@ public class AAIRestInterface { * @param xml the xml * @return the string */ - public ResponseWithRequestInfo RestGet(String fromAppId, String transId, String requestUri, boolean xml) { + public ResponseWithRequestInfo RestGet(String fromAppId, String transId, URI requestUri, boolean xml) { return RestGet(fromAppId, transId, requestUri, xml, false); } - public ResponseWithRequestInfo RestGet(String fromAppId, String transId, String requestUri, boolean xml, boolean propagateExceptions) { - String methodName = "RestGet"; - String url = systemPropertyHelper.getFullServicePath(requestUri); + public ResponseWithRequestInfo RestGet(String fromAppId, String transId, URI requestUri, boolean xml, boolean propagateExceptions) { + return doRest(fromAppId, transId, requestUri, null, HttpMethod.GET, xml, propagateExceptions); + } + + public ResponseWithRequestInfo doRest(String fromAppId, String transId, URI requestUri, String payload, HttpMethod method, boolean xml, boolean propagateExceptions) { + String url = null; + String methodName = "Rest"+method.name(); try { + + url = systemPropertyHelper.getFullServicePath(requestUri); + initRestClient(propagateExceptions); logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING); logger.debug(EELFLoggerDelegate.debugLogger, url + " for the get REST API"); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.GET, url); + Logging.logRequest(outgoingRequestsLogger, method, url, payload); final Response response; - Invocation.Builder requestBuilder = client.target(url) - .request() - .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) - .header(TRANSACTION_ID_HEADER, transId) - .header(FROM_APP_ID_HEADER, fromAppId) - .header("Content-Type", MediaType.APPLICATION_JSON) - .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()); - response = systemPropertyHelper.isClientCertEnabled() ? - requestBuilder.get() : authenticateRequest(requestBuilder).get(); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.GET, url, response); - - if (response.getStatusInfo().equals(Response.Status.OK)) { + Invocation.Builder requestBuilder = client.target(url) + .request() + .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) + .header(TRANSACTION_ID_HEADER, transId) + .header(FROM_APP_ID_HEADER, fromAppId) + .header("Content-Type", MediaType.APPLICATION_JSON) + .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()); + + requestBuilder = systemPropertyHelper.isClientCertEnabled() ? + requestBuilder : authenticateRequest(requestBuilder); + + Invocation restInvocation = StringUtils.isEmpty(payload) ? + requestBuilder.build(method.name()) : + requestBuilder.build(method.name(), Entity.entity(payload, MediaType.APPLICATION_JSON)); + + response = restInvocation.invoke(); + Logging.logResponse(outgoingRequestsLogger, method, url, response); + + if (response.getStatusInfo().getFamily() == Response.Status.Family.SUCCESSFUL) { logger.debug(EELFLoggerDelegate.debugLogger, methodName + SUCCESSFUL_API_MESSAGE); logger.info(EELFLoggerDelegate.errorLogger, methodName + SUCCESSFUL_API_MESSAGE); } else { logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, response)); } - return new ResponseWithRequestInfo(response, url, HttpMethod.GET); + return new ResponseWithRequestInfo(response, url, method); } catch (Exception e) { logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e)); if (propagateExceptions) { - throw new ExceptionWithRequestInfo(HttpMethod.GET, defaultIfNull(url, requestUri), e); - } else { - return new ResponseWithRequestInfo(null, url, HttpMethod.GET); - } + throw new ExceptionWithRequestInfo(method, defaultIfNull(url, requestUri.toASCIIString()), e); + } else { + return new ResponseWithRequestInfo(null, url, method); + } } } @@ -222,7 +238,7 @@ public class AAIRestInterface { transId += ":" + UUID.randomUUID().toString(); logger.debug(methodName + START_STRING); Boolean response = false; - String url = systemPropertyHelper.getFullServicePath(path);; + String url = systemPropertyHelper.getFullServicePath(path); try { initRestClient(); @@ -263,37 +279,11 @@ public class AAIRestInterface { * @param path the path * @param payload the payload * @param xml the xml + * @param propagateExceptions * @return the string */ - public Response RestPut(String fromAppId, String path, String payload, boolean xml) { - String methodName = "RestPut"; - String url=systemPropertyHelper.getFullServicePath(path); - String transId = UUID.randomUUID().toString(); - logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING); - - Response response = null; - try { - initRestClient(); - Logging.logRequest(outgoingRequestsLogger, HttpMethod.PUT, url, payload); - response = authenticateRequest(client.target(url) - .request() - .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) - .header(TRANSACTION_ID_HEADER, transId) - .header(FROM_APP_ID_HEADER, fromAppId)) - .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()) - .put(Entity.entity(payload, MediaType.APPLICATION_JSON)); - Logging.logResponse(outgoingRequestsLogger, HttpMethod.PUT, url, response); - - if (response.getStatusInfo().getFamily().equals(Response.Status.Family.SUCCESSFUL)) { - logger.info(EELFLoggerDelegate.errorLogger, getValidResponseLogMessage(methodName)); - logger.debug(EELFLoggerDelegate.debugLogger, getValidResponseLogMessage(methodName)); - } else { - logger.debug(EELFLoggerDelegate.debugLogger, getInvalidResponseLogMessage(url, methodName, response)); - } - } catch (Exception e) { - logger.debug(EELFLoggerDelegate.debugLogger, getFailedResponseLogMessage(url, methodName, e)); - } - return response; + public ResponseWithRequestInfo RestPut(String fromAppId, String path, String payload, boolean xml, boolean propagateExceptions) { + return doRest(fromAppId, UUID.randomUUID().toString(), Unchecked.toURI(path), payload, HttpMethod.PUT, xml, propagateExceptions); } @@ -313,13 +303,13 @@ public class AAIRestInterface { String transId = UUID.randomUUID().toString(); logger.debug(EELFLoggerDelegate.debugLogger, methodName + START_STRING); - Response response = null; + Response response = null; try { initRestClient(); Logging.logRequest(outgoingRequestsLogger, HttpMethod.POST, url, payload); - response = authenticateRequest(client.target(systemPropertyHelper.getServiceBasePath(path)) + response = authenticateRequest(client.target(url) .request() - .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) + .accept(xml ? MediaType.APPLICATION_XML : MediaType.APPLICATION_JSON) .header(TRANSACTION_ID_HEADER, transId) .header(FROM_APP_ID_HEADER, fromAppId)) .header(REQUEST_ID_HEADER_KEY, extractOrGenerateRequestId()) |