2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.config.yang.bgp.rib.impl;
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12 import static org.mockito.Mockito.doReturn;
13 import com.google.common.collect.Lists;
14 import java.util.Collections;
15 import java.util.List;
16 import javax.management.InstanceAlreadyExistsException;
17 import javax.management.ObjectName;
18 import org.junit.Test;
19 import org.opendaylight.controller.config.api.IdentityAttributeRef;
20 import org.opendaylight.controller.config.api.ValidationException;
21 import org.opendaylight.controller.config.api.jmx.CommitStatus;
22 import org.opendaylight.controller.config.spi.ModuleFactory;
23 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
24 import org.opendaylight.controller.config.yang.tcpmd5.jni.cfg.NativeKeyAccessFactoryModuleFactory;
25 import org.opendaylight.controller.config.yang.tcpmd5.netty.cfg.MD5ClientChannelFactoryModuleFactory;
26 import org.opendaylight.controller.config.yang.tcpmd5.netty.cfg.MD5ClientChannelFactoryModuleMXBean;
27 import org.opendaylight.tcpmd5.jni.NativeTestSupport;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
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;
36 public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
38 private static final String INSTANCE_NAME = "bgp-peer-module-impl";
39 private static final String FACTORY_NAME = BGPPeerModuleFactory.NAME;
41 private static final String HOST = "127.0.0.1";
42 private static final PortNumber portNumber = new PortNumber(1);
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);
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;
64 public void testValidationExceptionPortNotSet() throws Exception {
66 createBgpPeerInstance(HOST, null, false);
68 } catch (final ValidationException e) {
69 assertTrue(e.getMessage().contains("Port value is not set."));
74 public void testValidationExceptionInternalPeerRole() throws Exception {
76 createInternalBgpPeerInstance();
78 } catch (final ValidationException e) {
79 assertTrue(e.getMessage().contains("Internal Peer Role is reserved for Application Peer use."));
84 public void testValidationExceptionHostNotSet() throws Exception {
86 createBgpPeerInstance(null, portNumber, false);
88 } catch (final ValidationException e) {
89 assertTrue(e.getMessage().contains("Host value is not set."));
94 public void testCreateBean() throws Exception {
95 final CommitStatus status = createBgpPeerInstance();
96 assertBeanCount(1, FACTORY_NAME);
97 assertStatus(status, 16, 0, 0);
101 public void testCreateBeanWithMD5() throws Exception {
102 NativeTestSupport.assumeSupportedPlatform();
103 final CommitStatus status = createBgpPeerInstance(true);
104 assertBeanCount(1, FACTORY_NAME);
105 assertStatus(status, 18, 0, 0);
109 public void testMD5ValidationFailure() throws Exception {
110 NativeTestSupport.assumeSupportedPlatform();
111 createBgpPeerInstance(true);
112 // now remove md5 from dispatcher
113 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
114 final ObjectName nameCreated = transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME);
115 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
116 final BGPDispatcherImplModuleMXBean bgpDispatcherImplModuleMXBean = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
117 bgpDispatcherImplModuleMXBean.setMd5ChannelFactory(null);
119 transaction.validateConfig();
121 } catch (final ValidationException e) {
122 assertTrue(e.getMessage(), e.getMessage().contains("Underlying dispatcher does not support MD5 clients"));
127 public void testReusingOldInstance() throws Exception {
128 CommitStatus status = createBgpPeerInstance();
129 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
130 assertBeanCount(1, FACTORY_NAME);
131 status = transaction.commit();
132 assertBeanCount(1, FACTORY_NAME);
133 assertStatus(status, 0, 0, 16);
137 public void testReconfigure() throws Exception {
138 CommitStatus status = createBgpPeerInstance();
139 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
140 assertBeanCount(1, FACTORY_NAME);
141 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
142 BGPPeerModuleMXBean.class);
143 mxBean.setPort(new PortNumber(10));
144 status = transaction.commit();
145 assertBeanCount(1, FACTORY_NAME);
146 assertStatus(status, 0, 1, 15);
149 private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final String host,
150 final PortNumber port, final boolean md5, final boolean internalPeerRole) throws Exception {
151 final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
152 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
154 mxBean.setPeerRegistry(createPeerRegistry(transaction));
156 // FIXME JMX crashes if union was not created via artificial constructor - Bug:1276
157 // annotated for JMX as value
158 // IpAddress host1 = new IpAddress(new Ipv4Address(host));
159 mxBean.setHost(host == null ? null : new IpAddress(host.toCharArray()));
160 mxBean.setPort(port);
161 mxBean.setAdvertizedTable(Collections.<ObjectName>emptyList());
163 final ObjectName ribON = createRIBImplModuleInstance(transaction);
164 mxBean.setRib(ribON);
167 final BGPDispatcherImplModuleMXBean bgpDispatcherProxy = getBgpDispatcherImplModuleMXBean(transaction, mxBean);
168 final ObjectName jniON = transaction.createModule(NativeKeyAccessFactoryModuleFactory.NAME, NativeKeyAccessFactoryModuleFactory.NAME);
169 final ObjectName md5ClientON = transaction.createModule(MD5ClientChannelFactoryModuleFactory.NAME,
170 MD5ClientChannelFactoryModuleFactory.NAME);
171 final MD5ClientChannelFactoryModuleMXBean md5ClientProxy =
172 transaction.newMXBeanProxy(md5ClientON, MD5ClientChannelFactoryModuleMXBean.class);
173 md5ClientProxy.setKeyAccessFactory(jniON);
175 bgpDispatcherProxy.setMd5ChannelFactory(md5ClientON);
177 mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
181 if(internalPeerRole) {
182 mxBean.setPeerRole(PeerRole.Internal);
185 mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
186 new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
187 new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
191 private static ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
192 return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
195 private static BGPDispatcherImplModuleMXBean getBgpDispatcherImplModuleMXBean(final ConfigTransactionJMXClient transaction,
196 final BGPPeerModuleMXBean mxBean) {
197 final RIBImplModuleMXBean ribProxy = transaction.newMXBeanProxy(mxBean.getRib(), RIBImplModuleMXBean.class);
198 final ObjectName dispatcherON = ribProxy.getBgpDispatcher();
199 return transaction.newMXBeanProxy(dispatcherON, BGPDispatcherImplModuleMXBean.class);
202 private CommitStatus createBgpPeerInstance() throws Exception {
203 return createBgpPeerInstance(false);
206 private CommitStatus createBgpPeerInstance(final boolean md5) throws Exception {
207 return createBgpPeerInstance(HOST, portNumber, md5);
210 private CommitStatus createBgpPeerInstance(final String host, final PortNumber port, final boolean md5) throws Exception {
211 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
212 createBgpPeerInstance(transaction, host, port, md5, false);
213 return transaction.commit();
216 private CommitStatus createInternalBgpPeerInstance()
218 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
219 createBgpPeerInstance(transaction, HOST, portNumber, false, true);
220 return transaction.commit();