ONOS-4083,ONOS-4084:JUNIT for ISIS PDU TLV Data Structures

Change-Id: I0849d87ad1c7f9a8e08aabebd547dfd2dfde49df
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IsExtendedReachability.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IsExtendedReachability.java
index c6cd9f0..b2eb6c9 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IsExtendedReachability.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/IsExtendedReachability.java
@@ -1,27 +1,23 @@
 /*
- * Copyright 2016-present Open Networking Laboratory
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+* Copyright 2016-present Open Networking Laboratory
+*
+* Licensed under the Apache License, Version 2.0 (the "License");
+* you may not use this file except in compliance with the License.
+* You may obtain a copy of the License at
+*
+*     http://www.apache.org/licenses/LICENSE-2.0
+*
+* Unless required by applicable law or agreed to in writing, software
+* distributed under the License is distributed on an "AS IS" BASIS,
+* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+* See the License for the specific language governing permissions and
+* limitations under the License.
+*/
 package org.onosproject.isis.io.isispacket.tlv;
 
 import com.google.common.base.MoreObjects;
 import com.google.common.primitives.Bytes;
 import org.jboss.netty.buffer.ChannelBuffer;
-import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvFinder;
-import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvToBytes;
-import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvType;
-import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv;
 import org.onosproject.isis.io.util.IsisUtil;
 
 import java.util.ArrayList;
@@ -32,9 +28,7 @@
  */
 public class IsExtendedReachability extends TlvHeader implements IsisTlv {
 
-    private String neighborId;
-    private int metric;
-    private List<TrafficEngineeringSubTlv> trafEnginSubTlv = new ArrayList<>();
+    private List<NeighborForExtendedIs> neighbors = new ArrayList<>();
 
     /**
      * Creates an instance of IP external reachability TLV.
@@ -47,67 +41,20 @@
     }
 
     /**
-     * Returns neighbor ID.
+     * Adds the neighbor for extended IS instance to IS extended reachability TLV.
      *
-     * @return neighbor ID
+     * @param neighbor neighbor for extended IS instance
      */
-    public String neighborId() {
-        return neighborId;
-    }
-
-    /**
-     * Sets neighbor ID.
-     *
-     * @param neighborId neighbor ID
-     */
-    public void setNeighborId(String neighborId) {
-        this.neighborId = neighborId;
-    }
-
-    /**
-     * Returns metric.
-     *
-     * @return metric
-     */
-    public int metric() {
-        return metric;
-    }
-
-    /**
-     * Sets metric.
-     *
-     * @param metric metric
-     */
-    public void setMetric(int metric) {
-        this.metric = metric;
-    }
-
-    /**
-     * Adds the traffic engineering sub TLV to IS extended reachability TLV.
-     *
-     * @param trafEnginSubTlv traffic engineering sub TLV
-     */
-    public void addSubTlv(TrafficEngineeringSubTlv trafEnginSubTlv) {
-        this.trafEnginSubTlv.add(trafEnginSubTlv);
+    public void addNeighbor(NeighborForExtendedIs neighbor) {
+        this.neighbors.add(neighbor);
     }
 
     @Override
     public void readFrom(ChannelBuffer channelBuffer) {
-        byte[] tempByteArray = new byte[IsisUtil.ID_SIX_BYTES];
-        channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_SIX_BYTES);
-        this.setNeighborId(IsisUtil.systemId(tempByteArray));
-        this.setMetric(channelBuffer.readUnsignedMedium());
-        while (channelBuffer.readableBytes() > 0) {
-            TlvHeader tlvHeader = new TlvHeader();
-            tlvHeader.setTlvType(channelBuffer.readByte());
-            tlvHeader.setTlvLength(channelBuffer.readByte());
-            SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType());
-            if (tlvValue != null) {
-                this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader,
-                                                       channelBuffer.readBytes(tlvHeader.tlvLength())));
-            } else {
-                channelBuffer.readBytes(tlvHeader.tlvLength());
-            }
+        while (channelBuffer.readableBytes() >= IsisUtil.EIGHT_BYTES + IsisUtil.THREE_BYTES) {
+            NeighborForExtendedIs extendedIs = new NeighborForExtendedIs();
+            extendedIs.readFrom(channelBuffer);
+            this.addNeighbor(extendedIs);
         }
     }
 
