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