ONOS-734 Add unit tests for 4 Octets AS numbers in SDN-IP

 * Fix a bug in the storing, handling and verification of the AS numbers
   with 4 octet AS capability is used.

 * Add an unit test to test the decoding and parsing of supported
   BGP Capabilities: Multiprotocol Extensions AFI/SAFI, and 4 octet AS.

 * Minor refactoring of the BGP unit test framework.

Change-Id: I474b356bc00369c307ac0c5c214b065c1cc0c52c
diff --git a/apps/sdnip/src/test/java/org/onosproject/sdnip/bgp/BgpSessionManagerTest.java b/apps/sdnip/src/test/java/org/onosproject/sdnip/bgp/BgpSessionManagerTest.java
index 11a8b5e..45ee321 100644
--- a/apps/sdnip/src/test/java/org/onosproject/sdnip/bgp/BgpSessionManagerTest.java
+++ b/apps/sdnip/src/test/java/org/onosproject/sdnip/bgp/BgpSessionManagerTest.java
@@ -85,9 +85,10 @@
     private BgpSessionManager bgpSessionManager;
 
     // Remote Peer state
-    TestBgpPeer peer1 = new TestBgpPeer(BGP_PEER1_ID);
-    TestBgpPeer peer2 = new TestBgpPeer(BGP_PEER2_ID);
-    TestBgpPeer peer3 = new TestBgpPeer(BGP_PEER3_ID);
+    private final Collection<TestBgpPeer> peers = new LinkedList<>();
+    TestBgpPeer peer1;
+    TestBgpPeer peer2;
+    TestBgpPeer peer3;
 
     // Local BGP per-peer session state
     BgpSession bgpSession1;
@@ -238,6 +239,14 @@
 
     @Before
     public void setUp() throws Exception {
+        peer1 = new TestBgpPeer(BGP_PEER1_ID);
+        peer2 = new TestBgpPeer(BGP_PEER2_ID);
+        peer3 = new TestBgpPeer(BGP_PEER3_ID);
+        peers.clear();
+        peers.add(peer1);
+        peers.add(peer2);
+        peers.add(peer3);
+
         //
         // Setup the BGP Session Manager to test, and start listening for BGP
         // connections.
@@ -366,24 +375,14 @@
         // Test the fields from the BGP OPEN message:
         // BGP version, AS number, BGP ID
         //
-        assertThat(peer1.peerFrameDecoder.remoteBgpVersion,
-                   is(BgpConstants.BGP_VERSION));
-        assertThat(peer1.peerFrameDecoder.remoteAs,
-                   is(TestBgpPeerChannelHandler.PEER_AS));
-        assertThat(peer1.peerFrameDecoder.remoteBgpIdentifier,
-                   is(IP_LOOPBACK_ID));
-        assertThat(peer2.peerFrameDecoder.remoteBgpVersion,
-                   is(BgpConstants.BGP_VERSION));
-        assertThat(peer2.peerFrameDecoder.remoteAs,
-                   is(TestBgpPeerChannelHandler.PEER_AS));
-        assertThat(peer2.peerFrameDecoder.remoteBgpIdentifier,
-                   is(IP_LOOPBACK_ID));
-        assertThat(peer3.peerFrameDecoder.remoteBgpVersion,
-                   is(BgpConstants.BGP_VERSION));
-        assertThat(peer3.peerFrameDecoder.remoteAs,
-                   is(TestBgpPeerChannelHandler.PEER_AS));
-        assertThat(peer3.peerFrameDecoder.remoteBgpIdentifier,
-                   is(IP_LOOPBACK_ID));
+        for (TestBgpPeer peer : peers) {
+            assertThat(peer.peerFrameDecoder.remoteInfo.bgpVersion(),
+                       is(BgpConstants.BGP_VERSION));
+            assertThat(peer.peerFrameDecoder.remoteInfo.bgpId(),
+                       is(IP_LOOPBACK_ID));
+            assertThat(peer.peerFrameDecoder.remoteInfo.asNumber(),
+                       is(TestBgpPeerChannelHandler.PEER_AS));
+        }
 
         //
         // Test that the BgpSession instances have been created
@@ -399,6 +398,72 @@
         }
     }
 
+
+    /**
+     * Tests that the BGP OPEN with Capability messages have been exchanged,
+     * followed by KEEPALIVE.
+     * <p>
+     * The BGP Peer opens the sessions and transmits OPEN Message, eventually
+     * followed by KEEPALIVE. The tested BGP listener should respond by
+     * OPEN Message, followed by KEEPALIVE.
+     *
+     * @throws TestUtilsException TestUtils error
+     */
+    @Test
+    public void testExchangedBgpOpenCapabilityMessages()
+            throws InterruptedException, TestUtilsException {
+        //
+        // Setup the BGP Capabilities for all peers
+        //
+        for (TestBgpPeer peer : peers) {
+            peer.peerChannelHandler.localInfo.setIpv4Unicast();
+            peer.peerChannelHandler.localInfo.setIpv4Multicast();
+            peer.peerChannelHandler.localInfo.setIpv6Unicast();
+            peer.peerChannelHandler.localInfo.setIpv6Multicast();
+            peer.peerChannelHandler.localInfo.setAs4OctetCapability();
+            peer.peerChannelHandler.localInfo.setAs4Number(
+                TestBgpPeerChannelHandler.PEER_AS4);
+        }
+
+        // Initiate the connections
+        peer1.connect(connectToSocket);
+        peer2.connect(connectToSocket);
+        peer3.connect(connectToSocket);
+
+        //
+        // Test the fields from the BGP OPEN message:
+        // BGP version, BGP ID
+        //
+        for (TestBgpPeer peer : peers) {
+            assertThat(peer.peerFrameDecoder.remoteInfo.bgpVersion(),
+                       is(BgpConstants.BGP_VERSION));
+            assertThat(peer.peerFrameDecoder.remoteInfo.bgpId(),
+                       is(IP_LOOPBACK_ID));
+        }
+
+        //
+        // Test that the BgpSession instances have been created,
+        // and contain the appropriate BGP session information.
+        //
+        assertThat(bgpSessionManager.getMyBgpId(), is(IP_LOOPBACK_ID));
+        assertThat(bgpSessionManager.getBgpSessions(), hasSize(3));
+        assertThat(bgpSession1, notNullValue());
+        assertThat(bgpSession2, notNullValue());
+        assertThat(bgpSession3, notNullValue());
+        for (BgpSession bgpSession : bgpSessionManager.getBgpSessions()) {
+            BgpSessionInfo localInfo = bgpSession.localInfo();
+            assertThat(localInfo.ipv4Unicast(), is(true));
+            assertThat(localInfo.ipv4Multicast(), is(true));
+            assertThat(localInfo.ipv6Unicast(), is(true));
+            assertThat(localInfo.ipv6Multicast(), is(true));
+            assertThat(localInfo.as4OctetCapability(), is(true));
+            assertThat(localInfo.asNumber(),
+                       is(TestBgpPeerChannelHandler.PEER_AS4));
+            assertThat(localInfo.as4Number(),
+                       is(TestBgpPeerChannelHandler.PEER_AS4));
+        }
+    }
+
     /**
      * Tests that the BGP UPDATE messages have been received and processed.
      */