5e50483f91e67e1a6f5908f03d6a1e759fa507da
[netvirt.git] / aclservice / impl / src / test / java / org / opendaylight / netvirt / aclservice / utils / AclDataUtilTest.java
1 /*
2  * Copyright (c) 2017 Inocybe Technologies 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 package org.opendaylight.netvirt.aclservice.utils;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15
16 import java.math.BigInteger;
17 import java.util.ArrayList;
18 import java.util.Arrays;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24 import org.junit.Test;
25 import org.opendaylight.netvirt.aclservice.api.utils.AclInterface;
26 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev130715.Uuid;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionBase;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.netvirt.aclservice.rev160608.DirectionIngress;
29
30 /**
31  * Unit tests for AclDataUtil.
32  *
33  * @author Thomas Pantelis
34  */
35 public class AclDataUtilTest {
36     private static final Uuid ACL1 = new Uuid("85cc3048-abc3-43cc-89b3-377341426ac1");
37     private static final Uuid ACL2 = new Uuid("85cc3048-abc3-43cc-89b3-377341426ac2");
38     private static final Uuid ACL3 = new Uuid("85cc3048-abc3-43cc-89b3-377341426ac3");
39     private static final Uuid ACL4 = new Uuid("85cc3048-abc3-43cc-89b3-377341426ac4");
40     private static final Uuid ACL5 = new Uuid("85cc3048-abc3-43cc-89b3-377341426ac5");
41
42     private static final AclInterface PORT1 = newPort("1");
43     private static final AclInterface PORT2 = newPort("2");
44     private static final AclInterface PORT3 = newPort("3");
45
46     private final AclDataUtil aclDataUtil = new AclDataUtil();
47
48     @Test
49     public void testAclInterfaces() {
50         assertTrue(aclDataUtil.getInterfaceList(ACL1).isEmpty());
51
52         aclDataUtil.removeAclInterfaceMap(Arrays.asList(ACL1), PORT1);
53
54         final BigInteger dpId = new BigInteger("123");
55         assertFalse(aclDataUtil.doesDpnHaveAclInterface(dpId));
56
57         aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL1, ACL2), PORT1);
58         assertAclInterfaces(ACL1, PORT1);
59         assertAclInterfaces(ACL2, PORT1);
60
61         aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL1), PORT2);
62         assertAclInterfaces(ACL1, PORT1, PORT2);
63         assertAclInterfaces(ACL2, PORT1);
64
65         assertFalse(aclDataUtil.doesDpnHaveAclInterface(dpId));
66
67         aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL1), PORT2);
68         assertAclInterfaces(ACL1, PORT1, PORT2);
69
70         aclDataUtil.addOrUpdateAclInterfaceMap(Arrays.asList(ACL1), PORT3);
71         assertAclInterfaces(ACL1, PORT1, PORT2, PORT3);
72
73         AclInterface updatedPort2 = AclInterface.builder().interfaceId(PORT2.getInterfaceId()).dpId(dpId).build();
74
75         aclDataUtil.addOrUpdateAclInterfaceMap(Arrays.asList(ACL1), updatedPort2);
76         assertAclInterfaces(ACL1, PORT1, updatedPort2, PORT3);
77
78         assertTrue(aclDataUtil.doesDpnHaveAclInterface(dpId));
79
80         aclDataUtil.removeAclInterfaceMap(Arrays.asList(ACL1, ACL2), PORT1);
81         assertAclInterfaces(ACL1, updatedPort2, PORT3);
82         assertAclInterfaces(ACL2);
83
84         aclDataUtil.removeAclInterfaceMap(Arrays.asList(ACL1, ACL2), updatedPort2);
85         assertAclInterfaces(ACL1, PORT3);
86         assertAclInterfaces(ACL2);
87
88         assertFalse(aclDataUtil.doesDpnHaveAclInterface(dpId));
89
90         aclDataUtil.addOrUpdateAclInterfaceMap(Arrays.asList(ACL2), PORT2);
91         assertAclInterfaces(ACL2, PORT2);
92
93         aclDataUtil.addOrUpdateAclInterfaceMap(Arrays.asList(ACL3), PORT1);
94         assertAclInterfaces(ACL3, PORT1);
95     }
96
97     @Test
98     public void testIngressRemoteAclInterfaces() {
99         Class<? extends DirectionBase> direction = DirectionIngress.class;
100         assertNull(aclDataUtil.getRemoteAcl(ACL1, direction));
101         assertNull(aclDataUtil.getRemoteAclInterfaces(ACL1, direction));
102
103         aclDataUtil.removeRemoteAclId(ACL1, ACL2, direction);
104
105         assertEquals(0, aclDataUtil.getAllRemoteAclInterfaces(direction).size());
106
107         aclDataUtil.addRemoteAclId(ACL1, ACL2, direction);
108         assertRemoteAcls(ACL1, ACL2);
109
110         aclDataUtil.addRemoteAclId(ACL1, ACL3, direction);
111         assertRemoteAcls(ACL1, ACL2, ACL3);
112
113         aclDataUtil.addRemoteAclId(ACL4, ACL5, direction);
114         assertRemoteAcls(ACL4, ACL5);
115         assertRemoteAcls(ACL1, ACL2, ACL3);
116
117         Map<String, Set<AclInterface>> map = aclDataUtil.getRemoteAclInterfaces(ACL1, direction);
118         assertNotNull(map);
119         assertEquals(0, map.size());
120
121         aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL2), PORT1);
122         aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL2), PORT2);
123         map = aclDataUtil.getRemoteAclInterfaces(ACL1, direction);
124         assertEquals(1, map.size());
125         assertAclInterfaces(map.get(ACL2.getValue()), PORT1, PORT2);
126
127         aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL3), PORT3);
128         map = aclDataUtil.getRemoteAclInterfaces(ACL1, direction);
129         assertEquals(2, map.size());
130         assertAclInterfaces(map.get(ACL2.getValue()), PORT1, PORT2);
131         assertAclInterfaces(map.get(ACL3.getValue()), PORT3);
132
133         aclDataUtil.removeRemoteAclId(ACL1, ACL2, direction);
134         assertRemoteAcls(ACL1, ACL3);
135     }
136
137     private static AclInterface newPort(String interfaceId) {
138         return AclInterface.builder().interfaceId(interfaceId).dpId(new BigInteger(interfaceId)).build();
139     }
140
141     private void assertAclInterfaces(Uuid acl, AclInterface... expPorts) {
142         assertAclInterfaces(aclDataUtil.getInterfaceList(acl), expPorts);
143     }
144
145     private void assertAclInterfaces(Collection<AclInterface> actualPorts, AclInterface... expPorts) {
146         assertNotNull(actualPorts);
147         List<AclInterface> actualPortList = new ArrayList<>(actualPorts);
148         sortPorts(actualPortList);
149
150         List<AclInterface> expPortsList = Arrays.asList(expPorts);
151         sortPorts(expPortsList);
152
153         assertEquals("AclInterfaces", expPortsList, actualPortList);
154     }
155
156     private void assertRemoteAcls(Uuid acl, Uuid... expAcls) {
157         List<Uuid> actualAclList = getRemoteAcls(acl, DirectionIngress.class);
158         sortAcls(actualAclList);
159
160         List<Uuid> expAclList = Arrays.asList(expAcls);
161         sortAcls(expAclList);
162
163         assertEquals("Remote Acls", expAclList, actualAclList);
164     }
165
166     private List<Uuid> getRemoteAcls(Uuid acl, Class<? extends DirectionBase> direction) {
167         Collection<Uuid> acls = aclDataUtil.getRemoteAcl(acl, direction);
168         assertNotNull(acls);
169         return new ArrayList<>(acls);
170     }
171
172     private static void sortPorts(List<AclInterface> ports) {
173         Collections.sort(ports, (p1, p2) -> p1.getInterfaceId().compareTo(p2.getInterfaceId()));
174     }
175
176     private static void sortAcls(List<Uuid> acls) {
177         Collections.sort(acls, (a1, a2) -> a1.getValue().compareTo(a2.getValue()));
178     }
179 }