From b6ecb09c2f98736d11c2684239063a3d43214631 Mon Sep 17 00:00:00 2001 From: "Hockla, Ali (ah999m)" Date: Thu, 2 Nov 2017 12:14:29 -0500 Subject: Added fix to catch null pointer in SOManager Issue-ID: POLICY-408 Change-Id: I9f6681b8cbafdd9639966d862e7b1df3b729297e Signed-off-by: Hockla, Ali (ah999m) --- .../main/java/org/onap/policy/so/SOManager.java | 84 ++++++++++++---------- 1 file changed, 45 insertions(+), 39 deletions(-) (limited to 'controlloop/common') 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 fb2cc3d60..1caa045e4 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 @@ -141,45 +141,51 @@ public final class SOManager { @Override public void run() { - - /* - * TODO: What if these are null? - */ - String serverRoot = PolicyEngine.manager.getEnvironmentProperty("so.url"); - String username = PolicyEngine.manager.getEnvironmentProperty("so.username"); - String password = PolicyEngine.manager.getEnvironmentProperty("so.password"); - - String url = serverRoot + "/serviceInstances/v5/" + serviceInstanceId + "/vnfs/" + vnfInstanceId + "/vfModulesHTTPS/1.1"; - - String auth = username + ":" + password; - - Map headers = new HashMap(); - byte[] encodedBytes = Base64.getEncoder().encode(auth.getBytes()); - headers.put("Accept", "application/json"); - headers.put("Authorization", "Basic " + new String(encodedBytes)); - - Gson gsonPretty = new GsonBuilder().disableHtmlEscaping() - .setPrettyPrinting() - .create(); - - String soJson = gsonPretty.toJson(request); - - SOResponse so = new SOResponse(); - netLogger.info("[OUT|{}|{}|]{}{}", "SO", url, System.lineSeparator(), soJson); - Pair httpResponse = RESTManager.post(url, "policy", "policy", headers, "application/json", soJson); - - if (httpResponse != null) { - netLogger.info("[IN|{}|{}|]{}{}", url, "SO", System.lineSeparator(), httpResponse.b); - - Gson gson = new Gson(); - so = gson.fromJson(httpResponse.b, SOResponse.class); - so.httpResponseCode = httpResponse.a; - } else { - logger.error("SO Response returned null."); - } - - wm.insert(so); - logger.info("SOResponse inserted " + gsonPretty.toJson(so)); + try { + String serverRoot = PolicyEngine.manager.getEnvironmentProperty("so.url"); + String username = PolicyEngine.manager.getEnvironmentProperty("so.username"); + String password = PolicyEngine.manager.getEnvironmentProperty("so.password"); + + String url = serverRoot + "/serviceInstances/v5/" + serviceInstanceId + "/vnfs/" + vnfInstanceId + "/vfModulesHTTPS/1.1"; + + String auth = username + ":" + password; + + Map headers = new HashMap(); + byte[] encodedBytes = Base64.getEncoder().encode(auth.getBytes()); + headers.put("Accept", "application/json"); + headers.put("Authorization", "Basic " + new String(encodedBytes)); + + Gson gsonPretty = new GsonBuilder().disableHtmlEscaping() + .setPrettyPrinting() + .create(); + + String soJson = gsonPretty.toJson(request); + + SOResponse so = new SOResponse(); + netLogger.info("[OUT|{}|{}|]{}{}", "SO", url, System.lineSeparator(), soJson); + Pair httpResponse = RESTManager.post(url, "policy", "policy", headers, "application/json", soJson); + + if (httpResponse != null) { + netLogger.info("[IN|{}|{}|]{}{}", url, "SO", System.lineSeparator(), httpResponse.b); + + Gson gson = new Gson(); + so = gson.fromJson(httpResponse.b, SOResponse.class); + so.httpResponseCode = httpResponse.a; + } else { + logger.error("SO Response returned null."); + so.httpResponseCode = 999; + } + + wm.insert(so); + logger.info("SOResponse inserted " + gsonPretty.toJson(so)); + } catch (Exception e) { + logger.error("Error while performing asyncSORestCall: "+ e.getMessage(),e); + + // create dummy SO object to trigger cleanup + SOResponse so = new SOResponse(); + so.httpResponseCode = 999; + wm.insert(so); + } } }); } -- cgit 1.2.3-korg