Add changed-leaf-nodes-only subscription extension
[netconf.git] / netconf / callhome-provider / src / main / java / org / opendaylight / netconf / callhome / mount / tls / SslHandlerFactoryAdapter.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 io.netty.handler.ssl.SslHandler;
11 import java.util.Set;
12 import org.opendaylight.mdsal.binding.api.DataBroker;
13 import org.opendaylight.netconf.callhome.protocol.tls.TlsAllowedDevicesMonitor;
14 import org.opendaylight.netconf.client.SslHandlerFactory;
15 import org.opendaylight.netconf.sal.connect.netconf.sal.NetconfKeystoreAdapter;
16 import org.opendaylight.netconf.sal.connect.util.SslHandlerFactoryImpl;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 public class SslHandlerFactoryAdapter implements SslHandlerFactory {
21     private static final Logger LOG = LoggerFactory.getLogger(SslHandlerFactoryAdapter.class);
22
23     private final TlsAllowedDevicesMonitor allowedDevicesMonitor;
24     private final SslHandlerFactory sslHandlerFactory;
25
26     public SslHandlerFactoryAdapter(final DataBroker dataBroker, final TlsAllowedDevicesMonitor allowedDevicesMonitor) {
27         final NetconfKeystoreAdapter keystoreAdapter = new NetconfKeystoreAdapter(dataBroker);
28         this.sslHandlerFactory = new SslHandlerFactoryImpl(keystoreAdapter);
29         this.allowedDevicesMonitor = allowedDevicesMonitor;
30     }
31
32     @Override
33     public SslHandler createSslHandler() {
34         return createSslHandlerFilteredByKeys();
35     }
36
37     @Override
38     public SslHandler createSslHandler(final Set<String> allowedKeys) {
39         return createSslHandlerFilteredByKeys();
40     }
41
42     private SslHandler createSslHandlerFilteredByKeys() {
43         if (allowedDevicesMonitor.findAllowedKeys().isEmpty()) {
44             LOG.error("No associated keys for TLS authentication were found");
45             throw new IllegalStateException("No associated keys for TLS authentication were found");
46         }
47         return sslHandlerFactory.createSslHandler(allowedDevicesMonitor.findAllowedKeys());
48     }
49 }