Stale flows in ACL tables 216/246
[netvirt.git] / sfc / classifier / impl / src / test / java / org / opendaylight / netvirt / sfc / classifier / providers / GeniusProviderTest.java
1 /*
2  * Copyright © 2017 Ericsson, 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.netvirt.sfc.classifier.providers;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertTrue;
14
15 import java.util.Optional;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.opendaylight.controller.md.sal.binding.test.ConstantSchemaAbstractDataBrokerTest;
19 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
20 import org.opendaylight.genius.mdsalutil.MDSALUtil;
21 import org.opendaylight.genius.mdsalutil.NwConstants;
22 import org.opendaylight.yang.gen.v1.urn.ericsson.params.xml.ns.yang.sfc.sff.logical.rev160620.DpnIdType;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.interfacemanager.servicebinding.rev160406.service.bindings.services.info.BoundServices;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26
27 public class GeniusProviderTest extends ConstantSchemaAbstractDataBrokerTest {
28     private GeniusProvider geniusProvider;
29
30     @Before
31     public void setUp() throws Exception {
32         geniusProvider = new GeniusProvider(getDataBroker(), TestOdlInterfaceRpcService.newInstance(),
33                 TestInterfaceManager.newInstance());
34     }
35
36     @Test
37     public void bindPortOnIngressClassifier() {
38         // Bind the Ingress service
39         geniusProvider.bindPortOnIngressClassifier(GeniusProviderTestParams.INTERFACE_NAME);
40
41         // Now make sure its in the data store
42         InstanceIdentifier<BoundServices> id = geniusProvider.getBindServiceId(NwConstants.SFC_CLASSIFIER_INDEX,
43                 GeniusProviderTestParams.INTERFACE_NAME, true);
44         Optional<BoundServices> boundServices = getBoundServices(id);
45         assertTrue(boundServices.isPresent());
46
47         // UnBind the Ingress Service
48         geniusProvider.unbindPortOnIngressClassifier(GeniusProviderTestParams.INTERFACE_NAME);
49
50         // Now make sure its NOT in the data store
51         assertFalse(getBoundServices(id).isPresent());
52     }
53
54     @Test
55     public void bindPortOnEgressClassifier() {
56         // Bind the Egress service
57         geniusProvider.bindPortOnEgressClassifier(
58                 GeniusProviderTestParams.INTERFACE_NAME,
59                 GeniusProviderTestParams.IPV4_ADDRESS_STR);
60
61         // Now make sure its in the data store
62         InstanceIdentifier<BoundServices> id = geniusProvider.getBindServiceId(
63                 NwConstants.EGRESS_SFC_CLASSIFIER_SERVICE_INDEX,
64                 GeniusProviderTestParams.INTERFACE_NAME,
65                 false);
66         Optional<BoundServices> boundServices = getBoundServices(id);
67         assertTrue(boundServices.isPresent());
68
69         // UnBind the Egress Service
70         geniusProvider.unbindPortOnEgressClassifier(GeniusProviderTestParams.INTERFACE_NAME);
71
72         // Now make sure its NOT in the data store
73         assertFalse(getBoundServices(id).isPresent());
74     }
75
76     @Test
77     public void getNodeIdFromLogicalInterface() {
78         //Optional<NodeId> getNodeIdFromLogicalInterface(String logicalInterface)
79         // Test that it correctly handles the case when the ifName doesnt exist
80         Optional<NodeId> nodeId = this.geniusProvider.getNodeIdFromLogicalInterface(
81                 GeniusProviderTestParams.INTERFACE_NAME_NO_EXIST);
82         assertFalse(nodeId.isPresent());
83
84         // Test that it correctly handles RPC errors
85         nodeId = this.geniusProvider.getNodeIdFromLogicalInterface(
86                 GeniusProviderTestParams.INTERFACE_NAME_INVALID);
87         assertFalse(nodeId.isPresent());
88
89         // Test that it correctly returns the DpnId when everything is correct
90         nodeId = this.geniusProvider.getNodeIdFromLogicalInterface(
91                 GeniusProviderTestParams.INTERFACE_NAME);
92         assertTrue(nodeId.isPresent());
93         assertEquals(nodeId.get().getValue(), GeniusProviderTestParams.NODE_ID);
94     }
95
96     @Test
97     public void getNodeIdFromDpnId() {
98         // Test that it correctly handles null input
99         Optional<NodeId> nodeId = this.geniusProvider.getNodeIdFromDpnId(null);
100         assertFalse(nodeId.isPresent());
101
102         // Test that it correctly returns the nodeId when everything is correct
103         nodeId = this.geniusProvider.getNodeIdFromDpnId(new DpnIdType(GeniusProviderTestParams.DPN_ID));
104         assertTrue(nodeId.isPresent());
105         assertEquals(nodeId.get().getValue(), GeniusProviderTestParams.NODE_ID);
106     }
107
108     @Test
109     public void getIpFromDpnId() {
110         // Test that it correctly handles the case when the ifName doesnt exist
111         Optional<String> ipStr = this.geniusProvider.getIpFromDpnId(
112                 new DpnIdType(GeniusProviderTestParams.DPN_ID_NO_EXIST));
113         assertFalse(ipStr.isPresent());
114
115         // Test that it correctly handles RPC errors
116         ipStr = this.geniusProvider.getIpFromDpnId(
117                 new DpnIdType(GeniusProviderTestParams.DPN_ID_INVALID));
118         assertFalse(ipStr.isPresent());
119
120         // Test that it correctly returns the ipStr when everything is correct
121         ipStr = this.geniusProvider.getIpFromDpnId(
122                 new DpnIdType(GeniusProviderTestParams.DPN_ID));
123         assertTrue(ipStr.isPresent());
124         assertEquals(ipStr.get(), GeniusProviderTestParams.IPV4_ADDRESS_STR);
125     }
126
127     @Test
128     public void getDpnIdFromInterfaceName() {
129         // Test that it correctly handles the case when the ifName doesnt exist
130         Optional<DpnIdType> dpnId = this.geniusProvider.getDpnIdFromInterfaceName(
131                 GeniusProviderTestParams.INTERFACE_NAME_NO_EXIST);
132         assertFalse(dpnId.isPresent());
133
134         // Test that it correctly handles RPC errors
135         dpnId = this.geniusProvider.getDpnIdFromInterfaceName(
136                 GeniusProviderTestParams.INTERFACE_NAME_INVALID);
137         assertFalse(dpnId.isPresent());
138
139         // Test that it correctly returns the DpnId when everything is correct
140         dpnId = this.geniusProvider.getDpnIdFromInterfaceName(
141                 GeniusProviderTestParams.INTERFACE_NAME);
142         assertTrue(dpnId.isPresent());
143         assertEquals(dpnId.get().getValue(), GeniusProviderTestParams.DPN_ID);
144     }
145
146     @Test
147     public void getNodeConnectorIdFromInterfaceName() {
148         // Test that it correctly handles the case when the ifName doesnt exist
149         Optional<String> nodeConnStr = this.geniusProvider.getNodeConnectorIdFromInterfaceName(
150                 GeniusProviderTestParams.INTERFACE_NAME_NO_EXIST);
151         assertFalse(nodeConnStr.isPresent());
152
153         // Test that it correctly handles RPC errors
154         nodeConnStr = this.geniusProvider.getNodeConnectorIdFromInterfaceName(
155                 GeniusProviderTestParams.INTERFACE_NAME_INVALID);
156         assertFalse(nodeConnStr.isPresent());
157
158         // Test that it correctly returns the NodeConnectorId when everything is correct
159         nodeConnStr = this.geniusProvider.getNodeConnectorIdFromInterfaceName(
160                 GeniusProviderTestParams.INTERFACE_NAME);
161         assertTrue(nodeConnStr.isPresent());
162         assertEquals(nodeConnStr.get(), GeniusProviderTestParams.NODE_CONNECTOR_ID_PREFIX
163                 + GeniusProviderTestParams.INTERFACE_NAME);
164     }
165
166     @Test
167     public void getEgressVxlanPortForNode() {
168         // Test that it correctly handles the case when the dpnId doesnt exist
169         Optional<Long> ofPort = this.geniusProvider.getEgressVxlanPortForNode(
170                 GeniusProviderTestParams.DPN_ID_NO_EXIST);
171         assertFalse(ofPort.isPresent());
172
173         // Test that it correctly handles when there are no tunnel ports on the bridge
174         ofPort = this.geniusProvider.getEgressVxlanPortForNode(GeniusProviderTestParams.DPN_ID_NO_PORTS);
175         assertFalse(ofPort.isPresent());
176
177         // Test that it correctly handles when there are no VXGPE tunnel ports on the bridge
178         ofPort = this.geniusProvider.getEgressVxlanPortForNode(GeniusProviderTestParams.DPN_ID_NO_VXGPE_PORTS);
179         assertFalse(ofPort.isPresent());
180
181         // Test that is correctly handles when a terminationPoint has no options
182         ofPort = this.geniusProvider.getEgressVxlanPortForNode(GeniusProviderTestParams.DPN_ID_NO_OPTIONS);
183         assertFalse(ofPort.isPresent());
184
185         // Test that it correctly returns the OpenFlow port when everything is correct
186         ofPort = this.geniusProvider.getEgressVxlanPortForNode(GeniusProviderTestParams.DPN_ID);
187         assertTrue(ofPort.isPresent());
188         assertEquals(ofPort.get().longValue(), GeniusProviderTestParams.OF_PORT);
189     }
190
191     Optional<BoundServices> getBoundServices(InstanceIdentifier<BoundServices> id) {
192         return Optional.ofNullable(MDSALUtil.read(getDataBroker(), LogicalDatastoreType.CONFIGURATION, id).orNull());
193     }
194
195 }