Remove use of yangtools.concepts.Builder
[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.shaded.sshd.client.SshClient;
19
20 public class NetconfCallHomeServerBuilder {
21     private static final long DEFAULT_SESSION_TIMEOUT_MILLIS = TimeUnit.SECONDS.toMillis(5);
22     private static final int DEFAULT_CALL_HOME_PORT = 4334;
23
24     private SshClient sshClient;
25     private EventLoopGroup nettyGroup;
26     private NetconfClientSessionNegotiatorFactory negotiationFactory;
27     private InetSocketAddress bindAddress;
28
29     private final CallHomeAuthorizationProvider authProvider;
30     private final CallHomeNetconfSubsystemListener subsystemListener;
31     private final StatusRecorder recorder;
32
33     public NetconfCallHomeServerBuilder(final CallHomeAuthorizationProvider authProvider,
34             final CallHomeNetconfSubsystemListener subsystemListener, final StatusRecorder recorder) {
35         this.authProvider = authProvider;
36         this.subsystemListener = subsystemListener;
37         this.recorder = recorder;
38     }
39
40     public @NonNull NetconfCallHomeServer build() {
41         return new NetconfCallHomeServer(sshClient(), authProvider(),
42             new CallHomeSessionContext.Factory(nettyGroup(), negotiatorFactory(), subsystemListener()),
43             bindAddress(), recorder);
44     }
45
46     public SshClient getSshClient() {
47         return sshClient;
48     }
49
50     public void setSshClient(final SshClient sshClient) {
51         this.sshClient = sshClient;
52     }
53
54     public EventLoopGroup getNettyGroup() {
55         return nettyGroup;
56     }
57
58     public void setNettyGroup(final EventLoopGroup nettyGroup) {
59         this.nettyGroup = nettyGroup;
60     }
61
62     public NetconfClientSessionNegotiatorFactory getNegotiationFactory() {
63         return negotiationFactory;
64     }
65
66     public void setNegotiationFactory(final NetconfClientSessionNegotiatorFactory negotiationFactory) {
67         this.negotiationFactory = negotiationFactory;
68     }
69
70     public InetSocketAddress getBindAddress() {
71         return bindAddress;
72     }
73
74     public void setBindAddress(final InetSocketAddress bindAddress) {
75         this.bindAddress = bindAddress;
76     }
77
78     public CallHomeAuthorizationProvider getAuthProvider() {
79         return authProvider;
80     }
81
82     private InetSocketAddress bindAddress() {
83         return bindAddress != null ? bindAddress : defaultBindAddress();
84     }
85
86     private EventLoopGroup nettyGroup() {
87         return nettyGroup != null ? nettyGroup : defaultNettyGroup();
88     }
89
90     private NetconfClientSessionNegotiatorFactory negotiatorFactory() {
91         return negotiationFactory != null ? negotiationFactory : defaultNegotiationFactory();
92     }
93
94     private CallHomeNetconfSubsystemListener subsystemListener() {
95         return subsystemListener;
96     }
97
98     private CallHomeAuthorizationProvider authProvider() {
99         return authProvider;
100     }
101
102     private SshClient sshClient() {
103         return sshClient != null ? sshClient : defaultSshClient();
104     }
105
106     private static SshClient defaultSshClient() {
107         return SshClient.setUpDefaultClient();
108     }
109
110     private static NetconfClientSessionNegotiatorFactory defaultNegotiationFactory() {
111         return new NetconfClientSessionNegotiatorFactory(new HashedWheelTimer(),
112                                                          Optional.empty(), DEFAULT_SESSION_TIMEOUT_MILLIS);
113     }
114
115     private static EventLoopGroup defaultNettyGroup() {
116         return new DefaultEventLoopGroup();
117     }
118
119     private static InetSocketAddress defaultBindAddress() {
120         return new InetSocketAddress(DEFAULT_CALL_HOME_PORT);
121     }
122 }