Rework BGP timers to work with channel
[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         final 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         final CodecRegistry codecReg = super.getCodecRegistry();
53         doReturn(idCodec).when(codecReg).getIdentityCodec();
54         return codecReg;
55     }
56
57     @Override
58     protected List<ModuleFactory> getModuleFactories() {
59         final 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, 16, 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, 18, 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         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
109         final ObjectName nameCreated = transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME);
110         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
111         final BGPDispatcherImplModuleMXBean bgpDispatcherImplModuleMXBean = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
112         bgpDispatcherImplModuleMXBean.setMd5ChannelFactory(null);
113         try {
114             transaction.validateConfig();
115             fail();
116         } catch (final 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         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
125         assertBeanCount(1, FACTORY_NAME);
126         status = transaction.commit();
127         assertBeanCount(1, FACTORY_NAME);
128         assertStatus(status, 0, 0, 16);
129     }
130
131     @Test
132     public void testReconfigure() throws Exception {
133         CommitStatus status = createBgpPeerInstance();
134         final 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, 15);
142     }
143
144     private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final String host,
145             final PortNumber port, final boolean md5) throws Exception {
146         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
147         final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
148
149         mxBean.setPeerRegistry(createPeerRegistry(transaction));
150
151         // FIXME JMX crashes if union was not created via artificial constructor - Bug:1276
152         // annotated for JMX as value
153         // IpAddress host1 = new IpAddress(new Ipv4Address(host));
154         mxBean.setHost(host == null ? null : new IpAddress(host.toCharArray()));
155         mxBean.setPort(port);
156         mxBean.setAdvertizedTable(Collections.<ObjectName> emptyList());
157         {
158             final ObjectName ribON = createRIBImplModuleInstance(transaction);
159             mxBean.setRib(ribON);
160         }
161         if (md5) {
162             final BGPDispatcherImplModuleMXBean bgpDispatcherProxy = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
163             final ObjectName jniON = transaction.createModule(NativeKeyAccessFactoryModuleFactory.NAME, NativeKeyAccessFactoryModuleFactory.NAME);
164             final ObjectName md5ClientON = transaction.createModule(MD5ClientChannelFactoryModuleFactory.NAME,
165                     MD5ClientChannelFactoryModuleFactory.NAME);
166             final MD5ClientChannelFactoryModuleMXBean md5ClientProxy =
167                     transaction.newMXBeanProxy(md5ClientON, MD5ClientChannelFactoryModuleMXBean.class);
168             md5ClientProxy.setKeyAccessFactory(jniON);
169
170             bgpDispatcherProxy.setMd5ChannelFactory(md5ClientON);
171
172             mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
173
174         }
175
176         mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
177                 new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
178                 new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
179         return nameCreated;
180     }
181
182     private ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
183         return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
184     }
185
186     private BGPDispatcherImplModuleMXBean getBgpDispatcherImplModuleMXBean(final ConfigTransactionJMXClient transaction,
187             final BGPPeerModuleMXBean mxBean) {
188         final RIBImplModuleMXBean ribProxy = transaction.newMXBeanProxy(mxBean.getRib(), RIBImplModuleMXBean.class);
189         final ObjectName dispatcherON = ribProxy.getBgpDispatcher();
190         return transaction.newMXBeanProxy(dispatcherON, BGPDispatcherImplModuleMXBean.class);
191     }
192
193     private CommitStatus createBgpPeerInstance() throws Exception {
194         return createBgpPeerInstance(false);
195     }
196
197     private CommitStatus createBgpPeerInstance(final boolean md5) throws Exception {
198         return createBgpPeerInstance(HOST, portNumber, md5);
199     }
200
201     private CommitStatus createBgpPeerInstance(final String host, final PortNumber port, final boolean md5) throws Exception {
202         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
203         createBgpPeerInstance(transaction, host, port, md5);
204         return transaction.commit();
205     }
206 }