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.applications.tablemissenforcer;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.when;
17 import java.util.Collections;
18 import org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.ArgumentCaptor;
24 import org.mockito.Captor;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.junit.MockitoJUnitRunner;
28 import org.opendaylight.mdsal.binding.api.DataBroker;
29 import org.opendaylight.mdsal.binding.api.DataObjectModification;
30 import org.opendaylight.mdsal.binding.api.DataObjectModification.ModificationType;
31 import org.opendaylight.mdsal.binding.api.DataTreeIdentifier;
32 import org.opendaylight.mdsal.binding.api.DataTreeModification;
33 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
34 import org.opendaylight.openflowplugin.applications.deviceownershipservice.DeviceOwnershipService;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
46 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
47 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
48 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
49 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
52 * Test for {@link LLDPPacketPuntEnforcer}.
54 @RunWith(MockitoJUnitRunner.class)
55 public class LLDPDataTreeChangeListenerTest {
56 private LLDPPacketPuntEnforcer lldpPacketPuntEnforcer;
57 private static final InstanceIdentifier<Node> NODE_IID = InstanceIdentifier.create(Nodes.class)
58 .child(Node.class, new NodeKey(new NodeId("testnode:1")));
60 private SalFlowService flowService;
62 private DataTreeModification<FlowCapableNode> dataTreeModification;
64 private DeviceOwnershipService deviceOwnershipService;
66 private ArgumentCaptor<AddFlowInput> addFlowInputCaptor;
70 doReturn(RpcResultBuilder.success().buildFuture()).when(flowService).addFlow(any());
71 lldpPacketPuntEnforcer = new LLDPPacketPuntEnforcer(flowService, Mockito.mock(DataBroker.class),
72 deviceOwnershipService);
73 final DataTreeIdentifier<FlowCapableNode> identifier = DataTreeIdentifier.create(
74 LogicalDatastoreType.OPERATIONAL, NODE_IID.augmentation(FlowCapableNode.class));
75 Mockito.when(dataTreeModification.getRootPath()).thenReturn(identifier);
76 Mockito.when(dataTreeModification.getRootNode()).thenReturn(Mockito.mock(DataObjectModification.class));
77 Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.WRITE);
78 when(deviceOwnershipService.isEntityOwned(any())).thenReturn(true);
82 public void testOnDataTreeChanged() {
83 lldpPacketPuntEnforcer.onDataTreeChanged(Collections.singleton(dataTreeModification));
84 Mockito.verify(flowService).addFlow(addFlowInputCaptor.capture());
85 AddFlowInput captured = addFlowInputCaptor.getValue();
86 Assert.assertEquals(NODE_IID, captured.getNode().getValue());
90 public void testCreateFlow() {
91 final Flow flow = lldpPacketPuntEnforcer.createFlow();
92 evaluateInstructions(flow.getInstructions());
96 public void tearDown() {
97 lldpPacketPuntEnforcer.close();
100 private static void evaluateInstructions(final Instructions instructions) {
101 assertNotNull(instructions.getInstruction());
102 assertEquals(1, instructions.getInstruction().size());
103 Instruction instruction = instructions.getInstruction().get(0);
104 evaluateInstruction(instruction);
107 private static void evaluateInstruction(final Instruction instruction) {
108 if (instruction.getInstruction() instanceof ApplyActionsCase) {
109 ApplyActionsCase applyActionsCase = (ApplyActionsCase) instruction.getInstruction();
110 assertNotNull(applyActionsCase.getApplyActions().getAction());
111 assertEquals(1, applyActionsCase.getApplyActions().getAction().size());
112 Action action = applyActionsCase.getApplyActions().getAction().get(0);
113 evaluateAction(action);
117 private static void evaluateAction(final Action action) {
118 if (action.getAction() instanceof OutputActionCase) {
119 OutputActionCase outputActionCase = (OutputActionCase) action.getAction();
120 assertEquals("CONTROLLER", outputActionCase.getOutputAction().getOutputNodeConnector().getValue());
121 assertEquals(new Integer(0xffff).intValue(), outputActionCase.getOutputAction().getMaxLength().intValue());