fix ServiceHandler SpotBugs false positives
[transportpce.git] / tests / honeynode / 1.2.1 / netconf / src / main / java / io / fd / honeycomb / northbound / netconf / NetconfSshServerProvider.java
1 /*
2  * Copyright (c) 2017 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.northbound.netconf;
18
19 import com.google.inject.Inject;
20 import io.fd.honeycomb.binding.init.ProviderTrait;
21 import io.fd.honeycomb.data.init.ShutdownHandler;
22 import io.fd.honeycomb.northbound.CredentialsConfiguration;
23 import io.fd.honeycomb.northbound.NetconfConfiguration;
24 import io.netty.channel.nio.NioEventLoopGroup;
25 import io.netty.util.concurrent.GlobalEventExecutor;
26 import org.opendaylight.netconf.api.NetconfServerDispatcher;
27 import org.opendaylight.netconf.auth.AuthProvider;
28 import org.opendaylight.netconf.ssh.NetconfNorthboundSshServer;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public final class NetconfSshServerProvider extends ProviderTrait<NetconfNorthboundSshServer> {
33
34     private static final Logger LOG = LoggerFactory.getLogger(NetconfSshServerProvider.class);
35
36     @Inject
37     private NetconfServerDispatcher dispatcher;
38     @Inject
39     private NetconfConfiguration cfgAttributes;
40     @Inject
41     private NioEventLoopGroup nettyThreadgroup;
42     @Inject
43     private CredentialsConfiguration credentialsCfg;
44     @Inject
45     private ShutdownHandler shutdownHandler;
46
47     @Override
48     protected NetconfNorthboundSshServer create() {
49         if (!cfgAttributes.isNetconfSshEnabled()) {
50             LOG.info("NETCONF SSH disabled, skipping initialization");
51             return null;
52         }
53         LOG.info("Starting NETCONF SSH");
54
55         final NetconfNorthboundSshServer netconfServer = new NetconfNorthboundSshServer(dispatcher, nettyThreadgroup,
56             GlobalEventExecutor.INSTANCE, cfgAttributes.netconfSshBindingAddress.get(),
57             cfgAttributes.netconfSshBindingPort.get().toString(), new SimplelAuthProvider(credentialsCfg));
58         shutdownHandler.register("netconf-northbound-ssh-server", netconfServer::close);
59         return netconfServer;
60     }
61
62     private static final class SimplelAuthProvider implements AuthProvider {
63
64         private final CredentialsConfiguration cfgAttributes;
65
66         SimplelAuthProvider(final CredentialsConfiguration cfgAttributes) {
67             this.cfgAttributes = cfgAttributes;
68         }
69
70         @Override
71         public boolean authenticated(final String uname, final String passwd) {
72             return cfgAttributes.username.equals(uname) && cfgAttributes.password.equals(passwd);
73         }
74     }
75 }