X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=openflowplugin-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fopenflowplugin%2Fimpl%2Fdevice%2FDeviceContextImpl.java;h=aed3f3c79ecaf3f510cffdabf7f39f69cf846cfa;hb=84e9a29a401256b949527776c345153aaab2b6d1;hp=88ed939ddde65de8c870eb46b979927587986f08;hpb=b996c77fd73f043cda13094b4befefd99c837ba9;p=openflowplugin.git diff --git a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java index 88ed939ddd..aed3f3c79e 100644 --- a/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java +++ b/openflowplugin-impl/src/main/java/org/opendaylight/openflowplugin/impl/device/DeviceContextImpl.java @@ -7,46 +7,37 @@ */ package org.opendaylight.openflowplugin.impl.device; -import javax.annotation.CheckForNull; -import javax.annotation.Nonnull; -import java.math.BigInteger; -import java.util.Collection; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Preconditions; -import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.base.Verify; import com.google.common.util.concurrent.FutureCallback; import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; -import io.netty.util.HashedWheelTimer; import io.netty.util.Timeout; +import java.math.BigInteger; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import javax.annotation.Nonnull; import org.opendaylight.controller.md.sal.binding.api.DataBroker; import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService; -import org.opendaylight.controller.md.sal.binding.api.NotificationService; -import org.opendaylight.controller.md.sal.binding.api.ReadTransaction; -import org.opendaylight.controller.md.sal.binding.api.WriteTransaction; +import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction; import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType; -import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException; import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter; import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue; import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey; import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext; import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider; import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext; +import org.opendaylight.openflowplugin.api.openflow.device.DeviceInfo; import org.opendaylight.openflowplugin.api.openflow.device.DeviceState; import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator; import org.opendaylight.openflowplugin.api.openflow.device.RequestContext; import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary; import org.opendaylight.openflowplugin.api.openflow.device.Xid; -import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextClosedHandler; import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector; +import org.opendaylight.openflowplugin.api.openflow.lifecycle.LifecycleConductor; import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher; import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey; import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry; @@ -56,7 +47,6 @@ import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKe import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry; import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry; import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleKeeper; -import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext; import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener; import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy; import org.opendaylight.openflowplugin.extension.api.ConvertorMessageFromOFJava; @@ -100,7 +90,6 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived; import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData; import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder; -import org.opendaylight.yang.gen.v1.urn.opendaylight.role.service.rev150727.OfpRole; import org.opendaylight.yangtools.yang.binding.DataObject; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier; @@ -115,7 +104,7 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi private static final Logger LOG = LoggerFactory.getLogger(DeviceContextImpl.class); // TODO: drain factor should be parametrized - public static final float REJECTED_DRAIN_FACTOR = 0.25f; + private static final float REJECTED_DRAIN_FACTOR = 0.25f; // TODO: low water mark factor should be parametrized private static final float LOW_WATERMARK_FACTOR = 0.75f; // TODO: high water mark factor should be parametrized @@ -124,77 +113,64 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi private final ConnectionContext primaryConnectionContext; private final DeviceState deviceState; private final DataBroker dataBroker; - private final HashedWheelTimer hashedWheelTimer; private final Map auxiliaryConnectionContexts; - private TransactionChainManager transactionChainManager; + private final TransactionChainManager transactionChainManager; private final DeviceFlowRegistry deviceFlowRegistry; private final DeviceGroupRegistry deviceGroupRegistry; private final DeviceMeterRegistry deviceMeterRegistry; - private final Collection closeHandlers = new HashSet<>(); private final PacketInRateLimiter packetInLimiter; private final MessageSpy messageSpy; private final ItemLifeCycleKeeper flowLifeCycleKeeper; private NotificationPublishService notificationPublishService; - private NotificationService notificationService; private final OutboundQueue outboundQueueProvider; private Timeout barrierTaskTimeout; private final MessageTranslator portStatusTranslator; private final MessageTranslator packetInTranslator; private final MessageTranslator flowRemovedTranslator; private final TranslatorLibrary translatorLibrary; - private final Map nodeConnectorCache; - private ItemLifeCycleRegistry itemLifeCycleSourceRegistry; - private RpcContext rpcContext; + private final ItemLifeCycleRegistry itemLifeCycleSourceRegistry; private ExtensionConverterProvider extensionConverterProvider; + private final DeviceInfo deviceInfo; + + private volatile DEVICE_CONTEXT_STATE deviceCtxState; @VisibleForTesting DeviceContextImpl(@Nonnull final ConnectionContext primaryConnectionContext, @Nonnull final DeviceState deviceState, @Nonnull final DataBroker dataBroker, - @Nonnull final HashedWheelTimer hashedWheelTimer, - @Nonnull final MessageSpy _messageSpy, + @Nonnull final LifecycleConductor conductor, @Nonnull final OutboundQueueProvider outboundQueueProvider, - @Nonnull final TranslatorLibrary translatorLibrary) { + @Nonnull final TranslatorLibrary translatorLibrary) { this.primaryConnectionContext = Preconditions.checkNotNull(primaryConnectionContext); this.deviceState = Preconditions.checkNotNull(deviceState); this.dataBroker = Preconditions.checkNotNull(dataBroker); - this.hashedWheelTimer = Preconditions.checkNotNull(hashedWheelTimer); + Preconditions.checkNotNull(conductor); this.outboundQueueProvider = Preconditions.checkNotNull(outboundQueueProvider); - primaryConnectionContext.setDeviceDisconnectedHandler(DeviceContextImpl.this); - this.transactionChainManager = new TransactionChainManager(dataBroker, deviceState); + deviceInfo = primaryConnectionContext.getDeviceInfo(); + this.transactionChainManager = new TransactionChainManager(dataBroker, deviceInfo, conductor); auxiliaryConnectionContexts = new HashMap<>(); deviceFlowRegistry = new DeviceFlowRegistryImpl(); deviceGroupRegistry = new DeviceGroupRegistryImpl(); deviceMeterRegistry = new DeviceMeterRegistryImpl(); - messageSpy = _messageSpy; + messageSpy = conductor.getMessageIntelligenceAgency(); + packetInLimiter = new PacketInRateLimiter(primaryConnectionContext.getConnectionAdapter(), /*initial*/ 1000, /*initial*/2000, messageSpy, REJECTED_DRAIN_FACTOR); this.translatorLibrary = translatorLibrary; portStatusTranslator = translatorLibrary.lookupTranslator( - new TranslatorKey(deviceState.getVersion(), PortGrouping.class.getName())); + new TranslatorKey(deviceInfo.getVersion(), PortGrouping.class.getName())); packetInTranslator = translatorLibrary.lookupTranslator( - new TranslatorKey(deviceState.getVersion(), PacketIn.class.getName())); + new TranslatorKey(deviceInfo.getVersion(), PacketIn.class.getName())); flowRemovedTranslator = translatorLibrary.lookupTranslator( - new TranslatorKey(deviceState.getVersion(), FlowRemoved.class.getName())); - - - nodeConnectorCache = new ConcurrentHashMap<>(); + new TranslatorKey(deviceInfo.getVersion(), FlowRemoved.class.getName())); itemLifeCycleSourceRegistry = new ItemLifeCycleRegistryImpl(); flowLifeCycleKeeper = new ItemLifeCycleSourceImpl(); itemLifeCycleSourceRegistry.registerLifeCycleSource(flowLifeCycleKeeper); - } - - /** - * @deprecated will be deleted - * @param txChainManager - */ - @Deprecated - void setTransactionChainManager(final TransactionChainManager txChainManager) { - this.transactionChainManager = Preconditions.checkNotNull(txChainManager); + deviceCtxState = DEVICE_CONTEXT_STATE.INITIALIZATION; } /** @@ -205,20 +181,13 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi transactionChainManager.initialSubmitWriteTransaction(); } - /** - * This method is called fron - */ - void cancelTransaction() { - transactionChainManager.cancelWriteTransaction(); - } - @Override - public Long getReservedXid() { + public Long reserveXidForDeviceMessage() { return outboundQueueProvider.reserveEntry(); } @Override - public void addAuxiliaryConenctionContext(final ConnectionContext connectionContext) { + public void addAuxiliaryConnectionContext(final ConnectionContext connectionContext) { final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext); auxiliaryConnectionContexts.put(connectionDistinguisher, connectionContext); } @@ -228,8 +197,11 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi } @Override - public void removeAuxiliaryConenctionContext(final ConnectionContext connectionContext) { - // TODO Auto-generated method stub + public void removeAuxiliaryConnectionContext(final ConnectionContext connectionContext) { + final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext); + LOG.debug("auxiliary connection dropped: {}, nodeId:{}", connectionContext.getConnectionAdapter() + .getRemoteAddress(), deviceInfo.getNodeId()); + auxiliaryConnectionContexts.remove(connectionDistinguisher); } @Override @@ -238,28 +210,28 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi } @Override - public ReadTransaction getReadTransaction() { - return dataBroker.newReadOnlyTransaction(); + public DeviceInfo getDeviceInfo() { + return this.deviceInfo; } @Override - public void onClusterRoleChange(@CheckForNull final OfpRole role) { - this.onClusterRoleChange(role, true); + public ReadOnlyTransaction getReadTransaction() { + return dataBroker.newReadOnlyTransaction(); } @Override - public void onInitClusterRoleChange(@CheckForNull final OfpRole role) { - this.onClusterRoleChange(role, false); + public void writeToTransaction(final LogicalDatastoreType store, + final InstanceIdentifier path, final T data) throws Exception { + transactionChainManager.writeToTransaction(store, path, data, false); } @Override - public void writeToTransaction(final LogicalDatastoreType store, - final InstanceIdentifier path, final T data) { - transactionChainManager.writeToTransaction(store, path, data); + public void writeToTransactionWithParentsSlow(LogicalDatastoreType store, InstanceIdentifier path, T data) throws Exception { + transactionChainManager.writeToTransaction(store, path, data, true); } @Override - public void addDeleteToTxChain(final LogicalDatastoreType store, final InstanceIdentifier path) { + public void addDeleteToTxChain(final LogicalDatastoreType store, final InstanceIdentifier path) throws Exception { transactionChainManager.addDeleteOperationTotTxChain(store, path); } @@ -315,23 +287,25 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi if (itemLifecycleListener != null) { //1. translate to general flow (table, priority, match, cookie) final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved flowRemovedNotification = - flowRemovedTranslator.translate(flowRemoved, this, null); + flowRemovedTranslator.translate(flowRemoved, deviceInfo, null); //2. create registry key - FlowRegistryKey flowRegKey = FlowRegistryKeyFactory.create(flowRemovedNotification); + final FlowRegistryKey flowRegKey = FlowRegistryKeyFactory.create(flowRemovedNotification); //3. lookup flowId final FlowDescriptor flowDescriptor = deviceFlowRegistry.retrieveIdForFlow(flowRegKey); //4. if flowId present: if (flowDescriptor != null) { // a) construct flow path - KeyedInstanceIdentifier flowPath = getDeviceState().getNodeInstanceIdentifier() + final KeyedInstanceIdentifier flowPath = getDeviceInfo().getNodeInstanceIdentifier() .augmentation(FlowCapableNode.class) .child(Table.class, flowDescriptor.getTableKey()) .child(Flow.class, new FlowKey(flowDescriptor.getFlowId())); // b) notify listener itemLifecycleListener.onRemoved(flowPath); + // c) trigger off a notification + notificationPublishService.offerNotification(flowRemovedNotification); } else { LOG.debug("flow id not found: nodeId={} tableId={}, priority={}", - getDeviceState().getNodeId(), flowRegKey.getTableId(), flowRemovedNotification.getPriority()); + getDeviceInfo().getNodeId(), flowRegKey.getTableId(), flowRemovedNotification.getPriority()); } } } @@ -339,24 +313,28 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi @Override public void processPortStatusMessage(final PortStatusMessage portStatus) { messageSpy.spyMessage(portStatus.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS); - final FlowCapableNodeConnector flowCapableNodeConnector = portStatusTranslator.translate(portStatus, this, null); + final FlowCapableNodeConnector flowCapableNodeConnector = portStatusTranslator.translate(portStatus, deviceInfo, null); final KeyedInstanceIdentifier iiToNodeConnector = provideIIToNodeConnector(portStatus.getPortNo(), portStatus.getVersion()); - if (portStatus.getReason().equals(PortReason.OFPPRADD) || portStatus.getReason().equals(PortReason.OFPPRMODIFY)) { - // because of ADD status node connector has to be created - final NodeConnectorBuilder nConnectorBuilder = new NodeConnectorBuilder().setKey(iiToNodeConnector.getKey()); - nConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build()); - nConnectorBuilder.addAugmentation(FlowCapableNodeConnector.class, flowCapableNodeConnector); - writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector, nConnectorBuilder.build()); - } else if (portStatus.getReason().equals(PortReason.OFPPRDELETE)) { - addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector); + try { + if (portStatus.getReason().equals(PortReason.OFPPRADD) || portStatus.getReason().equals(PortReason.OFPPRMODIFY)) { + // because of ADD status node connector has to be created + final NodeConnectorBuilder nConnectorBuilder = new NodeConnectorBuilder().setKey(iiToNodeConnector.getKey()); + nConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build()); + nConnectorBuilder.addAugmentation(FlowCapableNodeConnector.class, flowCapableNodeConnector); + writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector, nConnectorBuilder.build()); + } else if (portStatus.getReason().equals(PortReason.OFPPRDELETE)) { + addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector); + } + submitTransaction(); + } catch (final Exception e) { + LOG.warn("Error processing port status message: {}", e.getMessage()); } - submitTransaction(); } private KeyedInstanceIdentifier provideIIToNodeConnector(final long portNo, final short version) { - final InstanceIdentifier iiToNodes = deviceState.getNodeInstanceIdentifier(); - final BigInteger dataPathId = deviceState.getFeatures().getDatapathId(); + final InstanceIdentifier iiToNodes = deviceInfo.getNodeInstanceIdentifier(); + final BigInteger dataPathId = deviceInfo.getDatapathId(); final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), portNo, version); return iiToNodes.child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId)); } @@ -365,11 +343,11 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi public void processPacketInMessage(final PacketInMessage packetInMessage) { messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH); final ConnectionAdapter connectionAdapter = getPrimaryConnectionContext().getConnectionAdapter(); - final PacketReceived packetReceived = packetInTranslator.translate(packetInMessage, this, null); + final PacketReceived packetReceived = packetInTranslator.translate(packetInMessage, deviceInfo, null); if (packetReceived == null) { LOG.debug("Received a null packet from switch {}", connectionAdapter.getRemoteAddress()); - messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_SRC_FAILURE); + messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_SRC_FAILURE); return; } else { messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS); @@ -382,7 +360,7 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi return; } - final ListenableFuture offerNotification = notificationPublishService.offerNotification(packetReceived); + final ListenableFuture offerNotification = notificationPublishService.offerNotification(packetReceived); if (NotificationPublishService.REJECTED.equals(offerNotification)) { LOG.debug("notification offer rejected"); messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_NOTIFICATION_REJECTED); @@ -409,17 +387,17 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi } @Override - public void processExperimenterMessage(ExperimenterMessage notification) { + public void processExperimenterMessage(final ExperimenterMessage notification) { // lookup converter - ExperimenterDataOfChoice vendorData = notification.getExperimenterDataOfChoice(); - MessageTypeKey key = new MessageTypeKey<>( - deviceState.getVersion(), + final ExperimenterDataOfChoice vendorData = notification.getExperimenterDataOfChoice(); + final MessageTypeKey key = new MessageTypeKey<>( + deviceInfo.getVersion(), (Class) vendorData.getImplementedInterface()); final ConvertorMessageFromOFJava messageConverter = extensionConverterProvider.getMessageConverter(key); if (messageConverter == null) { LOG.warn("custom converter for {}[OF:{}] not found", notification.getExperimenterDataOfChoice().getImplementedInterface(), - deviceState.getVersion()); + deviceInfo.getVersion()); return; } // build notification @@ -427,12 +405,12 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi try { messageOfChoice = messageConverter.convert(vendorData, MessagePath.MESSAGE_NOTIFICATION); final ExperimenterMessageFromDevBuilder experimenterMessageFromDevBld = new ExperimenterMessageFromDevBuilder() - .setNode(new NodeRef(deviceState.getNodeInstanceIdentifier())) + .setNode(new NodeRef(deviceInfo.getNodeInstanceIdentifier())) .setExperimenterMessageOfChoice(messageOfChoice); // publish notificationPublishService.offerNotification(experimenterMessageFromDevBld.build()); - } catch (ConversionException e) { - LOG.warn("Conversion of experimenter notification failed", e); + } catch (final ConversionException e) { + LOG.error("Conversion of experimenter notification failed", e); } } @@ -442,85 +420,12 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi } @Override - public HashedWheelTimer getTimer() { - return hashedWheelTimer; - } - - @Override - public void close() { + public synchronized void close() { LOG.debug("closing deviceContext: {}, nodeId:{}", getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress(), - getDeviceState().getNodeId()); - - tearDown(); - - primaryConnectionContext.closeConnection(false); - } - - private synchronized void tearDown() { - if (deviceState.isValid()) { - deviceState.setValid(false); - - for (final ConnectionContext connectionContext : auxiliaryConnectionContexts.values()) { - connectionContext.closeConnection(false); - } - - LOG.info("Closing transaction chain manager without cleaning inventory operational"); - transactionChainManager.close(); - - deviceGroupRegistry.close(); - deviceFlowRegistry.close(); - deviceMeterRegistry.close(); - - final LinkedList reversedCloseHandlers = new LinkedList<>(closeHandlers); - Collections.reverse(reversedCloseHandlers); - for (final DeviceContextClosedHandler deviceContextClosedHandler : reversedCloseHandlers) { - deviceContextClosedHandler.onDeviceContextClosed(this); - } - closeHandlers.clear(); - } - } - - /** - */ - @Override - public void onDeviceDisconnectedFromCluster(final boolean removeNodeFromDS) { - LOG.info("Removing device from operational and closing transaction Manager for device:{}", getDeviceState().getNodeId()); - if (removeNodeFromDS) { - // FIXME : it has to be hooked for some another part of code - final WriteTransaction write = dataBroker.newWriteOnlyTransaction(); - write.delete(LogicalDatastoreType.OPERATIONAL, deviceState.getNodeInstanceIdentifier()); - final CheckedFuture deleteFuture = write.submit(); - Futures.addCallback(deleteFuture, new FutureCallback() { - - @Override - public void onSuccess(final Void result) { - LOG.debug("Delete Node {} was successful", deviceState.getNodeId()); - } - - @Override - public void onFailure(final Throwable t) { - LOG.warn("Delete Node {} fail.", deviceState.getNodeId(), t); - } - }); - } - } - - @Override - public void onDeviceDisconnected(final ConnectionContext connectionContext) { - if (getPrimaryConnectionContext().equals(connectionContext)) { - try { - tearDown(); - } catch (final Exception e) { - LOG.trace("Error closing device context."); - } - } else { - LOG.debug("auxiliary connection dropped: {}, nodeId:{}", - connectionContext.getConnectionAdapter().getRemoteAddress(), - getDeviceState().getNodeId()); - final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext); - auxiliaryConnectionContexts.remove(connectionDistinguisher); - } + getDeviceInfo().getNodeId()); + // NOOP + throw new UnsupportedOperationException("Autocloseble.close will be removed soon"); } @Override @@ -533,11 +438,6 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi return barrierTaskTimeout; } - @Override - public void setNotificationService(final NotificationService notificationServiceParam) { - notificationService = notificationServiceParam; - } - @Override public void setNotificationPublishService(final NotificationPublishService notificationPublishService) { this.notificationPublishService = notificationPublishService; @@ -548,13 +448,10 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi return messageSpy; } - @Override - public void addDeviceContextClosedHandler(final DeviceContextClosedHandler deviceContextClosedHandler) { - closeHandlers.add(deviceContextClosedHandler); - } - @Override public void onPublished() { + Verify.verify(DEVICE_CONTEXT_STATE.INITIALIZATION.equals(deviceCtxState)); + deviceCtxState = DEVICE_CONTEXT_STATE.WORKING; primaryConnectionContext.getConnectionAdapter().setPacketInFiltering(false); for (final ConnectionContext switchAuxConnectionContext : auxiliaryConnectionContexts.values()) { switchAuxConnectionContext.getConnectionAdapter().setPacketInFiltering(false); @@ -567,56 +464,63 @@ public class DeviceContextImpl implements DeviceContext, ExtensionConverterProvi } @Override - public NodeConnectorRef lookupNodeConnectorRef(Long portNumber) { - return nodeConnectorCache.get(portNumber); + public void updatePacketInRateLimit(final long upperBound) { + packetInLimiter.changeWaterMarks((int) (LOW_WATERMARK_FACTOR * upperBound), (int) (HIGH_WATERMARK_FACTOR * upperBound)); } @Override - public void storeNodeConnectorRef(final Long portNumber, final NodeConnectorRef nodeConnectorRef) { - nodeConnectorCache.put( - Preconditions.checkNotNull(portNumber), - Preconditions.checkNotNull(nodeConnectorRef)); + public ItemLifeCycleRegistry getItemLifeCycleSourceRegistry() { + return itemLifeCycleSourceRegistry; } @Override - public void updatePacketInRateLimit(long upperBound) { - packetInLimiter.changeWaterMarks((int) (LOW_WATERMARK_FACTOR * upperBound), (int) (HIGH_WATERMARK_FACTOR * upperBound)); + public void setExtensionConverterProvider(final ExtensionConverterProvider extensionConverterProvider) { + this.extensionConverterProvider = extensionConverterProvider; } @Override - public ItemLifeCycleRegistry getItemLifeCycleSourceRegistry() { - return itemLifeCycleSourceRegistry; + public ExtensionConverterProvider getExtensionConverterProvider() { + return extensionConverterProvider; } @Override - public void setRpcContext(RpcContext rpcContext) { - this.rpcContext = rpcContext; - } + public synchronized void shutdownConnection() { + LOG.debug("Shutdown method for node {}", deviceInfo.getNodeId()); + if (DEVICE_CONTEXT_STATE.TERMINATION.equals(deviceCtxState)) { + LOG.debug("DeviceCtx for Node {} is in termination process.", deviceInfo.getNodeId()); + return; + } + deviceCtxState = DEVICE_CONTEXT_STATE.TERMINATION; - @Override - public RpcContext getRpcContext() { - return rpcContext; + if (ConnectionContext.CONNECTION_STATE.RIP.equals(getPrimaryConnectionContext().getConnectionState())) { + LOG.debug("ConnectionCtx for Node {} is in RIP state.", deviceInfo.getNodeId()); + return; + } + /* Terminate Auxiliary Connection */ + for (final ConnectionContext connectionContext : auxiliaryConnectionContexts.values()) { + LOG.debug("Closing auxiliary connection {}", connectionContext.getNodeId()); + connectionContext.closeConnection(false); + } + /* Terminate Primary Connection */ + getPrimaryConnectionContext().closeConnection(true); + /* Close all Group Registry */ + deviceGroupRegistry.close(); + deviceFlowRegistry.close(); + deviceMeterRegistry.close(); } @Override - public void setExtensionConverterProvider(ExtensionConverterProvider extensionConverterProvider) { - this.extensionConverterProvider = extensionConverterProvider; + public DEVICE_CONTEXT_STATE getDeviceContextState() { + return deviceCtxState; } @Override - public ExtensionConverterProvider getExtensionConverterProvider() { - return extensionConverterProvider; + public ListenableFuture shuttingDownDataStoreTransactions() { + return transactionChainManager.shuttingDown(); } - private void onClusterRoleChange(@CheckForNull final OfpRole role, final boolean enableSubmit) { - LOG.debug("onClusterRoleChange {} for node:", role, deviceState.getNodeId()); - Preconditions.checkArgument(role != null); - if (OfpRole.BECOMESLAVE.equals(role)) { - transactionChainManager.activateTransactionManager(enableSubmit); - } else if (OfpRole.BECOMEMASTER.equals(role)) { - transactionChainManager.deactivateTransactionManager(); - } else { - LOG.warn("Unknow OFCluster Role {} for Node {}", role, deviceState.getNodeId()); - } + @VisibleForTesting + TransactionChainManager getTransactionChainManager() { + return this.transactionChainManager; } }