BGP Monitoring Api

Add Junit
Add Interface

Change-Id: I86ac6cb366c324472c1aebb2d05e1901813916a2
diff --git a/apps/bgpmonitoring/api/src/main/java/org/onosproject/bgpmonitoring/BmpMessage.java b/apps/bgpmonitoring/api/src/main/java/org/onosproject/bgpmonitoring/BmpMessage.java
new file mode 100644
index 0000000..4508f0e
--- /dev/null
+++ b/apps/bgpmonitoring/api/src/main/java/org/onosproject/bgpmonitoring/BmpMessage.java
@@ -0,0 +1,66 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * 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.bgpmonitoring;
+
+import org.onlab.packet.bmp.Bmp;
+import org.onlab.packet.bmp.BmpPeer;
+
+/**
+ * Abstraction of an entity providing BMP Messages.
+ */
+public interface BmpMessage {
+
+    /**
+     * Returns BMP Header of BMP Message.
+     *
+     * @return BMP Header of BMP Message
+     */
+    Bmp getHeader();
+
+    /**
+     * Returns BMP Peer Header of BMP Message.
+     *
+     * @return BMP Peer Header of BMP Message
+     */
+    default BmpPeer getPeerHeader() {
+        throw new UnsupportedOperationException("Bmp Per Peer Header not supported");
+    }
+
+    /**
+     * Is bmp message has peer header.
+     *
+     * @return true if bmp has peer header otherwise false
+     */
+    default boolean hasPeerHeader() {
+        return true;
+    }
+
+    /**
+     * Returns version of BMP Message.
+     *
+     * @return version of BMP Message
+     */
+    default BmpVersion getVersion() {
+        return BmpVersion.BMP_3;
+    }
+
+    /**
+     * Returns BMP Type of BMP Message.
+     *
+     * @return BMP Type of BMP Message
+     */
+    BmpType getType();
+}
diff --git a/apps/bgpmonitoring/api/src/main/java/org/onosproject/bgpmonitoring/BmpMessageReader.java b/apps/bgpmonitoring/api/src/main/java/org/onosproject/bgpmonitoring/BmpMessageReader.java
new file mode 100644
index 0000000..56362ea
--- /dev/null
+++ b/apps/bgpmonitoring/api/src/main/java/org/onosproject/bgpmonitoring/BmpMessageReader.java
@@ -0,0 +1,35 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * 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.bgpmonitoring;
+
+import org.jboss.netty.buffer.ChannelBuffer;
+import org.onlab.packet.bmp.Bmp;
+
+/**
+ * Abstraction of an entity providing BMP Message Reader.
+ */
+public interface BmpMessageReader<T> {
+
+    /**
+     * Reads the Objects in the BMP Message and Returns BMP Message.
+     *
+     * @param cb        Channel Buffer
+     * @param bmpHeader BMP message header
+     * @return BMP Message
+     * @throws BmpParseException on while parsing BMP message.
+     */
+    T readFrom(ChannelBuffer cb, Bmp bmpHeader) throws BmpParseException;
+}
diff --git a/utils/misc/src/main/java/org/onlab/packet/bmp/Bmp.java b/utils/misc/src/main/java/org/onlab/packet/bmp/Bmp.java
index b64e34b..6721f4d 100644
--- a/utils/misc/src/main/java/org/onlab/packet/bmp/Bmp.java
+++ b/utils/misc/src/main/java/org/onlab/packet/bmp/Bmp.java
@@ -21,6 +21,7 @@
 import org.onlab.packet.Deserializer;
 
 import java.nio.ByteBuffer;
+import java.util.Objects;
 
 import static org.onlab.packet.PacketUtils.checkInput;
 
@@ -135,8 +136,8 @@
         final ByteBuffer bb = ByteBuffer.wrap(data);
 
         bb.put(this.version);
-        bb.put(this.type);
         bb.putInt(this.length);
+        bb.put(this.type);
 
         return data;
     }
@@ -170,5 +171,34 @@
                 .add("length", length)
                 .toString();
     }
+
+    @Override
+    public int hashCode() {
+        return Objects.hash(super.hashCode(), version, type, length);
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (!(obj instanceof Bmp)) {
+            return false;
+        }
+        final Bmp other = (Bmp) obj;
+        if (this.version != other.version) {
+            return false;
+        }
+        if (this.type != other.type) {
+            return false;
+        }
+        if (this.length != other.length) {
+            return false;
+        }
+        return true;
+    }
 }
 
diff --git a/utils/misc/src/main/java/org/onlab/packet/bmp/BmpPeer.java b/utils/misc/src/main/java/org/onlab/packet/bmp/BmpPeer.java
index 50c5dac..cff7fdc 100644
--- a/utils/misc/src/main/java/org/onlab/packet/bmp/BmpPeer.java
+++ b/utils/misc/src/main/java/org/onlab/packet/bmp/BmpPeer.java
@@ -24,6 +24,8 @@
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Objects;
 
 import static org.onlab.packet.PacketUtils.checkInput;
 import static org.slf4j.LoggerFactory.getLogger;
