Split up OSGiCallHomeProvider
[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.mdsal.binding.api.DataBroker;
14 import org.opendaylight.netconf.callhome.protocol.CallHomeNetconfSubsystemListener;
15 import org.opendaylight.netconf.callhome.protocol.tls.NetconfCallHomeTlsServer;
16 import org.opendaylight.netconf.callhome.protocol.tls.NetconfCallHomeTlsServerBuilder;
17 import org.opendaylight.netconf.callhome.protocol.tls.TlsAllowedDevicesMonitor;
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 DataBroker dataBroker,
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         // FIXME: tie together with OSGi Config Admin
38         this(dataBroker, allowedDevicesMonitor, subsystemListener, bossGroup, workerGroup, defaultTlsConfiguration());
39     }
40
41     public NetconfCallHomeTlsService(final DataBroker dataBroker,
42                                      final TlsAllowedDevicesMonitor allowedDevicesMonitor,
43                                      final CallHomeNetconfSubsystemListener subsystemListener,
44                                      final EventLoopGroup bossGroup,
45                                      final EventLoopGroup workerGroup, final Configuration config) {
46         LOG.info("Initializing Call Home TLS server instance");
47         server = new NetconfCallHomeTlsServerBuilder()
48             .setHost(config.getHost())
49             .setPort(config.getPort())
50             .setTimeout(config.getTimeout())
51             .setMaxConnections(config.getMaxConnections())
52             .setAllowedDevicesMonitor(requireNonNull(allowedDevicesMonitor))
53             .setSslHandlerFactory(new SslHandlerFactoryAdapter(dataBroker, allowedDevicesMonitor))
54             .setSubsystemListener(requireNonNull(subsystemListener))
55             .setBossGroup(requireNonNull(bossGroup))
56             .setWorkerGroup(requireNonNull(workerGroup))
57             .build();
58         server.start();
59
60         LOG.info("Initializing Call Home TLS server instance completed successfuly");
61     }
62
63     private static Configuration defaultTlsConfiguration() {
64         final var conf = new Configuration();
65         conf.setHost("0.0.0.0");
66         conf.setPort(4335);
67         conf.setTimeout(10_000);
68         conf.setMaxConnections(64);
69         return conf;
70     }
71
72     @Deactivate
73     @Override
74     public void close() {
75         server.stop();
76     }
77 }