2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.openflowjava.protocol.impl.connection;
11 import io.netty.channel.Channel;
12 import io.netty.channel.ChannelFuture;
13 import io.netty.util.concurrent.GenericFutureListener;
15 import java.util.Collection;
16 import java.util.concurrent.Future;
17 import java.util.concurrent.TimeUnit;
19 import org.opendaylight.controller.sal.common.util.RpcErrors;
20 import org.opendaylight.controller.sal.common.util.Rpcs;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestMessage;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
48 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
49 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
50 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
51 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
52 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
53 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
54 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
55 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
56 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
57 import org.opendaylight.yangtools.yang.binding.DataObject;
58 import org.opendaylight.yangtools.yang.binding.Notification;
59 import org.opendaylight.yangtools.yang.common.RpcError;
60 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
61 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
62 import org.opendaylight.yangtools.yang.common.RpcResult;
63 import org.slf4j.Logger;
64 import org.slf4j.LoggerFactory;
66 import com.google.common.cache.Cache;
67 import com.google.common.cache.CacheBuilder;
68 import com.google.common.cache.RemovalListener;
69 import com.google.common.cache.RemovalNotification;
70 import com.google.common.collect.Lists;
71 import com.google.common.util.concurrent.SettableFuture;
75 * @author michal.polkorab
77 public class ConnectionAdapterImpl implements ConnectionFacade {
79 /** after this time, rpc future response objects will be thrown away (in minutes) */
80 public static final int RPC_RESPONSE_EXPIRATION = 1;
82 protected static final Logger LOG = LoggerFactory
83 .getLogger(ConnectionAdapterImpl.class);
85 private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
86 private static final String TAG = "OPENFLOW";
87 private Channel channel;
88 private OpenflowProtocolListener messageListener;
89 /** expiring cache for future rpcResponses */
90 protected Cache<RpcResponseKey, SettableFuture<?>> responseCache;
96 public ConnectionAdapterImpl() {
97 responseCache = CacheBuilder.newBuilder()
99 .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES)
100 .removalListener(new RemovalListener<RpcResponseKey, SettableFuture<?>>() {
103 public void onRemoval(
104 RemovalNotification<RpcResponseKey, SettableFuture<?>> notification) {
105 LOG.warn("rpc response discarded: "+notification.getKey());
106 notification.getValue().cancel(true);
109 LOG.info("ConnectionAdapter created");
113 * @param channel the channel to be set - used for communication
115 public void setChannel(Channel channel) {
116 this.channel = channel;
120 public Future<RpcResult<BarrierOutput>> barrier(BarrierInput input) {
121 return sendToSwitchExpectRpcResultFuture(
122 input, BarrierOutput.class, "barrier-input sending failed");
126 public Future<RpcResult<EchoOutput>> echo(EchoInput input) {
127 return sendToSwitchExpectRpcResultFuture(
128 input, EchoOutput.class, "echo-input sending failed");
132 public Future<RpcResult<Void>> echoReply(EchoReplyInput input) {
133 return sendToSwitchFuture(input, "echo-reply sending failed");
137 public Future<RpcResult<Void>> experimenter(ExperimenterInput input) {
138 return sendToSwitchFuture(input, "experimenter sending failed");
142 public Future<RpcResult<Void>> flowMod(FlowModInput input) {
143 return sendToSwitchFuture(input, "flow-mod sending failed");
147 public Future<RpcResult<GetConfigOutput>> getConfig(GetConfigInput input) {
148 return sendToSwitchExpectRpcResultFuture(
149 input, GetConfigOutput.class, "get-config-input sending failed");
153 public Future<RpcResult<GetFeaturesOutput>> getFeatures(
154 GetFeaturesInput input) {
155 return sendToSwitchExpectRpcResultFuture(
156 input, GetFeaturesOutput.class, "get-features-input sending failed");
160 public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
161 GetQueueConfigInput input) {
162 return sendToSwitchExpectRpcResultFuture(
163 input, GetQueueConfigOutput.class, "get-queue-config-input sending failed");
167 public Future<RpcResult<Void>> groupMod(GroupModInput input) {
168 return sendToSwitchFuture(input, "group-mod-input sending failed");
172 public Future<RpcResult<Void>> hello(HelloInput input) {
173 return sendToSwitchFuture(input, "hello-input sending failed");
177 public Future<RpcResult<Void>> meterMod(MeterModInput input) {
178 return sendToSwitchFuture(input, "meter-mod-input sending failed");
182 public Future<RpcResult<Void>> packetOut(PacketOutInput input) {
183 return sendToSwitchFuture(input, "packet-out-input sending failed");
187 public Future<RpcResult<Void>> portMod(PortModInput input) {
188 return sendToSwitchFuture(input, "port-mod-input sending failed");
192 public Future<RpcResult<RoleRequestOutput>> roleRequest(
193 RoleRequestInput input) {
194 return sendToSwitchExpectRpcResultFuture(
195 input, RoleRequestOutput.class, "role-request-config-input sending failed");
199 public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
200 return sendToSwitchFuture(input, "set-config-input sending failed");
204 public Future<RpcResult<Void>> tableMod(TableModInput input) {
205 return sendToSwitchFuture(input, "table-mod-input sending failed");
209 public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
210 return sendToSwitchExpectRpcResultFuture(
211 input, GetAsyncOutput.class, "get-async-input sending failed");
215 public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
216 return sendToSwitchFuture(input, "set-async-input sending failed");
220 public Future<Boolean> disconnect() {
221 ChannelFuture disconnectResult = channel.disconnect();
223 String failureInfo = "switch disconnecting failed";
224 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
225 String message = "Check the switch connection";
226 return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
230 public boolean isAlive() {
231 return channel.isOpen();
235 public void setMessageListener(OpenflowProtocolListener messageListener) {
236 this.messageListener = messageListener;
240 public void consume(DataObject message) {
241 if (message instanceof Notification) {
242 if (message instanceof EchoRequestMessage) {
243 messageListener.onEchoRequestMessage((EchoRequestMessage) message);
244 } else if (message instanceof ErrorMessage) {
245 messageListener.onErrorMessage((ErrorMessage) message);
246 } else if (message instanceof ExperimenterMessage) {
247 messageListener.onExperimenterMessage((ExperimenterMessage) message);
248 } else if (message instanceof FlowRemovedMessage) {
249 messageListener.onFlowRemovedMessage((FlowRemovedMessage) message);
250 } else if (message instanceof HelloMessage) {
251 LOG.info("Hello received / branch");
252 messageListener.onHelloMessage((HelloMessage) message);
253 } else if (message instanceof MultipartReplyMessage) {
254 messageListener.onMultipartReplyMessage((MultipartReplyMessage) message);
255 } else if (message instanceof MultipartRequestMessage) {
256 messageListener.onMultipartRequestMessage((MultipartRequestMessage) message);
257 } else if (message instanceof PacketInMessage) {
258 messageListener.onPacketInMessage((PacketInMessage) message);
259 } else if (message instanceof PortStatusMessage) {
260 messageListener.onPortStatusMessage((PortStatusMessage) message);
262 LOG.warn("message listening not supported for type: "+message.getClass());
265 if (message instanceof OfHeader) {
266 RpcResponseKey key = createRpcResponseKey((OfHeader) message);
267 SettableFuture<RpcResult<?>> rpcFuture = findRpcResponse(key);
268 if (rpcFuture != null) {
269 rpcFuture.set(Rpcs.getRpcResult(true, message, null));
270 responseCache.invalidate(key);
272 LOG.warn("received unexpected rpc response: "+key);
276 LOG.warn("message listening not supported for type: "+message.getClass());
282 * sends given message to switch, sending result will be reported via return value
283 * @param input message to send
284 * @param failureInfo describes, what type of message caused failure by sending
285 * @return future object, <ul>
286 * <li>if send successful, {@link RpcResult} without errors and successful
287 * status will be returned, </li>
288 * <li>else {@link RpcResult} will contain errors and failed status</li>
291 private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
292 DataObject input, final String failureInfo) {
293 ChannelFuture resultFuture = channel.writeAndFlush(input);
295 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
296 String errorMessage = "check switch connection";
297 return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, errorMessage);
301 * sends given message to switch, sending result or switch response will be reported via return value
302 * @param input message to send
303 * @param responseClazz type of response
304 * @param failureInfo describes, what type of message caused failure by sending
305 * @return future object, <ul>
306 * <li>if send fails, {@link RpcResult} will contain errors and failed status </li>
307 * <li>else {@link RpcResult} will be stored in responseCache and wait for particular timeout
308 * ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}),
309 * <ul><li>either switch will manage to answer
310 * and then corresponding response message will be set into returned future</li>
311 * <li>or response in cache will expire and returned future will be cancelled</li></ul>
315 private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> sendToSwitchExpectRpcResultFuture(
316 IN input, Class<OUT> responseClazz, final String failureInfo) {
317 ChannelFuture resultFuture = channel.writeAndFlush(input);
319 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
320 String errorMessage = "check switch connection";
321 return handleRpcChannelFutureWithResponse(resultFuture, failureInfo, errorSeverity,
322 errorMessage, input, responseClazz);
326 * @param resultFuture
330 private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
331 ChannelFuture resultFuture, final String failureInfo,
332 final ErrorSeverity errorSeverity, final String errorMessage) {
334 final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
336 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
339 public void operationComplete(
340 io.netty.util.concurrent.Future<? super Void> future)
342 Collection<RpcError> errors = null;
344 if (future.cause() != null) {
345 RpcError rpcError = buildRpcError(failureInfo,
346 errorSeverity, errorMessage, future.cause());
347 errors = Lists.newArrayList(rpcError);
350 rpcResult.set(Rpcs.getRpcResult(
362 * @param responseClazz
363 * @param resultFuture
365 * @param errorSeverity
366 * @param errorMessage
369 private <IN extends OfHeader, OUT extends OfHeader> SettableFuture<RpcResult<OUT>> handleRpcChannelFutureWithResponse(
370 ChannelFuture resultFuture, final String failureInfo,
371 final ErrorSeverity errorSeverity, final String errorMessage,
372 final IN input, Class<OUT> responseClazz) {
373 final SettableFuture<RpcResult<OUT>> rpcResult = SettableFuture.create();
375 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
378 public void operationComplete(
379 io.netty.util.concurrent.Future<? super Void> future)
382 if (future.cause() != null) {
383 Collection<RpcError> errors = null;
384 RpcError rpcError = buildRpcError(failureInfo,
385 errorSeverity, errorMessage, future.cause());
386 errors = Lists.newArrayList(rpcError);
387 rpcResult.set(Rpcs.getRpcResult(
393 RpcResponseKey key = new RpcResponseKey(input.getXid(), input.getClass().toString());
394 if (responseCache.getIfPresent(key) != null) {
395 responseCache.invalidate(key);
397 responseCache.put(key, rpcResult);
405 * @param resultFuture
407 * @param errorSeverity
411 private static SettableFuture<Boolean> handleTransportChannelFuture(
412 ChannelFuture resultFuture, final String failureInfo,
413 final ErrorSeverity errorSeverity, final String message) {
415 final SettableFuture<Boolean> transportResult = SettableFuture.create();
417 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
420 public void operationComplete(
421 io.netty.util.concurrent.Future<? super Void> future)
423 transportResult.set(future.isSuccess());
424 transportResult.setException(future.cause());
427 return transportResult;
434 protected RpcError buildRpcError(String info, ErrorSeverity severity, String message,
436 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
437 ErrorType.RPC, cause);
445 protected RpcError buildTransportError(String info, ErrorSeverity severity, String message,
447 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
448 ErrorType.TRANSPORT, cause);
456 private static RpcResponseKey createRpcResponseKey(OfHeader message) {
457 return new RpcResponseKey(message.getXid(), message.getClass().toString());
463 @SuppressWarnings("unchecked")
464 private SettableFuture<RpcResult<?>> findRpcResponse(RpcResponseKey key) {
465 return (SettableFuture<RpcResult<?>>) responseCache.getIfPresent(key);