Split up NetconfKeystoreAdapter
[netconf.git] / apps / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / tls / NetconfCallHomeTlsService.java
1 /*
2  * Copyright (c) 2020 Pantheon Technologies, s.r.o. 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.netconf.callhome.mount.tls;
9
10 import static java.util.Objects.requireNonNull;
11
12 import io.netty.channel.EventLoopGroup;
13 import org.opendaylight.netconf.callhome.protocol.CallHomeNetconfSubsystemListener;
14 import org.opendaylight.netconf.callhome.protocol.tls.NetconfCallHomeTlsServer;
15 import org.opendaylight.netconf.callhome.protocol.tls.NetconfCallHomeTlsServerBuilder;
16 import org.opendaylight.netconf.callhome.protocol.tls.TlsAllowedDevicesMonitor;
17 import org.opendaylight.netconf.client.mdsal.api.KeyStoreProvider;
18 import org.osgi.service.component.annotations.Activate;
19 import org.osgi.service.component.annotations.Component;
20 import org.osgi.service.component.annotations.Deactivate;
21 import org.osgi.service.component.annotations.Reference;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 @Component(service = { })
26 public class NetconfCallHomeTlsService implements AutoCloseable {
27     private static final Logger LOG = LoggerFactory.getLogger(NetconfCallHomeTlsService.class);
28
29     private final NetconfCallHomeTlsServer server;
30
31     @Activate
32     public NetconfCallHomeTlsService(@Reference final KeyStoreProvider keyStoreProvider,
33             @Reference final TlsAllowedDevicesMonitor allowedDevicesMonitor,
34             @Reference final CallHomeNetconfSubsystemListener subsystemListener,
35             @Reference(target = "(type=global-boss-group)") final EventLoopGroup bossGroup,
36             @Reference(target = "(type=global-worker-group)") final EventLoopGroup workerGroup) {
37         this(keyStoreProvider, allowedDevicesMonitor, subsystemListener, bossGroup, workerGroup,
38             // FIXME: tie together with OSGi Config Admin
39             defaultTlsConfiguration());
40     }
41
42     public NetconfCallHomeTlsService(final KeyStoreProvider keyStoreProvider,
43                                      final TlsAllowedDevicesMonitor allowedDevicesMonitor,
44                                      final CallHomeNetconfSubsystemListener subsystemListener,
45                                      final EventLoopGroup bossGroup,
46                                      final EventLoopGroup workerGroup, final Configuration config) {
47         LOG.info("Initializing Call Home TLS server instance");
48         server = new NetconfCallHomeTlsServerBuilder()
49             .setHost(config.getHost())
50             .setPort(config.getPort())
51             .setTimeout(config.getTimeout())
52             .setMaxConnections(config.getMaxConnections())
53             .setAllowedDevicesMonitor(requireNonNull(allowedDevicesMonitor))
54             .setSslHandlerFactory(new SslHandlerFactoryAdapter(keyStoreProvider, allowedDevicesMonitor))
55             .setSubsystemListener(requireNonNull(subsystemListener))
56             .setBossGroup(requireNonNull(bossGroup))
57             .setWorkerGroup(requireNonNull(workerGroup))
58             .build();
59         server.start();
60
61         LOG.info("Initializing Call Home TLS server instance completed successfuly");
62     }
63
64     private static Configuration defaultTlsConfiguration() {
65         final var conf = new Configuration();
66         conf.setHost("0.0.0.0");
67         conf.setPort(4335);
68         conf.setTimeout(10_000);
69         conf.setMaxConnections(64);
70         return conf;
71     }
72
73     @Deactivate
74     @Override
75     public void close() {
76         server.stop();
77     }
78 }