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 io.netty.channel.epoll.Epoll;
15 import java.util.ArrayList;
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.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.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.PortNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
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.rfc2385.cfg.rev160324.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 IpAddress HOST = new IpAddress(new Ipv4Address("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 StrictBgpPeerRegistryModuleFactory());
58 moduleFactories.add(new AddPathImplModuleFactory());
59 return moduleFactories;
63 public void testValidationExceptionPortNotSet() throws Exception {
65 createBgpPeerInstance(HOST, null, false);
67 } catch (final ValidationException e) {
68 assertTrue(e.getMessage().contains("Port value is not set."));
73 public void testValidationExceptionInternalPeerRole() throws Exception {
75 createInternalBgpPeerInstance();
77 } catch (final ValidationException e) {
78 assertTrue(e.getMessage().contains("Internal Peer Role is reserved for Application Peer use."));
83 public void testValidationExceptionHostNotSet() throws Exception {
85 createBgpPeerInstance(null, portNumber, false);
87 } catch (final ValidationException e) {
88 assertTrue(e.getMessage().contains("Host value is not set."));
93 public void testCreateBean() throws Exception {
94 final CommitStatus status = createBgpPeerInstance();
95 assertBeanCount(1, FACTORY_NAME);
96 assertStatus(status, 21, 0, 0);
100 public void testReusingOldInstance() throws Exception {
101 CommitStatus status = createBgpPeerInstance();
102 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
103 assertBeanCount(1, FACTORY_NAME);
104 status = transaction.commit();
105 assertBeanCount(1, FACTORY_NAME);
106 assertStatus(status, 0, 0, 21);
110 public void testReconfigure() throws Exception {
111 CommitStatus status = createBgpPeerInstance();
112 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
113 assertBeanCount(1, FACTORY_NAME);
114 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
115 BGPPeerModuleMXBean.class);
116 mxBean.setPort(new PortNumber(10));
117 status = transaction.commit();
118 assertBeanCount(1, FACTORY_NAME);
119 assertStatus(status, 0, 1, 20);
122 private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final IpAddress host,
123 final PortNumber port, final boolean md5, final boolean internalPeerRole) throws Exception {
124 final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
125 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
127 mxBean.setPeerRegistry(createPeerRegistry(transaction));
128 mxBean.setHost(host);
129 mxBean.setPort(port);
130 mxBean.setAdvertizedTable(Collections.<ObjectName>emptyList());
131 mxBean.setRouteRefresh(false);
133 final ObjectName ribON = createRIBImplModuleInstance(transaction);
134 mxBean.setRib(ribON);
136 mxBean.setAddPath(createAddPathCollection(transaction));
137 if (Epoll.isAvailable()) {
138 mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
140 if (internalPeerRole) {
141 mxBean.setPeerRole(PeerRole.Internal);
144 mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
145 new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
146 new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
148 final ObjectName notificationBrokerON = createNotificationBrokerInstance(transaction);
149 final ObjectName bindingBrokerON = createBindingBrokerImpl(transaction, createCompatibleDataBrokerInstance(transaction), notificationBrokerON);
150 mxBean.setRpcRegistry(bindingBrokerON);
154 private static List<ObjectName> createAddPathCollection(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
155 final ObjectName name1 = transaction.createModule(AddPathImplModuleFactory.NAME, "add-path-inst-1");
156 final AddPathImplModuleMXBean mxBean = transaction.newMXBeanProxy(name1, AddPathImplModuleMXBean.class);
157 mxBean.setAddressFamily(AddPathImplModuleTest.createAddressFamily(transaction, "add-path-inst-1"));
158 mxBean.setSendReceive(SendReceive.Both);
160 final ObjectName name2 = transaction.createModule(AddPathImplModuleFactory.NAME, "add-path-inst-2");
161 final AddPathImplModuleMXBean mxBean2 = transaction.newMXBeanProxy(name2, AddPathImplModuleMXBean.class);
162 mxBean2.setAddressFamily(AddPathImplModuleTest.createAddressFamily(transaction, "add-path-inst-2"));
163 mxBean2.setSendReceive(SendReceive.Receive);
165 final List<ObjectName> ret = new ArrayList<ObjectName>();
171 private static ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
172 return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
175 private static BGPDispatcherImplModuleMXBean getBgpDispatcherImplModuleMXBean(final ConfigTransactionJMXClient transaction,
176 final BGPPeerModuleMXBean mxBean) {
177 final RIBImplModuleMXBean ribProxy = transaction.newMXBeanProxy(mxBean.getRib(), RIBImplModuleMXBean.class);
178 final ObjectName dispatcherON = ribProxy.getBgpDispatcher();
179 return transaction.newMXBeanProxy(dispatcherON, BGPDispatcherImplModuleMXBean.class);
182 private CommitStatus createBgpPeerInstance() throws Exception {
183 return createBgpPeerInstance(false);
186 private CommitStatus createBgpPeerInstance(final boolean md5) throws Exception {
187 return createBgpPeerInstance(HOST, portNumber, md5);
190 private CommitStatus createBgpPeerInstance(final IpAddress host, final PortNumber port, final boolean md5) throws Exception {
191 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
192 createBgpPeerInstance(transaction, host, port, md5, false);
193 return transaction.commit();
196 private CommitStatus createInternalBgpPeerInstance()
198 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
199 createBgpPeerInstance(transaction, HOST, portNumber, false, true);
200 return transaction.commit();