Optimize DefaultNetconfKeystoreAdapter
[netconf.git] / apps / 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 static java.util.Objects.requireNonNull;
11
12 import io.netty.handler.ssl.SslHandler;
13 import java.util.Set;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.netconf.callhome.protocol.tls.TlsAllowedDevicesMonitor;
16 import org.opendaylight.netconf.client.SslHandlerFactory;
17 import org.opendaylight.netconf.client.mdsal.api.NetconfKeystoreAdapter;
18 import org.opendaylight.netconf.sal.connect.util.SslHandlerFactoryImpl;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 public class SslHandlerFactoryAdapter implements SslHandlerFactory {
23     private static final Logger LOG = LoggerFactory.getLogger(SslHandlerFactoryAdapter.class);
24
25     private final TlsAllowedDevicesMonitor allowedDevicesMonitor;
26     private final SslHandlerFactory sslHandlerFactory;
27
28     public SslHandlerFactoryAdapter(final NetconfKeystoreAdapter keystoreAdapter,
29             final @NonNull TlsAllowedDevicesMonitor allowedDevicesMonitor) {
30         this.allowedDevicesMonitor = requireNonNull(allowedDevicesMonitor);
31         sslHandlerFactory = new SslHandlerFactoryImpl(keystoreAdapter);
32     }
33
34     @Override
35     public SslHandler createSslHandler() {
36         return createSslHandlerFilteredByKeys();
37     }
38
39     @Override
40     public SslHandler createSslHandler(final Set<String> allowedKeys) {
41         // FIXME: we are ignoring passed in keys?!
42         return createSslHandlerFilteredByKeys();
43     }
44
45     private SslHandler createSslHandlerFilteredByKeys() {
46         final var allowedKeys = allowedDevicesMonitor.findAllowedKeys();
47         if (allowedKeys.isEmpty()) {
48             LOG.error("No associated keys for TLS authentication were found");
49             throw new IllegalStateException("No associated keys for TLS authentication were found");
50         }
51         return sslHandlerFactory.createSslHandler(allowedKeys);
52     }
53 }