58f64130073767a2c5e6a29ff50bdbbf7d678d19
[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 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doReturn;
15
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;
48
49 /**
50  * Test for {@link LLDPPacketPuntEnforcer}.
51  */
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")));
57     @Mock
58     private SalFlowService flowService;
59     @Mock
60     private DataTreeModification<FlowCapableNode> dataTreeModification;
61     @Captor
62     private ArgumentCaptor<AddFlowInput> addFlowInputCaptor;
63
64     @Before
65     public void setUp() {
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,
69                                                                                       NODE_IID);
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);
73     }
74
75     @Test
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());
81     }
82
83     @Test
84     public void testCreateFlow() {
85         final Flow flow = lldpPacketPuntEnforcer.createFlow();
86         evaluateInstructions(flow.getInstructions());
87     }
88
89     @After
90     public void tearDown() {
91         lldpPacketPuntEnforcer.close();
92     }
93
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);
99     }
100
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);
108         }
109     }
110
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());
116         }
117     }
118 }