NetconfClientDispatcher moved to constructor arguments in NetconfClient
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfClient.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.netconf.client;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.Sets;
13 import io.netty.util.concurrent.Future;
14 import io.netty.util.concurrent.GlobalEventExecutor;
15 import org.opendaylight.controller.netconf.api.NetconfMessage;
16 import org.opendaylight.protocol.framework.NeverReconnectStrategy;
17 import org.opendaylight.protocol.framework.ReconnectStrategy;
18 import org.opendaylight.protocol.framework.TimedReconnectStrategy;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import java.io.Closeable;
23 import java.io.IOException;
24 import java.net.InetSocketAddress;
25 import java.util.Set;
26 import java.util.concurrent.CancellationException;
27 import java.util.concurrent.ExecutionException;
28
29 public class NetconfClient implements Closeable {
30
31     private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
32
33     public static final int DEFAULT_CONNECT_TIMEOUT = 5000;
34     private final NetconfClientDispatcher dispatch;
35     private final String label;
36     private final NetconfClientSession clientSession;
37     private final NetconfClientSessionListener sessionListener;
38     private final long sessionId;
39     private final InetSocketAddress address;
40
41     // TODO test reconnecting constructor
42     public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectionAttempts,
43             int attemptMsTimeout, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
44         this(clientLabelForLogging, address, getReconnectStrategy(connectionAttempts, attemptMsTimeout),
45                 netconfClientDispatcher);
46     }
47
48     private NetconfClient(String clientLabelForLogging, InetSocketAddress address, ReconnectStrategy strat, NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
49         this.label = clientLabelForLogging;
50         dispatch = netconfClientDispatcher;
51
52         sessionListener = new NetconfClientSessionListener();
53         Future<NetconfClientSession> clientFuture = dispatch.createClient(address, sessionListener, strat);
54         this.address = address;
55         clientSession = get(clientFuture);
56         this.sessionId = clientSession.getSessionId();
57     }
58
59     private NetconfClientSession get(Future<NetconfClientSession> clientFuture) throws InterruptedException {
60         try {
61             return clientFuture.get();
62         } catch (CancellationException e) {
63             throw new RuntimeException("Netconf client interrupted", e);
64         } catch (ExecutionException e) {
65             throw new IllegalStateException("Unable to create netconf client", e);
66         }
67     }
68
69     public NetconfClient(String clientLabelForLogging, InetSocketAddress address, int connectTimeoutMs,
70             NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
71         this(clientLabelForLogging, address,
72                 new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE, connectTimeoutMs), netconfClientDispatcher);
73     }
74
75     public NetconfClient(String clientLabelForLogging, InetSocketAddress address,
76             NetconfClientDispatcher netconfClientDispatcher) throws InterruptedException {
77         this(clientLabelForLogging, address, new NeverReconnectStrategy(GlobalEventExecutor.INSTANCE,
78                 DEFAULT_CONNECT_TIMEOUT), netconfClientDispatcher);
79     }
80
81     public NetconfMessage sendMessage(NetconfMessage message) {
82         return sendMessage(message, 5, 1000);
83     }
84
85     public NetconfMessage sendMessage(NetconfMessage message, int attempts, int attemptMsDelay) {
86         Preconditions.checkState(clientSession.isUp(), "Session was not up yet");
87         clientSession.sendMessage(message);
88         try {
89             return sessionListener.getLastMessage(attempts, attemptMsDelay);
90         } catch (InterruptedException e) {
91             throw new RuntimeException(this + " Cannot read message from " + address, e);
92         } catch (IllegalStateException e) {
93             throw new IllegalStateException(this + " Cannot read message from " + address, e);
94         }
95     }
96
97     @Override
98     public void close() throws IOException {
99         clientSession.close();
100     }
101
102     private static ReconnectStrategy getReconnectStrategy(int connectionAttempts, int attemptMsTimeout) {
103         return new TimedReconnectStrategy(GlobalEventExecutor.INSTANCE, attemptMsTimeout, 1000, 1.0, null,
104                 Long.valueOf(connectionAttempts), null);
105     }
106
107     @Override
108     public String toString() {
109         final StringBuffer sb = new StringBuffer("NetconfClient{");
110         sb.append("label=").append(label);
111         sb.append(", sessionId=").append(sessionId);
112         sb.append('}');
113         return sb.toString();
114     }
115
116     public long getSessionId() {
117         return sessionId;
118     }
119
120     public Set<String> getCapabilities() {
121         Preconditions.checkState(clientSession != null, "Client was not initialized successfully");
122         return Sets.newHashSet(clientSession.getServerCapabilities());
123     }
124 }