NONRTRIC - bugfix 44/8844/3
authorPatrikBuhr <patrik.buhr@est.tech>
Mon, 8 Aug 2022 09:47:05 +0000 (11:47 +0200)
committerPatrikBuhr <patrik.buhr@est.tech>
Tue, 9 Aug 2022 08:07:06 +0000 (10:07 +0200)
Taking care of incorrect/malformed input to the PM parsing.
Removing "map" output from PM filter that should not be there.

Signed-off-by: PatrikBuhr <patrik.buhr@est.tech>
Issue-ID: NONRTRIC-773
Change-Id: Ic18d8f7ee3176ea39b91c850bec79ce36582c7ef

src/main/java/org/oran/dmaapadapter/filter/PmReport.java
src/main/java/org/oran/dmaapadapter/filter/PmReportFilter.java
src/test/java/org/oran/dmaapadapter/filter/PmReportFilterTest.java

index 21cc672..45a59ed 100644 (file)
 
 package org.oran.dmaapadapter.filter;
 
+import com.google.gson.annotations.Expose;
+
 import java.util.ArrayList;
 import java.util.Collection;
 
 public class PmReport {
 
+    @Expose
     Event event = new Event();
 
     public static class CommonEventHeader {
+        @Expose
         String domain;
+
+        @Expose
         String eventId;
+
+        @Expose
         int sequence;
+
+        @Expose
         String eventName;
+
+        @Expose
         String sourceName;
+
+        @Expose
         String reportingEntityName;
+
+        @Expose
         String priority;
+
+        @Expose
         long startEpochMicrosec;
+
+        @Expose
         long lastEpochMicrosec;
+
+        @Expose
         String version;
+
+        @Expose
         String vesEventListenerVersion;
+
+        @Expose
         String timeZoneOffset;
     }
 
     public static class MeasInfoId {
-        String sMeasInfoId;
+        @Expose
+        String sMeasInfoId = "";
     }
 
     public static class MeasTypes {
@@ -54,17 +81,26 @@ public class PmReport {
             return sMeasTypesList.get(pValue - 1);
         }
 
+        @Expose
         protected ArrayList<String> sMeasTypesList = new ArrayList<>();
     }
 
     public static class MeasResult {
+        @Expose
         int p;
-        String sValue;
+
+        @Expose
+        String sValue = "";
     }
 
     public static class MeasValuesList {
+        @Expose
         String measObjInstId;
+
+        @Expose
         String suspectFlag;
+
+        @Expose
         Collection<MeasResult> measResults = new ArrayList<>();
 
         public MeasValuesList shallowClone() {
@@ -76,8 +112,13 @@ public class PmReport {
     }
 
     public static class MeasInfoList {
+        @Expose
         MeasInfoId measInfoId;
+
+        @Expose
         MeasTypes measTypes;
+
+        @Expose
         Collection<MeasValuesList> measValuesList = new ArrayList<>();
 
         public MeasInfoList shallowClone() {
@@ -89,20 +130,35 @@ public class PmReport {
     }
 
     public static class MeasDataCollection {
+        @Expose
         int granularityPeriod;
+
+        @Expose
         String measuredEntityUserName;
+
+        @Expose
         String measuredEntityDn;
+
+        @Expose
         String measuredEntitySoftwareVersion;
+
+        @Expose
         Collection<MeasInfoList> measInfoList = new ArrayList<>();
     }
 
     public static class Perf3gppFields {
+        @Expose
         String perf3gppFieldsVersion;
+
+        @Expose
         MeasDataCollection measDataCollection;
     }
 
     public static class Event {
+        @Expose
         CommonEventHeader commonEventHeader;
+
+        @Expose
         Perf3gppFields perf3gppFields;
     }
 
index c120585..bac1369 100644 (file)
@@ -20,6 +20,7 @@
 
 package org.oran.dmaapadapter.filter;
 
+import java.lang.invoke.MethodHandles;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
@@ -28,11 +29,24 @@ import java.util.Map;
 
 import lombok.Getter;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.thymeleaf.util.StringUtils;
 
 public class PmReportFilter implements Filter {
+    private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
+
+    private static com.google.gson.Gson gson = new com.google.gson.GsonBuilder() //
+            .disableHtmlEscaping() //
+            .excludeFieldsWithoutExposeAnnotation() //
+            .create();
+
+    // excludeFieldsWithoutExposeAnnotation is not needed when parsing and this is a
+    // bit quicker
+    private static com.google.gson.Gson gsonParse = new com.google.gson.GsonBuilder() //
+            .disableHtmlEscaping() //
+            .create();
 
-    private static com.google.gson.Gson gson = new com.google.gson.GsonBuilder().disableHtmlEscaping().create();
     private final FilterData filterData;
 
     @Getter
@@ -45,6 +59,7 @@ public class PmReportFilter implements Filter {
     }
 
     private static class MeasTypesIndexed extends PmReport.MeasTypes {
+
         private Map<String, Integer> map = new HashMap<>();
 
         public int addP(String measTypeName) {
@@ -52,9 +67,9 @@ public class PmReportFilter implements Filter {
             if (p != null) {
                 return p;
             } else {
-                this.sMeasTypesList.add(measTypeName);
-                this.map.put(measTypeName, this.sMeasTypesList.size());
-                return this.sMeasTypesList.size();
+                sMeasTypesList.add(measTypeName);
+                this.map.put(measTypeName, sMeasTypesList.size());
+                return sMeasTypesList.size();
             }
         }
     }
@@ -65,11 +80,21 @@ public class PmReportFilter implements Filter {
 
     @Override
     public String filter(String data) {
-        PmReport report = gson.fromJson(data, PmReport.class);
-        if (!filter(report, this.filterData)) {
+        try {
+            PmReport report = gsonParse.fromJson(data, PmReport.class);
+            if (report.event.perf3gppFields == null) {
+                logger.warn("Received PM report with no perf3gppFields, ignored. {}", data);
+                return "";
+            }
+
+            if (!filter(report, this.filterData)) {
+                return "";
+            }
+            return gson.toJson(report);
+        } catch (Exception e) {
+            logger.warn("Could not parse PM data. {}, reason: {}", data, e.getMessage());
             return "";
         }
-        return gson.toJson(report);
     }
 
     /**
index 6a11639..23447f5 100644 (file)
@@ -109,7 +109,24 @@ class PmReportFilterTest {
         filterData.measuredEntityDns.add("ManagedElement=RNC-Gbg-1");
         filter = new PmReportFilter(filterData);
         filtered = filter.filter(loadReport());
-        assertThat(filtered).contains("RNC-Gbg-1"); // '=' is escaped to unicode by gson. OK
+        assertThat(filtered).contains("ManagedElement=RNC-Gbg-1");
+    }
+
+    @Test
+    void testCrapInput() {
+        PmReportFilter.FilterData filterData = new PmReportFilter.FilterData();
+        PmReportFilter filter = new PmReportFilter(filterData);
+
+        String filtered = filter.filter("junk");
+        assertThat(filtered).isEmpty();
+
+        filtered = filter.filter(reQuote("{'msg': 'test'}"));
+        assertThat(filtered).isEmpty();
+
+    }
+
+    private String reQuote(String str) {
+        return str.replaceAll("'", "\\\"");
     }
 
     @Test