Merge "Resolve Bug:853 - remove groovy from config code generator."
[controller.git] / opendaylight / netconf / netconf-it / src / test / java / org / opendaylight / controller / netconf / it / NetconfITSecureTest.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
9 package org.opendaylight.controller.netconf.it;
10
11 import io.netty.channel.ChannelFuture;
12 import org.junit.After;
13 import org.junit.Before;
14 import org.junit.Test;
15 import org.opendaylight.controller.config.manager.impl.factoriesresolver.HardcodedModuleFactoriesResolver;
16 import org.opendaylight.controller.config.spi.ModuleFactory;
17 import org.opendaylight.controller.netconf.client.NetconfClientDispatcher;
18 import org.opendaylight.controller.netconf.client.test.TestingNetconfClient;
19 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.NetconfOperationServiceFactoryImpl;
20 import org.opendaylight.controller.netconf.confignetconfconnector.osgi.YangStoreException;
21 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
22 import org.opendaylight.controller.netconf.impl.NetconfServerDispatcher;
23 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListenerImpl;
24
25 import javax.net.ssl.KeyManagerFactory;
26 import javax.net.ssl.SSLContext;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.lang.management.ManagementFactory;
30 import java.net.InetSocketAddress;
31 import java.security.KeyManagementException;
32 import java.security.KeyStoreException;
33 import java.security.NoSuchAlgorithmException;
34 import java.security.UnrecoverableKeyException;
35 import java.security.cert.CertificateException;
36 import java.util.Collection;
37 import java.util.List;
38
39 public class NetconfITSecureTest extends AbstractNetconfConfigTest {
40
41     private static final InetSocketAddress tlsAddress = new InetSocketAddress("127.0.0.1", 12024);
42
43     private DefaultCommitNotificationProducer commitNot;
44     private NetconfServerDispatcher dispatchS;
45
46
47     @Before
48     public void setUp() throws Exception {
49         super.initConfigTransactionManagerImpl(new HardcodedModuleFactoriesResolver(mockedContext,getModuleFactories().toArray(
50                 new ModuleFactory[0])));
51
52         NetconfOperationServiceFactoryListenerImpl factoriesListener = new NetconfOperationServiceFactoryListenerImpl();
53         factoriesListener.onAddNetconfOperationServiceFactory(new NetconfOperationServiceFactoryImpl(getYangStore()));
54
55         commitNot = new DefaultCommitNotificationProducer(ManagementFactory.getPlatformMBeanServer());
56
57
58         dispatchS = createDispatcher(factoriesListener);
59         ChannelFuture s = dispatchS.createServer(tlsAddress);
60         s.await();
61     }
62
63     private NetconfServerDispatcher createDispatcher(NetconfOperationServiceFactoryListenerImpl factoriesListener) {
64         return super.createDispatcher(factoriesListener, NetconfITTest.getNetconfMonitoringListenerService(), commitNot);
65     }
66
67     @After
68     public void tearDown() throws Exception {
69         commitNot.close();
70     }
71
72     private SSLContext getSslContext() throws KeyStoreException, NoSuchAlgorithmException, CertificateException,
73             IOException, UnrecoverableKeyException, KeyManagementException {
74         final InputStream keyStore = getClass().getResourceAsStream("/keystore.jks");
75         final InputStream trustStore = getClass().getResourceAsStream("/keystore.jks");
76         SSLContext sslContext = SSLUtil.initializeSecureContext("password", keyStore, trustStore, KeyManagerFactory.getDefaultAlgorithm());
77         keyStore.close();
78         trustStore.close();
79         return sslContext;
80     }
81
82     private HardcodedYangStoreService getYangStore() throws YangStoreException, IOException {
83         final Collection<InputStream> yangDependencies = NetconfITTest.getBasicYangs();
84         return new HardcodedYangStoreService(yangDependencies);
85     }
86
87     protected List<ModuleFactory> getModuleFactories() {
88         return NetconfITTest.getModuleFactoriesS();
89     }
90
91     @Test
92     public void testSecure() throws Exception {
93         NetconfClientDispatcher dispatch = new NetconfClientDispatcher(nettyThreadgroup, nettyThreadgroup, 5000);
94         try (TestingNetconfClient netconfClient = new TestingNetconfClient("tls-client", tlsAddress, 4000, dispatch))  {
95
96         }
97     }
98 }