Migrate AbstractConfigTest subclasses from CodecRegistry
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / controller / config / yang / bgp / rib / impl / BGPPeerModuleTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.config.yang.bgp.rib.impl;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.doReturn;
13
14 import com.google.common.collect.Lists;
15 import java.util.Collections;
16 import java.util.List;
17 import javax.management.InstanceAlreadyExistsException;
18 import javax.management.ObjectName;
19 import org.junit.Test;
20 import org.opendaylight.controller.config.api.IdentityAttributeRef;
21 import org.opendaylight.controller.config.api.ValidationException;
22 import org.opendaylight.controller.config.api.jmx.CommitStatus;
23 import org.opendaylight.controller.config.spi.ModuleFactory;
24 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
25 import org.opendaylight.controller.config.yang.tcpmd5.jni.cfg.NativeKeyAccessFactoryModuleFactory;
26 import org.opendaylight.controller.config.yang.tcpmd5.netty.cfg.MD5ClientChannelFactoryModuleFactory;
27 import org.opendaylight.controller.config.yang.tcpmd5.netty.cfg.MD5ClientChannelFactoryModuleMXBean;
28 import org.opendaylight.tcpmd5.jni.NativeTestSupport;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.MplsLabeledVpnSubsequentAddressFamily;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.tcpmd5.cfg.rev140427.Rfc2385Key;
34 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
35
36 public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
37
38     private static final String INSTANCE_NAME = "bgp-peer-module-impl";
39     private static final String FACTORY_NAME = BGPPeerModuleFactory.NAME;
40
41     private static final String HOST = "127.0.0.1";
42     private static final PortNumber portNumber = new PortNumber(1);
43
44     @Override
45     protected BindingRuntimeContext getBindingRuntimeContext() {
46         final BindingRuntimeContext ret = super.getBindingRuntimeContext();
47         doReturn(Ipv4AddressFamily.class).when(ret).getIdentityClass(Ipv4AddressFamily.QNAME);
48         doReturn(MplsLabeledVpnSubsequentAddressFamily.class).when(ret).getIdentityClass(MplsLabeledVpnSubsequentAddressFamily.QNAME);
49         return ret;
50     }
51
52     @Override
53     protected List<ModuleFactory> getModuleFactories() {
54         final List<ModuleFactory> moduleFactories = super.getModuleFactories();
55         moduleFactories.add(new BGPPeerModuleFactory());
56         moduleFactories.add(new BGPTableTypeImplModuleFactory());
57         moduleFactories.add(new NativeKeyAccessFactoryModuleFactory());
58         moduleFactories.add(new MD5ClientChannelFactoryModuleFactory());
59         moduleFactories.add(new StrictBgpPeerRegistryModuleFactory());
60         return moduleFactories;
61     }
62
63     @Test
64     public void testValidationExceptionPortNotSet() throws Exception {
65         try {
66             createBgpPeerInstance(HOST, null, false);
67             fail();
68         } catch (final ValidationException e) {
69             assertTrue(e.getMessage().contains("Port value is not set."));
70         }
71     }
72
73     @Test
74     public void testValidationExceptionHostNotSet() throws Exception {
75         try {
76             createBgpPeerInstance(null, portNumber, false);
77             fail();
78         } catch (final ValidationException e) {
79             assertTrue(e.getMessage().contains("Host value is not set."));
80         }
81     }
82
83     @Test
84     public void testCreateBean() throws Exception {
85         final CommitStatus status = createBgpPeerInstance();
86         assertBeanCount(1, FACTORY_NAME);
87         assertStatus(status, 16, 0, 0);
88     }
89
90     @Test
91     public void testCreateBeanWithMD5() throws Exception {
92         NativeTestSupport.assumeSupportedPlatform();
93         final CommitStatus status = createBgpPeerInstance(true);
94         assertBeanCount(1, FACTORY_NAME);
95         assertStatus(status, 18, 0, 0);
96     }
97
98     @Test
99     public void testMD5ValidationFailure() throws Exception {
100         NativeTestSupport.assumeSupportedPlatform();
101         createBgpPeerInstance(true);
102         // now remove md5 from dispatcher
103         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
104         final ObjectName nameCreated = transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME);
105         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
106         final BGPDispatcherImplModuleMXBean bgpDispatcherImplModuleMXBean = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
107         bgpDispatcherImplModuleMXBean.setMd5ChannelFactory(null);
108         try {
109             transaction.validateConfig();
110             fail();
111         } catch (final ValidationException e) {
112             assertTrue(e.getMessage(), e.getMessage().contains("Underlying dispatcher does not support MD5 clients"));
113         }
114     }
115
116     @Test
117     public void testReusingOldInstance() throws Exception {
118         CommitStatus status = createBgpPeerInstance();
119         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
120         assertBeanCount(1, FACTORY_NAME);
121         status = transaction.commit();
122         assertBeanCount(1, FACTORY_NAME);
123         assertStatus(status, 0, 0, 16);
124     }
125
126     @Test
127     public void testReconfigure() throws Exception {
128         CommitStatus status = createBgpPeerInstance();
129         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
130         assertBeanCount(1, FACTORY_NAME);
131         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
132                 BGPPeerModuleMXBean.class);
133         mxBean.setPort(new PortNumber(10));
134         status = transaction.commit();
135         assertBeanCount(1, FACTORY_NAME);
136         assertStatus(status, 0, 1, 15);
137     }
138
139     private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final String host,
140             final PortNumber port, final boolean md5) throws Exception {
141         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
142         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
143
144         mxBean.setPeerRegistry(createPeerRegistry(transaction));
145
146         // FIXME JMX crashes if union was not created via artificial constructor - Bug:1276
147         // annotated for JMX as value
148         // IpAddress host1 = new IpAddress(new Ipv4Address(host));
149         mxBean.setHost(host == null ? null : new IpAddress(host.toCharArray()));
150         mxBean.setPort(port);
151         mxBean.setAdvertizedTable(Collections.<ObjectName> emptyList());
152         {
153             final ObjectName ribON = createRIBImplModuleInstance(transaction);
154             mxBean.setRib(ribON);
155         }
156         if (md5) {
157             final BGPDispatcherImplModuleMXBean bgpDispatcherProxy = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
158             final ObjectName jniON = transaction.createModule(NativeKeyAccessFactoryModuleFactory.NAME, NativeKeyAccessFactoryModuleFactory.NAME);
159             final ObjectName md5ClientON = transaction.createModule(MD5ClientChannelFactoryModuleFactory.NAME,
160                     MD5ClientChannelFactoryModuleFactory.NAME);
161             final MD5ClientChannelFactoryModuleMXBean md5ClientProxy =
162                     transaction.newMXBeanProxy(md5ClientON, MD5ClientChannelFactoryModuleMXBean.class);
163             md5ClientProxy.setKeyAccessFactory(jniON);
164
165             bgpDispatcherProxy.setMd5ChannelFactory(md5ClientON);
166
167             mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
168
169         }
170
171         mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
172                 new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
173                 new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
174         return nameCreated;
175     }
176
177     private ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
178         return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
179     }
180
181     private BGPDispatcherImplModuleMXBean getBgpDispatcherImplModuleMXBean(final ConfigTransactionJMXClient transaction,
182             final BGPPeerModuleMXBean mxBean) {
183         final RIBImplModuleMXBean ribProxy = transaction.newMXBeanProxy(mxBean.getRib(), RIBImplModuleMXBean.class);
184         final ObjectName dispatcherON = ribProxy.getBgpDispatcher();
185         return transaction.newMXBeanProxy(dispatcherON, BGPDispatcherImplModuleMXBean.class);
186     }
187
188     private CommitStatus createBgpPeerInstance() throws Exception {
189         return createBgpPeerInstance(false);
190     }
191
192     private CommitStatus createBgpPeerInstance(final boolean md5) throws Exception {
193         return createBgpPeerInstance(HOST, portNumber, md5);
194     }
195
196     private CommitStatus createBgpPeerInstance(final String host, final PortNumber port, final boolean md5) throws Exception {
197         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
198         createBgpPeerInstance(transaction, host, port, md5);
199         return transaction.commit();
200     }
201 }