OPNFLWPLUG-1032: Neon-MRI: Bump odlparent, yangtools, mdsal
[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.ArgumentMatchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.when;
16
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.runners.MockitoJUnitRunner;
28 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
29 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification;
30 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
31 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
32 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
33 import org.opendaylight.controller.md.sal.common.api.data.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;
50
51 /**
52  * Test for {@link LLDPPacketPuntEnforcer}.
53  */
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")));
59     @Mock
60     private SalFlowService flowService;
61     @Mock
62     private DataTreeModification<FlowCapableNode> dataTreeModification;
63     @Mock
64     private DeviceOwnershipService deviceOwnershipService;
65     @Captor
66     private ArgumentCaptor<AddFlowInput> addFlowInputCaptor;
67
68     @Before
69     public void setUp() {
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 = new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL,
74                                                                                       NODE_IID);
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);
79     }
80
81     @Test
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());
87     }
88
89     @Test
90     public void testCreateFlow() {
91         final Flow flow = lldpPacketPuntEnforcer.createFlow();
92         evaluateInstructions(flow.getInstructions());
93     }
94
95     @After
96     public void tearDown() {
97         lldpPacketPuntEnforcer.close();
98     }
99
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);
105     }
106
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);
114         }
115     }
116
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());
122         }
123     }
124 }