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