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;
14 import com.google.common.collect.Lists;
15 import io.netty.channel.epoll.Epoll;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import javax.management.InstanceAlreadyExistsException;
20 import javax.management.ObjectName;
21 import org.junit.Test;
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.mdsal.binding.generator.util.BindingRuntimeContext;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
29 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
30 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.PortNumber;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.multiprotocol.rev130919.SendReceive;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.rib.rev130925.PeerRole;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.Ipv4AddressFamily;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.bgp.types.rev130919.MplsLabeledVpnSubsequentAddressFamily;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.rfc2385.cfg.rev160324.Rfc2385Key;
37 public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
39 private static final int EXP_INSTANCES = 18;
40 private static final String INSTANCE_NAME = "bgp-peer-module-impl";
41 private static final String FACTORY_NAME = BGPPeerModuleFactory.NAME;
43 private static final IpAddress HOST = new IpAddress(new Ipv4Address("127.0.0.1"));
44 private static final PortNumber PORT_NUMBER = new PortNumber(1);
47 protected BindingRuntimeContext getBindingRuntimeContext() {
48 final BindingRuntimeContext ret = super.getBindingRuntimeContext();
49 doReturn(Ipv4AddressFamily.class).when(ret).getIdentityClass(Ipv4AddressFamily.QNAME);
50 doReturn(MplsLabeledVpnSubsequentAddressFamily.class).when(ret).getIdentityClass(MplsLabeledVpnSubsequentAddressFamily.QNAME);
55 protected List<ModuleFactory> getModuleFactories() {
56 final List<ModuleFactory> moduleFactories = super.getModuleFactories();
57 moduleFactories.add(new BGPPeerModuleFactory());
58 moduleFactories.add(new BGPTableTypeImplModuleFactory());
59 moduleFactories.add(new StrictBgpPeerRegistryModuleFactory());
60 moduleFactories.add(new AddPathImplModuleFactory());
61 return moduleFactories;
65 public void testValidationExceptionPortNotSet() throws Exception {
67 createBgpPeerInstance(HOST, null, false);
69 } catch (final ValidationException e) {
70 assertTrue(e.getMessage().contains("Port value is not set."));
75 public void testValidationExceptionInternalPeerRole() throws Exception {
77 createInternalBgpPeerInstance();
79 } catch (final ValidationException e) {
80 assertTrue(e.getMessage().contains("Internal Peer Role is reserved for Application Peer use."));
85 public void testValidationExceptionHostNotSet() throws Exception {
87 createBgpPeerInstance(null, PORT_NUMBER, false);
89 } catch (final ValidationException e) {
90 assertTrue(e.getMessage().contains("Host value is not set."));
95 public void testCreateBean() throws Exception {
96 final CommitStatus status = createBgpPeerInstance();
97 assertBeanCount(1, FACTORY_NAME);
98 assertStatus(status, EXP_INSTANCES, 0, 0);
102 public void testReusingOldInstance() throws Exception {
103 CommitStatus status = createBgpPeerInstance();
104 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
105 assertBeanCount(1, FACTORY_NAME);
106 status = transaction.commit();
107 assertBeanCount(1, FACTORY_NAME);
108 assertStatus(status, 0, 0, EXP_INSTANCES);
112 public void testReconfigure() throws Exception {
113 CommitStatus status = createBgpPeerInstance();
114 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
115 assertBeanCount(1, FACTORY_NAME);
116 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
117 BGPPeerModuleMXBean.class);
118 mxBean.setPort(new PortNumber(10));
119 status = transaction.commit();
120 assertBeanCount(1, FACTORY_NAME);
121 assertStatus(status, 0, 1, EXP_INSTANCES - 1);
124 private ObjectName createBgpPeerInstance(final ConfigTransactionJMXClient transaction, final IpAddress host,
125 final PortNumber port, final boolean md5, final boolean internalPeerRole) throws Exception {
126 final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
127 final BGPPeerModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, BGPPeerModuleMXBean.class);
129 mxBean.setPeerRegistry(createPeerRegistry(transaction));
130 mxBean.setHost(host);
131 mxBean.setPort(port);
132 mxBean.setAdvertizedTable(Collections.emptyList());
133 mxBean.setRouteRefresh(false);
135 final ObjectName ribON = createRIBImplModuleInstance(transaction);
136 mxBean.setRib(ribON);
138 mxBean.setAddPath(createAddPathCollection(transaction));
139 if (Epoll.isAvailable()) {
140 mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
142 if (internalPeerRole) {
143 mxBean.setPeerRole(PeerRole.Internal);
146 mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
147 new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
148 new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
150 final ObjectName notificationBrokerON = createNotificationBrokerInstance(transaction);
151 final ObjectName bindingBrokerON = createBindingBrokerImpl(transaction, createCompatibleDataBrokerInstance(transaction), notificationBrokerON);
152 mxBean.setRpcRegistry(bindingBrokerON);
156 private static List<ObjectName> createAddPathCollection(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
157 final ObjectName name1 = transaction.createModule(AddPathImplModuleFactory.NAME, "add-path-inst-1");
158 final AddPathImplModuleMXBean mxBean = transaction.newMXBeanProxy(name1, AddPathImplModuleMXBean.class);
159 mxBean.setAddressFamily(AddPathImplModuleTest.createAddressFamily(transaction, "add-path-inst-1"));
160 mxBean.setSendReceive(SendReceive.Both);
162 final ObjectName name2 = transaction.createModule(AddPathImplModuleFactory.NAME, "add-path-inst-2");
163 final AddPathImplModuleMXBean mxBean2 = transaction.newMXBeanProxy(name2, AddPathImplModuleMXBean.class);
164 mxBean2.setAddressFamily(AddPathImplModuleTest.createAddressFamily(transaction, "add-path-inst-2"));
165 mxBean2.setSendReceive(SendReceive.Receive);
167 final List<ObjectName> ret = new ArrayList<ObjectName>();
173 private static ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
174 return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
177 private CommitStatus createBgpPeerInstance() throws Exception {
178 return createBgpPeerInstance(false);
181 private CommitStatus createBgpPeerInstance(final boolean md5) throws Exception {
182 return createBgpPeerInstance(HOST, PORT_NUMBER, md5);
185 private CommitStatus createBgpPeerInstance(final IpAddress host, final PortNumber port, final boolean md5) throws Exception {
186 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
187 createBgpPeerInstance(transaction, host, port, md5, false);
188 return transaction.commit();
191 private CommitStatus createInternalBgpPeerInstance()
193 final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
194 createBgpPeerInstance(transaction, HOST, PORT_NUMBER, false, true);
195 return transaction.commit();