Use lombok in xacml-pdp
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / std / StdMatchableTranslator.java
index 329a21c..cca2d21 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.
  * Modifications Copyright (C) 2021 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -43,6 +43,7 @@ import java.util.LinkedList;
 import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
+import lombok.NoArgsConstructor;
 import lombok.Setter;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
@@ -51,7 +52,7 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
 import org.apache.commons.lang3.tuple.Pair;
-import org.onap.policy.common.endpoints.parameters.RestServerParameters;
+import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.common.utils.coder.StandardYamlCoder;
@@ -83,6 +84,7 @@ import org.slf4j.LoggerFactory;
  * @author pameladragosh
  *
  */
+@NoArgsConstructor
 public class StdMatchableTranslator  extends StdBaseTranslator implements MatchableCallback {
 
     private static final Logger LOGGER = LoggerFactory.getLogger(StdMatchableTranslator.class);
@@ -92,14 +94,10 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
     private final Map<ToscaConceptIdentifier, MatchablePolicyType> matchableCache = new HashMap<>();
 
     @Setter
-    private RestServerParameters apiRestParameters;
+    private BusTopicParams apiRestParameters;
     @Setter
     private Path pathForData;
 
-    public StdMatchableTranslator() {
-        super();
-    }
-
     @Override
     public Request convertRequest(DecisionRequest request) throws ToscaPolicyConversionException {
         LOGGER.info("Converting Request {}", request);
@@ -167,14 +165,14 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
      * contents and their details.
      *
      * @param closestMatches Map holding the current set of highest weight policy types
-     * @param Obligation Obligation object
+     * @param obligation Obligation object
      */
     protected void scanClosestMatchObligation(
             Map<String, Map<Integer, List<Pair<String, Map<String, Object>>>>> closestMatches, Obligation obligation) {
         //
         // Create our OnapObligation object
         //
-        OnapObligation onapObligation = new OnapObligation(obligation);
+        var onapObligation = new OnapObligation(obligation);
         //
         // All 4 *should* be there
         //
@@ -257,7 +255,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
         //
         // Set it as the policy ID
         //
-        PolicyType newPolicyType = new PolicyType();
+        var newPolicyType = new PolicyType();
         newPolicyType.setPolicyId(policyName);
         //
         // Optional description
@@ -280,7 +278,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
         //
         // Now represent the policy as Json
         //
-        StandardCoder coder = new StandardCoder();
+        var coder = new StandardCoder();
         String jsonPolicy;
         try {
             jsonPolicy = coder.encode(toscaPolicy);
@@ -294,7 +292,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
         //
         // Now create the Permit Rule.
         //
-        RuleType rule = new RuleType();
+        var rule = new RuleType();
         rule.setDescription("Default is to PERMIT if the policy matches.");
         rule.setRuleId(policyName + ":rule");
         rule.setEffect(EffectType.PERMIT);
@@ -311,7 +309,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
         //
         // Log output of the policy
         //
-        try (ByteArrayOutputStream os = new ByteArrayOutputStream()) {
+        try (var os = new ByteArrayOutputStream()) {
             XACMLPolicyWriter.writePolicyFile(os, newPolicyType);
             LOGGER.info("{}", os);
         } catch (IOException e) {
@@ -371,19 +369,19 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
      * For generating target type, we scan for matchable properties
      * and use those to build the policy.
      *
-     * @param properties Properties section of policy
-     * @param policyTypes Collection of policy Type to find matchable metadata
+     * @param policy the policy
+     * @param template template containing the policy
      * @return {@code Pair<TargetType, Integer>} Returns a TargetType and a Total Weight of matchables.
      */
     protected Pair<TargetType, Integer> generateTargetType(ToscaPolicy policy, ToscaServiceTemplate template) {
         //
         // Our return object
         //
-        TargetType target = new TargetType();
+        var target = new TargetType();
         //
         // See if we have a matchable in the cache already
         //
-        MatchablePolicyType matchablePolicyType = matchableCache.get(policy.getTypeIdentifier());
+        var matchablePolicyType = matchableCache.get(policy.getTypeIdentifier());
         //
         // If not found, create one
         //
@@ -391,7 +389,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
             //
             // Our callback
             //
-            MyMatchableCallback myCallback = new MyMatchableCallback(this, template);
+            var myCallback = new MyMatchableCallback(this, template);
             //
             // Create the matchable
             //
@@ -459,7 +457,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
     }
 
     protected int calculateWeight(TargetType target) {
-        int weight = 0;
+        var weight = 0;
         for (AnyOfType anyOf : target.getAnyOf()) {
             for (AllOfType allOf : anyOf.getAllOf()) {
                 weight += allOf.getMatch().size();
@@ -477,7 +475,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
      * @param policyTypeId ToscaConceptIdentifier to find
      * @return ToscaPolicyType object. Can be null if failure.
      */
-    private ToscaServiceTemplate findPolicyType(ToscaConceptIdentifier policyTypeId) {
+    protected ToscaServiceTemplate findPolicyType(ToscaConceptIdentifier policyTypeId) {
         //
         // Is it loaded in memory?
         //
@@ -508,11 +506,11 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
      * @param policyTypeId ToscaConceptIdentifier input
      * @return ToscaPolicyType object. Null if failure.
      */
-    private ToscaServiceTemplate loadPolicyType(ToscaConceptIdentifier policyTypeId) {
+    protected ToscaServiceTemplate loadPolicyType(ToscaConceptIdentifier policyTypeId) {
         //
         // Construct what the file name should be
         //
-        Path policyTypePath = this.constructLocalFilePath(policyTypeId);
+        var policyTypePath = this.constructLocalFilePath(policyTypeId);
         //
         // See if it exists
         //
@@ -546,7 +544,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
             //
             // Ensure all the fields are setup correctly
             //
-            JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+            var jtst = new JpaToscaServiceTemplate();
             jtst.fromAuthorative(template);
             return jtst.toAuthorative();
         } catch (CoderException e) {
@@ -567,19 +565,18 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
      * @param policyTypePath Path object to store locally
      * @return ToscaPolicyType object. Null if failure.
      */
-    private synchronized ToscaServiceTemplate pullPolicyType(ToscaConceptIdentifier policyTypeId,
+    protected synchronized ToscaServiceTemplate pullPolicyType(ToscaConceptIdentifier policyTypeId,
             Path policyTypePath) {
         //
         // This is what we return
         //
         ToscaServiceTemplate policyTemplate = null;
         try {
-            PolicyApiCaller api = new PolicyApiCaller(this.apiRestParameters);
+            var api = new PolicyApiCaller(this.apiRestParameters);
 
             policyTemplate = api.getPolicyType(policyTypeId);
         } catch (PolicyApiException e) {
             LOGGER.error("Failed to make API call", e);
-            LOGGER.error("parameters: {} ", this.apiRestParameters);
             return null;
         }
         LOGGER.info("Successfully pulled {}", policyTypeId);
@@ -604,7 +601,7 @@ public class StdMatchableTranslator  extends StdBaseTranslator implements Matcha
      * @param policyTypeId ToscaConceptIdentifier
      * @return Path object
      */
-    private Path constructLocalFilePath(ToscaConceptIdentifier policyTypeId) {
+    protected Path constructLocalFilePath(ToscaConceptIdentifier policyTypeId) {
         return Paths.get(this.pathForData.toAbsolutePath().toString(), policyTypeId.getName() + "-"
                 + policyTypeId.getVersion() + ".yaml");
     }