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.Matchers.any;
14 import static org.mockito.Mockito.doReturn;
16 import java.util.Collections;
17 import org.junit.After;
18 import org.junit.Assert;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.junit.runner.RunWith;
22 import org.mockito.ArgumentCaptor;
23 import org.mockito.Captor;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.runners.MockitoJUnitRunner;
27 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
28 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
29 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
30 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
31 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
32 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
46 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
47 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
50 * Test for {@link LLDPPacketPuntEnforcer}.
52 @RunWith(MockitoJUnitRunner.class)
53 public class LLDPDataTreeChangeListenerTest {
54 private LLDPPacketPuntEnforcer lldpPacketPuntEnforcer;
55 private static final InstanceIdentifier<Node> NODE_IID = InstanceIdentifier.create(Nodes.class)
56 .child(Node.class, new NodeKey(new NodeId("testnode:1")));
58 private SalFlowService flowService;
60 private DataTreeModification<FlowCapableNode> dataTreeModification;
62 private ArgumentCaptor<AddFlowInput> addFlowInputCaptor;
66 doReturn(RpcResultBuilder.success().buildFuture()).when(flowService).addFlow(any());
67 lldpPacketPuntEnforcer = new LLDPPacketPuntEnforcer(flowService, Mockito.mock(DataBroker.class));
68 final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
70 Mockito.when(dataTreeModification.getRootPath()).thenReturn(identifier);
71 Mockito.when(dataTreeModification.getRootNode()).thenReturn(Mockito.mock(DataObjectModification.class));
72 Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.WRITE);
76 public void testOnDataTreeChanged() {
77 lldpPacketPuntEnforcer.onDataTreeChanged(Collections.singleton(dataTreeModification));
78 Mockito.verify(flowService).addFlow(addFlowInputCaptor.capture());
79 AddFlowInput captured = addFlowInputCaptor.getValue();
80 Assert.assertEquals(NODE_IID, captured.getNode().getValue());
84 public void testCreateFlow() {
85 final Flow flow = lldpPacketPuntEnforcer.createFlow();
86 evaluateInstructions(flow.getInstructions());
90 public void tearDown() {
91 lldpPacketPuntEnforcer.close();
94 private static void evaluateInstructions(final Instructions instructions) {
95 assertNotNull(instructions.getInstruction());
96 assertEquals(1, instructions.getInstruction().size());
97 Instruction instruction = instructions.getInstruction().get(0);
98 evaluateInstruction(instruction);
101 private static void evaluateInstruction(final Instruction instruction) {
102 if (instruction.getInstruction() instanceof ApplyActionsCase) {
103 ApplyActionsCase applyActionsCase = (ApplyActionsCase) instruction.getInstruction();
104 assertNotNull(applyActionsCase.getApplyActions().getAction());
105 assertEquals(1, applyActionsCase.getApplyActions().getAction().size());
106 Action action = applyActionsCase.getApplyActions().getAction().get(0);
107 evaluateAction(action);
111 private static void evaluateAction(final Action action) {
112 if (action.getAction() instanceof OutputActionCase) {
113 OutputActionCase outputActionCase = (OutputActionCase) action.getAction();
114 assertEquals("CONTROLLER", outputActionCase.getOutputAction().getOutputNodeConnector().getValue());
115 assertEquals(new Integer(0xffff).intValue(), outputActionCase.getOutputAction().getMaxLength().intValue());