Added fix to catch null pointer in SOManager 21/21921/3
authorHockla, Ali (ah999m) <ah999m@att.com>
Thu, 2 Nov 2017 17:14:29 +0000 (12:14 -0500)
committerHockla, Ali (ah999m) <ah999m@att.com>
Thu, 2 Nov 2017 20:22:52 +0000 (15:22 -0500)
Issue-ID: POLICY-408
Change-Id: I9f6681b8cbafdd9639966d862e7b1df3b729297e
Signed-off-by: Hockla, Ali (ah999m) <ah999m@att.com>
controlloop/common/model-impl/so/src/main/java/org/onap/policy/so/SOManager.java

index fb2cc3d..1caa045 100644 (file)
@@ -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<String, String> headers = new HashMap<String, String>();
-                               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<Integer, String> 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<String, String> headers = new HashMap<String, String>();
+                                         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<Integer, String> 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);
+                                 }
                          }
                        });
          }