/* * 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.openflowplugin.applications.tablemissenforcer; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; import java.util.Collections; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.ArgumentCaptor; import org.mockito.Captor; import org.mockito.Mock; import org.mockito.Mockito; import org.mockito.junit.MockitoJUnitRunner; import org.opendaylight.mdsal.binding.api.DataBroker; import org.opendaylight.mdsal.binding.api.DataObjectModification; import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType; import org.opendaylight.mdsal.binding.api.DataTreeIdentifier; import org.opendaylight.mdsal.binding.api.DataTreeModification; import org.opendaylight.mdsal.common.api.LogicalDatastoreType; import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService; import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase; import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node; import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey; import org.opendaylight.yangtools.yang.binding.InstanceIdentifier; import org.opendaylight.yangtools.yang.common.RpcResultBuilder; /** * Test for {@link LLDPPacketPuntEnforcer}. */ @RunWith(MockitoJUnitRunner.class) public class LLDPDataTreeChangeListenerTest { private LLDPPacketPuntEnforcer lldpPacketPuntEnforcer; private static final InstanceIdentifier NODE_IID = InstanceIdentifier.create(Nodes.class) .child(Node.class, new NodeKey(new NodeId("testnode:1"))); @Mock private SalFlowService flowService; @Mock private DataTreeModification dataTreeModification; @Mock private DeviceOwnershipService deviceOwnershipService; @Captor private ArgumentCaptor addFlowInputCaptor; @Before public void setUp() { doReturn(RpcResultBuilder.success().buildFuture()).when(flowService).addFlow(any()); lldpPacketPuntEnforcer = new LLDPPacketPuntEnforcer(flowService, Mockito.mock(DataBroker.class), deviceOwnershipService); final DataTreeIdentifier identifier = DataTreeIdentifier.create( LogicalDatastoreType.OPERATIONAL, NODE_IID.augmentation(FlowCapableNode.class)); Mockito.when(dataTreeModification.getRootPath()).thenReturn(identifier); Mockito.when(dataTreeModification.getRootNode()).thenReturn(Mockito.mock(DataObjectModification.class)); Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.WRITE); when(deviceOwnershipService.isEntityOwned(any())).thenReturn(true); } @Test public void testOnDataTreeChanged() { lldpPacketPuntEnforcer.onDataTreeChanged(Collections.singleton(dataTreeModification)); Mockito.verify(flowService).addFlow(addFlowInputCaptor.capture()); AddFlowInput captured = addFlowInputCaptor.getValue(); Assert.assertEquals(NODE_IID, captured.getNode().getValue()); } @Test public void testCreateFlow() { final Flow flow = lldpPacketPuntEnforcer.createFlow(); evaluateInstructions(flow.getInstructions()); } @After public void tearDown() { lldpPacketPuntEnforcer.close(); } private static void evaluateInstructions(final Instructions instructions) { assertNotNull(instructions.getInstruction()); assertEquals(1, instructions.getInstruction().size()); Instruction instruction = instructions.getInstruction().get(0); evaluateInstruction(instruction); } private static void evaluateInstruction(final Instruction instruction) { if (instruction.getInstruction() instanceof ApplyActionsCase) { ApplyActionsCase applyActionsCase = (ApplyActionsCase) instruction.getInstruction(); assertNotNull(applyActionsCase.getApplyActions().getAction()); assertEquals(1, applyActionsCase.getApplyActions().getAction().size()); Action action = applyActionsCase.getApplyActions().getAction().get(0); evaluateAction(action); } } private static void evaluateAction(final Action action) { if (action.getAction() instanceof OutputActionCase) { OutputActionCase outputActionCase = (OutputActionCase) action.getAction(); assertEquals("CONTROLLER", outputActionCase.getOutputAction().getOutputNodeConnector().getValue()); assertEquals(new Integer(0xffff).intValue(), outputActionCase.getOutputAction().getMaxLength().intValue()); } } }