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