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 static org.mockito.Mockito.mock;
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;
38 public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
40 private static final String INSTANCE_NAME = "bgp-peer-module-impl";
41 private static final String FACTORY_NAME = BGPPeerModuleFactory.NAME;
43 private static final String HOST = "127.0.0.1";
44 private static final PortNumber portNumber = new PortNumber(1);
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);
52 CodecRegistry codecReg = super.getCodecRegistry();
53 doReturn(idCodec).when(codecReg).getIdentityCodec();
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;
69 public void testValidationExceptionPortNotSet() throws Exception {
71 createBgpPeerInstance(HOST, null, false);
73 } catch (final ValidationException e) {
74 assertTrue(e.getMessage().contains("Port value is not set."));
79 public void testValidationExceptionHostNotSet() throws Exception {
81 createBgpPeerInstance(null, portNumber, false);
83 } catch (final ValidationException e) {
84 assertTrue(e.getMessage().contains("Host value is not set."));
89 public void testCreateBean() throws Exception {
90 final CommitStatus status = createBgpPeerInstance();
91 assertBeanCount(1, FACTORY_NAME);
92 assertStatus(status, 17, 0, 0);
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);
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);
114 transaction.validateConfig();
116 } catch (ValidationException e) {
117 assertTrue(e.getMessage(), e.getMessage().contains("Underlying dispatcher does not support MD5 clients"));
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);
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);
144 private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final String host,
145 final PortNumber port, boolean md5)
147 final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
148 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
150 mxBean.setPeerRegistry(createPeerRegistry(transaction));
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());
159 ObjectName ribON = createRIBImplModuleInstance(transaction);
160 mxBean.setRib(ribON);
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);
171 bgpDispatcherProxy.setMd5ChannelFactory(md5ClientON);
173 mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
177 mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
178 new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
179 new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
183 private ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
184 return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
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);
194 private CommitStatus createBgpPeerInstance() throws Exception {
195 return createBgpPeerInstance(false);
198 private CommitStatus createBgpPeerInstance(boolean md5) throws Exception {
199 return createBgpPeerInstance(HOST, portNumber, md5);
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();