Utilize time extensions
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / ToscaPolicyTranslatorUtils.java
index f1d3d5e..a5e804e 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,11 +25,15 @@ package org.onap.policy.pdp.xacml.application.common;
 import com.att.research.xacml.api.Identifier;
 import com.att.research.xacml.api.XACML3;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AllOfType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.AnyOfType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ApplyType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeDesignatorType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.AttributeValueType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.ConditionType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.MatchType;
 import oasis.names.tc.xacml._3_0.core.schema.wd_17.ObjectFactory;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
+import oasis.names.tc.xacml._3_0.core.schema.wd_17.VariableReferenceType;
 import org.apache.commons.lang3.StringUtils;
 
 /**
@@ -112,7 +116,7 @@ public final class ToscaPolicyTranslatorUtils {
      * @param end ISO8601 timestamp
      * @return ApplyType
      */
-    public static ApplyType generateTimeInRange(String start, String end) {
+    public static ApplyType generateTimeInRange(String start, String end, boolean useRecurringFunction) {
         if (StringUtils.isBlank(start) || StringUtils.isBlank(end)) {
             return null;
         }
@@ -138,7 +142,11 @@ public final class ToscaPolicyTranslatorUtils {
 
         ApplyType applyTimeInRange = new ApplyType();
         applyTimeInRange.setDescription("return true if current time is in range.");
-        applyTimeInRange.setFunctionId(XACML3.ID_FUNCTION_TIME_IN_RANGE.stringValue());
+        if (useRecurringFunction) {
+            applyTimeInRange.setFunctionId(XACML3.ID_FUNCTION_TIME_IN_RECURRING_RANGE.stringValue());
+        } else {
+            applyTimeInRange.setFunctionId(XACML3.ID_FUNCTION_TIME_IN_RANGE.stringValue());
+        }
         applyTimeInRange.getExpression().add(factory.createApply(applyOneAndOnly));
         applyTimeInRange.getExpression().add(factory.createAttributeValue(valueStart));
         applyTimeInRange.getExpression().add(factory.createAttributeValue(valueEnd));
@@ -166,4 +174,70 @@ public final class ToscaPolicyTranslatorUtils {
         }
         return theInt;
     }
+
+    /**
+     * For a given MatchType or AnyOfType, builds it and appends it into the
+     * AnyOfType.
+     *
+     * @param anyOf AnyOfType - will create if null
+     * @param type MatchType or AnyOfType
+     * @return returns the given anyOf or new AnyTypeOf if null
+     */
+    public static AnyOfType buildAndAppendAllof(AnyOfType anyOf, Object type) {
+        if (type instanceof MatchType) {
+            AllOfType allOf = new AllOfType();
+            allOf.getMatch().add((MatchType) type);
+            if (anyOf == null) {
+                anyOf = new AnyOfType();
+            }
+            anyOf.getAllOf().add(allOf);
+        } else if (type instanceof AllOfType) {
+            if (anyOf == null) {
+                anyOf = new AnyOfType();
+            }
+            anyOf.getAllOf().add((AllOfType) type);
+        }
+
+        return anyOf;
+    }
+
+    /**
+     * buildAndAppendTarget - adds in the potential object into TargetType.
+     *
+     * @param target TargetType - must exist
+     * @param object AnyOfType or MatchType
+     * @return TargetType
+     */
+    public static TargetType buildAndAppendTarget(TargetType target, Object object) {
+        if (object instanceof AnyOfType) {
+            target.getAnyOf().add((AnyOfType) object);
+        } else if (object instanceof MatchType) {
+            AllOfType allOf = new AllOfType();
+            allOf.getMatch().add((MatchType) object);
+            AnyOfType anyOf = new AnyOfType();
+            anyOf.getAllOf().add(allOf);
+            target.getAnyOf().add(anyOf);
+        }
+        return target;
+    }
+
+    /**
+     * For an existing ConditionType, this method adds in a check for a variable. You must specify
+     * the function that compares the existing ConditionType's expression against the Variable.
+     *
+     * @param condition Existing ConditionType to use
+     * @param variable VariableReferenceType to use
+     * @param functionId XACML 3.0 identifier for the function
+     * @return a new ConditionType
+     */
+    public static ConditionType addVariableToCondition(ConditionType condition, VariableReferenceType variable,
+            Identifier functionId) {
+        ApplyType applyFunction = new ApplyType();
+        applyFunction.setFunctionId(functionId.stringValue());
+        applyFunction.getExpression().add(condition.getExpression());
+        applyFunction.getExpression().add(factory.createVariableReference(variable));
+        ConditionType newCondition = new ConditionType();
+        newCondition.setExpression(factory.createApply(applyFunction));
+        return newCondition;
+    }
 }