@@ -128,12 +75,8 @@
      */
     private byte[] tlvBodyAsBytes() {
         List<Byte> byteList = new ArrayList<>();
-        byteList.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborId()));
-        byteList.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.metric())));
-        if (this.trafEnginSubTlv.size() > 0) {
-            for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.trafEnginSubTlv) {
-                byteList.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv));
-            }
+        for (NeighborForExtendedIs neighbor : this.neighbors) {
+            byteList.addAll(Bytes.asList(neighbor.neighborBodyAsbytes()));
         }
         return Bytes.toArray(byteList);
     }
@@ -142,10 +85,7 @@
     public String toString() {
         return MoreObjects.toStringHelper(getClass())
                 .omitNullValues()
-                .add("neighborId", neighborId)
-                .add("metric", metric)
-                .add("trafEnginSubTlv", trafEnginSubTlv)
+                .add("neighbors", neighbors)
                 .toString();
     }
-
-}
+}
\ No newline at end of file
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/NeighborForExtendedIs.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/NeighborForExtendedIs.java
new file mode 100644
index 0000000..88b8462
--- /dev/null
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/NeighborForExtendedIs.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.isis.io.isispacket.tlv;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.primitives.Bytes;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvFinder;
+import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvToBytes;
+import org.onosproject.isis.io.isispacket.tlv.subtlv.SubTlvType;
+import org.onosproject.isis.io.isispacket.tlv.subtlv.TrafficEngineeringSubTlv;
+import org.onosproject.isis.io.util.IsisUtil;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Representation of IS extended reachability neighbors.
+ */
+public class NeighborForExtendedIs {
+
+    private String neighborId;
+    private int metric;
+    private List<TrafficEngineeringSubTlv> teSubTlv = new ArrayList<>();
+
+    /**
+     * Returns neighbor ID.
+     *
+     * @return neighbor ID
+     */
+    public String neighborId() {
+        return neighborId;
+    }
+
+    /**
+     * Sets neighbor ID.
+     *
+     * @param neighborId neighbor ID
+     */
+    public void setNeighborId(String neighborId) {
+        this.neighborId = neighborId;
+    }
+
+    /**
+     * Returns metric.
+     *
+     * @return metric
+     */
+    public int metric() {
+        return metric;
+    }
+
+    /**
+     * Sets metric.
+     *
+     * @param metric metric
+     */
+    public void setMetric(int metric) {
+        this.metric = metric;
+    }
+
+    /**
+     * Adds the TE for extended IS instance to IS extended reachability TLV.
+     *
+     * @param trafficEngineeringSubTlv TE for extended IS instance
+     */
+    public void addSubTlv(TrafficEngineeringSubTlv trafficEngineeringSubTlv) {
+        this.teSubTlv.add(trafficEngineeringSubTlv);
+    }
+
+    /**
+     * Reads from the channel buffer and populate this instance.
+     *
+     * @param channelBuffer channel buffer instance
+     */
+    public void readFrom(ChannelBuffer channelBuffer) {
+        byte[] tempByteArray = new byte[IsisUtil.ID_PLUS_ONE_BYTE];
+        channelBuffer.readBytes(tempByteArray, 0, IsisUtil.ID_PLUS_ONE_BYTE);
+        this.setNeighborId(IsisUtil.systemIdPlus(tempByteArray));
+        this.setMetric(channelBuffer.readUnsignedMedium());
+        int nTlvPresent = channelBuffer.readByte();
+        if (nTlvPresent > 0) {
+            while (channelBuffer.readableBytes() > IsisUtil.TWO_BYTES) {
+                TlvHeader tlvHeader = new TlvHeader();
+                tlvHeader.setTlvType(channelBuffer.readByte());
+                tlvHeader.setTlvLength(channelBuffer.readByte());
+                SubTlvType tlvValue = SubTlvType.get(tlvHeader.tlvType());
+                int tlvLength = tlvHeader.tlvLength();
+                if (tlvValue != null) {
+                    if (channelBuffer.readableBytes() >= tlvLength) {
+                        this.addSubTlv(SubTlvFinder.findSubTlv(tlvHeader,
+                                                               channelBuffer.readBytes(tlvHeader.tlvLength())));
+                    }
+                } else {
+                    if (channelBuffer.readableBytes() >= tlvLength) {
+                        channelBuffer.readBytes(tlvLength);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * Returns neighbor details of IS extended reachability TLV.
+     *
+     * @return neighbor details of IS extended reachability TLV.
+     */
+    public byte[] neighborBodyAsbytes() {
+        List<Byte> byteList = new ArrayList<>();
+        byteList.addAll(IsisUtil.sourceAndLanIdToBytes(this.neighborId()));
+        byteList.addAll(Bytes.asList(IsisUtil.convertToThreeBytes(this.metric())));
+        if (this.teSubTlv.size() > 0) {
+            for (TrafficEngineeringSubTlv trafficEngineeringSubTlv : this.teSubTlv) {
+                byteList.addAll(SubTlvToBytes.tlvToBytes(trafficEngineeringSubTlv));
+            }
+        }
+        return Bytes.toArray(byteList);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .omitNullValues()
+                .add("neighborId", neighborId)
+                .add("metric", metric)
+                .add("teSubTlv", teSubTlv)
+                .toString();
+    }
+}
\ No newline at end of file
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/InterfaceIpAddress.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/InterfaceIpAddress.java
new file mode 100644
index 0000000..cc6174f
--- /dev/null
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/InterfaceIpAddress.java
@@ -0,0 +1,114 @@
+/*
+ * Copyright 2016-present Open Networking Laboratory
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.onosproject.isis.io.isispacket.tlv.subtlv;
+
+import com.google.common.base.MoreObjects;
+import com.google.common.primitives.Bytes;
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onlab.packet.Ip4Address;
+import org.onosproject.isis.io.isispacket.tlv.TlvHeader;
+import org.onosproject.isis.io.util.IsisUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.ArrayList;
+import java.util.List;
+
+/**
+ * Representation of interface ip address TE value.
+ */
+public class InterfaceIpAddress extends TlvHeader implements TrafficEngineeringSubTlv {
+    private static final Logger log =
+            LoggerFactory.getLogger(InterfaceIpAddress.class);
+    private List<Ip4Address> localInterfaceIPAddress = new ArrayList<>();
+
+    /**
+     * Creates an instance of local interface ip address.
+     *
+     * @param header tlv header instance
+     */
+    public InterfaceIpAddress(TlvHeader header) {
+        this.setTlvType(header.tlvType());
+        this.setTlvLength(header.tlvLength());
+    }
+
+    /**
+     * Adds local interface ip address.
+     *
+     * @param localAddress ip address
+     */
+    public void addLocalInterfaceIPAddress(Ip4Address localAddress) {
+        localInterfaceIPAddress.add(localAddress);
+    }
+
+    /**
+     * Returns local interface ip address.
+     *
+     * @return localAddress ip address
+     */
+    public List<Ip4Address> getLocalInterfaceIPAddress() {
+        return localInterfaceIPAddress;
+    }
+
+    /**
+     * Reads bytes from channel buffer.
+     *
+     * @param channelBuffer channel buffer instance
+     */
+    public void readFrom(ChannelBuffer channelBuffer) {
+        while (channelBuffer.readableBytes() >= IsisUtil.FOUR_BYTES) {
+            byte[] tempByteArray = new byte[IsisUtil.FOUR_BYTES];
+            channelBuffer.readBytes(tempByteArray, 0, IsisUtil.FOUR_BYTES);
+            this.addLocalInterfaceIPAddress(Ip4Address.valueOf(tempByteArray));
+        }
+    }
+
+    /**
+     * Returns local interface ip address as byte array.
+     *
+     * @return local interface ip address as byte array
+     */
+    public byte[] asBytes() {
+        byte[] linkSubType = null;
+        byte[] linkSubTlvHeader = tlvHeaderAsByteArray();
+        byte[] linkSubTlvBody = tlvBodyAsBytes();
+        linkSubType = Bytes.concat(linkSubTlvHeader, linkSubTlvBody);
+
+        return linkSubType;
+    }
+
+    /**
+     * Returns byte array of local interface ip address.
+     *
+     * @return byte array of local interface ip address
+     */
+    public byte[] tlvBodyAsBytes() {
+        List<Byte> linkSubTypeBody = new ArrayList<>();
+        for (Ip4Address remoteAddress : this.localInterfaceIPAddress) {
+            linkSubTypeBody.addAll(Bytes.asList(remoteAddress.toOctets()));
+        }
+
+        return Bytes.toArray(linkSubTypeBody);
+    }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(getClass())
+                .omitNullValues()
+                .add("localInterfaceIPAddress", localInterfaceIPAddress)
+                .toString();
+    }
+}
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvFinder.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvFinder.java
index 7958f53..92ea143 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvFinder.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvFinder.java
@@ -66,6 +66,11 @@
                 unreservedBandwidth.readFrom(channelBuffer);
                 subTlv = unreservedBandwidth;
                 break;
+            case INTERFACEADDRESS:
+                InterfaceIpAddress ipInterfaceAddressTlv = new InterfaceIpAddress(tlvHeader);
+                ipInterfaceAddressTlv.readFrom(channelBuffer);
+                subTlv = ipInterfaceAddressTlv;
+                break;
             default:
                 //TODO
                 break;
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvToBytes.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvToBytes.java
index 8774c04..8b41c4b 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvToBytes.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvToBytes.java
@@ -60,6 +60,9 @@
         } else if (subTlv instanceof UnreservedBandwidth) {
             UnreservedBandwidth unreservedBandwidth = (UnreservedBandwidth) subTlv;
             subTlvBytes.addAll(Bytes.asList(unreservedBandwidth.asBytes()));
+        } else if (subTlv instanceof InterfaceIpAddress) {
+            InterfaceIpAddress interfaceIpAddress = (InterfaceIpAddress) subTlv;
+            subTlvBytes.addAll(Bytes.asList(interfaceIpAddress.asBytes()));
         } else {
             log.debug("TlvsToBytes::UNKNOWN TLV TYPE ::TlvsToBytes ");
         }
diff --git a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvType.java b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvType.java
index 12c1127..5caecbf 100644
--- a/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvType.java
+++ b/protocols/isis/isisio/src/main/java/org/onosproject/isis/io/isispacket/tlv/subtlv/SubTlvType.java
@@ -23,11 +23,30 @@
  * Representation of sub tlv type.
  */
 public enum SubTlvType {
-    ADMINISTRATIVEGROUP(9),
-    MAXIMUMBANDWIDTH(6),
-    MAXIMUMRESERVABLEBANDWIDTH(7),
-    TRAFFICENGINEERINGMETRIC(5),
-    UNRESERVEDBANDWIDTH(8);
+    /**
+     * Represents traffic engineering administrative group TLV.
+     */
+    ADMINISTRATIVEGROUP(3),
+    /**
+     * Represents traffic engineering maximum bandwidth TLV.
+     */
+    MAXIMUMBANDWIDTH(9),
+    /**
+     * Represents traffic engineering maximum reservable bandwidth TLV.
+     */
+    MAXIMUMRESERVABLEBANDWIDTH(10),
+    /**
+     * Represents traffic engineering metric TLV.
+     */
+    TRAFFICENGINEERINGMETRIC(18),
+    /**
+     * Represents traffic engineering interface address TLV.
+     */
+    INTERFACEADDRESS(6),
+    /**
+     * Represents traffic engineering unreserved bandwidth TLV.
+     */
+    UNRESERVEDBANDWIDTH(11);
 
     // Reverse lookup table
     private static final Map<Integer, SubTlvType> LOOKUP = new HashMap<>();