0983c85c5e8f08c92d75ef3b34131a47d0524747
[openflowplugin.git] / applications / table-miss-enforcer / src / test / java / org / opendaylight / openflowplugin / applications / tableMissEnforcer / LLDPDataTreeChangeListenerTest.java
1 /**
2  * Copyright (c) 2014 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 final static InstanceIdentifier<Node> nodeIID = 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, nodeIID);
65         Mockito.when(dataTreeModification.getRootPath()).thenReturn(identifier);
66         Mockito.when(dataTreeModification.getRootNode()).thenReturn(Mockito.mock(DataObjectModification.class));
67         Mockito.when(dataTreeModification.getRootNode().getModificationType()).thenReturn(ModificationType.WRITE);
68     }
69
70     @Test
71     public void testOnDataTreeChanged() {
72         lldpPacketPuntEnforcer.onDataTreeChanged(Collections.singleton(dataTreeModification));
73         Mockito.verify(flowService).addFlow(addFlowInputCaptor.capture());
74         AddFlowInput captured = addFlowInputCaptor.getValue();
75         Assert.assertEquals(nodeIID, captured.getNode().getValue());
76     }
77
78     @Test
79     public void testCreateFlow() {
80         final Flow flow = lldpPacketPuntEnforcer.createFlow();
81         evaluateInstructions(flow.getInstructions());
82     }
83
84     @After
85     public void tearDown() {
86         lldpPacketPuntEnforcer.close();
87     }
88
89     private static void evaluateInstructions(final Instructions instructions) {
90         assertNotNull(instructions.getInstruction());
91         assertEquals(1, instructions.getInstruction().size());
92         Instruction instruction = instructions.getInstruction().get(0);
93         evaluateInstruction(instruction);
94     }
95
96     private static void evaluateInstruction(final Instruction instruction) {
97         if (instruction.getInstruction() instanceof ApplyActionsCase) {
98             ApplyActionsCase applyActionsCase = (ApplyActionsCase) instruction.getInstruction();
99             assertNotNull(applyActionsCase.getApplyActions().getAction());
100             assertEquals(1, applyActionsCase.getApplyActions().getAction().size());
101             Action action = applyActionsCase.getApplyActions().getAction().get(0);
102             evaluateAction(action);
103         }
104     }
105
106     private static void evaluateAction(final Action action) {
107         if (action.getAction() instanceof OutputActionCase) {
108             OutputActionCase outputActionCase = (OutputActionCase) action.getAction();
109             assertEquals("CONTROLLER", outputActionCase.getOutputAction().getOutputNodeConnector().getValue());
110             assertEquals(new Integer(0xffff).intValue(), outputActionCase.getOutputAction().getMaxLength().intValue());
111         }
112     }
113 }