Merge "BGPCEP/concepts - get rid of util dependency"
[bgpcep.git] / bgp / rib-impl-config / 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 import static org.mockito.Mockito.mock;
14
15 import com.google.common.collect.Lists;
16 import java.util.Collections;
17 import java.util.List;
18 import javax.management.ObjectName;
19 import org.junit.Test;
20 import org.opendaylight.bgpcep.tcpmd5.jni.NativeTestSupport;
21 import org.opendaylight.controller.config.api.IdentityAttributeRef;
22 import org.opendaylight.controller.config.api.ValidationException;
23 import org.opendaylight.controller.config.api.jmx.CommitStatus;
24 import org.opendaylight.controller.config.spi.ModuleFactory;
25 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
26 import org.opendaylight.controller.config.yang.tcpmd5.jni.cfg.NativeKeyAccessFactoryModuleFactory;
27 import org.opendaylight.controller.config.yang.tcpmd5.netty.cfg.MD5ClientChannelFactoryModuleFactory;
28 import org.opendaylight.controller.config.yang.tcpmd5.netty.cfg.MD5ClientChannelFactoryModuleMXBean;
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.yang.data.impl.codec.CodecRegistry;
35 import org.opendaylight.yangtools.yang.data.impl.codec.IdentityCodec;
36
37 public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
38
39     private static final String INSTANCE_NAME = "bgp-peer-module-impl";
40     private static final String FACTORY_NAME = BGPPeerModuleFactory.NAME;
41
42     private static final String HOST = "127.0.0.1";
43     private static final PortNumber portNumber = new PortNumber(1);
44
45     @Override
46     protected CodecRegistry getCodecRegistry() {
47         IdentityCodec<?> idCodec = mock(IdentityCodec.class);
48         doReturn(Ipv4AddressFamily.class).when(idCodec).deserialize(Ipv4AddressFamily.QNAME);
49         doReturn(MplsLabeledVpnSubsequentAddressFamily.class).when(idCodec).deserialize(MplsLabeledVpnSubsequentAddressFamily.QNAME);
50
51         CodecRegistry codecReg = super.getCodecRegistry();
52         doReturn(idCodec).when(codecReg).getIdentityCodec();
53         return codecReg;
54     }
55
56     @Override
57     protected List<ModuleFactory> getModuleFactories() {
58         List<ModuleFactory> moduleFactories = super.getModuleFactories();
59         moduleFactories.add(new BGPPeerModuleFactory());
60         moduleFactories.add(new BGPTableTypeImplModuleFactory());
61         moduleFactories.add(new NativeKeyAccessFactoryModuleFactory());
62         moduleFactories.add(new MD5ClientChannelFactoryModuleFactory());
63         return moduleFactories;
64     }
65
66     @Test
67     public void testValidationExceptionPortNotSet() throws Exception {
68         try {
69             createBgpPeerInstance(HOST, null, false);
70             fail();
71         } catch (final ValidationException e) {
72             assertTrue(e.getMessage().contains("Port value is not set."));
73         }
74     }
75
76     @Test
77     public void testValidationExceptionHostNotSet() throws Exception {
78         try {
79             createBgpPeerInstance(null, portNumber, false);
80             fail();
81         } catch (final ValidationException e) {
82             assertTrue(e.getMessage().contains("Host value is not set."));
83         }
84     }
85
86     @Test
87     public void testCreateBean() throws Exception {
88         final CommitStatus status = createBgpPeerInstance();
89         assertBeanCount(1, FACTORY_NAME);
90         assertStatus(status, 16, 0, 0);
91     }
92
93     @Test
94     public void testCreateBeanWithMD5() throws Exception {
95         NativeTestSupport.assumeSupportedPlatform();
96         final CommitStatus status = createBgpPeerInstance(true);
97         assertBeanCount(1, FACTORY_NAME);
98         assertStatus(status, 18, 0, 0);
99     }
100
101     @Test
102     public void testMD5ValidationFailure() throws Exception {
103         NativeTestSupport.assumeSupportedPlatform();
104         createBgpPeerInstance(true);
105         // now remove md5 from dispatcher
106         ConfigTransactionJMXClient transaction = configRegistryClient.createTransaction();
107         final ObjectName nameCreated = transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME);
108         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
109         BGPDispatcherImplModuleMXBean bgpDispatcherImplModuleMXBean = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
110         bgpDispatcherImplModuleMXBean.setMd5ChannelFactory(null);
111         try {
112             transaction.validateConfig();
113             fail();
114         } catch (ValidationException e) {
115             assertTrue(e.getMessage(), e.getMessage().contains("Underlying dispatcher does not support MD5 clients"));
116         }
117     }
118
119     @Test
120     public void testReusingOldInstance() throws Exception {
121         CommitStatus status = createBgpPeerInstance();
122         ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
123         assertBeanCount(1, FACTORY_NAME);
124         status = transaction.commit();
125         assertBeanCount(1, FACTORY_NAME);
126         assertStatus(status, 0, 0, 16);
127     }
128
129     @Test
130     public void testReconfigure() throws Exception {
131         CommitStatus status = createBgpPeerInstance();
132         ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
133         assertBeanCount(1, FACTORY_NAME);
134         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
135                 BGPPeerModuleMXBean.class);
136         mxBean.setPort(new PortNumber(10));
137         status = transaction.commit();
138         assertBeanCount(1, FACTORY_NAME);
139         assertStatus(status, 0, 1, 15);
140     }
141
142     private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final String host,
143                                              final PortNumber port, boolean md5)
144             throws Exception {
145         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
146         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
147
148         // FIXME JMX crashes if union was not created via artificial constructor - Bug:1276
149         // annotated for JMX as value
150         // IpAddress host1 = new IpAddress(new Ipv4Address(host));
151         mxBean.setHost(host == null ? null : new IpAddress(host.toCharArray()));
152         mxBean.setPort(port);
153         mxBean.setAdvertizedTable(Collections.<ObjectName> emptyList());
154         {
155             ObjectName ribON = createRIBImplModuleInstance(transaction);
156             mxBean.setRib(ribON);
157         }
158         if (md5) {
159             BGPDispatcherImplModuleMXBean bgpDispatcherProxy = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
160             ObjectName jniON = transaction.createModule(NativeKeyAccessFactoryModuleFactory.NAME, NativeKeyAccessFactoryModuleFactory.NAME);
161             ObjectName md5ClientON = transaction.createModule(MD5ClientChannelFactoryModuleFactory.NAME,
162                     MD5ClientChannelFactoryModuleFactory.NAME);
163             MD5ClientChannelFactoryModuleMXBean md5ClientProxy =
164                     transaction.newMXBeanProxy(md5ClientON, MD5ClientChannelFactoryModuleMXBean.class);
165             md5ClientProxy.setKeyAccessFactory(jniON);
166
167             bgpDispatcherProxy.setMd5ChannelFactory(md5ClientON);
168
169             mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
170
171         }
172
173         mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
174                 new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
175                 new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
176         return nameCreated;
177     }
178
179     private BGPDispatcherImplModuleMXBean getBgpDispatcherImplModuleMXBean(ConfigTransactionJMXClient transaction,
180                                                                            BGPPeerModuleMXBean mxBean) {
181         RIBImplModuleMXBean ribProxy = transaction.newMXBeanProxy(mxBean.getRib(), RIBImplModuleMXBean.class);
182         ObjectName dispatcherON = ribProxy.getBgpDispatcher();
183         return transaction.newMXBeanProxy(dispatcherON, BGPDispatcherImplModuleMXBean.class);
184     }
185
186     private CommitStatus createBgpPeerInstance() throws Exception {
187         return createBgpPeerInstance(false);
188     }
189
190     private CommitStatus createBgpPeerInstance(boolean md5) throws Exception {
191         return createBgpPeerInstance(HOST, portNumber, md5);
192     }
193
194     private CommitStatus createBgpPeerInstance(final String host, final PortNumber port, boolean md5) throws Exception {
195         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
196         createBgpPeerInstance(transaction, host, port, md5);
197         return transaction.commit();
198     }
199 }