From cccc72339d6c385d4d7aa09786a4ea241321a0e1 Mon Sep 17 00:00:00 2001 From: Vaclav Demcak Date: Fri, 25 Sep 2015 00:06:54 +0200 Subject: [PATCH] Barrier turn on/off - Split ConnectionAdapter functionality Split ConnectionAdapter functionaly to Abstract parents for clean implementation * add AbstractConnectionAdapter primary implement OpenflowProtocolSevice * add AbstractConnectionAdapterStatistics StatCouter wrapper Change-Id: Ibfd809d6bc7e2e95339f7c21e26958306b157d41 Signed-off-by: Vaclav Demcak (cherry picked from commit e35d417dacc1bf002076e432b575931e15769f65) --- .../connection/AbstractConnectionAdapter.java | 331 +++++++++++++++++ .../AbstractConnectionAdapterStatistics.java | 78 ++++ .../connection/ConnectionAdapterImpl.java | 349 +----------------- 3 files changed, 424 insertions(+), 334 deletions(-) create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/AbstractConnectionAdapter.java create mode 100644 openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/AbstractConnectionAdapterStatistics.java diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/AbstractConnectionAdapter.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/AbstractConnectionAdapter.java new file mode 100644 index 00000000..57f1498c --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/AbstractConnectionAdapter.java @@ -0,0 +1,331 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.openflowjava.protocol.impl.core.connection; + +import com.google.common.annotations.VisibleForTesting; +import com.google.common.base.Preconditions; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.RemovalCause; +import com.google.common.cache.RemovalListener; +import com.google.common.cache.RemovalNotification; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; +import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; +import io.netty.util.concurrent.GenericFutureListener; +import java.net.InetSocketAddress; +import java.util.concurrent.Future; +import java.util.concurrent.RejectedExecutionException; +import java.util.concurrent.TimeUnit; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.common.RpcResult; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * {@link ConnectionAdapter} interface contains couple of OF message handling approaches. + * {@link AbstractConnectionAdapter} class contains direct RPC processing from OpenflowProtocolService + * {@link org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolService} + */ +abstract class AbstractConnectionAdapter implements ConnectionAdapter { + + private static final Logger LOG = LoggerFactory.getLogger(AbstractConnectionAdapter.class); + + /** after this time, RPC future response objects will be thrown away (in minutes) */ + private static final int RPC_RESPONSE_EXPIRATION = 1; + + private static final Exception QUEUE_FULL_EXCEPTION = new RejectedExecutionException("Output queue is full"); + + /** + * Default depth of write queue, e.g. we allow these many messages + * to be queued up before blocking producers. + */ + private static final int DEFAULT_QUEUE_DEPTH = 1024; + + protected static final RemovalListener> REMOVAL_LISTENER = new RemovalListener>() { + @Override + public void onRemoval(final RemovalNotification> notification) { + if (!notification.getCause().equals(RemovalCause.EXPLICIT)) { + notification.getValue().discard(); + } + } + }; + + protected final Channel channel; + protected final InetSocketAddress address; + protected boolean disconnectOccured = false; + protected final ChannelOutboundQueue output; + + /** expiring cache for future rpcResponses */ + protected Cache> responseCache; + + + AbstractConnectionAdapter(@Nonnull final Channel channel, @Nullable final InetSocketAddress address) { + this.channel = Preconditions.checkNotNull(channel); + this.address = address; + + responseCache = CacheBuilder.newBuilder().concurrencyLevel(1) + .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES).removalListener(REMOVAL_LISTENER).build(); + this.output = new ChannelOutboundQueue(channel, DEFAULT_QUEUE_DEPTH, address); + channel.pipeline().addLast(output); + } + + @Override + public Future disconnect() { + final ChannelFuture disconnectResult = channel.disconnect(); + responseCache.invalidateAll(); + disconnectOccured = true; + + return handleTransportChannelFuture(disconnectResult); + } + + @Override + public Future> barrier(final BarrierInput input) { + return sendToSwitchExpectRpcResultFuture(input, BarrierOutput.class, "barrier-input sending failed"); + } + + @Override + public Future> echo(final EchoInput input) { + return sendToSwitchExpectRpcResultFuture(input, EchoOutput.class, "echo-input sending failed"); + } + + @Override + public Future> echoReply(final EchoReplyInput input) { + return sendToSwitchFuture(input, "echo-reply sending failed"); + } + + @Override + public Future> experimenter(final ExperimenterInput input) { + return sendToSwitchFuture(input, "experimenter sending failed"); + } + + @Override + public Future> flowMod(final FlowModInput input) { + return sendToSwitchFuture(input, "flow-mod sending failed"); + } + + @Override + public Future> getConfig(final GetConfigInput input) { + return sendToSwitchExpectRpcResultFuture(input, GetConfigOutput.class, "get-config-input sending failed"); + } + + @Override + public Future> getFeatures(final GetFeaturesInput input) { + return sendToSwitchExpectRpcResultFuture(input, GetFeaturesOutput.class, "get-features-input sending failed"); + } + + @Override + public Future> getQueueConfig(final GetQueueConfigInput input) { + return sendToSwitchExpectRpcResultFuture(input, GetQueueConfigOutput.class, + "get-queue-config-input sending failed"); + } + + @Override + public Future> groupMod(final GroupModInput input) { + return sendToSwitchFuture(input, "group-mod-input sending failed"); + } + + @Override + public Future> hello(final HelloInput input) { + return sendToSwitchFuture(input, "hello-input sending failed"); + } + + @Override + public Future> meterMod(final MeterModInput input) { + return sendToSwitchFuture(input, "meter-mod-input sending failed"); + } + + @Override + public Future> packetOut(final PacketOutInput input) { + return sendToSwitchFuture(input, "packet-out-input sending failed"); + } + + @Override + public Future> multipartRequest(final MultipartRequestInput input) { + return sendToSwitchFuture(input, "multi-part-request sending failed"); + } + + @Override + public Future> portMod(final PortModInput input) { + return sendToSwitchFuture(input, "port-mod-input sending failed"); + } + + @Override + public Future> roleRequest(final RoleRequestInput input) { + return sendToSwitchExpectRpcResultFuture(input, RoleRequestOutput.class, + "role-request-config-input sending failed"); + } + + @Override + public Future> setConfig(final SetConfigInput input) { + return sendToSwitchFuture(input, "set-config-input sending failed"); + } + + @Override + public Future> tableMod(final TableModInput input) { + return sendToSwitchFuture(input, "table-mod-input sending failed"); + } + + @Override + public Future> getAsync(final GetAsyncInput input) { + return sendToSwitchExpectRpcResultFuture(input, GetAsyncOutput.class, "get-async-input sending failed"); + } + + @Override + public Future> setAsync(final SetAsyncInput input) { + return sendToSwitchFuture(input, "set-async-input sending failed"); + } + + @Override + public boolean isAlive() { + return channel.isOpen(); + } + + @Override + public boolean isAutoRead() { + return channel.config().isAutoRead(); + } + + @Override + public void setAutoRead(final boolean autoRead) { + channel.config().setAutoRead(autoRead); + } + + @Override + public InetSocketAddress getRemoteAddress() { + return (InetSocketAddress) channel.remoteAddress(); + } + + /** + * Used only for testing purposes + * @param cache replacement + */ + @VisibleForTesting + void setResponseCache(final Cache> cache) { + this.responseCache = cache; + } + + /** + * Return cached RpcListener or {@code null} if not cached + * @return + */ + protected ResponseExpectedRpcListener findRpcResponse(final RpcResponseKey key) { + return responseCache.getIfPresent(key); + } + + /** + * sends given message to switch, sending result or switch response will be reported via return value + * + * @param input message to send + * @param responseClazz type of response + * @param failureInfo describes, what type of message caused failure by sending + * @return future object, + *
    + *
  • if send fails, {@link RpcResult} will contain errors and failed status
  • + *
  • else {@link RpcResult} will be stored in responseCache and wait for particular timeout ( + * {@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}), + *
      + *
    • either switch will manage to answer and then corresponding response message will be set into returned + * future
    • + *
    • or response in cache will expire and returned future will be cancelled
    • + *
    + *
  • + *
+ */ + protected ListenableFuture> sendToSwitchExpectRpcResultFuture( + final IN input, final Class responseClazz, final String failureInfo) { + final RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz.getName()); + final ResponseExpectedRpcListener listener = new ResponseExpectedRpcListener<>(input, failureInfo, + responseCache, key); + return enqueueMessage(listener); + } + + /** + * sends given message to switch, sending result will be reported via return value + * + * @param input message to send + * @param failureInfo describes, what type of message caused failure by sending + * @return future object, + *
    + *
  • if send successful, {@link RpcResult} without errors and successful status will be returned,
  • + *
  • else {@link RpcResult} will contain errors and failed status
  • + *
+ */ + protected ListenableFuture> sendToSwitchFuture(final DataObject input, final String failureInfo) { + return enqueueMessage(new SimpleRpcListener(input, failureInfo)); + } + + private ListenableFuture> enqueueMessage(final AbstractRpcListener promise) { + LOG.debug("Submitting promise {}", promise); + + if (!output.enqueue(promise)) { + LOG.debug("Message queue is full, rejecting execution"); + promise.failedRpc(QUEUE_FULL_EXCEPTION); + } else { + LOG.debug("Promise enqueued successfully"); + } + + return promise.getResult(); + } + + /** + * @param resultFuture + * @param failureInfo + * @param errorSeverity + * @param message + * @return + */ + private static SettableFuture handleTransportChannelFuture(final ChannelFuture resultFuture) { + + final SettableFuture transportResult = SettableFuture.create(); + + resultFuture.addListener(new GenericFutureListener>() { + + @Override + public void operationComplete(final io.netty.util.concurrent.Future future) throws Exception { + transportResult.set(future.isSuccess()); + if (!future.isSuccess()) { + transportResult.setException(future.cause()); + } + } + }); + return transportResult; + } +} diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/AbstractConnectionAdapterStatistics.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/AbstractConnectionAdapterStatistics.java new file mode 100644 index 00000000..730403b6 --- /dev/null +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/AbstractConnectionAdapterStatistics.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.openflowjava.protocol.impl.core.connection; + +import com.google.common.util.concurrent.ListenableFuture; +import io.netty.channel.Channel; +import java.net.InetSocketAddress; +import java.util.concurrent.Future; +import org.opendaylight.openflowjava.statistics.CounterEventTypes; +import org.opendaylight.openflowjava.statistics.StatisticsCounters; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent; +import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent; +import org.opendaylight.yangtools.yang.binding.DataObject; +import org.opendaylight.yangtools.yang.binding.Notification; +import org.opendaylight.yangtools.yang.common.RpcResult; + +/** + * Class is only wrapper for {@link AbstractConnectionAdapter} to provide statistics + * records for counting all needed RPC messages in Openflow Java. + */ +abstract class AbstractConnectionAdapterStatistics extends AbstractConnectionAdapter implements MessageConsumer { + + private final StatisticsCounters statisticsCounters; + + AbstractConnectionAdapterStatistics(final Channel channel, final InetSocketAddress address) { + super(channel, address); + statisticsCounters = StatisticsCounters.getInstance(); + } + + @Override + public Future> flowMod(final FlowModInput input) { + statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_ENTERED); + return super.flowMod(input); + } + + @Override + protected ListenableFuture> sendToSwitchExpectRpcResultFuture( + final IN input, final Class responseClazz, final String failureInfo) { + statisticsCounters.incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA); + return super.sendToSwitchExpectRpcResultFuture(input, responseClazz, failureInfo); + } + + @Override + protected ListenableFuture> sendToSwitchFuture(final DataObject input, final String failureInfo) { + statisticsCounters.incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA); + return super.sendToSwitchFuture(input, failureInfo); + } + + @Override + public void consume(final DataObject message) { + if (Notification.class.isInstance(message)) { + if (!(DisconnectEvent.class.isInstance(message) || SwitchIdleEvent.class.isInstance(message))) { + statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); + } + } else if (OfHeader.class.isInstance(message)) { + statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); + } + consumeDeviceMessage(message); + } + + /** + * Method is equivalent to {@link MessageConsumer#consume(DataObject)} to prevent missing method + * in every children of {@link AbstractConnectionAdapterStatistics} class, because we overriding + * original method for {@link StatisticsCounters} + * + * @param message from device to processing + */ + protected abstract void consumeDeviceMessage(DataObject message); +} + diff --git a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImpl.java b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImpl.java index 81a9aceb..d22f2f06 100644 --- a/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImpl.java +++ b/openflow-protocol-impl/src/main/java/org/opendaylight/openflowjava/protocol/impl/core/connection/ConnectionAdapterImpl.java @@ -10,69 +10,28 @@ package org.opendaylight.openflowjava.protocol.impl.core.connection; import com.google.common.base.Preconditions; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import com.google.common.cache.RemovalCause; -import com.google.common.cache.RemovalListener; -import com.google.common.cache.RemovalNotification; -import com.google.common.util.concurrent.ListenableFuture; -import com.google.common.util.concurrent.SettableFuture; import io.netty.channel.Channel; -import io.netty.channel.ChannelFuture; -import io.netty.util.concurrent.GenericFutureListener; import java.net.InetSocketAddress; -import java.util.concurrent.Future; -import java.util.concurrent.RejectedExecutionException; -import java.util.concurrent.TimeUnit; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionReadyListener; import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandler; import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueueHandlerRegistration; import org.opendaylight.openflowjava.protocol.impl.core.OFVersionDetector; import org.opendaylight.openflowjava.protocol.impl.core.PipelineHandlers; -import org.opendaylight.openflowjava.statistics.CounterEventTypes; -import org.opendaylight.openflowjava.statistics.StatisticsCounters; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.BarrierOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoReplyInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.EchoRequestMessage; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ErrorMessage; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowModInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessage; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetConfigOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetQueueConfigOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GroupModInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.HelloMessage; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MeterModInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReplyMessage; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartRequestInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OpenflowProtocolListener; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketOutInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortModInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.RoleRequestOutput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetAsyncInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.SetConfigInput; -import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.TableModInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.DisconnectEvent; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEvent; import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SystemNotificationsListener; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.Notification; -import org.opendaylight.yangtools.yang.common.RpcResult; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -81,199 +40,49 @@ import org.slf4j.LoggerFactory; * @author mirehak * @author michal.polkorab */ -public class ConnectionAdapterImpl implements ConnectionFacade { - /** after this time, RPC future response objects will be thrown away (in minutes) */ - public static final int RPC_RESPONSE_EXPIRATION = 1; +public class ConnectionAdapterImpl extends AbstractConnectionAdapterStatistics implements ConnectionFacade { - /** - * Default depth of write queue, e.g. we allow these many messages - * to be queued up before blocking producers. - */ - public static final int DEFAULT_QUEUE_DEPTH = 1024; - - private static final Logger LOG = LoggerFactory - .getLogger(ConnectionAdapterImpl.class); - private static final Exception QUEUE_FULL_EXCEPTION = - new RejectedExecutionException("Output queue is full"); - - private static final RemovalListener> REMOVAL_LISTENER = - new RemovalListener>() { - @Override - public void onRemoval( - final RemovalNotification> notification) { - if (! notification.getCause().equals(RemovalCause.EXPLICIT)) { - notification.getValue().discard(); - } - } - }; - - /** expiring cache for future rpcResponses */ - private Cache> responseCache; - - private final ChannelOutboundQueue output; - private final Channel channel; + private static final Logger LOG = LoggerFactory.getLogger(ConnectionAdapterImpl.class); private ConnectionReadyListener connectionReadyListener; private OpenflowProtocolListener messageListener; private SystemNotificationsListener systemListener; private OutboundQueueManager outputManager; - private boolean disconnectOccured = false; - private final StatisticsCounters statisticsCounters; private OFVersionDetector versionDetector; - private final InetSocketAddress address; private final boolean useBarrier; /** * default ctor + * * @param channel the channel to be set - used for communication * @param address client address (used only in case of UDP communication, - * as there is no need to store address over tcp (stable channel)) + * as there is no need to store address over tcp (stable channel)) + * @param useBarrier value is configurable by configSubsytem */ public ConnectionAdapterImpl(final Channel channel, final InetSocketAddress address, final boolean useBarrier) { - this.channel = Preconditions.checkNotNull(channel); - this.output = new ChannelOutboundQueue(channel, DEFAULT_QUEUE_DEPTH, address); - this.address = address; - - responseCache = CacheBuilder.newBuilder() - .concurrencyLevel(1) - .expireAfterWrite(RPC_RESPONSE_EXPIRATION, TimeUnit.MINUTES) - .removalListener(REMOVAL_LISTENER).build(); - + super(channel, address); this.useBarrier = useBarrier; - channel.pipeline().addLast(output); - statisticsCounters = StatisticsCounters.getInstance(); - LOG.debug("ConnectionAdapter created"); } @Override - public Future> barrier(final BarrierInput input) { - return sendToSwitchExpectRpcResultFuture( - input, BarrierOutput.class, "barrier-input sending failed"); - } - - @Override - public Future> echo(final EchoInput input) { - return sendToSwitchExpectRpcResultFuture( - input, EchoOutput.class, "echo-input sending failed"); - } - - @Override - public Future> echoReply(final EchoReplyInput input) { - return sendToSwitchFuture(input, "echo-reply sending failed"); - } - - @Override - public Future> experimenter(final ExperimenterInput input) { - return sendToSwitchFuture(input, "experimenter reply sending failed"); - } - - @Override - public Future> flowMod(final FlowModInput input) { - statisticsCounters.incrementCounter(CounterEventTypes.DS_FLOW_MODS_ENTERED); - return sendToSwitchFuture(input, "flow-mod sending failed"); - } - - @Override - public Future> getConfig(final GetConfigInput input) { - return sendToSwitchExpectRpcResultFuture( - input, GetConfigOutput.class, "get-config-input sending failed"); - } - - @Override - public Future> getFeatures( - final GetFeaturesInput input) { - return sendToSwitchExpectRpcResultFuture( - input, GetFeaturesOutput.class, "get-features-input sending failed"); - } - - @Override - public Future> getQueueConfig( - final GetQueueConfigInput input) { - return sendToSwitchExpectRpcResultFuture( - input, GetQueueConfigOutput.class, "get-queue-config-input sending failed"); - } - - @Override - public Future> groupMod(final GroupModInput input) { - return sendToSwitchFuture(input, "group-mod-input sending failed"); - } - - @Override - public Future> hello(final HelloInput input) { - return sendToSwitchFuture(input, "hello-input sending failed"); - } - - @Override - public Future> meterMod(final MeterModInput input) { - return sendToSwitchFuture(input, "meter-mod-input sending failed"); - } - - @Override - public Future> packetOut(final PacketOutInput input) { - return sendToSwitchFuture(input, "packet-out-input sending failed"); - } - - @Override - public Future> multipartRequest(final MultipartRequestInput input) { - return sendToSwitchFuture(input, "multi-part-request sending failed"); - } - - @Override - public Future> portMod(final PortModInput input) { - return sendToSwitchFuture(input, "port-mod-input sending failed"); - } - - @Override - public Future> roleRequest( - final RoleRequestInput input) { - return sendToSwitchExpectRpcResultFuture( - input, RoleRequestOutput.class, "role-request-config-input sending failed"); - } - - @Override - public Future> setConfig(final SetConfigInput input) { - return sendToSwitchFuture(input, "set-config-input sending failed"); - } - - @Override - public Future> tableMod(final TableModInput input) { - return sendToSwitchFuture(input, "table-mod-input sending failed"); - } - - @Override - public Future> getAsync(final GetAsyncInput input) { - return sendToSwitchExpectRpcResultFuture( - input, GetAsyncOutput.class, "get-async-input sending failed"); - } - - @Override - public Future> setAsync(final SetAsyncInput input) { - return sendToSwitchFuture(input, "set-async-input sending failed"); - } - - @Override - public Future disconnect() { - final ChannelFuture disconnectResult = channel.disconnect(); - responseCache.invalidateAll(); - disconnectOccured = true; - - return handleTransportChannelFuture(disconnectResult); + public void setMessageListener(final OpenflowProtocolListener messageListener) { + this.messageListener = messageListener; } @Override - public boolean isAlive() { - return channel.isOpen(); + public void setConnectionReadyListener(final ConnectionReadyListener connectionReadyListener) { + this.connectionReadyListener = connectionReadyListener; } @Override - public void setMessageListener(final OpenflowProtocolListener messageListener) { - this.messageListener = messageListener; + public void setSystemListener(final SystemNotificationsListener systemListener) { + this.systemListener = systemListener; } @Override - public void consume(final DataObject message) { + public void consumeDeviceMessage(final DataObject message) { LOG.debug("ConsumeIntern msg on {}", channel); if (disconnectOccured ) { return; @@ -294,38 +103,30 @@ public class ConnectionAdapterImpl implements ConnectionFacade { } else { messageListener.onEchoRequestMessage((EchoRequestMessage) message); } - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); } else if (message instanceof ErrorMessage) { // Send only unmatched errors if (outputManager == null || !outputManager.onMessage((OfHeader) message)) { messageListener.onErrorMessage((ErrorMessage) message); } - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); } else if (message instanceof ExperimenterMessage) { if (outputManager != null) { outputManager.onMessage((OfHeader) message); } messageListener.onExperimenterMessage((ExperimenterMessage) message); - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); } else if (message instanceof FlowRemovedMessage) { messageListener.onFlowRemovedMessage((FlowRemovedMessage) message); - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); } else if (message instanceof HelloMessage) { LOG.info("Hello received / branch"); messageListener.onHelloMessage((HelloMessage) message); - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); } else if (message instanceof MultipartReplyMessage) { if (outputManager != null) { outputManager.onMessage((OfHeader) message); } messageListener.onMultipartReplyMessage((MultipartReplyMessage) message); - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); } else if (message instanceof PacketInMessage) { messageListener.onPacketInMessage((PacketInMessage) message); - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); } else if (message instanceof PortStatusMessage) { messageListener.onPortStatusMessage((PortStatusMessage) message); - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); } else { LOG.warn("message listening not supported for type: {}", message.getClass()); } @@ -338,7 +139,6 @@ public class ConnectionAdapterImpl implements ConnectionFacade { if (listener != null) { LOG.debug("corresponding rpcFuture found"); listener.completed((OfHeader)message); - statisticsCounters.incrementCounter(CounterEventTypes.US_MESSAGE_PASS); LOG.debug("after setting rpcFuture"); responseCache.invalidate(key); } else { @@ -350,86 +150,6 @@ public class ConnectionAdapterImpl implements ConnectionFacade { } } - private ListenableFuture> enqueueMessage(final AbstractRpcListener promise) { - LOG.debug("Submitting promise {}", promise); - - if (!output.enqueue(promise)) { - LOG.debug("Message queue is full, rejecting execution"); - promise.failedRpc(QUEUE_FULL_EXCEPTION); - } else { - LOG.debug("Promise enqueued successfully"); - } - - return promise.getResult(); - } - - /** - * sends given message to switch, sending result will be reported via return value - * @param input message to send - * @param failureInfo describes, what type of message caused failure by sending - * @return future object,
    - *
  • if send successful, {@link RpcResult} without errors and successful - * status will be returned,
  • - *
  • else {@link RpcResult} will contain errors and failed status
  • - *
- */ - private ListenableFuture> sendToSwitchFuture( - final DataObject input, final String failureInfo) { - statisticsCounters.incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA); - return enqueueMessage(new SimpleRpcListener(input, failureInfo)); - } - - /** - * sends given message to switch, sending result or switch response will be reported via return value - * @param input message to send - * @param responseClazz type of response - * @param failureInfo describes, what type of message caused failure by sending - * @return future object,
    - *
  • if send fails, {@link RpcResult} will contain errors and failed status
  • - *
  • else {@link RpcResult} will be stored in responseCache and wait for particular timeout - * ({@link ConnectionAdapterImpl#RPC_RESPONSE_EXPIRATION}), - *
    • either switch will manage to answer - * and then corresponding response message will be set into returned future
    • - *
    • or response in cache will expire and returned future will be cancelled
    - *
  • - *
- */ - private ListenableFuture> sendToSwitchExpectRpcResultFuture( - final IN input, final Class responseClazz, final String failureInfo) { - final RpcResponseKey key = new RpcResponseKey(input.getXid(), responseClazz.getName()); - final ResponseExpectedRpcListener listener = - new ResponseExpectedRpcListener<>(input, failureInfo, responseCache, key); - statisticsCounters.incrementCounter(CounterEventTypes.DS_ENTERED_OFJAVA); - return enqueueMessage(listener); - } - - /** - * @param resultFuture - * @param failureInfo - * @param errorSeverity - * @param message - * @return - */ - private static SettableFuture handleTransportChannelFuture( - final ChannelFuture resultFuture) { - - final SettableFuture transportResult = SettableFuture.create(); - - resultFuture.addListener(new GenericFutureListener>() { - - @Override - public void operationComplete( - final io.netty.util.concurrent.Future future) - throws Exception { - transportResult.set(future.isSuccess()); - if (!future.isSuccess()) { - transportResult.setException(future.cause()); - } - } - }); - return transportResult; - } - /** * @param message * @return @@ -438,18 +158,6 @@ public class ConnectionAdapterImpl implements ConnectionFacade { return new RpcResponseKey(message.getXid(), message.getImplementedInterface().getName()); } - /** - * @return - */ - private ResponseExpectedRpcListener findRpcResponse(final RpcResponseKey key) { - return responseCache.getIfPresent(key); - } - - @Override - public void setSystemListener(final SystemNotificationsListener systemListener) { - this.systemListener = systemListener; - } - @Override public void checkListeners() { final StringBuilder buffer = new StringBuilder(); @@ -479,35 +187,6 @@ public class ConnectionAdapterImpl implements ConnectionFacade { }).start(); } - @Override - public void setConnectionReadyListener( - final ConnectionReadyListener connectionReadyListener) { - this.connectionReadyListener = connectionReadyListener; - } - - @Override - public InetSocketAddress getRemoteAddress() { - return (InetSocketAddress) channel.remoteAddress(); - } - - /** - * Used only for testing purposes - * @param cache - */ - public void setResponseCache(final Cache> cache) { - this.responseCache = cache; - } - - @Override - public boolean isAutoRead() { - return channel.config().isAutoRead(); - } - - @Override - public void setAutoRead(final boolean autoRead) { - channel.config().setAutoRead(autoRead); - } - @Override public OutboundQueueHandlerRegistration registerOutboundQueueHandler( final T handler, final int maxQueueDepth, final long maxBarrierNanos) { @@ -519,6 +198,8 @@ public class ConnectionAdapterImpl implements ConnectionFacade { final OutboundQueueManager ret = new OutboundQueueManager<>(this, address, handler, maxQueueDepth, maxBarrierNanos); outputManager = ret; + /* we don't need it anymore */ + channel.pipeline().remove(output); channel.pipeline().addLast(outputManager); return new OutboundQueueHandlerRegistrationImpl(handler) { -- 2.36.6