Reuse NetconfClientBuilder in NetconfCallHomeServer
[netconf.git] / netconf / callhome-protocol / src / main / java / org / opendaylight / netconf / callhome / protocol / NetconfCallHomeServerBuilder.java
1 /*
2  * Copyright (c) 2016 Brocade Communication Systems 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.protocol;
9
10 import io.netty.channel.DefaultEventLoopGroup;
11 import io.netty.channel.EventLoopGroup;
12 import io.netty.util.HashedWheelTimer;
13 import java.net.InetSocketAddress;
14 import java.util.Optional;
15 import java.util.concurrent.TimeUnit;
16 import org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.netconf.client.NetconfClientSessionNegotiatorFactory;
18 import org.opendaylight.netconf.nettyutil.handler.ssh.client.NetconfClientBuilder;
19 import org.opendaylight.netconf.shaded.sshd.client.SshClient;
20
21 public class NetconfCallHomeServerBuilder {
22     private static final long DEFAULT_SESSION_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(5);
23     private static final int DEFAULT_CALL_HOME_PORT = 4334;
24
25     private SshClient sshClient;
26     private EventLoopGroup nettyGroup;
27     private NetconfClientSessionNegotiatorFactory negotiationFactory;
28     private InetSocketAddress bindAddress;
29
30     private final CallHomeAuthorizationProvider authProvider;
31     private final CallHomeNetconfSubsystemListener subsystemListener;
32     private final StatusRecorder recorder;
33
34     public NetconfCallHomeServerBuilder(final CallHomeAuthorizationProvider authProvider,
35             final CallHomeNetconfSubsystemListener subsystemListener, final StatusRecorder recorder) {
36         this.authProvider = authProvider;
37         this.subsystemListener = subsystemListener;
38         this.recorder = recorder;
39     }
40
41     public @NonNull NetconfCallHomeServer build() {
42         return new NetconfCallHomeServer(sshClient(), authProvider(),
43             new CallHomeSessionContext.Factory(nettyGroup(), negotiatorFactory(), subsystemListener()),
44             bindAddress(), recorder);
45     }
46
47     public SshClient getSshClient() {
48         return sshClient;
49     }
50
51     public void setSshClient(final SshClient sshClient) {
52         this.sshClient = sshClient;
53     }
54
55     public EventLoopGroup getNettyGroup() {
56         return nettyGroup;
57     }
58
59     public void setNettyGroup(final EventLoopGroup nettyGroup) {
60         this.nettyGroup = nettyGroup;
61     }
62
63     public NetconfClientSessionNegotiatorFactory getNegotiationFactory() {
64         return negotiationFactory;
65     }
66
67     public void setNegotiationFactory(final NetconfClientSessionNegotiatorFactory negotiationFactory) {
68         this.negotiationFactory = negotiationFactory;
69     }
70
71     public InetSocketAddress getBindAddress() {
72         return bindAddress;
73     }
74
75     public void setBindAddress(final InetSocketAddress bindAddress) {
76         this.bindAddress = bindAddress;
77     }
78
79     public CallHomeAuthorizationProvider getAuthProvider() {
80         return authProvider;
81     }
82
83     private InetSocketAddress bindAddress() {
84         return bindAddress != null ? bindAddress : defaultBindAddress();
85     }
86
87     private EventLoopGroup nettyGroup() {
88         return nettyGroup != null ? nettyGroup : defaultNettyGroup();
89     }
90
91     private NetconfClientSessionNegotiatorFactory negotiatorFactory() {
92         return negotiationFactory != null ? negotiationFactory : defaultNegotiationFactory();
93     }
94
95     private CallHomeNetconfSubsystemListener subsystemListener() {
96         return subsystemListener;
97     }
98
99     private CallHomeAuthorizationProvider authProvider() {
100         return authProvider;
101     }
102
103     private SshClient sshClient() {
104         return sshClient != null ? sshClient : defaultSshClient();
105     }
106
107     private static SshClient defaultSshClient() {
108         return new NetconfClientBuilder().build();
109     }
110
111     private static NetconfClientSessionNegotiatorFactory defaultNegotiationFactory() {
112         return new NetconfClientSessionNegotiatorFactory(new HashedWheelTimer(),
113                                                          Optional.empty(), DEFAULT_SESSION_TIMEOUT_MILLIS);
114     }
115
116     private static EventLoopGroup defaultNettyGroup() {
117         return new DefaultEventLoopGroup();
118     }
119
120     private static InetSocketAddress defaultBindAddress() {
121         return new InetSocketAddress(DEFAULT_CALL_HOME_PORT);
122     }
123 }