TCA:Support for string & decimal policy FieldPath 05/18605/2
authorSingla, Rajiv (rs153v) <rs153v@att.com>
Thu, 12 Oct 2017 17:44:30 +0000 (13:44 -0400)
committerSingla, Rajiv (rs153v) <rs153v@att.com>
Thu, 12 Oct 2017 18:07:49 +0000 (14:07 -0400)
Change-Id: I37a236be2cb4afccffbcf72e3914fb75dadbbb88
Signed-off-by: Singla, Rajiv (rs153v) <rs153v@att.com>
Issue-Id: DCAEGEN2-158

dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/domain/policy/tca/Direction.java
dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/domain/policy/tca/Threshold.java
dcae-analytics-model/src/main/java/org/openecomp/dcae/apod/analytics/model/util/json/mixin/policy/tca/ThresholdMixin.java
dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/processor/TCACEFPolicyThresholdsProcessor.java
dcae-analytics-tca/src/main/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtils.java
dcae-analytics-tca/src/test/java/org/openecomp/dcae/apod/analytics/tca/utils/TCAUtilsTest.java

index 76c8086..0d7203b 100644 (file)
@@ -20,6 +20,8 @@
 \r
 package org.openecomp.dcae.apod.analytics.model.domain.policy.tca;\r
 \r
+import java.math.BigDecimal;\r
+\r
 import javax.annotation.Nonnull;\r
 \r
 /**\r
@@ -32,32 +34,32 @@ public enum Direction implements TCAPolicyModel {
 \r
     EQUAL {\r
         @Override\r
-        public Boolean operate(@Nonnull Long value1, @Nonnull Long value2) {\r
-            return value1.equals(value2);\r
+        public Boolean operate(@Nonnull BigDecimal value1, @Nonnull BigDecimal value2) {\r
+            return value1.compareTo(value2) == 0;\r
         }\r
     },\r
     LESS {\r
         @Override\r
-        public Boolean operate(@Nonnull Long value1, @Nonnull Long value2) {\r
-            return value1 < value2;\r
+        public Boolean operate(@Nonnull BigDecimal value1, @Nonnull BigDecimal value2) {\r
+            return value1.compareTo(value2) < 0;\r
         }\r
     },\r
     LESS_OR_EQUAL {\r
         @Override\r
-        public Boolean operate(@Nonnull Long value1, @Nonnull Long value2) {\r
-            return value1 <= value2;\r
+        public Boolean operate(@Nonnull BigDecimal value1, @Nonnull BigDecimal value2) {\r
+            return value1.compareTo(value2) <= 0;\r
         }\r
     },\r
     GREATER {\r
         @Override\r
-        public Boolean operate(@Nonnull Long value1, @Nonnull Long value2) {\r
-            return value1 > value2;\r
+        public Boolean operate(@Nonnull BigDecimal value1, @Nonnull BigDecimal value2) {\r
+            return value1.compareTo(value2) > 0;\r
         }\r
     },\r
     GREATER_OR_EQUAL {\r
         @Override\r
-        public Boolean operate(@Nonnull Long value1, @Nonnull Long value2) {\r
-            return value1 >= value2;\r
+        public Boolean operate(@Nonnull BigDecimal value1, @Nonnull BigDecimal value2) {\r
+            return value1.compareTo(value2) >= 0;\r
         }\r
     };\r
 \r
@@ -69,6 +71,6 @@ public enum Direction implements TCAPolicyModel {
      *\r
      * @return result of operation for the direction logic\r
      */\r
-    public abstract Boolean operate(@Nonnull Long value1, @Nonnull Long value2);\r
+    public abstract Boolean operate(@Nonnull BigDecimal value1, @Nonnull BigDecimal value2);\r
 \r
 }\r
index 0d6193d..4f98fa5 100644 (file)
@@ -24,6 +24,8 @@ import lombok.Data;
 import lombok.EqualsAndHashCode;\r
 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity;\r
 \r
+import java.math.BigDecimal;\r
+\r
 /**\r
  *\r
  * @author Rajiv Singla . Creation Date: 11/5/2016.\r
@@ -100,7 +102,7 @@ public class Threshold extends BaseTCAPolicyModel {
      * @param actualFieldValue new value for actual Field value that caused the violation\r
      * @return actual field value that caused the violation\r
      */\r
