BUG-4070: Define new specific Peer role for App Peer.
[bgpcep.git] / bgp / rib-impl / src / test / java / org / opendaylight / controller / config / yang / bgp / rib / impl / BGPPeerModuleTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.config.yang.bgp.rib.impl;
9
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;
35
36 public class BGPPeerModuleTest extends AbstractRIBImplModuleTest {
37
38     private static final String INSTANCE_NAME = "bgp-peer-module-impl";
39     private static final String FACTORY_NAME = BGPPeerModuleFactory.NAME;
40
41     private static final String HOST = "127.0.0.1";
42     private static final PortNumber portNumber = new PortNumber(1);
43
44     @Override
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);
49         return ret;
50     }
51
52     @Override
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;
61     }
62
63     @Test
64     public void testValidationExceptionPortNotSet() throws Exception {
65         try {
66             createBgpPeerInstance(HOST, null, false);
67             fail();
68         } catch (final ValidationException e) {
69             assertTrue(e.getMessage().contains("Port value is not set."));
70         }
71     }
72
73     @Test
74     public void testValidationExceptionInternalPeerRole() throws Exception {
75         try {
76             createInternalBgpPeerInstance();
77             fail();
78         } catch (final ValidationException e) {
79             assertTrue(e.getMessage().contains("Internal Peer Role is reserved for Application Peer use."));
80         }
81     }
82
83     @Test
84     public void testValidationExceptionHostNotSet() throws Exception {
85         try {
86             createBgpPeerInstance(null, portNumber, false);
87             fail();
88         } catch (final ValidationException e) {
89             assertTrue(e.getMessage().contains("Host value is not set."));
90         }
91     }
92
93     @Test
94     public void testCreateBean() throws Exception {
95         final CommitStatus status = createBgpPeerInstance();
96         assertBeanCount(1, FACTORY_NAME);
97         assertStatus(status, 16, 0, 0);
98     }
99
100     @Test
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);
106     }
107
108     @Test
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);
118         try {
119             transaction.validateConfig();
120             fail();
121         } catch (final ValidationException e) {
122             assertTrue(e.getMessage(), e.getMessage().contains("Underlying dispatcher does not support MD5 clients"));
123         }
124     }
125
126     @Test
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);
134     }
135
136     @Test
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);
147     }
148
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);
153
154         mxBean.setPeerRegistry(createPeerRegistry(transaction));
155
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());
162         {
163             final ObjectName ribON = createRIBImplModuleInstance(transaction);
164             mxBean.setRib(ribON);
165         }
166         if (md5) {
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);
174
175             bgpDispatcherProxy.setMd5ChannelFactory(md5ClientON);
176
177             mxBean.setPassword(Rfc2385Key.getDefaultInstance("foo"));
178
179         }
180
181         if(internalPeerRole) {
182             mxBean.setPeerRole(PeerRole.Internal);
183         }
184
185         mxBean.setAdvertizedTable(Lists.newArrayList(BGPTableTypeImplModuleTest.createTableInstance(transaction,
186             new IdentityAttributeRef(Ipv4AddressFamily.QNAME.toString()),
187             new IdentityAttributeRef(MplsLabeledVpnSubsequentAddressFamily.QNAME.toString()))));
188         return nameCreated;
189     }
190
191     private static ObjectName createPeerRegistry(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
192         return transaction.createModule(StrictBgpPeerRegistryModuleFactory.NAME, "peer-registry");
193     }
194
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);
200     }
201
202     private CommitStatus createBgpPeerInstance() throws Exception {
203         return createBgpPeerInstance(false);
204     }
205
206     private CommitStatus createBgpPeerInstance(final boolean md5) throws Exception {
207         return createBgpPeerInstance(HOST, portNumber, md5);
208     }
209
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();
214     }
215
216     private CommitStatus createInternalBgpPeerInstance()
217         throws Exception {
218         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
219         createBgpPeerInstance(transaction, HOST, portNumber, false, true);
220         return transaction.commit();
221     }
222 }