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