-    private Long actualFieldValue;\r
+    private BigDecimal actualFieldValue;\r
 \r
     /**\r
      * Creates a deep copy of give {@link Threshold}\r
index 02d4b83..27344f8 100644 (file)
@@ -23,6 +23,8 @@ package org.openecomp.dcae.apod.analytics.model.util.json.mixin.policy.tca;
 import com.fasterxml.jackson.annotation.JsonIgnore;\r
 import com.fasterxml.jackson.annotation.JsonProperty;\r
 \r
+import java.math.BigDecimal;\r
+\r
 /**\r
  *\r
  * @author Rajiv Singla . Creation Date: 11/5/2016.\r
@@ -30,15 +32,15 @@ import com.fasterxml.jackson.annotation.JsonProperty;
 public abstract class ThresholdMixin extends BaseTCAPolicyModelMixin {\r
 \r
     @JsonIgnore\r
-    private Long actualFieldValue;\r
+    private BigDecimal actualFieldValue;\r
 \r
     @JsonIgnore\r
-    public Long getActualFieldValue() {\r
+    public BigDecimal getActualFieldValue() {\r
         return actualFieldValue;\r
     }\r
 \r
     @JsonProperty\r
-    public void setActualFieldValue(Long actualFieldValue) {\r
+    public void setActualFieldValue(BigDecimal actualFieldValue) {\r
         this.actualFieldValue = actualFieldValue;\r
     }\r
 }\r
index 704151d..0a62aa4 100644 (file)
@@ -32,6 +32,7 @@ import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
 import org.slf4j.Logger;\r
 import org.slf4j.LoggerFactory;\r
 \r
+import java.math.BigDecimal;\r
 import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.Map;\r
@@ -93,11 +94,12 @@ public class TCACEFPolicyThresholdsProcessor extends AbstractTCAECEFPolicyProces
         final Set<String> policyFieldPaths = policyFieldPathsMap.keySet();\r
 \r
         // Get Json Values for Policy Fields\r
-        final Map<String, List<Long>> messageFieldValuesMap = TCAUtils.getJsonPathValue(cefMessage, policyFieldPaths);\r
+        final Map<String, List<BigDecimal>> messageFieldValuesMap =\r
+                TCAUtils.getJsonPathValue(cefMessage, policyFieldPaths);\r
 \r
         // Determine all violated thresholds per message field Path\r
         final Map<String, Threshold> violatedThresholdsMap = new HashMap<>();\r
-        for (Map.Entry<String, List<Long>> messageFieldValuesMapEntry : messageFieldValuesMap.entrySet()) {\r
+        for (Map.Entry<String, List<BigDecimal>> messageFieldValuesMapEntry : messageFieldValuesMap.entrySet()) {\r
             final String messageFieldPath = messageFieldValuesMapEntry.getKey();\r
             final List<Threshold> messageFieldAssociatedPolicyThresholds = policyFieldPathsMap.get(messageFieldPath);\r
             if (messageFieldAssociatedPolicyThresholds != null) {\r
index dd37aa2..4011e52 100644 (file)
@@ -43,6 +43,7 @@ import org.apache.commons.lang3.tuple.ImmutablePair;
 import org.apache.commons.lang3.tuple.Pair;\r
 import org.openecomp.dcae.apod.analytics.aai.service.AAIEnrichmentClient;\r
 import org.openecomp.dcae.apod.analytics.common.AnalyticsConstants;\r
+import org.openecomp.dcae.apod.analytics.common.exception.DCAEAnalyticsRuntimeException;\r
 import org.openecomp.dcae.apod.analytics.common.exception.MessageProcessingException;\r
 import org.openecomp.dcae.apod.analytics.common.service.processor.AbstractMessageProcessor;\r
 import org.openecomp.dcae.apod.analytics.common.service.processor.GenericMessageChainProcessor;\r
@@ -83,6 +84,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;\r
 \r
 import java.io.IOException;\r
+import java.math.BigDecimal;\r
 import java.util.ArrayList;\r
 import java.util.Collections;\r
 import java.util.Comparator;\r
@@ -266,20 +268,30 @@ public abstract class TCAUtils extends AnalyticsModelJsonUtils {
      * @param jsonFieldPaths Json Field Paths\r
      * @return Map containing key as json path and values as values associated with that json path\r
      */\r
