BUG-338 Allow incomming BGP connections.
[bgpcep.git] / bgp / rib-impl-config / src / test / java / org / opendaylight / controller / config / yang / bgp / rib / impl / BGPPeerModuleTest.java
index 1f5ef4fd0d8b89359b8a497d538d5c54a74a65e7..3fa6b0057cc7705051911e3251d89816f8da30d4 100644 (file)
@@ -12,26 +12,29 @@ import static org.junit.Assert.fail;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
 
+import com.google.common.collect.Lists;
 import java.util.Collections;
 import java.util.List;
-
+import javax.management.InstanceAlreadyExistsException;
 import javax.management.ObjectName;
-
 import org.junit.Test;
+import org.opendaylight.bgpcep.tcpmd5.jni.NativeTestSupport;
 import org.opendaylight.controller.config.api.IdentityAttributeRef;
 import org.opendaylight.controller.config.api.ValidationException;
 import org.opendaylight.controller.config.api.jmx.CommitStatus;
 import org.opendaylight.controller.config.spi.ModuleFactory;
 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
+import org.opendaylight.controller.config.yang.tcpmd5.jni.cfg.NativeKeyAccessFactoryModuleFactory;
+import org.opendaylight.controller.config.yang.tcpmd5.netty.cfg.MD5ClientChannelFactoryModuleFactory;
+import org.opendaylight.controller.config.yang.tcpmd5.netty.cfg.MD5ClientChannelFactoryModuleMXBean;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.MplsLabeledVpnSubsequentAddressFamily;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.tcpmd5.cfg.rev140427.Rfc2385Key;
 import org.opendaylight.yangtools.yang.data.impl.codec.CodecRegistry;
 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
 
-import com.google.common.collect.Lists;
-
 public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
 
     private static final String INSTANCE_NAME = "bgp-peer-module-impl";
@@ -44,8 +47,7 @@ public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
     protected CodecRegistry getCodecRegistry() {
         IdentityCodec<?> idCodec = mock(IdentityCodec.class);
         doReturn(Ipv4AddressFamily.class).when(idCodec).deserialize(Ipv4AddressFamily.QNAME);
-        doReturn(MplsLabeledVpnSubsequentAddressFamily.class).when(idCodec).deserialize(
-                MplsLabeledVpnSubsequentAddressFamily.QNAME);
+        doReturn(MplsLabeledVpnSubsequentAddressFamily.class).when(idCodec).deserialize(MplsLabeledVpnSubsequentAddressFamily.QNAME);
 
         CodecRegistry codecReg = super.getCodecRegistry();
         doReturn(idCodec).when(codecReg).getIdentityCodec();
@@ -57,13 +59,16 @@ public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
         List<ModuleFactory> moduleFactories = super.getModuleFactories();
         moduleFactories.add(new BGPPeerModuleFactory());
         moduleFactories.add(new BGPTableTypeImplModuleFactory());
+        moduleFactories.add(new NativeKeyAccessFactoryModuleFactory());
+        moduleFactories.add(new MD5ClientChannelFactoryModuleFactory());
+        moduleFactories.add(new StrictBgpPeerRegistryModuleFactory());
         return moduleFactories;
     }
 
     @Test
     public void testValidationExceptionPortNotSet() throws Exception {
         try {
-            createBgpPeerInstance(HOST, null);
+            createBgpPeerInstance(HOST, null, false);
             fail();
         } catch (final ValidationException e) {
             assertTrue(e.getMessage().contains("Port value is not set."));
@@ -73,7 +78,7 @@ public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
     @Test
     public void testValidationExceptionHostNotSet() throws Exception {
         try {
-            createBgpPeerInstance(null, portNumber);
+            createBgpPeerInstance(null, portNumber, false);
             fail();
         } catch (final ValidationException e) {
             assertTrue(e.getMessage().contains("Host value is not set."));
@@ -84,7 +89,33 @@ public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
     public void testCreateBean() throws Exception {
         final CommitStatus status = createBgpPeerInstance();
         assertBeanCount(1, FACTORY_NAME);
-        assertStatus(status, 16, 0, 0);
+        assertStatus(status, 17, 0, 0);
+    }
+
+    @Test
+    public void testCreateBeanWithMD5() throws Exception {
+        NativeTestSupport.assumeSupportedPlatform();
+        final CommitStatus status = createBgpPeerInstance(true);
+        assertBeanCount(1, FACTORY_NAME);
+        assertStatus(status, 19, 0, 0);
+    }
+
+    @Test
+    public void testMD5ValidationFailure() throws Exception {
+        NativeTestSupport.assumeSupportedPlatform();
+        createBgpPeerInstance(true);
+        // now remove md5 from dispatcher
+        ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
+        final ObjectName nameCreated = transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME);
+        final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
+        BGPDispatcherImplModuleMXBean bgpDispatcherImplModuleMXBean = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
+        bgpDispatcherImplModuleMXBean.setMd5ChannelFactory(null);
+        try {
+            transaction.validateConfig();
+            fail();
+        } catch (ValidationException e) {
+            assertTrue(e.getMessage(), e.getMessage().contains("Underlying dispatcher does not support MD5 clients"));
+        }
     }
 
     @Test
@@ -94,7 +125,7 @@ public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
         assertBeanCount(1, FACTORY_NAME);
         status = transaction.commit();
         assertBeanCount(1, FACTORY_NAME);
-        assertStatus(status, 0, 0, 16);
+        assertStatus(status, 0, 0, 17);
     }
 
     @Test
@@ -102,39 +133,75 @@ public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
         CommitStatus status = createBgpPeerInstance();
         ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
         assertBeanCount(1, FACTORY_NAME);
-        final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(
-                transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME), BGPPeerModuleMXBean.class);
+        final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
+                BGPPeerModuleMXBean.class);
         mxBean.setPort(new PortNumber(10));
         status = transaction.commit();
         assertBeanCount(1, FACTORY_NAME);
