Fix for ONOS-5306:NPE in Flows Instructions Decoder

Change-Id: I708a074c927841577b4eaf0ffe75ec98fac227f6
(cherry picked from commit af08dcdd4459eb47f9a995aef36674830641a8d3)
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/DecodeInstructionCodecHelper.java b/core/common/src/main/java/org/onosproject/codec/impl/DecodeInstructionCodecHelper.java
index 5e69b0e..316e9d24 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/DecodeInstructionCodecHelper.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/DecodeInstructionCodecHelper.java
@@ -76,7 +76,8 @@
      * @throws IllegalArgumentException if the JSON is invalid
      */
     private Instruction decodeL2() {
-        String subType = json.get(InstructionCodec.SUBTYPE).asText();
+        String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
+                InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
 
         if (subType.equals(L2ModificationInstruction.L2SubType.ETH_SRC.name())) {
             String mac = nullIsIllegal(json.get(InstructionCodec.MAC),
@@ -124,7 +125,8 @@
      * @throws IllegalArgumentException if the JSON is invalid
      */
     private Instruction decodeL3() {
-        String subType = json.get(InstructionCodec.SUBTYPE).asText();
+        String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
+                InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
 
         if (subType.equals(L3ModificationInstruction.L3SubType.IPV4_SRC.name())) {
             IpAddress ip = IpAddress.valueOf(nullIsIllegal(json.get(InstructionCodec.IP),
@@ -158,7 +160,8 @@
      * @throws IllegalArgumentException if the JSON is invalid
      */
     private Instruction decodeL0() {
-        String subType = json.get(InstructionCodec.SUBTYPE).asText();
+        String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
+                InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
 
         if (subType.equals(L0ModificationInstruction.L0SubType.OCH.name())) {
             String gridTypeString = nullIsIllegal(json.get(InstructionCodec.GRID_TYPE),
@@ -193,7 +196,8 @@
      * @throws IllegalArgumentException if the JSON is invalid
      */
     private Instruction decodeL1() {
-        String subType = json.get(InstructionCodec.SUBTYPE).asText();
+        String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
+                InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
         if (subType.equals(L1ModificationInstruction.L1SubType.ODU_SIGID.name())) {
             int tributaryPortNumber = nullIsIllegal(json.get(InstructionCodec.TRIBUTARY_PORT_NUMBER),
                     InstructionCodec.TRIBUTARY_PORT_NUMBER + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt();
@@ -217,7 +221,8 @@
      * @throws IllegalArgumentException if the JSON is invalid
      */
     private Instruction decodeL4() {
-        String subType = json.get(InstructionCodec.SUBTYPE).asText();
+        String subType = nullIsIllegal(json.get(InstructionCodec.SUBTYPE),
+                InstructionCodec.SUBTYPE + InstructionCodec.ERROR_MESSAGE).asText();
 
         if (subType.equals(L4ModificationInstruction.L4SubType.TCP_DST.name())) {
             TpPort tcpPort = TpPort.tpPort(nullIsIllegal(json.get(InstructionCodec.TCP_PORT),
@@ -292,19 +297,15 @@
      */
     private PortNumber getPortNumber(ObjectNode jsonNode) {
         PortNumber portNumber;
-        if (jsonNode.get(InstructionCodec.PORT).isLong() || jsonNode.get(InstructionCodec.PORT).isInt()) {
-            portNumber = PortNumber
-                    .portNumber(nullIsIllegal(jsonNode.get(InstructionCodec.PORT)
-                            .asLong(), InstructionCodec.PORT
-                            + InstructionCodec.MISSING_MEMBER_MESSAGE));
-        } else if (jsonNode.get(InstructionCodec.PORT).isTextual()) {
-            portNumber = PortNumber
-                    .fromString(nullIsIllegal(jsonNode.get(InstructionCodec.PORT)
-                            .textValue(), InstructionCodec.PORT
-                            + InstructionCodec.MISSING_MEMBER_MESSAGE));
+        JsonNode portNode = nullIsIllegal(jsonNode.get(InstructionCodec.PORT),
+                InstructionCodec.PORT + InstructionCodec.ERROR_MESSAGE);
+        if (portNode.isLong() || portNode.isInt()) {
+            portNumber = PortNumber.portNumber(portNode.asLong());
+        } else if (portNode.isTextual()) {
+            portNumber = PortNumber.fromString(portNode.textValue());
         } else {
             throw new IllegalArgumentException("Port value "
-                    + jsonNode.get(InstructionCodec.PORT).toString()
+                    + portNode.toString()
                     + " is not supported");
         }
         return portNumber;
@@ -317,26 +318,27 @@
      * @throws IllegalArgumentException if the JSON is invalid
      */
     public Instruction decode() {
-        String type = json.get(InstructionCodec.TYPE).asText();
+        String type = nullIsIllegal(json.get(InstructionCodec.TYPE),
+                InstructionCodec.TYPE + InstructionCodec.ERROR_MESSAGE).asText();
 
         if (type.equals(Instruction.Type.OUTPUT.name())) {
             return Instructions.createOutput(getPortNumber(json));
         } else if (type.equals(Instruction.Type.NOACTION.name())) {
             return Instructions.createNoAction();
         } else if (type.equals(Instruction.Type.TABLE.name())) {
-            return Instructions.transition(nullIsIllegal(json.get(InstructionCodec.TABLE_ID)
-                    .asInt(), InstructionCodec.TABLE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE));
+            return Instructions.transition(nullIsIllegal(json.get(InstructionCodec.TABLE_ID),
+                    InstructionCodec.TABLE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
         } else if (type.equals(Instruction.Type.GROUP.name())) {
-            GroupId groupId = new DefaultGroupId(nullIsIllegal(json.get(InstructionCodec.GROUP_ID)
-                    .asInt(), InstructionCodec.GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE));
+            GroupId groupId = new DefaultGroupId(nullIsIllegal(json.get(InstructionCodec.GROUP_ID),
+                    InstructionCodec.GROUP_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asInt());
             return Instructions.createGroup(groupId);
         } else if (type.equals(Instruction.Type.METER.name())) {
-            MeterId meterId = MeterId.meterId(nullIsIllegal(json.get(InstructionCodec.METER_ID)
-                    .asLong(), InstructionCodec.METER_ID + InstructionCodec.MISSING_MEMBER_MESSAGE));
+            MeterId meterId = MeterId.meterId(nullIsIllegal(json.get(InstructionCodec.METER_ID),
+                    InstructionCodec.METER_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong());
             return Instructions.meterTraffic(meterId);
         } else if (type.equals(Instruction.Type.QUEUE.name())) {
-            long queueId = nullIsIllegal(json.get(InstructionCodec.QUEUE_ID)
-                    .asLong(), InstructionCodec.QUEUE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE);
+            long queueId = nullIsIllegal(json.get(InstructionCodec.QUEUE_ID),
+                    InstructionCodec.QUEUE_ID + InstructionCodec.MISSING_MEMBER_MESSAGE).asLong();
             return Instructions.setQueue(queueId, getPortNumber(json));
         } else if (type.equals(Instruction.Type.L0MODIFICATION.name())) {
             return decodeL0();
diff --git a/core/common/src/main/java/org/onosproject/codec/impl/InstructionCodec.java b/core/common/src/main/java/org/onosproject/codec/impl/InstructionCodec.java
index 9da240d..3078fa8 100644
--- a/core/common/src/main/java/org/onosproject/codec/impl/InstructionCodec.java
+++ b/core/common/src/main/java/org/onosproject/codec/impl/InstructionCodec.java
@@ -62,6 +62,8 @@
 
     protected static final String MISSING_MEMBER_MESSAGE =
             " member is required in Instruction";
+    protected static final String ERROR_MESSAGE =
+            " not specified in Instruction";
 
 
     @Override