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