-        assertStatus(status, 0, 1, 15);
+        assertStatus(status, 0, 1, 16);
     }
 
     private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final String host,
-            final PortNumber port) throws Exception {
+                                             final PortNumber port, boolean md5)
+            throws Exception {
         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
 
-        // FIXME JMX crashes if union was not created via artificial constructor
+        mxBean.setPeerRegistry(createPeerRegistry(transaction));
+
+        // FIXME JMX crashes if union was not created via artificial constructor - Bug:1276
         // annotated for JMX as value
         // IpAddress host1 = new IpAddress(new Ipv4Address(host));
         mxBean.setHost(host == null ? null : new IpAddress(host.toCharArray()));
         mxBean.setPort(port);
         mxBean.setAdvertizedTable(Collections.<ObjectName> emptyList());
-        mxBean.setRib(createRIBImplModuleInstance(transaction));
+        {
+            ObjectName ribON = createRIBImplModuleInstance(transaction);
+            mxBean.setRib(ribON);
+        }
+        if (md5) {
+            BGPDispatcherImplModuleMXBean bgpDispatcherProxy = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
+            ObjectName jniON = transaction.createModule(NativeKeyAccessFactoryModuleFactory.NAME, NativeKeyAccessFactoryModuleFactory.NAME);
+            ObjectName md5ClientON = transaction.createModule(MD5ClientChannelFactoryModuleFactory.NAME,
+                    MD5ClientChannelFactoryModuleFactory.NAME);
+            MD5ClientChannelFactoryModuleMXBean md5ClientProxy =
+                    transaction.newMXBeanProxy(md5ClientON, MD5ClientChannelFactoryModuleMXBean.class);
+            md5ClientProxy.setKeyAccessFactory(jniON);
+
+            bgpDispatcherProxy.setMd5ChannelFactory(md5ClientON);
+
+            mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
+
+        }
+
         mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
-                new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()), new IdentityAttributeRef(
-                        MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
+                new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
+                new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
         return nameCreated;
     }
 
+    private ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
+        return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
+    }
+
+    private BGPDispatcherImplModuleMXBean getBgpDispatcherImplModuleMXBean(ConfigTransactionJMXClient transaction,
+                                                                           BGPPeerModuleMXBean mxBean) {
+        RIBImplModuleMXBean ribProxy = transaction.newMXBeanProxy(mxBean.getRib(), RIBImplModuleMXBean.class);
+        ObjectName dispatcherON = ribProxy.getBgpDispatcher();
+        return transaction.newMXBeanProxy(dispatcherON, BGPDispatcherImplModuleMXBean.class);
+    }
+
     private CommitStatus createBgpPeerInstance() throws Exception {
-        return createBgpPeerInstance(HOST, portNumber);
+        return createBgpPeerInstance(false);
+    }
+
+    private CommitStatus createBgpPeerInstance(boolean md5) throws Exception {
+        return createBgpPeerInstance(HOST, portNumber, md5);
     }
 
-    private CommitStatus createBgpPeerInstance(final String host, final PortNumber port) throws Exception {
+    private CommitStatus createBgpPeerInstance(final String host, final PortNumber port, boolean md5) throws Exception {
         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
-        createBgpPeerInstance(transaction, host, port);
+        createBgpPeerInstance(transaction, host, port, md5);
         return transaction.commit();
     }
 }