Merge "Option to receive only leaf nodes in websocket notifs"
[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
36     public NetconfCallHomeServerBuilder(CallHomeAuthorizationProvider authProvider,
37             CallHomeNetconfSubsystemListener subsystemListener) {
38         this.authProvider = authProvider;
39         this.subsystemListener = subsystemListener;
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());
47     }
48
49     public SshClient getSshClient() {
50         return sshClient;
51     }
52
53     public void setSshClient(SshClient sshClient) {
54         this.sshClient = sshClient;
55     }
56
57     public EventLoopGroup getNettyGroup() {
58         return nettyGroup;
59     }
60
61     public void setNettyGroup(EventLoopGroup nettyGroup) {
62         this.nettyGroup = nettyGroup;
63     }
64
65     public NetconfClientSessionNegotiatorFactory getNegotiationFactory() {
66         return negotiationFactory;
67     }
68
69     public void setNegotiationFactory(NetconfClientSessionNegotiatorFactory negotiationFactory) {
70         this.negotiationFactory = negotiationFactory;
71     }
72
73     public InetSocketAddress getBindAddress() {
74         return bindAddress;
75     }
76
77     public void setBindAddress(InetSocketAddress bindAddress) {
78         this.bindAddress = bindAddress;
79     }
80
81     public CallHomeAuthorizationProvider getAuthProvider() {
82         return authProvider;
83     }
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 SshClient defaultSshClient() {
111         return SshClient.setUpDefaultClient();
112     }
113
114     private NetconfClientSessionNegotiatorFactory defaultNegotiationFactory() {
115         return new NetconfClientSessionNegotiatorFactory(new HashedWheelTimer(),
116                 Optional.<NetconfHelloMessageAdditionalHeader>absent(), DEFAULT_SESSION_TIMEOUT_MILLIS);
117     }
118
119     private EventLoopGroup defaultNettyGroup() {
120         return new LocalEventLoopGroup();
121     }
122
123     private InetSocketAddress defaultBindAddress() {
124         return new InetSocketAddress(DEFAULT_CALL_HOME_PORT);
125     }
126
127 }