VES7.x to 5.x transformation 39/62139/1
authorPATRACHARI <gp2421@att.com>
Thu, 23 Aug 2018 14:08:30 +0000 (10:08 -0400)
committerPATRACHARI <gp2421@att.com>
Thu, 23 Aug 2018 14:41:23 +0000 (10:41 -0400)
Update transformation code based on Min's requirement. This includes all
Measurement transformations.

Issue-ID: DCAEGEN2-600
Change-Id: I082df846e159a4f158619e700a6dfed6de43cd95
Signed-off-by: PATRACHARI <gp2421@att.com>
etc/eventTransform.json
src/main/java/org/onap/dcae/commonFunction/ConfigProcessors.java
src/test/java/org/onap/dcae/vestest/TestConfigProcessor.java
src/test/resources/measurement5x.json [new file with mode: 0644]
src/test/resources/measurement7x.json [new file with mode: 0644]

index cdefda0..838fca0 100644 (file)
@@ -5,14 +5,29 @@
                        "event.commonEventHeader.domain":"heartbeat", 
                        "VESversion":"v7"
                },
-       
                "processors": 
                [
+                       {
+                       "functionName": "addAttribute",
+                       "args":{
+                               "field": "event.commonEventHeader.version",
+                               "value": "3.0",
+                               "fieldType": "number"
+                       }
+               },
                {
                        "functionName": "addAttribute",
                        "args":{
                                "field": "event.heartbeatFields.heartbeatFieldsVersion",
-                               "value": "2.0"
+                               "value": "2.0",
+                               "fieldType": "number"
+                       }
+               },
+               {
+                       "functionName": "map",
+                       "args":{
+                               "field": "event.heartbeatFields.additionalFields",
+                               "mapType": "HashmapToNameValueArray"
                        }
                }
                ]
                },
                "processors": 
                [
+                       {
+                       "functionName": "addAttribute",
+                       "args":{
+                               "field": "event.commonEventHeader.version",
+                               "value": "3.0",
+                               "fieldType": "number"
+                       }
+               },
                {
                        "functionName": "addAttribute",
                        "args":{
                                "value": "3.0",
                                "fieldType": "number"
                        }
+               },
+               {
+                       "functionName": "map",
+                       "args":{
+                               "field": "event.faultFields.alarmAdditionalInformation",
+                               "mapType": "HashmapToNameValueArray"
+                       }
                }
                ]
        },
                },
                "processors": 
                [
+                       {
+                       "functionName": "addAttribute",
+                       "args":{
+                               "field": "event.commonEventHeader.version",
+                               "value": "3.0",
+                               "fieldType": "number"
+                       }
+               },
                {
                        "functionName": "addAttribute",
                        "args":{
                                "value": "3.0",
                                "fieldType": "number"
                        }
+               },
+               {
+                       "functionName": "map",
+                       "args":{
+                               "field": "event.thresholdCrossingFields.additionalFields",
+                               "mapType": "HashmapToNameValueArray"
+                       }
+               }
+               ]
+       },
+       {
+               "filter":
+               {
+                       "event.commonEventHeader.domain":"measurement", 
+                       "VESversion":"v7"
+               },
+               "processors": 
+               [
+                       {
+                       "functionName": "addAttribute",
+                       "args":{
+                               "field": "event.commonEventHeader.version",
+                               "value": "3.0",
+                               "fieldType": "number"
+                       }
+               },
+               {
+                       "functionName": "removeAttribute",
+                       "args":{
+                               "field": "event.measurementFields.measurementFieldsVersion"
+                       }
+               },
+               {
+                       "functionName": "addAttribute",
+                       "args":{
+                               "field": "event.measurementFields.measurementsForVfScalingVersion",
+                               "value": "3.0",
+                               "fieldType": "number"
+                       }
+               },
+               {
+                       "functionName": "map",
+                       "args":{
+                               "field": "event.measurementFields.vNicPerformanceArray[]",
+                               "oldField": "event.measurementFields.nicPerformanceArray[]",
+                               "attrMap":{
+                                       "nicIdentifier":"vNicIdentifier"
+                               }
+                       }
+               },
+               {
+                       "functionName": "map",
+                       "args":{
+                               "field": "event.measurementFields.additionalFields",
+                               "oldField": "event.measurementFields.additionalFields",
+                               "mapType": "hashmapToNameValueArray"
+                       }
+               },
+               {
+                       "functionName": "map",
+                       "args":{
+                               "field": "event.measurementsForVfScalingFields",
+                               "oldField": "event.measurementFields",
+                               "mapType": "renameObject"
+                       }
+               },
+               {
+                       "functionName": "addAttribute",
+                       "args":{
+                               "field": "event.commonEventHeader.domain",
+                               "value": "measurementsForVfScaling"
+                       }
                }
                ]
        },
