2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.openflowplugin.impl.device;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Matchers.eq;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.verify;
17 import static org.mockito.Mockito.when;
19 import java.math.BigInteger;
20 import java.net.InetSocketAddress;
21 import java.util.concurrent.atomic.AtomicLong;
23 import com.google.common.base.Optional;
24 import com.google.common.collect.Lists;
25 import com.google.common.util.concurrent.CheckedFuture;
26 import com.google.common.util.concurrent.Futures;
27 import com.google.common.util.concurrent.ListenableFuture;
28 import com.google.common.util.concurrent.SettableFuture;
29 import io.netty.util.HashedWheelTimer;
30 import io.netty.util.Timeout;
31 import org.junit.Assert;
32 import org.junit.Before;
33 import org.junit.Ignore;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.InOrder;
37 import org.mockito.Mock;
38 import org.mockito.Mockito;
39 import org.mockito.invocation.InvocationOnMock;
40 import org.mockito.runners.MockitoJUnitRunner;
41 import org.mockito.stubbing.Answer;
42 import org.opendaylight.controller.md.sal.binding.api.BindingTransactionChain;
43 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
44 import org.opendaylight.controller.md.sal.binding.api.NotificationPublishService;
45 import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
46 import org.opendaylight.controller.md.sal.binding.api.ReadTransaction;
47 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
48 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
49 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
50 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
51 import org.opendaylight.openflowplugin.api.OFConstants;
52 import org.opendaylight.openflowplugin.api.openflow.connection.ConnectionContext;
53 import org.opendaylight.openflowplugin.api.openflow.connection.OutboundQueueProvider;
54 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
55 import org.opendaylight.openflowplugin.api.openflow.device.DeviceState;
56 import org.opendaylight.openflowplugin.api.openflow.device.MessageTranslator;
57 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
58 import org.opendaylight.openflowplugin.api.openflow.device.TranslatorLibrary;
59 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
60 import org.opendaylight.openflowplugin.api.openflow.device.handlers.DeviceContextClosedHandler;
61 import org.opendaylight.openflowplugin.api.openflow.md.core.TranslatorKey;
62 import org.opendaylight.openflowplugin.api.openflow.registry.flow.DeviceFlowRegistry;
63 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowDescriptor;
64 import org.opendaylight.openflowplugin.api.openflow.registry.flow.FlowRegistryKey;
65 import org.opendaylight.openflowplugin.api.openflow.registry.group.DeviceGroupRegistry;
66 import org.opendaylight.openflowplugin.api.openflow.registry.meter.DeviceMeterRegistry;
67 import org.opendaylight.openflowplugin.api.openflow.rpc.ItemLifeCycleSource;
68 import org.opendaylight.openflowplugin.api.openflow.rpc.listener.ItemLifecycleListener;
69 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageIntelligenceAgency;
70 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
71 import org.opendaylight.openflowplugin.impl.registry.flow.FlowDescriptorFactory;
72 import org.opendaylight.openflowplugin.impl.registry.flow.FlowRegistryKeyFactory;
73 import org.opendaylight.openflowplugin.impl.util.DeviceStateUtil;
74 import org.opendaylight.openflowplugin.openflow.md.util.OpenflowPortsUtil;
75 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
76 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNodeConnector;
77 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
78 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.Table;
79 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.TableKey;
80 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
81 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
82 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.FlowRemovedBuilder;
83 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.FlowCookie;
84 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
85 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorRef;
86 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
87 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
88 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
89 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
90 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.PortReason;
91 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
92 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FeaturesReply;
93 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemovedMessageBuilder;
94 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetAsyncReply;
95 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.GetFeaturesOutput;
96 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
97 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
98 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketIn;
99 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PacketInMessage;
100 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortGrouping;
101 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.PortStatusMessage;
102 import org.opendaylight.yang.gen.v1.urn.opendaylight.packet.service.rev130709.PacketReceived;
103 import org.opendaylight.yangtools.concepts.Registration;
104 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
105 import org.opendaylight.yangtools.yang.binding.KeyedInstanceIdentifier;
106 import org.opendaylight.yangtools.yang.common.RpcResult;
107 import org.slf4j.Logger;
108 import org.slf4j.LoggerFactory;
110 @RunWith(MockitoJUnitRunner.class)
111 public class DeviceContextImplTest {
112 private static final Logger LOG = LoggerFactory
113 .getLogger(DeviceContextImplTest.class);
114 private static final short DUMMY_AUXILIARY_ID = 33;
115 private static final BigInteger DUMMY_COOKIE = new BigInteger("33");
116 private static final Long DUMMY_XID = 544L;
117 private static final Long DUMMY_PORT_NUMBER = 159L;
118 private static final BigInteger DUMMY_DATAPATH_ID = new BigInteger("55");
121 DeviceContextImpl deviceContext;
123 TransactionChainManager txChainManager;
125 RequestContext<GetAsyncReply> requestContext;
127 RequestContext<MultipartReply> requestContextMultiReply;
130 ConnectionContext connectionContext;
132 DeviceState deviceState;
134 DataBroker dataBroker;
136 WriteTransaction wTx;
138 ReadOnlyTransaction rTx;
140 BindingTransactionChain txChainFactory;
142 HashedWheelTimer timer;
144 MessageIntelligenceAgency messageIntelligenceAgency;
146 OutboundQueueProvider outboundQueueProvider;
148 ConnectionAdapter connectionAdapter;
149 NodeId nodeId = new NodeId("h2g2:42");
150 KeyedInstanceIdentifier<Node, NodeKey> nodeKeyIdent = DeviceStateUtil.createNodeInstanceIdentifier(nodeId);
152 TranslatorLibrary translatorLibrary;
154 Registration registration;
156 MessageTranslator messageTranslatorPacketReceived;
158 MessageTranslator messageTranslatorFlowCapableNodeConnector;
160 private MessageTranslator<Object, Object> messageTranslatorFlowRemoved;
162 private InOrder inOrderDevState;
164 private final AtomicLong atomicLong = new AtomicLong(0);
167 public void setUp() {
168 final CheckedFuture<Optional<Node>, ReadFailedException> noExistNodeFuture = Futures.immediateCheckedFuture(Optional.<Node>absent());
169 Mockito.when(rTx.read(LogicalDatastoreType.OPERATIONAL, nodeKeyIdent)).thenReturn(noExistNodeFuture);
170 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
171 Mockito.when(dataBroker.createTransactionChain(Mockito.any(TransactionChainManager.class))).thenReturn(txChainFactory);
172 Mockito.when(deviceState.getNodeInstanceIdentifier()).thenReturn(nodeKeyIdent);
173 Mockito.when(deviceState.getNodeId()).thenReturn(nodeId);
174 // txChainManager = new TransactionChainManager(dataBroker, deviceState);
175 final SettableFuture<RpcResult<GetAsyncReply>> settableFuture = SettableFuture.create();
176 final SettableFuture<RpcResult<MultipartReply>> settableFutureMultiReply = SettableFuture.create();
177 Mockito.when(requestContext.getFuture()).thenReturn(settableFuture);
178 Mockito.doAnswer(new Answer<Object>() {
179 @SuppressWarnings("unchecked")
181 public Object answer(final InvocationOnMock invocation) {
182 settableFuture.set((RpcResult<GetAsyncReply>) invocation.getArguments()[0]);
185 }).when(requestContext).setResult(any(RpcResult.class));
187 Mockito.when(requestContextMultiReply.getFuture()).thenReturn(settableFutureMultiReply);
188 Mockito.doAnswer(new Answer<Object>() {
189 @SuppressWarnings("unchecked")
191 public Object answer(final InvocationOnMock invocation) {
192 settableFutureMultiReply.set((RpcResult<MultipartReply>) invocation.getArguments()[0]);
195 }).when(requestContextMultiReply).setResult(any(RpcResult.class));
196 Mockito.when(txChainFactory.newWriteOnlyTransaction()).thenReturn(wTx);
197 Mockito.when(dataBroker.newReadOnlyTransaction()).thenReturn(rTx);
198 Mockito.when(connectionContext.getOutboundQueueProvider()).thenReturn(outboundQueueProvider);
199 Mockito.when(connectionContext.getConnectionAdapter()).thenReturn(connectionAdapter);
201 Mockito.when(deviceState.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
202 Mockito.when(messageTranslatorPacketReceived.translate(any(Object.class), any(DeviceContext.class), any(Object.class))).thenReturn(mock(PacketReceived.class));
203 Mockito.when(messageTranslatorFlowCapableNodeConnector.translate(any(Object.class), any(DeviceContext.class), any(Object.class))).thenReturn(mock(FlowCapableNodeConnector.class));
204 Mockito.when(translatorLibrary.lookupTranslator(eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PacketIn.class.getName())))).thenReturn(messageTranslatorPacketReceived);
205 Mockito.when(translatorLibrary.lookupTranslator(eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3, PortGrouping.class.getName())))).thenReturn(messageTranslatorFlowCapableNodeConnector);
206 Mockito.when(translatorLibrary.lookupTranslator(eq(new TranslatorKey(OFConstants.OFP_VERSION_1_3,
207 org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.FlowRemoved.class.getName()))))
208 .thenReturn(messageTranslatorFlowRemoved);
210 deviceContext = new DeviceContextImpl(connectionContext, deviceState, dataBroker, timer, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, false);
212 xid = new Xid(atomicLong.incrementAndGet());
213 xidMulti = new Xid(atomicLong.incrementAndGet());
216 @Test(expected = NullPointerException.class)
217 public void testDeviceContextImplConstructorNullDataBroker() throws Exception {
218 new DeviceContextImpl(connectionContext, deviceState, null, timer, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, false).close();
221 @Test(expected = NullPointerException.class)
222 public void testDeviceContextImplConstructorNullDeviceState() throws Exception {
223 new DeviceContextImpl(connectionContext, null, dataBroker, timer, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, false).close();
226 @Test(expected = NullPointerException.class)
227 public void testDeviceContextImplConstructorNullTimer() throws Exception {
228 new DeviceContextImpl(null, deviceState, dataBroker, null, messageIntelligenceAgency, outboundQueueProvider, translatorLibrary, false).close();
232 public void testGetDeviceState() {
233 final DeviceState deviceSt = deviceContext.getDeviceState();
234 assertNotNull(deviceSt);
235 Assert.assertEquals(deviceState, deviceSt);
239 public void testGetReadTransaction() {
240 final ReadTransaction readTx = deviceContext.getReadTransaction();
241 assertNotNull(readTx);
242 Assert.assertEquals(rTx, readTx);
246 * FIXME: Need to change the test on behalf the clustering transaction chain manager changes
251 public void testInitialSubmitTransaction() throws Exception {
252 deviceContext.initialSubmitTransaction();
253 verify(txChainManager).initialSubmitWriteTransaction();
257 public void testGetReservedXid() {
258 deviceContext.reservedXidForDeviceMessage();
259 verify(outboundQueueProvider).reserveEntry();
263 public void testAuxiliaryConnectionContext() {
264 ConnectionContext mockedConnectionContext = addDummyAuxiliaryConnectionContext();
265 final ConnectionContext pickedConnectiobContexts = deviceContext.getAuxiliaryConnectiobContexts(DUMMY_COOKIE);
266 assertEquals(mockedConnectionContext, pickedConnectiobContexts);
269 private ConnectionContext addDummyAuxiliaryConnectionContext() {
270 ConnectionContext mockedConnectionContext = prepareConnectionContext();
271 deviceContext.addAuxiliaryConenctionContext(mockedConnectionContext);
272 return mockedConnectionContext;
275 private ConnectionContext prepareConnectionContext() {
276 ConnectionContext mockedConnectionContext = mock(ConnectionContext.class);
277 FeaturesReply mockedFeaturesReply = mock(FeaturesReply.class);
278 when(mockedFeaturesReply.getAuxiliaryId()).thenReturn(DUMMY_AUXILIARY_ID);
279 when(mockedConnectionContext.getFeatures()).thenReturn(mockedFeaturesReply);
280 return mockedConnectionContext;
284 * FIXME: Need to change the test on behalf the clustering transaction chain manager changes
289 public void testAddDeleteToTxChain() throws Exception{
290 InstanceIdentifier<Nodes> dummyII = InstanceIdentifier.create(Nodes.class);
291 deviceContext.addDeleteToTxChain(LogicalDatastoreType.CONFIGURATION, dummyII);
292 verify(txChainManager).addDeleteOperationTotTxChain(eq(LogicalDatastoreType.CONFIGURATION), eq(dummyII));
296 * FIXME: Need to change the test on behalf the clustering transaction chain manager changes
301 public void testSubmitTransaction() throws Exception {
302 deviceContext.submitTransaction();
303 verify(txChainManager).submitWriteTransaction();
307 public void testGetPrimaryConnectionContext() {
308 final ConnectionContext primaryConnectionContext = deviceContext.getPrimaryConnectionContext();
309 assertEquals(connectionContext, primaryConnectionContext);
313 public void testGetDeviceFlowRegistry() {
314 final DeviceFlowRegistry deviceFlowRegistry = deviceContext.getDeviceFlowRegistry();
315 assertNotNull(deviceFlowRegistry);
319 public void testGetDeviceGroupRegistry() {
320 final DeviceGroupRegistry deviceGroupRegistry = deviceContext.getDeviceGroupRegistry();
321 assertNotNull(deviceGroupRegistry);
325 public void testGetDeviceMeterRegistry() {
326 final DeviceMeterRegistry deviceMeterRegistry = deviceContext.getDeviceMeterRegistry();
327 assertNotNull(deviceMeterRegistry);
331 public void testProcessReply() {
332 Error mockedError = mock(Error.class);
333 deviceContext.processReply(mockedError);
334 verify(messageIntelligenceAgency).spyMessage(any(Class.class), eq(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE));
335 OfHeader mockedOfHeader = mock(OfHeader.class);
336 deviceContext.processReply(mockedOfHeader);
337 verify(messageIntelligenceAgency).spyMessage(any(Class.class), eq(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS));
341 public void testProcessReply2() {
342 MultipartReply mockedMultipartReply = mock(MultipartReply.class);
343 Xid dummyXid = new Xid(DUMMY_XID);
344 deviceContext.processReply(dummyXid, Lists.newArrayList(mockedMultipartReply));
345 verify(messageIntelligenceAgency).spyMessage(any(Class.class), eq(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_FAILURE));
349 public void testProcessPacketInMessageFutureSuccess() {
350 PacketInMessage mockedPacketInMessage = mock(PacketInMessage.class);
351 NotificationPublishService mockedNotificationPublishService = mock(NotificationPublishService.class);
352 final ListenableFuture stringListenableFuture = Futures.immediateFuture(new String("dummy value"));
354 when(mockedNotificationPublishService.offerNotification(any(PacketReceived.class))).thenReturn(stringListenableFuture);
355 deviceContext.setNotificationPublishService(mockedNotificationPublishService);
356 deviceContext.processPacketInMessage(mockedPacketInMessage);
357 verify(messageIntelligenceAgency).spyMessage(any(Class.class), eq(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_PUBLISHED_SUCCESS));
361 public void testProcessPacketInMessageFutureFailure() {
362 PacketInMessage mockedPacketInMessage = mock(PacketInMessage.class);
363 NotificationPublishService mockedNotificationPublishService = mock(NotificationPublishService.class);
364 final ListenableFuture dummyFuture = Futures.immediateFailedFuture(new IllegalStateException());
366 when(mockedNotificationPublishService.offerNotification(any(PacketReceived.class))).thenReturn(dummyFuture);
367 deviceContext.setNotificationPublishService(mockedNotificationPublishService);
368 deviceContext.processPacketInMessage(mockedPacketInMessage);
369 verify(messageIntelligenceAgency).spyMessage(any(Class.class), eq(MessageSpy.STATISTIC_GROUP.FROM_SWITCH_NOTIFICATION_REJECTED));
373 public void testTranslatorLibrary() {
374 final TranslatorLibrary pickedTranslatorLibrary = deviceContext.oook();
375 assertEquals(translatorLibrary, pickedTranslatorLibrary);
379 public void testGetTimer() {
380 final HashedWheelTimer pickedTimer = deviceContext.getTimer();
381 assertEquals(timer, pickedTimer);
385 public void testClose() {
386 ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
387 InetSocketAddress mockRemoteAddress = InetSocketAddress.createUnresolved("odl-unit.example.org",999);
388 when(mockedConnectionAdapter.getRemoteAddress()).thenReturn(mockRemoteAddress);
389 when(connectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
391 NodeId dummyNodeId = new NodeId("dummyNodeId");
392 when(deviceState.getNodeId()).thenReturn(dummyNodeId);
394 ConnectionContext mockedAuxiliaryConnectionContext = prepareConnectionContext();
395 deviceContext.addAuxiliaryConenctionContext(mockedAuxiliaryConnectionContext);
396 DeviceContextClosedHandler mockedDeviceContextClosedHandler = mock(DeviceContextClosedHandler.class);
397 deviceContext.addDeviceContextClosedHandler(mockedDeviceContextClosedHandler);
398 deviceContext.close();
399 verify(connectionContext).closeConnection(eq(false));
400 // verify(deviceState).setValid(eq(false));
401 // verify(mockedAuxiliaryConnectionContext).closeConnection(eq(false));
405 public void testBarrierFieldSetGet() {
406 Timeout mockedTimeout = mock(Timeout.class);
407 deviceContext.setCurrentBarrierTimeout(mockedTimeout);
408 final Timeout pickedBarrierTimeout = deviceContext.getBarrierTaskTimeout();
409 assertEquals(mockedTimeout, pickedBarrierTimeout);
413 public void testGetMessageSpy() {
414 final MessageSpy pickedMessageSpy = deviceContext.getMessageSpy();
415 assertEquals(messageIntelligenceAgency, pickedMessageSpy);
419 public void testNodeConnector() {
420 NodeConnectorRef mockedNodeConnectorRef = mock(NodeConnectorRef.class);
421 deviceContext.storeNodeConnectorRef(DUMMY_PORT_NUMBER, mockedNodeConnectorRef);
422 final NodeConnectorRef nodeConnectorRef = deviceContext.lookupNodeConnectorRef(DUMMY_PORT_NUMBER);
423 assertEquals(mockedNodeConnectorRef, nodeConnectorRef);
428 public void testOnPublished() {
429 final ConnectionContext auxiliaryConnectionContext = addDummyAuxiliaryConnectionContext();
431 ConnectionAdapter mockedAuxConnectionAdapter = mock(ConnectionAdapter.class);
432 when(auxiliaryConnectionContext.getConnectionAdapter()).thenReturn(mockedAuxConnectionAdapter);
434 ConnectionAdapter mockedConnectionAdapter = mock(ConnectionAdapter.class);
435 when(connectionContext.getConnectionAdapter()).thenReturn(mockedConnectionAdapter);
437 deviceContext.onPublished();
438 verify(mockedAuxConnectionAdapter).setPacketInFiltering(eq(false));
439 verify(mockedConnectionAdapter).setPacketInFiltering(eq(false));
443 public void testPortStatusMessage() {
444 PortStatusMessage mockedPortStatusMessage = mock(PortStatusMessage.class);
445 Class dummyClass = Class.class;
446 when(mockedPortStatusMessage.getImplementedInterface()).thenReturn(dummyClass);
449 GetFeaturesOutput mockedFeature = mock(GetFeaturesOutput.class);
450 when(mockedFeature.getDatapathId()).thenReturn(DUMMY_DATAPATH_ID);
451 when(deviceState.getFeatures()).thenReturn(mockedFeature);
453 when(mockedPortStatusMessage.getVersion()).thenReturn(OFConstants.OFP_VERSION_1_3);
454 when(mockedPortStatusMessage.getReason()).thenReturn(PortReason.OFPPRADD);
456 OpenflowPortsUtil.init();
457 deviceContext.processPortStatusMessage(mockedPortStatusMessage);
458 // verify(txChainManager).writeToTransaction(eq(LogicalDatastoreType.OPERATIONAL), any(InstanceIdentifier.class), any(DataObject.class));
462 public void testProcessFlowRemovedMessage() throws Exception {
463 // prepare translation result
464 final FlowRemovedBuilder flowRemovedMdsalBld = new FlowRemovedBuilder()
465 .setTableId((short) 0)
467 .setCookie(new FlowCookie(BigInteger.ONE))
468 .setMatch(new MatchBuilder().build());
470 Mockito.when(messageTranslatorFlowRemoved.translate(any(Object.class), any(DeviceContext.class), any(Object.class)))
471 .thenReturn(flowRemovedMdsalBld.build());
473 // insert flow+flowId into local registry
474 FlowRegistryKey flowRegKey = FlowRegistryKeyFactory.create(flowRemovedMdsalBld.build());
475 FlowDescriptor flowDescriptor = FlowDescriptorFactory.create((short) 0, new FlowId("ut-ofp:f456"));
476 deviceContext.getDeviceFlowRegistry().store(flowRegKey, flowDescriptor);
478 // plug in lifecycleListener
479 final ItemLifecycleListener itemLifecycleListener = Mockito.mock(ItemLifecycleListener.class);
480 for (ItemLifeCycleSource lifeCycleSource : deviceContext.getItemLifeCycleSourceRegistry().getLifeCycleSources()) {
481 lifeCycleSource.setItemLifecycleListener(itemLifecycleListener);
484 // prepare empty input message
485 final FlowRemovedMessageBuilder flowRemovedBld = new FlowRemovedMessageBuilder();
487 // prepare path to flow to be removed
488 KeyedInstanceIdentifier<Flow, FlowKey> flowToBeRemovedPath = nodeKeyIdent
489 .augmentation(FlowCapableNode.class)
490 .child(Table.class, new TableKey((short) 0))
491 .child(Flow.class, new FlowKey(new FlowId("ut-ofp:f456")));
493 deviceContext.processFlowRemovedMessage(flowRemovedBld.build());
494 Mockito.verify(itemLifecycleListener).onRemoved(flowToBeRemovedPath);
498 public void testOnDeviceDisconnected() throws Exception {
499 DeviceContextClosedHandler deviceContextClosedHandler = mock(DeviceContextClosedHandler.class);
500 deviceContext.addDeviceContextClosedHandler(deviceContextClosedHandler);
502 deviceContext.onDeviceDisconnected(connectionContext);
504 // Mockito.verify(deviceState).setValid(false);
505 // Mockito.verify(deviceContextClosedHandler).onDeviceContextClosed(deviceContext);
506 Assert.assertEquals(0, deviceContext.getDeviceFlowRegistry().getAllFlowDescriptors().size());
507 Assert.assertEquals(0, deviceContext.getDeviceGroupRegistry().getAllGroupIds().size());
508 Assert.assertEquals(0, deviceContext.getDeviceMeterRegistry().getAllMeterIds().size());