Set all cross references of policy/xacml-pdp
[policy/xacml-pdp.git] / applications / guard / src / main / java / org / onap / policy / xacml / pdp / application / guard / CoordinationGuardTranslator.java
index f0bc2b0..162b062 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -25,7 +25,6 @@ package org.onap.policy.xacml.pdp.application.guard;
 import com.att.research.xacml.api.Request;
 import com.att.research.xacml.api.Response;
 import com.att.research.xacml.util.XACMLPolicyScanner;
-
 import java.io.ByteArrayInputStream;
 import java.io.File;
 import java.io.FileInputStream;
@@ -35,6 +34,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.List;
 import java.util.Map;
 import java.util.UUID;
+import lombok.NoArgsConstructor;
 import org.apache.commons.io.IOUtils;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardYamlCoder;
@@ -47,14 +47,11 @@ import org.onap.policy.pdp.xacml.application.common.ToscaPolicyTranslator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+@NoArgsConstructor
 public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(CoordinationGuardTranslator.class);
 
-    public CoordinationGuardTranslator() {
-        super();
-    }
-
     @Override
     public Object convertPolicy(ToscaPolicy toscaPolicy) throws ToscaPolicyConversionException {
         LOGGER.debug("Using CoordinationGuardTranslator.convertPolicy");
@@ -62,13 +59,13 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
         // Policy name should be at the root
         //
         String type = toscaPolicy.getType();
-        String coordinationFunctionPath = "src/main/resources/coordination/function";
+        var coordinationFunctionPath = "coordination/function";
         Map<String, Object> policyProps = toscaPolicy.getProperties();
         LOGGER.debug("path = {}", coordinationFunctionPath);
         LOGGER.debug("props = {}", policyProps);
         @SuppressWarnings("unchecked")
         List<String> controlLoop = (List<String>) policyProps.get("controlLoop");
-        CoordinationDirective cd = new CoordinationDirective();
+        var cd = new CoordinationDirective();
         cd.setCoordinationFunction(type);
         cd.setControlLoop(controlLoop);
         LOGGER.debug("CoordinationDirective = {}", cd);
@@ -114,8 +111,11 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
      */
     public static CoordinationDirective loadCoordinationDirectiveFromFile(
         String directiveFilename) {
+        if (directiveFilename == null) {
+            return null;
+        }
         try (InputStream is = new FileInputStream(new File(directiveFilename))) {
-            String contents = IOUtils.toString(is, StandardCharsets.UTF_8);
+            var contents = IOUtils.toString(is, StandardCharsets.UTF_8);
             //
             // Read the yaml into our Java Object
             //
@@ -143,19 +143,19 @@ public class CoordinationGuardTranslator implements ToscaPolicyTranslator {
          */
         String xacmlProtoFilename =
             protoDir + File.separator + cd.getCoordinationFunction() + ".xml";
-        LOGGER.debug("xacmlProtoFilename={}", xacmlProtoFilename);
+        LOGGER.info("xacmlProtoFilename={}", xacmlProtoFilename);
         /*
-         * Values to be used for placeholders
+         * Values to be substituted for placeholder's
          */
-        final String uniqueId = UUID.randomUUID().toString();
+        final var uniqueId = UUID.randomUUID().toString();
         final String cLOne = cd.getControlLoop(0);
         final String cLTwo = cd.getControlLoop(1);
         /*
-         * Replace function placeholders with appropriate values
+         * Replace function placeholder's with appropriate values
          */
-        String policyXml = ResourceUtils.getResourceAsString(xacmlProtoFilename);
+        var policyXml = ResourceUtils.getResourceAsString(xacmlProtoFilename);
         if (policyXml == null) {
-            throw new ToscaPolicyConversionException("Error while generating XACML policy for coordination directive");
+            throw new ToscaPolicyConversionException("Unable to find prototype " + xacmlProtoFilename);
         }
         policyXml = policyXml.replace("UNIQUE_ID", uniqueId);
         policyXml = policyXml.replace("CONTROL_LOOP_ONE", cLOne);