index e3d5909..a9d6b98 100644 (file)
@@ -38,6 +38,7 @@ public class ConfigProcessors {
     private static final String REGEX = "\\[\\]";
     private static final String OBJECT_NOT_FOUND = "ObjectNotFound";
     private static final String FILTER_NOT_MET = "Filter not met";
+    private static final String MAP_TYPE = "mapType";
     private static final String COMP_FALSE = "==false";
 
     private final JSONObject event;
@@ -155,17 +156,43 @@ public class ConfigProcessors {
             log.info(FILTER_NOT_MET);
     }
 
+    private void renameObject(JSONObject jsonObject) // map
+    {
+        log.info("renameArrayInArray");
+        final String field = jsonObject.getString(FIELD);
+        final String oldField = jsonObject.getString(OLD_FIELD);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
 
+        if (filter == null || isFilterMet(filter)) {
+
+            final JSONObject oldValue = (JSONObject) getEventObjectVal(oldField);
+            if (!oldValue.equals(OBJECT_NOT_FOUND)) {
+                setEventObjectVal(field, oldValue);
+                removeEventKey(oldField);
+            }
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+    
     public void map(JSONObject jsonObject) {
 
         final String field = jsonObject.getString(FIELD);
+        final String mapType = jsonObject.optString(MAP_TYPE, "");
         if (field.contains("[]")) {
             if (field.matches(".*\\[\\]\\..*\\[\\]"))
                 renameArrayInArray(jsonObject);
             else
                 mapToJArray(jsonObject);
-        } else
-            mapAttribute(jsonObject);
+        }
+        else if (mapType.equals("hashmapToNameValueArray"))
+               mapHashmapToNameValueArray(jsonObject);
+        else if (mapType.equals("nameValueArrayToHashmap"))
+               mapNameValueArrayToHashmap(jsonObject);
+       else if (mapType.equals("renameObject"))
+               renameObject(jsonObject);
+               
+        else
+        mapAttribute(jsonObject);
     }
 
     private String performOperation(String operation, String value) {
@@ -252,6 +279,74 @@ public class ConfigProcessors {
         } else
             log.info(FILTER_NOT_MET);
     }
+    
+    // this method is to support the mapping 5.x to VES7.x format for additionalInformation field
+    private void mapNameValueArrayToHashmap(JSONObject jsonObject) {
+        log.info("mapNameValueArrayToHashmap");
+        String field = jsonObject.getString(FIELD);
+        String oldField = jsonObject.getString(FIELD);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+
+        if (filter == null || isFilterMet(filter)) {
+               JSONObject newHashMap = new JSONObject(); // this will hold the newly mapped hashmap elements
+            JSONArray arrayValue = (JSONArray) getEventObjectVal(oldField); // old Array structure value
+            JSONObject tempJObj = null;
+            String tempName = "";
+            String tempValue = "";
+            if (!arrayValue.equals(OBJECT_NOT_FOUND)) {
+                log.info("old value ==" + arrayValue.toString());
+                // Loop thru the JSONArray, get the name:value pair and write to new JSONObject as hashmap elements
+                for (int i = 0; i < arrayValue.length(); i++) {
+
+                       tempJObj = arrayValue.getJSONObject(i);
+                    if (tempJObj != null) {
+                        tempName = tempJObj.get("name").toString();
+                        tempValue = tempJObj.get("value").toString();
+                        newHashMap.put(tempName, tempValue);
+                    }
+                }
+                // remove the old Array structure
+                removeEventKey(oldField);
+                //Add the new Hashmap 
+                setEventObjectVal(field, newHashMap);
+            }
+        } else
+            log.info(FILTER_NOT_MET);
+    }
+    
+ // this method is to support the mapping 7.x to VES5.x format for additionalInformation field
+    private void mapHashmapToNameValueArray(JSONObject jsonObject) {
+        log.info("mapHashmapToNameValueArray");
+        System.out.println("mapHashmapToNameValueArray");
+        String field = jsonObject.getString(FIELD);
+        String oldField = jsonObject.getString(FIELD);
+        final JSONObject filter = jsonObject.optJSONObject(FILTER);
+
+        if (filter == null || isFilterMet(filter)) {
+               JSONArray newArray = new JSONArray(); // this will hold the new name:value JSONObject
+               JSONObject nameValJObj;
+               System.out.println("object ==" + getEventObjectVal(oldField).toString());
+               if (!getEventObjectVal(oldField).toString().equals(OBJECT_NOT_FOUND)) {
+                       
+                   JSONObject hashMap = (JSONObject) getEventObjectVal(oldField); // old hashmap structure value
+                   if (hashMap != null) {
+                       log.info("old value ==" + hashMap.toString());
+                       // Loop thru the hashMap JSONObject, get the hashmap elements add them as name:value JsonObject into the newArray
+                       for (String key : hashMap.keySet()) {
+                               nameValJObj = new JSONObject(); //create new object so not to overwrite in memory for Array insertion
+                               nameValJObj.put("name", key);
+                               nameValJObj.put("value", hashMap.get(key));
+                               newArray.put(nameValJObj);
+                       }
+                       // remove the old hashMap structure
+                       removeEventKey(oldField);
+                       //Add the newArray containing the name:value Object
+                       setEventObjectVal(field, newArray);
+                   }
+               }
+        } else
+            log.info(FILTER_NOT_MET);
+    }
 
     /**
      * example - { "functionName": "concatenateValue", "args":{ "filter":
index 09517ae..310e8be 100644 (file)
@@ -20,6 +20,7 @@
 package org.onap.dcae.vestest;\r
 \r
 import static org.junit.Assert.assertEquals;\r
+import static org.junit.Assert.assertNotEquals;\r
 \r
 import java.io.FileReader;\r
 import java.io.IOException;\r
@@ -116,7 +117,69 @@ public class TestConfigProcessor {
         System.out.println("responseData==" + responseData);\r
         assertEquals(receiveDiscards, responseData);\r
     }\r
+    \r
+     @Test\r
+    public void testMapRenameObject() {\r
+\r
+        final JSONObject jsonObject = getFileAsJsonObject();\r
+        //final String receiveDiscards = (((jsonObject.getJSONObject("event")).getJSONObject("faultFields")).get("errors")).get("receiveDiscards").toString();\r
+        System.out.println("event==" + jsonObject.toString());\r
+        //System.out.println("alarmAdditionalInformation==" + alarmAdditionalInformation);\r
+        final JSONObject jsonArgs = new JSONObject(\r
+                "{\"field\": \"event.faultFields.faultVFScalingFields\",\"oldField\": \"event.faultFields.errors\",\"mapType\":\"renameObject\"}");\r
+        ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
+        final String receiveDiscards = cpEvent.getEventObjectVal("event.faultFields.errors").toString();\r
+        System.out.println("receiveDiscards==" + receiveDiscards);\r
+        cpEvent.map(jsonArgs);\r
+        final String responseData = cpEvent\r
+                .getEventObjectVal("event.faultFields.faultVFScalingFields")\r
+                .toString();\r
+        System.out.println("modified event==" + jsonObject.toString());\r
+        System.out.println("responseData==" + responseData);\r
+        assertEquals(receiveDiscards, responseData);\r
+    }\r
+    @Test\r
+    public void testMapHashmapToNameValueArray() {\r
+\r
+        final JSONObject jsonObject = getFileAsJsonObject();\r
+        //final String receiveDiscards = (((jsonObject.getJSONObject("event")).getJSONObject("faultFields")).get("errors")).get("receiveDiscards").toString();\r
+        System.out.println("event==" + jsonObject.toString());\r
+        //System.out.println("alarmAdditionalInformation==" + alarmAdditionalInformation);\r
+        final JSONObject jsonArgs = new JSONObject(\r
+                "{\"field\": \"event.faultFields.errors\",\"mapType\":\"hashmapToNameValueArray\"}");\r
+        ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
+        final String receiveDiscards = cpEvent.getEventObjectVal("event.faultFields.errors").toString();\r
+        System.out.println("receiveDiscards==" + receiveDiscards);\r
+        cpEvent.map(jsonArgs);\r
+        final String responseData = cpEvent\r
+                .getEventObjectVal("event.faultFields.errors")\r
+                .toString();\r
+        System.out.println("modified event==" + jsonObject.toString());\r
+        System.out.println("responseData==" + responseData);\r
+        assertNotEquals(receiveDiscards, responseData);\r
+    }\r
 \r
+    @Test\r
+    public void testMapNameValueArrayToHashmap() {\r
+\r
+        final JSONObject jsonObject = getFileAsJsonObject();\r
+        //final String receiveDiscards = (((jsonObject.getJSONObject("event")).getJSONObject("faultFields")).get("errors")).get("receiveDiscards").toString();\r
+        System.out.println("event==" + jsonObject.toString());\r
+        //System.out.println("alarmAdditionalInformation==" + alarmAdditionalInformation);\r
+        final JSONObject jsonArgs = new JSONObject(\r
+                "{\"field\": \"event.faultFields.alarmAdditionalInformation\",\"mapType\":\"nameValueArrayToHashmap\"}");\r
+        ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);\r
+        final String receiveDiscards = cpEvent.getEventObjectVal("event.faultFields.alarmAdditionalInformation").toString();\r
+        System.out.println("receiveDiscards==" + receiveDiscards);\r
+        cpEvent.map(jsonArgs);\r
+        final String responseData = cpEvent\r
+                .getEventObjectVal("event.faultFields.alarmAdditionalInformation")\r
+                .toString();\r
+        System.out.println("modified event==" + jsonObject.toString());\r
+        System.out.println("responseData==" + responseData);\r
+        assertNotEquals(receiveDiscards, responseData);\r
+    }\r
+    \r
     @Test\r
     public void testAttrAdd() {\r
 \r
@@ -304,6 +367,6 @@ public class TestConfigProcessor {
 \r
         System.out.println("responseData==" + responseData);\r
         assertEquals("true", responseData);\r
-    }\r
+    } \r
 }  \r
 \r
diff --git a/src/test/resources/measurement5x.json b/src/test/resources/measurement5x.json
new file mode 100644 (file)
index 0000000..585ec37
--- /dev/null
@@ -0,0 +1,98 @@
+ {
+       "VESversion":"v5",
+       "event": {
+               "commonEventHeader": {
+                       "startEpochMicrosec": 1534868302434191,
+                       "eventId": "measurementsForVfScaling_vAfx_127.0.0.1",
+                       "nfcNamingCode": "AFX",
+                       "reportingEntityId": "bgtnj05vmh01afx",
+                       "eventType": "applicationVnf",
+                       "priority": "Normal",
+                       "version": 3,
+                       "reportingEntityName": "AFXM",
+                       "sequence": 0,
+                       "domain": "measurementsForVfScaling",
+                       "lastEpochMicrosec": 1534868602564759,
+                       "eventName": "measurementsForVfScaling_vAfx",
+                       "sourceName": "bgtnj05vmh01afx",
+                       "nfNamingCode": "AFX"
+               },
+               "measurementsForVfScalingFields": {
+                       "cpuUsageArray": [{
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.39,
+                               "cpuIdentifier": "all",
+                               "cpuIdle": 99.61,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.22
+                       }, {
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.36,
+                               "cpuIdentifier": "cpu0",
+                               "cpuIdle": 99.64,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.21
+                       }, {
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.34,
+                               "cpuIdentifier": "cpu1",
+                               "cpuIdle": 99.66,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.2
+                       }, {
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.52,
+                               "cpuIdentifier": "cpu2",
+                               "cpuIdle": 99.48,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.3
+                       }, {
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.33,
+                               "cpuIdentifier": "cpu3",
+                               "cpuIdle": 99.67,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.19
+                       }],
+                       "measurementInterval": 300,
+                       "measurementsForVfScalingVersion": 2,
+                       "vNicPerformanceArray": [{
+                               "transmittedOctetsDelta": 1533682,
+                               "receivedMulticastPacketsAccumulated": 0,
+                               "receivedMulticastPacketsDelta": 0,
+                               "receivedOctetsAccumulated": 1.215192543E9,
+                               "receivedTotalPacketsDelta": 7988,
+                               "vNicIdentifier": "ens2   DNS2_RSLOTPORT_VLAN_CLASSTYPE",
+                               "valuesAreSuspect": "true",
+                               "transmittedTotalPacketsDelta": 9326,
+                               "transmittedTotalPacketsAccumulated": 7460880,
+                               "receivedOctetsDelta": 548159,
+                               "transmittedOctetsAccumulated": 1.055825979E9,
+                               "receivedTotalPacketsAccumulated": 1.3555845E7
+                       }, {
+                               "transmittedOctetsDelta": 0,
+                               "receivedMulticastPacketsAccumulated": 0,
+                               "receivedMulticastPacketsDelta": 0,
+                               "receivedOctetsAccumulated": 5412138,
+                               "receivedTotalPacketsDelta": 0,
+                               "vNicIdentifier": "lo   DNS1_RSLOTPORT_VLAN_CLASSTYPE",
+                               "valuesAreSuspect": "true",
+                               "transmittedTotalPacketsDelta": 0,
+                               "transmittedTotalPacketsAccumulated": 53567,
+                               "receivedOctetsDelta": 0,
+                               "transmittedOctetsAccumulated": 5412138,
+                               "receivedTotalPacketsAccumulated": 53567
+                       }],
+                       "memoryUsageArray": [{
+                               "memorySlabUnrecl": 17560,
+                               "memoryCached": 976988,
+                               "memoryFree": 6446628,
+                               "memorySlabRecl": 83908,
+                               "memoryBuffered": 198020,
+                               "memoryUsed": 353492,
+                               "vmIdentifier": "RAM",
+                               "memoryConfigured": 8076596
+                       }]
+               }
+       }
+ }
\ No newline at end of file
diff --git a/src/test/resources/measurement7x.json b/src/test/resources/measurement7x.json
new file mode 100644 (file)
index 0000000..6e9fd1f
--- /dev/null
@@ -0,0 +1,107 @@
+ {
+       "VESversion":"v7",
+       "event": {
+               "commonEventHeader": {
+                       "startEpochMicrosec": 1534868302434191,
+                       "eventId": "measurementsForVfScaling_vAfx_127.0.0.1",
+                       "nfcNamingCode": "AFX",
+                       "reportingEntityId": "bgtnj05vmh01afx",
+                       "eventType": "applicationVnf",
+                       "priority": "Normal",
+                       "version": "4.0.1",
+                       "reportingEntityName": "AFXM",
+                       "sequence": 0,
+                       "domain": "measurement",
+                       "lastEpochMicrosec": 1534868602564759,
+                       "eventName": "measurement",
+                       "sourceName": "bgtnj05vmh01afx",
+                       "nfNamingCode": "AFX",
+            "vesEventListenerVersion" : "7.0.1"
+               },
+               "measurementFields": {
+                       "cpuUsageArray": [{
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.39,
+                               "cpuIdentifier": "all",
+                               "cpuIdle": 99.61,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.22,
+                               "cpuCapacityContention": 0,
+                               "cpuDemandAvg": 0,
+                               "cpuDemandMhz": 0,
+                               "cpuDemandPct": 0,
+                               "cpuLatencyAvg": 0,
+                               "cpuOverheadAvg": 0,
+                               "cpuSwapWaitTime": 0
+                               
+                       }, {
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.36,
+                               "cpuIdentifier": "cpu0",
+                               "cpuIdle": 99.64,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.21
+                       }, {
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.34,
+                               "cpuIdentifier": "cpu1",
+                               "cpuIdle": 99.66,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.2
+                       }, {
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.52,
+                               "cpuIdentifier": "cpu2",
+                               "cpuIdle": 99.48,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.3
+                       }, {
+                               "cpuUsageNice": 0,
+                               "percentUsage": 0.33,
+                               "cpuIdentifier": "cpu3",
+                               "cpuIdle": 99.67,
+                               "cpuUsageSystem": 0,
+                               "cpuUsageUser": 0.19
+                       }],
+                       "measurementInterval": 300,
+                       "measurementFieldsVersion": "4.0",
+                       "nicPerformanceArray": [{
+                               "transmittedOctetsDelta": 1533682,
+                               "receivedMulticastPacketsAccumulated": 0,
+                               "receivedMulticastPacketsDelta": 0,
+                               "receivedOctetsAccumulated": 1.215192543E9,
+                               "receivedTotalPacketsDelta": 7988,
+                               "nicIdentifier": "ens2   DNS2_RSLOTPORT_VLAN_CLASSTYPE",
+                               "valuesAreSuspect": "true",
+                               "transmittedTotalPacketsDelta": 9326,
+                               "transmittedTotalPacketsAccumulated": 7460880,
+                               "receivedOctetsDelta": 548159,
+                               "transmittedOctetsAccumulated": 1.055825979E9,
+                               "receivedTotalPacketsAccumulated": 1.3555845E7
+                       }, {
+                               "transmittedOctetsDelta": 0,
+                               "receivedMulticastPacketsAccumulated": 0,
+                               "receivedMulticastPacketsDelta": 0,
+                               "receivedOctetsAccumulated": 5412138,
+                               "receivedTotalPacketsDelta": 0,
+                               "nicIdentifier": "lo   DNS1_RSLOTPORT_VLAN_CLASSTYPE",
+                               "valuesAreSuspect": "true",
+                               "transmittedTotalPacketsDelta": 0,
+                               "transmittedTotalPacketsAccumulated": 53567,
+                               "receivedOctetsDelta": 0,
+                               "transmittedOctetsAccumulated": 5412138,
+                               "receivedTotalPacketsAccumulated": 53567
+                       }],
+                       "memoryUsageArray": [{
+                               "memorySlabUnrecl": 17560,
+                               "memoryCached": 976988,
+                               "memoryFree": 6446628,
+                               "memorySlabRecl": 83908,
+                               "memoryBuffered": 198020,
+                               "memoryUsed": 353492,
+                               "vmIdentifier": "RAM",
+                               "memoryConfigured": 8076596
+                       }]
+               }
+       }
+ }
\ No newline at end of file