-    public static Map<String, List<Long>> getJsonPathValue(@Nonnull String message, @Nonnull Set<String>\r
+    public static Map<String, List<BigDecimal>> getJsonPathValue(@Nonnull String message, @Nonnull Set<String>\r
             jsonFieldPaths) {\r
 \r
-        final Map<String, List<Long>> jsonFieldPathMap = new HashMap<>();\r
+        final Map<String, List<BigDecimal>> jsonFieldPathMap = new HashMap<>();\r
         final DocumentContext documentContext = JsonPath.parse(message);\r
 \r
         for (String jsonFieldPath : jsonFieldPaths) {\r
-            final List<Long> jsonFieldValues = documentContext.read(jsonFieldPath, new TypeRef<List<Long>>() {\r
-            });\r
+            List<BigDecimal> jsonFieldValues = null;\r
+\r
+            try {\r
+                jsonFieldValues = documentContext.read(jsonFieldPath, new TypeRef<List<BigDecimal>>() {\r
+                });\r
+            } catch (Exception e) {\r
+                final String errorMessage = String.format(\r
+                        "Unable to convert jsonFieldPath: %s value to valid number. " +\r
+                                "Json Path value is not in a valid number format. Incoming message: %s",\r
+                        jsonFieldPath, message);\r
+                throw new DCAEAnalyticsRuntimeException(errorMessage, LOG, e);\r
+            }\r
             // If Json Field Values are not or empty\r
             if (jsonFieldValues != null && !jsonFieldValues.isEmpty()) {\r
                 // Filter out all null values in the filed values list\r
-                final List<Long> nonNullValues = Lists.newLinkedList(Iterables.filter(jsonFieldValues,\r
-                        Predicates.<Long>notNull()));\r
+                final List<BigDecimal> nonNullValues = Lists.newLinkedList(Iterables.filter(jsonFieldValues,\r
+                        Predicates.<BigDecimal>notNull()));\r
                 // If there are non null values put them in the map\r
                 if (!nonNullValues.isEmpty()) {\r
                     jsonFieldPathMap.put(jsonFieldPath, nonNullValues);\r
@@ -298,15 +310,17 @@ public abstract class TCAUtils extends AnalyticsModelJsonUtils {
      * @param fieldThresholds Policy Thresholds for Field Path\r
      * @return Optional of violated threshold for a field path\r
      */\r
-    public static Optional<Threshold> thresholdCalculator(final List<Long> messageFieldValues, final List<Threshold>\r
+    public static Optional<Threshold> thresholdCalculator(final List<BigDecimal> messageFieldValues, final\r
+    List<Threshold>\r
             fieldThresholds) {\r
         // order thresholds by severity\r
         Collections.sort(fieldThresholds, THRESHOLD_COMPARATOR);\r
         // Now apply each threshold to field values\r
         for (Threshold fieldThreshold : fieldThresholds) {\r
-            for (Long messageFieldValue : messageFieldValues) {\r
+            for (BigDecimal messageFieldValue : messageFieldValues) {\r
                 final Boolean isThresholdViolated =\r
-                        fieldThreshold.getDirection().operate(messageFieldValue, fieldThreshold.getThresholdValue());\r
+                        fieldThreshold.getDirection().operate(messageFieldValue, new BigDecimal(fieldThreshold\r
+                                .getThresholdValue()));\r
                 if (isThresholdViolated) {\r
                     final Threshold violatedThreshold = Threshold.copy(fieldThreshold);\r
                     violatedThreshold.setActualFieldValue(messageFieldValue);\r
index 853ac54..426bd1e 100644 (file)
@@ -53,6 +53,7 @@ import org.quartz.Scheduler;
 import org.quartz.SimpleTrigger;\r
 import org.quartz.impl.StdSchedulerFactory;\r
 \r
+import java.math.BigDecimal;\r
 import java.util.Arrays;\r
 import java.util.HashMap;\r
 import java.util.List;\r
@@ -147,8 +148,9 @@ public class TCAUtilsTest extends BaseAnalyticsTCAUnitTest {
         final String jsonPath =\r
                 "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].receivedBroadcastPacketsAccumulated";\r
         final ImmutableSet<String> fieldPaths = ImmutableSet.of(jsonPath);\r
-        final Map<String, List<Long>> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths);\r
-        assertThat("Json Path value must match", jsonPathValueMap.get(jsonPath).get(0), is(5000L));\r
+        final Map<String, List<BigDecimal>> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths);\r
+        assertThat("Json Path value must match",\r
+                jsonPathValueMap.get(jsonPath).get(0), is(new BigDecimal(5000)));\r
 \r
     }\r
 \r
@@ -157,7 +159,7 @@ public class TCAUtilsTest extends BaseAnalyticsTCAUnitTest {
         final String cefMessageString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);\r
         final String jsonPath = "$.event.measurementsForVfScalingFields.vNicPerformanceArray[*].invalid";\r
         final ImmutableSet<String> fieldPaths = ImmutableSet.of(jsonPath);\r
-        final Map<String, List<Long>> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths);\r
+        final Map<String, List<BigDecimal>> jsonPathValueMap = TCAUtils.getJsonPathValue(cefMessageString, fieldPaths);\r
         assertThat("Json path value must be empty", jsonPathValueMap.size(), is(0));\r
 \r
     }\r
@@ -400,7 +402,7 @@ public class TCAUtilsTest extends BaseAnalyticsTCAUnitTest {
         violatedThreshold.setSeverity(severity);\r
         violatedThreshold.setDirection(Direction.GREATER);\r
         violatedThreshold.setClosedLoopControlName("violatedThresholdClosedLoopName");\r
-        violatedThreshold.setActualFieldValue(100L);\r
+        violatedThreshold.setActualFieldValue(new BigDecimal(100L));\r
         violatedThreshold.setFieldPath("violatedThresholdFieldPath");\r
         violatedThreshold.setVersion("violatedThresholdVersion");\r
         violatedThreshold.setClosedLoopEventStatus(ClosedLoopEventStatus.ONSET);\r