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;
18 import org.opendaylight.controller.sal.common.util.RpcErrors;
19 import org.opendaylight.controller.sal.common.util.Rpcs;
20 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
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.ExperimenterInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput;
47 import org.opendaylight.yangtools.yang.common.RpcError;
48 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
49 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
52 import com.google.common.collect.Lists;
53 import com.google.common.util.concurrent.SettableFuture;
59 public class ConnectionAdapterImpl implements ConnectionAdapter {
61 private static final String APPLICATION_TAG = "OPENFLOW_LIBRARY";
62 private static final String TAG = "OPENFLOW";
63 private Channel channel;
64 private OpenflowProtocolListener messageListener;
67 * @param channel the channel to set
69 public void setChannel(Channel channel) {
70 this.channel = channel;
74 public Future<RpcResult<BarrierOutput>> barrier(BarrierInput input) {
75 // TODO Auto-generated method stub
80 public Future<RpcResult<EchoOutput>> echo(EchoInput input) {
81 // TODO Auto-generated method stub
86 public Future<RpcResult<Void>> echoReply(EchoReplyInput input) {
87 return sendToSwitchFuture(input, "echo reply sending failed");
91 public Future<RpcResult<Void>> experimenter(ExperimenterInput input) {
92 // TODO Auto-generated method stub
97 public Future<RpcResult<Void>> flowMod(FlowModInput input) {
98 // TODO Auto-generated method stub
103 public Future<RpcResult<GetConfigOutput>> getConfig(GetConfigInput input) {
104 // TODO Auto-generated method stub
109 public Future<RpcResult<GetFeaturesOutput>> getFeatures(
110 GetFeaturesInput input) {
111 // TODO Auto-generated method stub
116 public Future<RpcResult<GetQueueConfigOutput>> getQueueConfig(
117 GetQueueConfigInput input) {
118 // TODO Auto-generated method stub
123 public Future<RpcResult<Void>> groupMod(GroupModInput input) {
124 // TODO Auto-generated method stub
129 public Future<RpcResult<Void>> hello(HelloInput input) {
130 // TODO Auto-generated method stub
135 public Future<RpcResult<Void>> meterMod(MeterModInput input) {
136 // TODO Auto-generated method stub
141 public Future<RpcResult<Void>> packetOut(PacketOutInput input) {
142 // TODO Auto-generated method stub
147 public Future<RpcResult<Void>> portMod(PortModInput input) {
148 // TODO Auto-generated method stub
153 public Future<RpcResult<RoleRequestOutput>> roleRequest(
154 RoleRequestInput input) {
155 // TODO Auto-generated method stub
160 public Future<RpcResult<Void>> setConfig(SetConfigInput input) {
161 // TODO Auto-generated method stub
166 public Future<RpcResult<Void>> tableMod(TableModInput input) {
167 // TODO Auto-generated method stub
172 * @see org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolService#getAsync(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput)
175 public Future<RpcResult<GetAsyncOutput>> getAsync(GetAsyncInput input) {
176 // TODO Auto-generated method stub
181 * @see org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolService#setAsync(org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput)
184 public Future<RpcResult<Void>> setAsync(SetAsyncInput input) {
185 // TODO Auto-generated method stub
190 public Future<Boolean> disconnect() {
191 ChannelFuture disconnectResult = channel.disconnect();
193 String failureInfo = "switch disconnecting failed";
194 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
195 String message = "Check the switch connection";
196 return handleTransportChannelFuture(disconnectResult, failureInfo, errorSeverity, message);
200 public boolean isAlive() {
201 return channel.isOpen();
205 public void setMessageListener(OpenflowProtocolListener messageListener) {
206 this.messageListener = messageListener;
213 private SettableFuture<RpcResult<Void>> sendToSwitchFuture(
214 EchoReplyInput input, final String failureInfo) {
215 ChannelFuture resultFuture = channel.writeAndFlush(input);
217 ErrorSeverity errorSeverity = ErrorSeverity.ERROR;
218 String message = "check switch connection";
219 return handleRpcChannelFuture(resultFuture, failureInfo, errorSeverity, message);
223 * @param resultFuture
227 private SettableFuture<RpcResult<Void>> handleRpcChannelFuture(
228 ChannelFuture resultFuture, final String failureInfo,
229 final ErrorSeverity errorSeverity, final String message) {
231 final SettableFuture<RpcResult<Void>> rpcResult = SettableFuture.create();
233 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
236 public void operationComplete(
237 io.netty.util.concurrent.Future<? super Void> future)
239 Collection<RpcError> errors = null;
241 if (future.cause() != null) {
242 RpcError rpcError = buildRpcError(failureInfo,
243 errorSeverity, message, future.cause());
244 errors = Lists.newArrayList(rpcError);
247 rpcResult.set(Rpcs.getRpcResult(
258 * @param resultFuture
262 private SettableFuture<Boolean> handleTransportChannelFuture(
263 ChannelFuture resultFuture, final String failureInfo,
264 final ErrorSeverity errorSeverity, final String message) {
266 final SettableFuture<Boolean> transportResult = SettableFuture.create();
268 resultFuture.addListener(new GenericFutureListener<io.netty.util.concurrent.Future<? super Void>>() {
271 public void operationComplete(
272 io.netty.util.concurrent.Future<? super Void> future)
274 transportResult.set(future.isSuccess());
277 return transportResult;
284 protected RpcError buildRpcError(String info, ErrorSeverity severity, String message,
286 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
287 ErrorType.RPC, cause);
295 protected RpcError buildTransportError(String info, ErrorSeverity severity, String message,
297 RpcError error = RpcErrors.getRpcError(APPLICATION_TAG, TAG, info, severity, message,
298 ErrorType.TRANSPORT, cause);