Fix a couple of sonar issues one blocker 54/85354/2
authorMichael Mokry <michael.mokry@att.com>
Mon, 15 Apr 2019 17:47:46 +0000 (12:47 -0500)
committerJoshua Reich <jreich@research.att.com>
Mon, 15 Apr 2019 21:20:11 +0000 (14:20 -0700)
1. fixed nullpointer blocker issue in CoordinationGuardTranslator.java
2. fixed "log or rethrow exception" critical issue
3. throw exceptions instead of returning null values

Change-Id: I2d567fe566c6f761ec2699016c4a868f203c9a01
Issue-ID: POLICY-1451
Signed-off-by: Michael Mokry <michael.mokry@att.com>
Signed-off-by: Joshua Reich <jreich@research.att.com>
applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java
applications/guard/src/main/java/org/onap/policy/xacml/pdp/application/guard/CoordinationGuardTranslator.java

index 2b8048c..451ef7e 100644 (file)
@@ -217,7 +217,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica
             XacmlPolicyUtils.storeXacmlProperties(newProperties,
                     XacmlPolicyUtils.getPropertiesPath(this.getDataPath()));
         } catch (IOException e) {
-            LOGGER.error("Failed to save the properties to disk {}", newProperties);
+            LOGGER.error("Failed to save the properties to disk {}", newProperties, e);
         }
         //
         // Reload the engine
index 10456e9..41c1428 100644 (file)
@@ -67,7 +67,6 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
         //
         // Policy name should be at the root
         //
-        String policyName = toscaPolicy.getMetadata().get("policy-id");
         String type = toscaPolicy.getType();
         String coordinationFunctionPath = "src/main/resources/coordination/function";
         Map<String, Object> policyProps = toscaPolicy.getProperties();
@@ -78,17 +77,19 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
         cd.setCoordinationFunction(type);
         cd.setControlLoop(controlLoop);
         LOGGER.debug("CoordinationDirective = {}", cd);
-
+        //
+        // Generate the xacml policy as a string
+        //
         String xacmlStr = generateXacmlFromCoordinationDirective(cd, coordinationFunctionPath);
-
         LOGGER.debug("xacmlStr\n{}", xacmlStr);
-        PolicyType scannedPolicy = null;
+        //
+        // Scan the string and convert to PoilcyType
+        //
         try (InputStream is = new ByteArrayInputStream(xacmlStr.getBytes(StandardCharsets.UTF_8))) {
-            scannedPolicy = (PolicyType) XACMLPolicyScanner.readPolicy(is);
+            return (PolicyType) XACMLPolicyScanner.readPolicy(is);
         } catch (IOException e) {
-            LOGGER.error("Failed to read policy", e);
+            throw new ToscaPolicyConversionException("Failed to read policy", e);
         }
-        return scannedPolicy;
     }
 
     @Override
@@ -99,7 +100,7 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
 
     @Override
     public DecisionResponse convertResponse(Response xacmlResponse) {
-        LOGGER.info("this convertRequest shouldn't be used");
+        LOGGER.info("this convertResponse shouldn't be used");
         return null;
     }
 
@@ -117,9 +118,7 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
             //
             Yaml yaml = new Yaml(new Constructor(CoordinationDirective.class));
             Object obj = yaml.load(contents);
-
             LOGGER.debug(contents);
-
             return (CoordinationDirective) obj;
         } catch (IOException e) {
             LOGGER.error("Error while loading YAML coordination directive", e);
@@ -135,7 +134,7 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
      * @return the generated Xacml policy
      */
     public static String generateXacmlFromCoordinationDirective(CoordinationDirective cd,
-                                                                String protoDir) {
+                                                                String protoDir) throws ToscaPolicyConversionException {
         /*
          * Determine file names
          */
@@ -148,18 +147,17 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
         final String cLOne = cd.getControlLoop(0);
         final String cLTwo = cd.getControlLoop(1);
         /*
-         * Replace prototype placeholders with appropriate values
+         * Replace function placeholders with appropriate values
          */
-        String xacmlPolicy = null;
         try (Stream<String> stream = Files.lines(Paths.get(xacmlProtoFilename))) {
-            xacmlPolicy = stream.map(s -> s.replaceAll("UNIQUE_ID", uniqueId))
+            return stream.map(s -> s.replaceAll("UNIQUE_ID", uniqueId))
                 .map(s -> s.replaceAll("CONTROL_LOOP_ONE", cLOne))
                 .map(s -> s.replaceAll("CONTROL_LOOP_TWO", cLTwo))
                 .collect(Collectors.joining(System.lineSeparator()));
         } catch (IOException e) {
-            LOGGER.error("Error while generating XACML policy for coordination directive", e);
+            throw new
+                ToscaPolicyConversionException("Error while generating XACML policy for coordination directive", e);
         }
-        return xacmlPolicy;
     }
 
 }