Bug 2231 - Secure transport for PCEP
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / controller / config / yang / pcep / impl / PCEPDispatcherImplModuleTest.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.pcep.impl;
9
10 import static org.junit.Assert.assertTrue;
11 import static org.junit.Assert.fail;
12
13 import javax.management.InstanceAlreadyExistsException;
14 import javax.management.ObjectName;
15 import org.junit.Before;
16 import org.junit.Test;
17 import org.opendaylight.controller.config.api.ValidationException;
18 import org.opendaylight.controller.config.api.jmx.CommitStatus;
19 import org.opendaylight.controller.config.manager.impl.AbstractConfigTest;
20 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
21 import org.opendaylight.controller.config.util.ConfigTransactionJMXClient;
22 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleFactory;
23 import org.opendaylight.controller.config.yang.netty.threadgroup.NettyThreadgroupModuleMXBean;
24 import org.opendaylight.controller.config.yang.pcep.spi.SimplePCEPExtensionProviderContextModuleFactory;
25 import org.opendaylight.controller.config.yang.pcep.spi.SimplePCEPExtensionProviderContextModuleMXBean;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.impl.rev130627.PathType;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.pcep.impl.rev130627.StoreType;
28
29 public class PCEPDispatcherImplModuleTest extends AbstractConfigTest {
30
31     private static final String INSTANCE_NAME = "pcep-dispatcher-impl";
32     private static final String FACTORY_NAME = PCEPDispatcherImplModuleFactory.NAME;
33
34     private static final String THREADGROUP_FACTORY_NAME = NettyThreadgroupModuleFactory.NAME;
35     private static final String BOSS_TG_INSTANCE_NAME = "boss-group";
36     private static final String WORKER_TG_INSTANCE_NAME = "worker-group";
37
38     private static final String EXTENSION_INSTANCE_NAME = "pcep-extension-privider";
39     private static final String EXTENSION_FACTORYNAME = SimplePCEPExtensionProviderContextModuleFactory.NAME;
40
41     private static final String STORE_PASSWORD = "opendaylight";
42     private static final String KEYSTORE = "ctl.jks";
43     private static final String TRUSTSTORE = "truststore.jks";
44
45     @Before
46     public void setUp() throws Exception {
47         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(this.mockedContext, new PCEPDispatcherImplModuleFactory(), new PCEPSessionProposalFactoryImplModuleFactory(), new NettyThreadgroupModuleFactory(), new SimplePCEPExtensionProviderContextModuleFactory()));
48     }
49
50     @Test
51     public void testValidationExceptionMaxUnknownMessagesNotSet() throws Exception {
52         try {
53             createDispatcherInstance(null);
54             fail();
55         } catch (final ValidationException e) {
56             assertTrue(e.getMessage().contains("MaxUnknownMessages value is not set"));
57         }
58     }
59
60     @Test
61     public void testValidationExceptionMaxUnknownMessagesMinValue() throws Exception {
62         try {
63             createDispatcherInstance(0);
64             fail();
65         } catch (final ValidationException e) {
66             assertTrue(e.getMessage().contains("must be greater than 0"));
67         }
68     }
69
70     @Test
71     public void testValidationExceptionCertificatePassword() throws Exception {
72         try {
73             createDispatcherInstance(1, createTls(null, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
74                     PathType.PATH, StoreType.JKS));
75             fail();
76         } catch (final ValidationException e) {
77             assertTrue(e.getMessage().contains("certificate password"));
78         }
79     }
80
81     @Test
82     public void testValidationExceptionKeystoreLocation() throws Exception {
83         try {
84             createDispatcherInstance(1, createTls(STORE_PASSWORD, null, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
85                     PathType.PATH, StoreType.JKS));
86             fail();
87         } catch (final ValidationException e) {
88             assertTrue(e.getMessage().contains("keystore location"));
89         }
90     }
91
92     @Test
93     public void testValidationExceptionKeystorePassword() throws Exception {
94         try {
95             createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, null, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
96                     PathType.PATH, StoreType.JKS));
97             fail();
98         } catch (final ValidationException e) {
99             assertTrue(e.getMessage().contains("keystore password"));
100         }
101     }
102
103     @Test
104     public void testValidationExceptionKeystorePathType() throws Exception {
105         try {
106             createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, null, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
107                     PathType.PATH, StoreType.JKS));
108             fail();
109         } catch (final ValidationException e) {
110             assertTrue(e.getMessage().contains("keystore path type"));
111         }
112     }
113
114     @Test
115     public void testValidationExceptionKeystoreType() throws Exception {
116         try {
117             createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, null, TRUSTSTORE, STORE_PASSWORD,
118                     PathType.PATH, StoreType.JKS));
119             fail();
120         } catch (final ValidationException e) {
121             assertTrue(e.getMessage().contains("keystore type"));
122         }
123     }
124
125     @Test
126     public void testValidationExceptionTruststoreLocation() throws Exception {
127         try {
128             createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, null, STORE_PASSWORD,
129                     PathType.PATH, StoreType.JKS));
130             fail();
131         } catch (final ValidationException e) {
132             assertTrue(e.getMessage().contains("truststore location"));
133         }
134     }
135
136     @Test
137     public void testValidationExceptionTruststorePassword() throws Exception {
138         try {
139             createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, null,
140                     PathType.PATH, StoreType.JKS));
141             fail();
142         } catch (final ValidationException e) {
143             assertTrue(e.getMessage().contains("truststore password"));
144         }
145     }
146
147     @Test
148     public void testValidationExceptionTruststorePathType() throws Exception {
149         try {
150             createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
151                     null, StoreType.JKS));
152             fail();
153         } catch (final ValidationException e) {
154             assertTrue(e.getMessage().contains("truststore path type"));
155         }
156     }
157
158     @Test
159     public void testValidationExceptionTruststoreType() throws Exception {
160         try {
161             createDispatcherInstance(1, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS, TRUSTSTORE, STORE_PASSWORD,
162                     PathType.PATH, null));
163             fail();
164         } catch (final ValidationException e) {
165             assertTrue(e.getMessage().contains("truststore type"));
166         }
167     }
168
169     @Test
170     public void testCreateBean() throws Exception {
171         final CommitStatus status = createDispatcherInstance(5);
172         assertBeanCount(1, FACTORY_NAME);
173         assertStatus(status, 5, 0, 0);
174     }
175
176     @Test
177     public void testReusingOldInstance() throws Exception {
178         createDispatcherInstance(5);
179         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
180         assertBeanCount(1, FACTORY_NAME);
181         final CommitStatus status = transaction.commit();
182         assertBeanCount(1, FACTORY_NAME);
183         assertStatus(status, 0, 0, 5);
184     }
185
186     @Test
187     public void testReconfigure() throws Exception {
188         createDispatcherInstance(5);
189         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
190         assertBeanCount(1, FACTORY_NAME);
191         final PCEPDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(transaction.lookupConfigBean(FACTORY_NAME, INSTANCE_NAME),
192                 PCEPDispatcherImplModuleMXBean.class);
193         mxBean.setMaxUnknownMessages(10);
194         final CommitStatus status = transaction.commit();
195         assertBeanCount(1, FACTORY_NAME);
196         assertStatus(status, 0, 1, 4);
197     }
198
199     @Test
200     public void testCreateBeanWithTls() throws Exception {
201         final CommitStatus status = createDispatcherInstance(5, createTls(STORE_PASSWORD, KEYSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS,
202                 TRUSTSTORE, STORE_PASSWORD, PathType.PATH, StoreType.JKS));
203         assertBeanCount(1, FACTORY_NAME);
204         assertStatus(status, 5, 0, 0);
205     }
206
207     private CommitStatus createDispatcherInstance(final Integer maxUnknownMessages) throws Exception {
208         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
209         createDispatcherInstance(transaction, maxUnknownMessages);
210         return transaction.commit();
211     }
212
213     private CommitStatus createDispatcherInstance(final Integer maxUnknownMessages, final Tls tls) throws Exception {
214         final ConfigTransactionJMXClient transaction = this.configRegistryClient.createTransaction();
215         createDispatcherInstance(transaction, maxUnknownMessages, tls);
216         return transaction.commit();
217     }
218
219     public static ObjectName createDispatcherInstance(final ConfigTransactionJMXClient transaction, final Integer maxUnknownMessages)
220             throws Exception {
221         final ObjectName nameCreated = transaction.createModule(FACTORY_NAME, INSTANCE_NAME);
222         final PCEPDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, PCEPDispatcherImplModuleMXBean.class);
223         mxBean.setPcepSessionProposalFactory(PCEPSessionProposalFactoryImplModuleTest.createSessionInstance(transaction));
224         mxBean.setMaxUnknownMessages(maxUnknownMessages);
225         mxBean.setBossGroup(createThreadGroupInstance(transaction, 10, BOSS_TG_INSTANCE_NAME));
226         mxBean.setWorkerGroup(createThreadGroupInstance(transaction, 10, WORKER_TG_INSTANCE_NAME));
227         mxBean.setPcepExtensions(createExtensionsInstance(transaction));
228         return nameCreated;
229     }
230
231     private static ObjectName createDispatcherInstance(final ConfigTransactionJMXClient transaction, final Integer maxUnknownMessages,
232             final Tls tls) throws Exception {
233         final ObjectName objName = createDispatcherInstance(transaction, maxUnknownMessages);
234         final PCEPDispatcherImplModuleMXBean mxBean = transaction.newMXBeanProxy(objName, PCEPDispatcherImplModuleMXBean.class);
235         mxBean.setTls(tls);
236         return objName;
237     }
238
239     private static ObjectName createExtensionsInstance(final ConfigTransactionJMXClient transaction) throws InstanceAlreadyExistsException {
240         final ObjectName nameCreated = transaction.createModule(EXTENSION_FACTORYNAME, EXTENSION_INSTANCE_NAME);
241         transaction.newMXBeanProxy(nameCreated, SimplePCEPExtensionProviderContextModuleMXBean.class);
242
243         return nameCreated;
244     }
245
246     private static ObjectName createThreadGroupInstance(final ConfigTransactionJMXClient transaction, final Integer threadCount,
247             final String instanceName) throws InstanceAlreadyExistsException {
248         final ObjectName nameCreated = transaction.createModule(THREADGROUP_FACTORY_NAME, instanceName);
249         final NettyThreadgroupModuleMXBean mxBean = transaction.newMXBeanProxy(nameCreated, NettyThreadgroupModuleMXBean.class);
250         mxBean.setThreadCount(threadCount);
251         return nameCreated;
252     }
253
254     private static Tls createTls(final String certificatePassword, final String keystore, final String keystorePassword,
255             final PathType keystorePathType, final StoreType keystoreType, final String truststore,
256             final String truststorePassword, final PathType truststorePathType, final StoreType truststoreType) {
257         final Tls tls = new Tls();
258         tls.setCertificatePassword(certificatePassword);
259         tls.setKeystore(keystore);
260         tls.setKeystorePassword(keystorePassword);
261         tls.setKeystorePathType(keystorePathType);
262         tls.setKeystoreType(keystoreType);
263         tls.setTruststore(truststore);
264         tls.setTruststorePassword(truststorePassword);
265         tls.setTruststorePathType(truststorePathType);
266         tls.setTruststoreType(truststoreType);
267         return tls;
268     }
269
270 }