@@ -160,9 +162,9 @@
 
     protected int peerBgpId;
 
-    protected long seconds;
+    protected int seconds;
 
-    protected long microseconds;
+    protected int microseconds;
 
     private static Logger log = getLogger(BmpPeer.class);
 
@@ -226,7 +228,7 @@
      *
      * @return the timestamp in sec
      */
-    public long getSeconds() {
+    public int getSeconds() {
         return seconds;
     }
 
@@ -235,7 +237,7 @@
      *
      * @return the timestamp in micro second
      */
-    public long getMicroseconds() {
+    public int getMicroseconds() {
         return microseconds;
     }
 
@@ -251,8 +253,8 @@
         bb.put(this.peerAddress.getAddress());
         bb.putInt(this.peerAs);
         bb.putInt(this.peerBgpId);
-        bb.putLong(this.seconds);
-        bb.putLong(this.microseconds);
+        bb.putInt(this.seconds);
+        bb.putInt(this.microseconds);
 
         return data;
     }
@@ -304,11 +306,58 @@
         return MoreObjects.toStringHelper(getClass())
                 .add("flags", flags)
                 .add("type", type)
+                .add("peerDistinguisher", Arrays.toString(peerDistinguisher))
                 .add("peerAddress", peerAddress.getHostAddress())
                 .add("peerAs", peerAs)
+                .add("peerBgpId", peerBgpId)
                 .add("seconds", seconds)
                 .add("microseconds", microseconds)
                 .toString();
     }
 
+    @Override
+    public int hashCode() {
+        return Objects.hash(super.hashCode(), flags, type, peerAddress,
+                Arrays.hashCode(peerDistinguisher), peerAs, peerBgpId, seconds, microseconds);
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (!(obj instanceof BmpPeer)) {
+            return false;
+        }
+        final BmpPeer other = (BmpPeer) obj;
+        if (this.type != other.type) {
+            return false;
+        }
+        if (this.flags != other.flags) {
+            return false;
+        }
+        if (!Arrays.equals(this.peerDistinguisher, other.peerDistinguisher)) {
+            return false;
+        }
+        if (this.peerAddress != other.peerAddress) {
+            return false;
+        }
+        if (this.peerAs != other.peerAs) {
+            return false;
+        }
+        if (this.peerBgpId != other.peerBgpId) {
+            return false;
+        }
+        if (this.seconds != other.seconds) {
+            return false;
+        }
+        if (this.microseconds != other.microseconds) {
+            return false;
+        }
+        return true;
+    }
+
 }
