diff options
author | mmis <michael.morris@ericsson.com> | 2018-07-23 12:22:07 +0100 |
---|---|---|
committer | mmis <michael.morris@ericsson.com> | 2018-07-30 23:00:38 +0100 |
commit | e87b2f7632dedf0067ea2417fb1157a8638df101 (patch) | |
tree | a79b8b16fd48c328cd9deedbebfbcc8ea84b9f0d /controlloop/common/model-impl/so/src/main/java | |
parent | f892e8b13ea87c1d7fb1c890f64ff00fc6e2de5f (diff) |
Copy policy-endpoints from drools-pdp to common
Replaced references to classes deleted from drools-pdp with references to the
corresponding in policy-common
Issue-ID: POLICY-967
Change-Id: Ia9d2ac704e6b7c434e5a9e7aee6d7dcf9198e4f2
Signed-off-by: mmis <michael.morris@ericsson.com>
Diffstat (limited to 'controlloop/common/model-impl/so/src/main/java')
-rw-r--r-- | controlloop/common/model-impl/so/src/main/java/org/onap/policy/so/SOManager.java | 107 |
1 files changed, 61 insertions, 46 deletions
diff --git a/controlloop/common/model-impl/so/src/main/java/org/onap/policy/so/SOManager.java b/controlloop/common/model-impl/so/src/main/java/org/onap/policy/so/SOManager.java index 8190b1a54..ae43f5bd9 100644 --- a/controlloop/common/model-impl/so/src/main/java/org/onap/policy/so/SOManager.java +++ b/controlloop/common/model-impl/so/src/main/java/org/onap/policy/so/SOManager.java @@ -40,14 +40,15 @@ import org.slf4j.LoggerFactory; /** - * This class handles the interface towards SO (Service Orchestrator) for the ONAP Policy Framework. The SO - * API is defined at this link: + * This class handles the interface towards SO (Service Orchestrator) for the ONAP Policy Framework. + * The SO API is defined at this link: * http://onap.readthedocs.io/en/latest/submodules/so.git/docs/SO_R1_Interface.html#get-orchestration-request - * + * */ public final class SOManager { private static final Logger logger = LoggerFactory.getLogger(SOManager.class); - private static final Logger netLogger = LoggerFactory.getLogger(org.onap.policy.drools.event.comm.Topic.NETWORK_LOGGER); + private static final Logger netLogger = + LoggerFactory.getLogger(org.onap.policy.common.endpoints.event.comm.Topic.NETWORK_LOGGER); private static ExecutorService executors = Executors.newCachedThreadPool(); private static final int SO_RESPONSE_ERROR = 999; @@ -55,7 +56,7 @@ public final class SOManager { private static final String LINE_SEPARATOR = System.lineSeparator(); // REST get timeout value in milliseconds - private static final int GET_REQUESTS_BEFORE_TIMEOUT = 20; + private static final int GET_REQUESTS_BEFORE_TIMEOUT = 20; private static final long GET_REQUEST_WAIT_INTERVAL = 20000; // The REST manager used for processing REST calls for this VFC manager @@ -72,6 +73,7 @@ public final class SOManager { /** * Create a service instance in SO. + * * @param url the SO URL * @param urlBase the base URL * @param username user name on SO @@ -79,24 +81,28 @@ public final class SOManager { * @param request the request to issue to SO * @return */ - public SOResponse createModuleInstance(final String url, final String urlBase, final String username, final String password, final SORequest request) { + public SOResponse createModuleInstance(final String url, final String urlBase, final String username, + final String password, final SORequest request) { // Issue the HTTP POST request to SO to create the service instance String requestJson = Serialization.gsonPretty.toJson(request); - netLogger.info("[OUT|{}|{}|{}|{}|{}|{}|]{}{}", "SO", url, username, password, createSimpleHeaders(), MEDIA_TYPE, LINE_SEPARATOR, requestJson); - Pair<Integer, String> httpResponse = restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, requestJson); + netLogger.info("[OUT|{}|{}|{}|{}|{}|{}|]{}{}", "SO", url, username, password, createSimpleHeaders(), MEDIA_TYPE, + LINE_SEPARATOR, requestJson); + Pair<Integer, String> httpResponse = + restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, requestJson); // Process the response from SO - SOResponse response = waitForSOOperationCompletion(urlBase, username, password, url, httpResponse); + SOResponse response = waitForSOOperationCompletion(urlBase, username, password, url, httpResponse); if (SO_RESPONSE_ERROR != response.getHttpResponseCode()) { return response; - } - else { + } else { return null; } } - + /** - * This method makes an asynchronous Rest call to MSO and inserts the response into Drools working memory. + * This method makes an asynchronous Rest call to MSO and inserts the response into Drools + * working memory. + * * @param wm the Drools working memory * @param url the URL to use on the POST request * @param urlBase the SO base URL @@ -105,7 +111,8 @@ public final class SOManager { * @param request the SO request * @return a concurrent Future for the thread that handles the request */ - public Future<SOResponse> asyncSORestCall(final String requestID, final WorkingMemory wm, final String serviceInstanceId, final String vnfInstanceId, final SORequest request) { + public Future<SOResponse> asyncSORestCall(final String requestID, final WorkingMemory wm, + final String serviceInstanceId, final String vnfInstanceId, final SORequest request) { return executors.submit(new AsyncSORestCallThread(requestID, wm, serviceInstanceId, vnfInstanceId, request)); } @@ -121,13 +128,15 @@ public final class SOManager { /** * Constructor, sets the context of the request. + * * @param requestID The request ID * @param wm reference to the Drools working memory * @param serviceInstanceId the service instance in SO to use * @param vnfInstanceId the VNF instance that is the subject of the request * @param request the request itself */ - private AsyncSORestCallThread(final String requestID, final WorkingMemory wm, final String serviceInstanceId, final String vnfInstanceId, final SORequest request) { + private AsyncSORestCallThread(final String requestID, final WorkingMemory wm, final String serviceInstanceId, + final String vnfInstanceId, final SORequest request) { this.requestID = requestID; this.wm = wm; this.serviceInstanceId = serviceInstanceId; @@ -145,37 +154,39 @@ public final class SOManager { String password = PolicyEngine.manager.getEnvironmentProperty("so.password"); // The URL of the request we will POST - String url = urlBase + "/serviceInstances/v5/" + serviceInstanceId + "/vnfs/" - + vnfInstanceId + "/vfModules"; + String url = + urlBase + "/serviceInstances/v5/" + serviceInstanceId + "/vnfs/" + vnfInstanceId + "/vfModules"; // Create a JSON representation of the request String soJson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create().toJson(request); netLogger.info("[OUT|{}|{}|]{}{}", "SO", url, LINE_SEPARATOR, soJson); - Pair<Integer, String> httpResponse = restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, soJson); + Pair<Integer, String> httpResponse = + restManager.post(url, username, password, createSimpleHeaders(), MEDIA_TYPE, soJson); // Process the response from SO - SOResponse response = waitForSOOperationCompletion(urlBase, username, password, url, httpResponse); - + SOResponse response = waitForSOOperationCompletion(urlBase, username, password, url, httpResponse); + // Return the response to Drools in its working memory SOResponseWrapper soWrapper = new SOResponseWrapper(response, requestID); wm.insert(soWrapper); - + return response; } } /** * Wait for the SO operation we have ordered to complete. - * @param urlBaseSO The base URL for SO + * + * @param urlBaseSO The base URL for SO * @param username user name on SO * @param password password on SO * @param initialRequestURL The URL of the initial HTTP request * @param initialHTTPResponse The initial HTTP message returned from SO - * @return The parsed final response of SO to the request + * @return The parsed final response of SO to the request */ - private SOResponse waitForSOOperationCompletion(final String urlBaseSO, final String username, final String password, - final String initialRequestURL, final Pair<Integer, String> initialHTTPResponse) { + private SOResponse waitForSOOperationCompletion(final String urlBaseSO, final String username, + final String password, final String initialRequestURL, final Pair<Integer, String> initialHTTPResponse) { // Process the initial response from SO, the response to a post SOResponse response = processSOResponse(initialRequestURL, initialHTTPResponse); if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) { @@ -190,7 +201,8 @@ public final class SOManager { // Wait for the response from SO for (int attemptsLeft = GET_REQUESTS_BEFORE_TIMEOUT; attemptsLeft >= 0; attemptsLeft--) { - // The SO request may have completed even on the first request so we check the response here before + // The SO request may have completed even on the first request so we check the response + // here before // issuing any other requests if (isRequestStateFinished(latestHTTPResponse, response)) { return response; @@ -199,16 +211,16 @@ public final class SOManager { // Wait for the defined interval before issuing a get try { Thread.sleep(restGetTimeout); - } - catch (InterruptedException e) { + } catch (InterruptedException e) { logger.error("Interrupted exception: ", e); Thread.currentThread().interrupt(); response.setHttpResponseCode(SO_RESPONSE_ERROR); return response; } - + // Issue a GET to find the current status of our request - netLogger.info("[OUT|{}|{}|{}|{}|{}|{}|]{}", "SO", urlGet, username, password, createSimpleHeaders(), MEDIA_TYPE, LINE_SEPARATOR); + netLogger.info("[OUT|{}|{}|{}|{}|{}|{}|]{}", "SO", urlGet, username, password, createSimpleHeaders(), + MEDIA_TYPE, LINE_SEPARATOR); Pair<Integer, String> httpResponse = restManager.get(urlGet, username, password, createSimpleHeaders()); // Get our response @@ -216,11 +228,11 @@ public final class SOManager { if (SO_RESPONSE_ERROR == response.getHttpResponseCode()) { return response; } - + // Our latest HTTP response code latestHTTPResponse = httpResponse; } - + // We have timed out on the SO request response.setHttpResponseCode(SO_RESPONSE_ERROR); return response; @@ -228,14 +240,16 @@ public final class SOManager { /** * Parse the response message from SO into a SOResponse object. - * @param requestURL The URL of the HTTP request + * + * @param requestURL The URL of the HTTP request * @param httpDetails The HTTP message returned from SO * @return The parsed response */ private SOResponse processSOResponse(final String requestURL, final Pair<Integer, String> httpResponse) { SOResponse response = new SOResponse(); - // A null httpDetails indicates a HTTP problem, a valid response from SO must be either 200 or 202 + // A null httpDetails indicates a HTTP problem, a valid response from SO must be either 200 + // or 202 if (!httpResultIsNullFree(httpResponse) || (httpResponse.a != 200 && httpResponse.a != 202)) { logger.error("Invalid HTTP response received from SO"); response.setHttpResponseCode(SO_RESPONSE_ERROR); @@ -245,8 +259,7 @@ public final class SOManager { // Parse the JSON of the response into our POJO try { response = Serialization.gsonPretty.fromJson(httpResponse.b, SOResponse.class); - } - catch (JsonSyntaxException e) { + } catch (JsonSyntaxException e) { logger.error("Failed to deserialize HTTP response into SOResponse: ", e); response.setHttpResponseCode(SO_RESPONSE_ERROR); return response; @@ -254,9 +267,9 @@ public final class SOManager { // Set the HTTP response code of the response if needed if (response.getHttpResponseCode() == 0) { - response.setHttpResponseCode(httpResponse.a); + response.setHttpResponseCode(httpResponse.a); } - + netLogger.info("[IN|{}|{}|]{}{}", "SO", requestURL, LINE_SEPARATOR, httpResponse.b); if (logger.isDebugEnabled()) { @@ -268,7 +281,8 @@ public final class SOManager { } /** - * Method to allow tuning of REST get timeout. + * Method to allow tuning of REST get timeout. + * * @param restGetTimeout the timeout value */ protected void setRestGetTimeout(final long restGetTimeout) { @@ -277,19 +291,19 @@ public final class SOManager { /** * Check that the request state of a response is defined. + * * @param response The response to check * @return true if the request for the response is defined */ private boolean isRequestStateDefined(final SOResponse response) { - return response != null && - response.getRequest() != null && - response.getRequest().getRequestStatus() != null && - response.getRequest().getRequestStatus().getRequestState() != null; + return response != null && response.getRequest() != null && response.getRequest().getRequestStatus() != null + && response.getRequest().getRequestStatus().getRequestState() != null; } /** * Check that the request state of a response is finished. - * @param latestHTTPDetails the HTTP details of the response + * + * @param latestHTTPDetails the HTTP details of the response * @param response The response to check * @return true if the request for the response is finished */ @@ -297,14 +311,14 @@ public final class SOManager { if (latestHTTPDetails != null && 200 == latestHTTPDetails.a && isRequestStateDefined(response)) { String requestState = response.getRequest().getRequestStatus().getRequestState(); return "COMPLETE".equalsIgnoreCase(requestState) || "FAILED".equalsIgnoreCase(requestState); - } - else { + } else { return false; } } /** * Check that a HTTP operation result has no nulls. + * * @param httpOperationResult the result to check * @return true if no nulls are found */ @@ -314,6 +328,7 @@ public final class SOManager { /** * Create simple HTTP headers for unauthenticated requests to SO. + * * @return the HTTP headers */ private Map<String, String> createSimpleHeaders() { |