Imported vpnservice as a subtree
[netvirt.git] / openstack / net-virt-providers / src / test / java / org / opendaylight / netvirt / openstack / netvirt / providers / openflow13 / services / ClassifierServiceTest.java
1 /*
2  * Copyright (c) 2015 Inocybe 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.netvirt.openstack.netvirt.providers.openflow13.services;
10
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Matchers.anyBoolean;
13 import static org.mockito.Mockito.times;
14 import static org.mockito.Mockito.verify;
15 import static org.mockito.Mockito.when;
16
17 import org.junit.Before;
18 import org.junit.Test;
19 import org.junit.runner.RunWith;
20 import org.mockito.InjectMocks;
21 import org.mockito.Mock;
22 import org.mockito.runners.MockitoJUnitRunner;
23 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
26 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
27 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.PipelineOrchestrator;
28 import org.opendaylight.netvirt.openstack.netvirt.providers.openflow13.Service;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
30 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
31
32 import com.google.common.util.concurrent.CheckedFuture;
33
34 /**
35  * Unit test for {@link ClassifierService}
36  */
37 @RunWith(MockitoJUnitRunner.class)
38 @SuppressWarnings("unchecked")
39 public class ClassifierServiceTest {
40
41     @InjectMocks private ClassifierService classifierService = new ClassifierService(Service.ARP_RESPONDER);
42
43     @Mock private DataBroker dataBroker;
44
45     private static final String MAC_ADDRESS = "87:1D:5E:02:40:B7";
46
47     @Mock private PipelineOrchestrator orchestrator;
48
49     @Mock private WriteTransaction writeTransaction;
50     @Mock private CheckedFuture<Void, TransactionCommitFailedException> commitFuture;
51
52     @Before
53     public void setUp() {
54         when(writeTransaction.submit()).thenReturn(commitFuture);
55         when(writeTransaction.submit()).thenReturn(commitFuture);
56
57         when(dataBroker.newWriteOnlyTransaction()).thenReturn(writeTransaction);
58
59         when(orchestrator.getNextServiceInPipeline(any(Service.class))).thenReturn(Service.ARP_RESPONDER);
60     }
61
62     /**
63      * Test method {@link ClassifierService#programLocalInPort(Long, String, Long, String, boolean)}
64      */
65     @Test
66     public void testProgramLocalInPort() throws Exception {
67         // write
68         classifierService.programLocalInPort(Long.valueOf(1212), "2", Long.valueOf(455), MAC_ADDRESS, true);
69         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
70         verify(writeTransaction, times(1)).submit();
71         verify(commitFuture, times(1)).get();
72
73         // remove
74         classifierService.programLocalInPort(Long.valueOf(1212), "2", Long.valueOf(455), MAC_ADDRESS, false);
75         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
76         verify(commitFuture, times(2)).get(); // 1 + 1 above
77     }
78
79     /**
80      * Test method {@link ClassifierService#programLocalInPortSetVlan(Long, String, Long, String, boolean)}
81      */
82     @Test
83     public void testProgramLocalInPortSetVlan() throws Exception {
84         // write
85         classifierService.programLocalInPortSetVlan(Long.valueOf(1212), "2", Long.valueOf(455), MAC_ADDRESS, true);
86         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
87         verify(writeTransaction, times(1)).submit();
88         verify(commitFuture, times(1)).get();
89
90         // remove
91         classifierService.programLocalInPortSetVlan(Long.valueOf(1212), "2", Long.valueOf(455), MAC_ADDRESS, false);
92         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
93         verify(commitFuture, times(2)).get(); // 1 + 1 above
94     }
95
96     /**
97      * Test method {@link ClassifierService#programDropSrcIface(Long, Long, boolean)}
98      */
99     @Test
100     public void testProgramDropSrcIface() throws Exception {
101         // write
102         classifierService.programDropSrcIface(Long.valueOf(1212), Long.valueOf(455), true);
103         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
104         verify(writeTransaction, times(1)).submit();
105         verify(commitFuture, times(1)).get();
106
107         // remove
108         classifierService.programDropSrcIface(Long.valueOf(1212), Long.valueOf(455), false);
109         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
110         verify(commitFuture, times(2)).get(); // 1 + 1 above
111     }
112
113     /**
114      * Test method {@link ClassifierService#programTunnelIn(Long, String, Long, boolean)}
115      */
116     @Test
117     public void testProgramTunnelIn() throws Exception {
118         // write
119         classifierService.programTunnelIn(Long.valueOf(1212), "2", Long.valueOf(455), true);
120         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
121         verify(writeTransaction, times(1)).submit();
122         verify(commitFuture, times(1)).get();
123
124         // remove
125         classifierService.programTunnelIn(Long.valueOf(1212), "2", Long.valueOf(455), false);
126         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
127         verify(commitFuture, times(2)).get(); // 1 + 1 above
128     }
129
130     /**
131      * Test method {@link ClassifierService#programVlanIn(Long, String, Long, boolean)}
132      */
133     @Test
134     public void testProgramVlanIn() throws Exception {
135         // write
136         classifierService.programVlanIn(Long.valueOf(1212), "2", Long.valueOf(455), true);
137         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
138         verify(writeTransaction, times(1)).submit();
139         verify(commitFuture, times(1)).get();
140
141         // remove
142         classifierService.programVlanIn(Long.valueOf(1212), "2", Long.valueOf(455), false);
143         verify(writeTransaction, times(1)).delete(any(LogicalDatastoreType.class), any(InstanceIdentifier.class));
144         verify(commitFuture, times(2)).get(); // 1 + 1 above
145     }
146
147     /**
148      * Test method {@link ClassifierService#programLLDPPuntRule(Long)}
149      */
150     @Test
151     public void testProgramLLDPPuntRule() throws Exception {
152         // write
153         classifierService.programLLDPPuntRule(Long.valueOf(1212));
154         verify(writeTransaction, times(2)).put(any(LogicalDatastoreType.class), any(InstanceIdentifier.class), any(Node.class), anyBoolean());
155         verify(writeTransaction, times(1)).submit();
156         verify(commitFuture, times(1)).get();
157     }
158 }