Merge "Bug 4957 RoleContext updated with initialization"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / device / DeviceContextImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.openflowplugin.impl.device;
9
10 import javax.annotation.Nonnull;
11 import java.math.BigInteger;
12 import java.util.Collection;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.concurrent.ConcurrentHashMap;
18
19 import com.google.common.annotations.VisibleForTesting;
20 import com.google.common.base.Preconditions;
21 import com.google.common.util.concurrent.FutureCallback;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import io.netty.util.HashedWheelTimer;
25 import io.netty.util.Timeout;
26 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
27 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
28 import org.opendaylight.controller.md.sal.binding.api.NotificationService;
29 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
32 import org.opendaylight.openflowjava.protocol.api.connection.OutboundQueue;
33 import org.opendaylight.openflowjava.protocol.api.keys.MessageTypeKey;
34 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
35 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
36 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
37 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
38 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
39 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
40 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
41 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
42 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextClosedHandler;
43 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
44 import org.opendaylight.openflowplugin.api.openflow.md.core.SwitchConnectionDistinguisher;
45 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
46 import org.opendaylight.openflowplugin.api.openflow.registry.ItemLifeCycleRegistry;
47 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
48 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
49 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
50 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
51 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
52 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleKeeper;
53 import org.opendaylight.openflowplugin.api.openflow.rpc.RpcContext;
54 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
55 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
56 import org.opendaylight.openflowplugin.extension.api.ConvertorMessageFromOFJava;
57 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterProviderKeeper;
58 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
59 import org.opendaylight.openflowplugin.extension.api.exception.ConversionException;
60 import org.opendaylight.openflowplugin.extension.api.path.MessagePath;
61 import org.opendaylight.openflowplugin.impl.common.ItemLifeCycleSourceImpl;
62 import org.opendaylight.openflowplugin.impl.common.NodeStaticReplyTranslatorUtil;
63 import org.opendaylight.openflowplugin.impl.device.listener.MultiMsgCollectorImpl;
64 import org.opendaylight.openflowplugin.impl.registry.flow.DeviceFlowRegistryImpl;
65 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
66 import org.opendaylight.openflowplugin.impl.registry.group.DeviceGroupRegistryImpl;
67 import org.opendaylight.openflowplugin.impl.registry.meter.DeviceMeterRegistryImpl;
68 import org.opendaylight.openflowplugin.openflow.md.core.session.SwitchConnectionCookieOFImpl;
69 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.ExperimenterMessageFromDevBuilder;
70 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
71 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
72 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
73 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
74 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeRef;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnector;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorBuilder;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.node.NodeConnectorKey;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.ExperimenterMessage;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketIn;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.experimenter.core.ExperimenterDataOfChoice;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.experimenter.core.message.ExperimenterMessageOfChoice;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsData;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.port.statistics.rev131214.FlowCapableNodeConnectorStatisticsDataBuilder;
97 import org.opendaylight.yangtools.yang.binding.DataObject;
98 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
99 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
100 import org.slf4j.Logger;
101 import org.slf4j.LoggerFactory;
102
103 /**
104  *
105  */
106 public class DeviceContextImpl implements DeviceContext, ExtensionConverterProviderKeeper {
107
108     private static final Logger LOG = LoggerFactory.getLogger(DeviceContextImpl.class);
109
110     // TODO: drain factor should be parametrized
111     public static final float REJECTED_DRAIN_FACTOR = 0.25f;
112     // TODO: low water mark factor should be parametrized
113     private static final float LOW_WATERMARK_FACTOR = 0.75f;
114     // TODO: high water mark factor should be parametrized
115     private static final float HIGH_WATERMARK_FACTOR = 0.95f;
116
117     private final ConnectionContext primaryConnectionContext;
118     private final DeviceState deviceState;
119     private final DataBroker dataBroker;
120     private final HashedWheelTimer hashedWheelTimer;
121     private final Map<SwitchConnectionDistinguisher, ConnectionContext> auxiliaryConnectionContexts;
122     private TransactionChainManager transactionChainManager;
123     private final DeviceFlowRegistry deviceFlowRegistry;
124     private final DeviceGroupRegistry deviceGroupRegistry;
125     private final DeviceMeterRegistry deviceMeterRegistry;
126     private final Collection<DeviceContextClosedHandler> closeHandlers = new HashSet<>();
127     private final PacketInRateLimiter packetInLimiter;
128     private final MessageSpy messageSpy;
129     private final ItemLifeCycleKeeper flowLifeCycleKeeper;
130     private NotificationPublishService notificationPublishService;
131     private NotificationService notificationService;
132     private final OutboundQueue outboundQueueProvider;
133     private Timeout barrierTaskTimeout;
134     private final MessageTranslator<PortGrouping, FlowCapableNodeConnector> portStatusTranslator;
135     private final MessageTranslator<PacketInMessage, PacketReceived> packetInTranslator;
136     private final MessageTranslator<FlowRemoved, org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved> flowRemovedTranslator;
137     private final TranslatorLibrary translatorLibrary;
138     private Map<Long, NodeConnectorRef> nodeConnectorCache;
139     private ItemLifeCycleRegistry itemLifeCycleSourceRegistry;
140     private RpcContext rpcContext;
141     private ExtensionConverterProvider extensionConverterProvider;
142
143
144     @VisibleForTesting
145     DeviceContextImpl(@Nonnull final ConnectionContext primaryConnectionContext,
146                       @Nonnull final DeviceState deviceState,
147                       @Nonnull final DataBroker dataBroker,
148                       @Nonnull final HashedWheelTimer hashedWheelTimer,
149                       @Nonnull final MessageSpy _messageSpy,
150                       @Nonnull final OutboundQueueProvider outboundQueueProvider,
151             @Nonnull final TranslatorLibrary translatorLibrary) {
152         this.primaryConnectionContext = Preconditions.checkNotNull(primaryConnectionContext);
153         this.deviceState = Preconditions.checkNotNull(deviceState);
154         this.dataBroker = Preconditions.checkNotNull(dataBroker);
155         this.hashedWheelTimer = Preconditions.checkNotNull(hashedWheelTimer);
156         this.outboundQueueProvider = Preconditions.checkNotNull(outboundQueueProvider);
157         primaryConnectionContext.setDeviceDisconnectedHandler(DeviceContextImpl.this);
158         auxiliaryConnectionContexts = new HashMap<>();
159         deviceFlowRegistry = new DeviceFlowRegistryImpl();
160         deviceGroupRegistry = new DeviceGroupRegistryImpl();
161         deviceMeterRegistry = new DeviceMeterRegistryImpl();
162         messageSpy = _messageSpy;
163
164         packetInLimiter = new PacketInRateLimiter(primaryConnectionContext.getConnectionAdapter(),
165                 /*initial*/ 1000, /*initial*/2000, messageSpy, REJECTED_DRAIN_FACTOR);
166
167         this.translatorLibrary = translatorLibrary;
168         portStatusTranslator = translatorLibrary.lookupTranslator(
169                 new TranslatorKey(deviceState.getVersion(), PortGrouping.class.getName()));
170         packetInTranslator = translatorLibrary.lookupTranslator(
171                 new TranslatorKey(deviceState.getVersion(), PacketIn.class.getName()));
172         flowRemovedTranslator = translatorLibrary.lookupTranslator(
173                 new TranslatorKey(deviceState.getVersion(), FlowRemoved.class.getName()));
174
175
176         nodeConnectorCache = new ConcurrentHashMap<>();
177
178         itemLifeCycleSourceRegistry = new ItemLifeCycleRegistryImpl();
179         flowLifeCycleKeeper = new ItemLifeCycleSourceImpl();
180         itemLifeCycleSourceRegistry.registerLifeCycleSource(flowLifeCycleKeeper);
181     }
182
183     void setTransactionChainManager(final TransactionChainManager txChainManager) {
184         this.transactionChainManager = Preconditions.checkNotNull(txChainManager);
185     }
186
187     /**
188      * This method is called from {@link DeviceManagerImpl} only. So we could say "posthandshake process finish"
189      * and we are able to set a scheduler for an automatic transaction submitting by time (0,5sec).
190      */
191     void initialSubmitTransaction() {
192         transactionChainManager.initialSubmitWriteTransaction();
193     }
194
195     /**
196      * This method is called fron
197      */
198     void cancelTransaction() {
199         transactionChainManager.cancelWriteTransaction();
200     }
201
202     @Override
203     public Long getReservedXid() {
204         return outboundQueueProvider.reserveEntry();
205     }
206
207     @Override
208     public void addAuxiliaryConenctionContext(final ConnectionContext connectionContext) {
209         final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext);
210         auxiliaryConnectionContexts.put(connectionDistinguisher, connectionContext);
211     }
212
213     private static SwitchConnectionDistinguisher createConnectionDistinguisher(final ConnectionContext connectionContext) {
214         return new SwitchConnectionCookieOFImpl(connectionContext.getFeatures().getAuxiliaryId());
215     }
216
217     @Override
218     public void removeAuxiliaryConenctionContext(final ConnectionContext connectionContext) {
219         // TODO Auto-generated method stub
220     }
221
222     @Override
223     public DeviceState getDeviceState() {
224         return deviceState;
225     }
226
227     @Override
228     public ReadTransaction getReadTransaction() {
229         return dataBroker.newReadOnlyTransaction();
230     }
231
232     @Override
233     public <T extends DataObject> void writeToTransaction(final LogicalDatastoreType store,
234                                                           final InstanceIdentifier<T> path, final T data) {
235         transactionChainManager.writeToTransaction(store, path, data);
236     }
237
238     @Override
239     public <T extends DataObject> void addDeleteToTxChain(final LogicalDatastoreType store, final InstanceIdentifier<T> path) {
240         transactionChainManager.addDeleteOperationTotTxChain(store, path);
241     }
242
243     @Override
244     public boolean submitTransaction() {
245         return transactionChainManager.submitWriteTransaction();
246     }
247
248     @Override
249     public ConnectionContext getPrimaryConnectionContext() {
250         return primaryConnectionContext;
251     }
252
253     @Override
254     public ConnectionContext getAuxiliaryConnectiobContexts(final BigInteger cookie) {
255         return auxiliaryConnectionContexts.get(new SwitchConnectionCookieOFImpl(cookie.longValue()));
256     }
257
258     @Override
259     public DeviceFlowRegistry getDeviceFlowRegistry() {
260         return deviceFlowRegistry;
261     }
262
263     @Override
264     public DeviceGroupRegistry getDeviceGroupRegistry() {
265         return deviceGroupRegistry;
266     }
267
268     @Override
269     public DeviceMeterRegistry getDeviceMeterRegistry() {
270         return deviceMeterRegistry;
271     }
272
273     @Override
274     public void processReply(final OfHeader ofHeader) {
275         if (ofHeader instanceof Error) {
276             messageSpy.spyMessage(ofHeader.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
277         } else {
278             messageSpy.spyMessage(ofHeader.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
279         }
280     }
281
282     @Override
283     public void processReply(final Xid xid, final List<MultipartReply> ofHeaderList) {
284         for (final MultipartReply multipartReply : ofHeaderList) {
285             messageSpy.spyMessage(multipartReply.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE);
286         }
287     }
288
289     @Override
290     public void processFlowRemovedMessage(final FlowRemoved flowRemoved) {
291         final ItemLifecycleListener itemLifecycleListener = flowLifeCycleKeeper.getItemLifecycleListener();
292         if (itemLifecycleListener != null) {
293             //1. translate to general flow (table, priority, match, cookie)
294             final org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemoved flowRemovedNotification =
295                     flowRemovedTranslator.translate(flowRemoved, this, null);
296             //2. create registry key
297             FlowRegistryKey flowRegKey = FlowRegistryKeyFactory.create(flowRemovedNotification);
298             //3. lookup flowId
299             final FlowDescriptor flowDescriptor = deviceFlowRegistry.retrieveIdForFlow(flowRegKey);
300             //4. if flowId present:
301             if (flowDescriptor != null) {
302                 // a) construct flow path
303                 KeyedInstanceIdentifier<Flow, FlowKey> flowPath = getDeviceState().getNodeInstanceIdentifier()
304                         .augmentation(FlowCapableNode.class)
305                         .child(Table.class, flowDescriptor.getTableKey())
306                         .child(Flow.class, new FlowKey(flowDescriptor.getFlowId()));
307                 // b) notify listener
308                 itemLifecycleListener.onRemoved(flowPath);
309             } else {
310                 LOG.debug("flow id not found: nodeId={} tableId={}, priority={}",
311                         getDeviceState().getNodeId(), flowRegKey.getTableId(), flowRemovedNotification.getPriority());
312             }
313         }
314     }
315
316     @Override
317     public void processPortStatusMessage(final PortStatusMessage portStatus) {
318         messageSpy.spyMessage(portStatus.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
319         final FlowCapableNodeConnector flowCapableNodeConnector = portStatusTranslator.translate(portStatus, this, null);
320
321         final KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> iiToNodeConnector = provideIIToNodeConnector(portStatus.getPortNo(), portStatus.getVersion());
322         if (portStatus.getReason().equals(PortReason.OFPPRADD) || portStatus.getReason().equals(PortReason.OFPPRMODIFY)) {
323             // because of ADD status node connector has to be created
324             final NodeConnectorBuilder nConnectorBuilder = new NodeConnectorBuilder().setKey(iiToNodeConnector.getKey());
325             nConnectorBuilder.addAugmentation(FlowCapableNodeConnectorStatisticsData.class, new FlowCapableNodeConnectorStatisticsDataBuilder().build());
326             nConnectorBuilder.addAugmentation(FlowCapableNodeConnector.class, flowCapableNodeConnector);
327             writeToTransaction(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector, nConnectorBuilder.build());
328         } else if (portStatus.getReason().equals(PortReason.OFPPRDELETE)) {
329             addDeleteToTxChain(LogicalDatastoreType.OPERATIONAL, iiToNodeConnector);
330         }
331         submitTransaction();
332     }
333
334     private KeyedInstanceIdentifier<NodeConnector, NodeConnectorKey> provideIIToNodeConnector(final long portNo, final short version) {
335         final InstanceIdentifier<Node> iiToNodes = deviceState.getNodeInstanceIdentifier();
336         final BigInteger dataPathId = deviceState.getFeatures().getDatapathId();
337         final NodeConnectorId nodeConnectorId = NodeStaticReplyTranslatorUtil.nodeConnectorId(dataPathId.toString(), portNo, version);
338         return iiToNodes.child(NodeConnector.class, new NodeConnectorKey(nodeConnectorId));
339     }
340
341     @Override
342     public void processPacketInMessage(final PacketInMessage packetInMessage) {
343         messageSpy.spyMessage(packetInMessage.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH);
344         final ConnectionAdapter connectionAdapter = getPrimaryConnectionContext().getConnectionAdapter();
345         final PacketReceived packetReceived = packetInTranslator.translate(packetInMessage, this, null);
346
347         if (packetReceived == null) {
348             LOG.debug("Received a null packet from switch {}", connectionAdapter.getRemoteAddress());
349             messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_SRC_FAILURE);
350             return;
351         } else {
352             messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_TRANSLATE_OUT_SUCCESS);
353         }
354
355         if (!packetInLimiter.acquirePermit()) {
356             LOG.debug("Packet limited");
357             // TODO: save packet into emergency slot if possible
358             messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PACKET_IN_LIMIT_REACHED_AND_DROPPED);
359             return;
360         }
361
362         final ListenableFuture<? extends Object> offerNotification = notificationPublishService.offerNotification(packetReceived);
363         if (NotificationPublishService.REJECTED.equals(offerNotification)) {
364             LOG.debug("notification offer rejected");
365             messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_NOTIFICATION_REJECTED);
366             packetInLimiter.drainLowWaterMark();
367             packetInLimiter.releasePermit();
368             return;
369         }
370
371         Futures.addCallback(offerNotification, new FutureCallback<Object>() {
372             @Override
373             public void onSuccess(final Object result) {
374                 messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS);
375                 packetInLimiter.releasePermit();
376             }
377
378             @Override
379             public void onFailure(final Throwable t) {
380                 messageSpy.spyMessage(packetReceived.getImplementedInterface(), MessageSpy.STATISTIC_GROUP.FROM_SWITCH_NOTIFICATION_REJECTED);
381                 LOG.debug("notification offer failed: {}", t.getMessage());
382                 LOG.trace("notification offer failed..", t);
383                 packetInLimiter.releasePermit();
384             }
385         });
386     }
387
388     @Override
389     public void processExperimenterMessage(ExperimenterMessage notification) {
390         // lookup converter
391         ExperimenterDataOfChoice vendorData = notification.getExperimenterDataOfChoice();
392         MessageTypeKey<? extends ExperimenterDataOfChoice> key = new MessageTypeKey<>(
393                 deviceState.getVersion(),
394                 (Class<? extends ExperimenterDataOfChoice>) vendorData.getImplementedInterface());
395         final ConvertorMessageFromOFJava<ExperimenterDataOfChoice, MessagePath> messageConverter = extensionConverterProvider.getMessageConverter(key);
396         if (messageConverter == null) {
397             LOG.warn("custom converter for {}[OF:{}] not found",
398                     notification.getExperimenterDataOfChoice().getImplementedInterface(),
399                     deviceState.getVersion());
400             return;
401         }
402         // build notification
403         final ExperimenterMessageOfChoice messageOfChoice;
404         try {
405             messageOfChoice = messageConverter.convert(vendorData, MessagePath.MESSAGE_NOTIFICATION);
406             final ExperimenterMessageFromDevBuilder experimenterMessageFromDevBld = new ExperimenterMessageFromDevBuilder()
407                 .setNode(new NodeRef(deviceState.getNodeInstanceIdentifier()))
408                     .setExperimenterMessageOfChoice(messageOfChoice);
409             // publish
410             notificationPublishService.offerNotification(experimenterMessageFromDevBld.build());
411         } catch (ConversionException e) {
412             LOG.warn("Conversion of experimenter notification failed", e);
413         }
414     }
415
416     @Override
417     public TranslatorLibrary oook() {
418         return translatorLibrary;
419     }
420
421     @Override
422     public HashedWheelTimer getTimer() {
423         return hashedWheelTimer;
424     }
425
426     @Override
427     public void close() {
428         LOG.debug("closing deviceContext: {}, nodeId:{}",
429                 getPrimaryConnectionContext().getConnectionAdapter().getRemoteAddress(),
430                 getDeviceState().getNodeId());
431
432         tearDown();
433
434         primaryConnectionContext.closeConnection(false);
435     }
436
437     private void tearDown() {
438         deviceState.setValid(false);
439
440         for (final ConnectionContext connectionContext : auxiliaryConnectionContexts.values()) {
441             connectionContext.closeConnection(false);
442         }
443
444         deviceGroupRegistry.close();
445         deviceFlowRegistry.close();
446         deviceMeterRegistry.close();
447
448         itemLifeCycleSourceRegistry.clear();
449
450
451         for (final DeviceContextClosedHandler deviceContextClosedHandler : closeHandlers) {
452             deviceContextClosedHandler.onDeviceContextClosed(this);
453         }
454
455         LOG.info("Closing transaction chain manager without cleaning inventory operational");
456         transactionChainManager.close();
457     }
458
459     @Override
460     public void onDeviceDisconnectedFromCluster() {
461         LOG.info("Removing device from operational and closing transaction Manager for device:{}", getDeviceState().getNodeId());
462         transactionChainManager.cleanupPostClosure();
463     }
464
465     @Override
466     public void onDeviceDisconnected(final ConnectionContext connectionContext) {
467         if (getPrimaryConnectionContext().equals(connectionContext)) {
468             try {
469                 tearDown();
470             } catch (final Exception e) {
471                 LOG.trace("Error closing device context.");
472             }
473         } else {
474             LOG.debug("auxiliary connection dropped: {}, nodeId:{}",
475                     connectionContext.getConnectionAdapter().getRemoteAddress(),
476                     getDeviceState().getNodeId());
477             final SwitchConnectionDistinguisher connectionDistinguisher = createConnectionDistinguisher(connectionContext);
478             auxiliaryConnectionContexts.remove(connectionDistinguisher);
479         }
480     }
481
482     @Override
483     public void setCurrentBarrierTimeout(final Timeout timeout) {
484         barrierTaskTimeout = timeout;
485     }
486
487     @Override
488     public Timeout getBarrierTaskTimeout() {
489         return barrierTaskTimeout;
490     }
491
492     @Override
493     public void setNotificationService(final NotificationService notificationServiceParam) {
494         notificationService = notificationServiceParam;
495     }
496
497     @Override
498     public void setNotificationPublishService(final NotificationPublishService notificationPublishService) {
499         this.notificationPublishService = notificationPublishService;
500     }
501
502     @Override
503     public MessageSpy getMessageSpy() {
504         return messageSpy;
505     }
506
507     @Override
508     public void addDeviceContextClosedHandler(final DeviceContextClosedHandler deviceContextClosedHandler) {
509         closeHandlers.add(deviceContextClosedHandler);
510     }
511
512     @Override
513     public void onPublished() {
514         primaryConnectionContext.getConnectionAdapter().setPacketInFiltering(false);
515         for (final ConnectionContext switchAuxConnectionContext : auxiliaryConnectionContexts.values()) {
516             switchAuxConnectionContext.getConnectionAdapter().setPacketInFiltering(false);
517         }
518     }
519
520     @Override
521     public MultiMsgCollector getMultiMsgCollector(final RequestContext<List<MultipartReply>> requestContext) {
522         return new MultiMsgCollectorImpl(this, requestContext);
523     }
524
525     @Override
526     public NodeConnectorRef lookupNodeConnectorRef(Long portNumber) {
527         return nodeConnectorCache.get(portNumber);
528     }
529
530     @Override
531     public void storeNodeConnectorRef(final Long portNumber, final NodeConnectorRef nodeConnectorRef) {
532         nodeConnectorCache.put(
533                 Preconditions.checkNotNull(portNumber),
534                 Preconditions.checkNotNull(nodeConnectorRef));
535     }
536
537     @Override
538     public void updatePacketInRateLimit(long upperBound) {
539         packetInLimiter.changeWaterMarks((int) (LOW_WATERMARK_FACTOR * upperBound), (int) (HIGH_WATERMARK_FACTOR * upperBound));
540     }
541
542     @Override
543     public ItemLifeCycleRegistry getItemLifeCycleSourceRegistry() {
544         return itemLifeCycleSourceRegistry;
545     }
546
547     @Override
548     public void setRpcContext(RpcContext rpcContext) {
549         this.rpcContext = rpcContext;
550     }
551
552     @Override
553     public RpcContext getRpcContext() {
554         return rpcContext;
555     }
556
557     @Override
558     public void setExtensionConverterProvider(ExtensionConverterProvider extensionConverterProvider) {
559         this.extensionConverterProvider = extensionConverterProvider;
560     }
561
562     @Override
563     public ExtensionConverterProvider getExtensionConverterProvider() {
564         return extensionConverterProvider;
565     }
566 }