Merge "Fix checkstyle warnings for impl/connection package and OpenFlowPluginProvider...
[openflowplugin.git] / applications / table-miss-enforcer / src / test / java / org / opendaylight / openflowplugin / applications / tablemissenforcer / LLDPDataTreeChangeListenerTest.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
9 package org.opendaylight.openflowplugin.applications.tablemissenforcer;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13
14 import java.util.Collections;
15 import org.junit.After;
16 import org.junit.Assert;
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.ArgumentCaptor;
21 import org.mockito.Captor;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.runners.MockitoJUnitRunner;
25 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
26 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
27 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
28 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
29 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
30 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.action.OutputActionCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowCapableNode;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.Flow;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.AddFlowInput;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.Instructions;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeKey;
44 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
45
46 /**
47  * Test for {@link LLDPPacketPuntEnforcer}.
48  */
49 @RunWith(MockitoJUnitRunner.class)
50 public class LLDPDataTreeChangeListenerTest {
51     private LLDPPacketPuntEnforcer lldpPacketPuntEnforcer;
52     private static final InstanceIdentifier<Node> NODE_IID = InstanceIdentifier.create(Nodes.class)
53             .child(Node.class, new NodeKey(new NodeId("testnode:1")));
54     @Mock
55     private SalFlowService flowService;
56     @Mock
57     private DataTreeModification<FlowCapableNode> dataTreeModification;
58     @Captor
59     private ArgumentCaptor<AddFlowInput> addFlowInputCaptor;
60
61     @Before
62     public void setUp() {
63         lldpPacketPuntEnforcer = new LLDPPacketPuntEnforcer(flowService, Mockito.mock(DataBroker.class));
64         final DataTreeIdentifier<FlowCapableNode> identifier = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
65                                                                                       NODE_IID);
66         Mockito.when(dataTreeModification.getRootPath()).thenReturn(identifier);
67         Mockito.when(dataTreeModification.getRootNode()).thenReturn(Mockito.mock(DataObjectModification.class));
68         Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.WRITE);
69     }
70
71     @Test
72     public void testOnDataTreeChanged() {
73         lldpPacketPuntEnforcer.onDataTreeChanged(Collections.singleton(dataTreeModification));
74         Mockito.verify(flowService).addFlow(addFlowInputCaptor.capture());
75         AddFlowInput captured = addFlowInputCaptor.getValue();
76         Assert.assertEquals(NODE_IID, captured.getNode().getValue());
77     }
78
79     @Test
80     public void testCreateFlow() {
81         final Flow flow = lldpPacketPuntEnforcer.createFlow();
82         evaluateInstructions(flow.getInstructions());
83     }
84
85     @After
86     public void tearDown() {
87         lldpPacketPuntEnforcer.close();
88     }
89
90     private static void evaluateInstructions(final Instructions instructions) {
91         assertNotNull(instructions.getInstruction());
92         assertEquals(1, instructions.getInstruction().size());
93         Instruction instruction = instructions.getInstruction().get(0);
94         evaluateInstruction(instruction);
95     }
96
97     private static void evaluateInstruction(final Instruction instruction) {
98         if (instruction.getInstruction() instanceof ApplyActionsCase) {
99             ApplyActionsCase applyActionsCase = (ApplyActionsCase) instruction.getInstruction();
100             assertNotNull(applyActionsCase.getApplyActions().getAction());
101             assertEquals(1, applyActionsCase.getApplyActions().getAction().size());
102             Action action = applyActionsCase.getApplyActions().getAction().get(0);
103             evaluateAction(action);
104         }
105     }
106
107     private static void evaluateAction(final Action action) {
108         if (action.getAction() instanceof OutputActionCase) {
109             OutputActionCase outputActionCase = (OutputActionCase) action.getAction();
110             assertEquals("CONTROLLER", outputActionCase.getOutputAction().getOutputNodeConnector().getValue());
111             assertEquals(new Integer(0xffff).intValue(), outputActionCase.getOutputAction().getMaxLength().intValue());
112         }
113     }
114 }