diff --git a/utils/misc/src/test/java/org/onlab/packet/bmp/BmpPeerTest.java b/utils/misc/src/test/java/org/onlab/packet/bmp/BmpPeerTest.java
new file mode 100644
index 0000000..4c73dc9
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/bmp/BmpPeerTest.java
@@ -0,0 +1,132 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * 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.onlab.packet.bmp;
+
+import com.google.common.testing.EqualsTester;
+import org.apache.commons.lang.StringUtils;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.PacketTestUtils;
+
+import java.net.InetAddress;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit test for BmpPeer class.
+ */
+public class BmpPeerTest {
+    private Deserializer<BmpPeer> deserializer;
+
+    private byte type = 0;
+    private byte flags = 0;
+    private byte[] peerDistinguisher = new byte[BmpPeer.PEER_DISTINGUISHER];
+    private InetAddress peerAddress;
+    private int peerAs = 60300;
+    private int peerBgpId = 65323;
+    private int seconds = 1024;
+    private int microseconds = 0;
+
+    private byte[] headerBytes;
+
+
+    @Before
+    public void setUp() throws Exception {
+        deserializer = BmpPeer.deserializer();
+        peerAddress = InetAddress.getByAddress(new byte[]{127, 0, 0, 1});
+
+        ByteBuffer bb = ByteBuffer.allocate(BmpPeer.PEER_HEADER_MINIMUM_LENGTH);
+
+        bb.put(this.type);
+        bb.put(this.flags);
+        bb.put(this.peerDistinguisher);
+        bb.put(new byte[]{0, 0, 0, 0,
+                0, 0, 0, 0,
+                0, 0, 0, 0,
+                127, 0, 0, 1});
+        bb.putInt(this.peerAs);
+        bb.putInt(this.peerBgpId);
+        bb.putInt(this.seconds);
+        bb.putInt(this.microseconds);
+
+        headerBytes = bb.array();
+    }
+
+    @Test
+    public void testDeserializeBadInput() throws Exception {
+        PacketTestUtils.testDeserializeBadInput(deserializer);
+    }
+
+    @Test
+    public void testDeserializeTruncated() throws Exception {
+        PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes);
+    }
+
+    /**
+     * Test Deserialize and getters.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testDeserialize() throws Exception {
+        BmpPeer bmpPeer = deserializer.deserialize(headerBytes, 0, headerBytes.length);
+
+        assertEquals(type, bmpPeer.getType());
+        assertEquals(flags, bmpPeer.getFlag());
+        assertEquals(peerDistinguisher.length, bmpPeer.getPeerDistinguisher().length);
+        assertEquals(peerAddress.getHostAddress(), bmpPeer.getIntAddress().getHostAddress());
+        assertEquals(peerAs, bmpPeer.getPeerAs());
+        assertEquals(peerBgpId, bmpPeer.getPeerBgpId());
+        assertEquals(seconds, bmpPeer.getSeconds());
+        assertEquals(microseconds, bmpPeer.getMicroseconds());
+    }
+
+    /**
+     * Tests toString.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testToStringBmp() throws Exception {
+        BmpPeer bmpPeer = deserializer.deserialize(headerBytes, 0, headerBytes.length);
+        String str = bmpPeer.toString();
+
+        assertTrue(StringUtils.contains(str, "flags=" + flags));
+        assertTrue(StringUtils.contains(str, "type=" + type));
+        assertTrue(StringUtils.contains(str, "peerDistinguisher=" + Arrays.toString(peerDistinguisher)));
+        assertTrue(StringUtils.contains(str, "peerAddress=" + peerAddress.getHostAddress()));
+        assertTrue(StringUtils.contains(str, "peerAs=" + peerAs));
+        assertTrue(StringUtils.contains(str, "peerBgpId=" + peerBgpId));
+        assertTrue(StringUtils.contains(str, "seconds=" + seconds));
+        assertTrue(StringUtils.contains(str, "microseconds=" + microseconds));
+    }
+
+    /**
+     * Tests equals method.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testEquality() throws Exception {
+        BmpPeer bmpPeer = deserializer.deserialize(headerBytes, 0, headerBytes.length);
+        new EqualsTester()
+                .addEqualityGroup(bmpPeer).testEquals();
+    }
+}
diff --git a/utils/misc/src/test/java/org/onlab/packet/bmp/BmpTest.java b/utils/misc/src/test/java/org/onlab/packet/bmp/BmpTest.java
new file mode 100644
index 0000000..d3eca29
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/bmp/BmpTest.java
@@ -0,0 +1,103 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * 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.onlab.packet.bmp;
+
+import com.google.common.testing.EqualsTester;
+import org.apache.commons.lang3.StringUtils;
+import org.junit.Before;
+import org.junit.Test;
+import org.onlab.packet.Deserializer;
+import org.onlab.packet.PacketTestUtils;
+
+import java.nio.ByteBuffer;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * Unit test for Bmp class.
+ */
+public class BmpTest {
+    private Deserializer<Bmp> deserializer;
+
+    private byte version = 3;
+    private int length = 6;
+    private byte type = 1;
+
+    private byte[] headerBytes;
+
+    @Before
+    public void setUp() throws Exception {
+        deserializer = Bmp.deserializer();
+        ByteBuffer bb = ByteBuffer.allocate(Bmp.DEFAULT_HEADER_LENGTH);
+
+        bb.put(version);
+        bb.putInt(length);
+        bb.put(type);
+
+        headerBytes = bb.array();
+    }
+
+    @Test
+    public void testDeserializeBadInput() throws Exception {
+        PacketTestUtils.testDeserializeBadInput(deserializer);
+    }
+
+    @Test
+    public void testDeserializeTruncated() throws Exception {
+        PacketTestUtils.testDeserializeTruncated(deserializer, headerBytes);
+    }
+
+    /**
+     * Test Deserialize and getters.
+     */
+    @Test
+    public void testDeserialize() throws Exception {
+        Bmp bmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
+
+        assertEquals(version, bmp.getVersion());
+        assertEquals(type, bmp.getType());
+        assertEquals(length, bmp.getLength());
+    }
+
+    /**
+     * Tests toString.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testToStringBmp() throws Exception {
+        Bmp bmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
+        String str = bmp.toString();
+
+        assertTrue(StringUtils.contains(str, "version=" + version));
+        assertTrue(StringUtils.contains(str, "type=" + type));
+        assertTrue(StringUtils.contains(str, "length=" + length));
+    }
+
+    /**
+     * Tests equals method.
+     *
+     * @throws Exception
+     */
+    @Test
+    public void testEquality() throws Exception {
+        Bmp bmp = deserializer.deserialize(headerBytes, 0, headerBytes.length);
+        new EqualsTester()
+                .addEqualityGroup(bmp).testEquals();
+    }
+
+}
diff --git a/utils/misc/src/test/java/org/onlab/packet/bmp/package-info.java b/utils/misc/src/test/java/org/onlab/packet/bmp/package-info.java
new file mode 100644
index 0000000..7df018f
--- /dev/null
+++ b/utils/misc/src/test/java/org/onlab/packet/bmp/package-info.java
@@ -0,0 +1,20 @@
+/*
+ * Copyright 2021-present Open Networking Foundation
+ *
+ * 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.
+ */
+
+/**
+ * BMP Packet test.
+ */
+package org.onlab.packet.bmp;
\ No newline at end of file