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.rev130715.IpAddress;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.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 int EXP_INSTANCES = 18;
39 private static final String INSTANCE_NAME = "bgp-peer-module-impl";
40 private static final String FACTORY_NAME = BGPPeerModuleFactory.NAME;
42 private static final IpAddress HOST = new IpAddress(new Ipv4Address("127.0.0.1"));
43 private static final PortNumber PORT_NUMBER = new PortNumber(1);
46 protected BindingRuntimeContext getBindingRuntimeContext() {
47 final BindingRuntimeContext ret = super.getBindingRuntimeContext();
48 doReturn(Ipv4AddressFamily.class).when(ret).getIdentityClass(Ipv4AddressFamily.QNAME);
49 doReturn(MplsLabeledVpnSubsequentAddressFamily.class).when(ret).getIdentityClass(MplsLabeledVpnSubsequentAddressFamily.QNAME);
54 protected List<ModuleFactory> getModuleFactories() {
55 final List<ModuleFactory> moduleFactories = super.getModuleFactories();
56 moduleFactories.add(new BGPPeerModuleFactory());
57 moduleFactories.add(new BGPTableTypeImplModuleFactory());
58 moduleFactories.add(new StrictBgpPeerRegistryModuleFactory());
59 moduleFactories.add(new AddPathImplModuleFactory());
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, PORT_NUMBER, 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, EXP_INSTANCES, 0, 0);
101 public void testReusingOldInstance() throws Exception {
102 CommitStatus status = createBgpPeerInstance();
103 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
104 assertBeanCount(1, FACTORY_NAME);
105 status = transaction.commit();
106 assertBeanCount(1, FACTORY_NAME);
107 assertStatus(status, 0, 0, EXP_INSTANCES);
111 public void testReconfigure() throws Exception {
112 CommitStatus status = createBgpPeerInstance();
113 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
114 assertBeanCount(1, FACTORY_NAME);
115 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
116 BGPPeerModuleMXBean.class);
117 mxBean.setPort(new PortNumber(10));
118 status = transaction.commit();
119 assertBeanCount(1, FACTORY_NAME);
120 assertStatus(status, 0, 1, EXP_INSTANCES - 1);
123 private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final IpAddress host,
124 final PortNumber port, final boolean md5, final boolean internalPeerRole) throws Exception {
125 final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
126 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
128 mxBean.setPeerRegistry(createPeerRegistry(transaction));
129 mxBean.setHost(host);
130 mxBean.setPort(port);
131 mxBean.setAdvertizedTable(Collections.<ObjectName>emptyList());
132 mxBean.setRouteRefresh(false);
134 final ObjectName ribON = createRIBImplModuleInstance(transaction);
135 mxBean.setRib(ribON);
137 mxBean.setAddPath(createAddPathCollection(transaction));
138 if (Epoll.isAvailable()) {
139 mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
141 if (internalPeerRole) {
142 mxBean.setPeerRole(PeerRole.Internal);
145 mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
146 new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
147 new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
149 final ObjectName notificationBrokerON = createNotificationBrokerInstance(transaction);
150 final ObjectName bindingBrokerON = createBindingBrokerImpl(transaction, createCompatibleDataBrokerInstance(transaction), notificationBrokerON);
151 mxBean.setRpcRegistry(bindingBrokerON);
155 private static List<ObjectName> createAddPathCollection(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
156 final ObjectName name1 = transaction.createModule(AddPathImplModuleFactory.NAME, "add-path-inst-1");
157 final AddPathImplModuleMXBean mxBean = transaction.newMXBeanProxy(name1, AddPathImplModuleMXBean.class);
158 mxBean.setAddressFamily(AddPathImplModuleTest.createAddressFamily(transaction, "add-path-inst-1"));
159 mxBean.setSendReceive(SendReceive.Both);
161 final ObjectName name2 = transaction.createModule(AddPathImplModuleFactory.NAME, "add-path-inst-2");
162 final AddPathImplModuleMXBean mxBean2 = transaction.newMXBeanProxy(name2, AddPathImplModuleMXBean.class);
163 mxBean2.setAddressFamily(AddPathImplModuleTest.createAddressFamily(transaction, "add-path-inst-2"));
164 mxBean2.setSendReceive(SendReceive.Receive);
166 final List<ObjectName> ret = new ArrayList<ObjectName>();
172 private static ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
173 return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
176 private CommitStatus createBgpPeerInstance() throws Exception {
177 return createBgpPeerInstance(false);
180 private CommitStatus createBgpPeerInstance(final boolean md5) throws Exception {
181 return createBgpPeerInstance(HOST, PORT_NUMBER, md5);
184 private CommitStatus createBgpPeerInstance(final IpAddress host, final PortNumber port, final boolean md5) throws Exception {
185 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
186 createBgpPeerInstance(transaction, host, port, md5, false);
187 return transaction.commit();
190 private CommitStatus createInternalBgpPeerInstance()
192 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
193 createBgpPeerInstance(transaction, HOST, PORT_NUMBER, false, true);
194 return transaction.commit();