2 * Copyright (c) 2017 Inocybe Technologies and others. All rights reserved.
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
8 package org.opendaylight.netvirt.aclservice.utils;
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;
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;
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;
31 * Unit tests for AclDataUtil.
33 * @author Thomas Pantelis
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");
42 private static final AclInterface PORT1 = newPort("1");
43 private static final AclInterface PORT2 = newPort("2");
44 private static final AclInterface PORT3 = newPort("3");
46 private final AclDataUtil aclDataUtil = new AclDataUtil();
49 public void testAclInterfaces() {
50 assertTrue(aclDataUtil.getInterfaceList(ACL1).isEmpty());
52 aclDataUtil.removeAclInterfaceMap(Arrays.asList(ACL1), PORT1);
54 final BigInteger dpId = new BigInteger("123");
55 assertFalse(aclDataUtil.doesDpnHaveAclInterface(dpId));
57 aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL1, ACL2), PORT1);
58 assertAclInterfaces(ACL1, PORT1);
59 assertAclInterfaces(ACL2, PORT1);
61 aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL1), PORT2);
62 assertAclInterfaces(ACL1, PORT1, PORT2);
63 assertAclInterfaces(ACL2, PORT1);
65 assertFalse(aclDataUtil.doesDpnHaveAclInterface(dpId));
67 aclDataUtil.addAclInterfaceMap(Arrays.asList(ACL1), PORT2);
68 assertAclInterfaces(ACL1, PORT1, PORT2);
70 aclDataUtil.addOrUpdateAclInterfaceMap(Arrays.asList(ACL1), PORT3);
71 assertAclInterfaces(ACL1, PORT1, PORT2, PORT3);
73 AclInterface updatedPort2 = AclInterface.builder().interfaceId(PORT2.getInterfaceId()).dpId(dpId).build();
75 aclDataUtil.addOrUpdateAclInterfaceMap(Arrays.asList(ACL1), updatedPort2);
76 assertAclInterfaces(ACL1, PORT1, updatedPort2, PORT3);
78 assertTrue(aclDataUtil.doesDpnHaveAclInterface(dpId));
80 aclDataUtil.removeAclInterfaceMap(Arrays.asList(ACL1, ACL2), PORT1);
81 assertAclInterfaces(ACL1, updatedPort2, PORT3);
82 assertAclInterfaces(ACL2);
84 aclDataUtil.removeAclInterfaceMap(Arrays.asList(ACL1, ACL2), updatedPort2);
85 assertAclInterfaces(ACL1, PORT3);
86 assertAclInterfaces(ACL2);
88 assertFalse(aclDataUtil.doesDpnHaveAclInterface(dpId));
90 aclDataUtil.addOrUpdateAclInterfaceMap(Arrays.asList(ACL2), PORT2);
91 assertAclInterfaces(ACL2, PORT2);
93 aclDataUtil.addOrUpdateAclInterfaceMap(Arrays.asList(ACL3), PORT1);
94 assertAclInterfaces(ACL3, PORT1);
98 public void testIngressRemoteAclInterfaces() {
99 Class<? extends DirectionBase> direction = DirectionIngress.class;
100 assertNull(aclDataUtil.getRemoteAcl(ACL1, direction));
101 assertNull(aclDataUtil.getRemoteAclInterfaces(ACL1, direction));
103 aclDataUtil.removeRemoteAclId(ACL1, ACL2, direction);
105 assertEquals(0, aclDataUtil.getAllRemoteAclInterfaces(direction).size());
107 aclDataUtil.addRemoteAclId(ACL1, ACL2, direction);
108 assertRemoteAcls(ACL1, ACL2);
110 aclDataUtil.addRemoteAclId(ACL1, ACL3, direction);
111 assertRemoteAcls(ACL1, ACL2, ACL3);
113 aclDataUtil.addRemoteAclId(ACL4, ACL5, direction);
114 assertRemoteAcls(ACL4, ACL5);
115 assertRemoteAcls(ACL1, ACL2, ACL3);
117 Map<String, Set<AclInterface>> map = aclDataUtil.getRemoteAclInterfaces(ACL1, direction);
119 assertEquals(0, map.size());
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);
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);
133 aclDataUtil.removeRemoteAclId(ACL1, ACL2, direction);
134 assertRemoteAcls(ACL1, ACL3);
137 private static AclInterface newPort(String interfaceId) {
138 return AclInterface.builder().interfaceId(interfaceId).dpId(new BigInteger(interfaceId)).build();
141 private void assertAclInterfaces(Uuid acl, AclInterface... expPorts) {
142 assertAclInterfaces(aclDataUtil.getInterfaceList(acl), expPorts);
145 private void assertAclInterfaces(Collection<AclInterface> actualPorts, AclInterface... expPorts) {
146 assertNotNull(actualPorts);
147 List<AclInterface> actualPortList = new ArrayList<>(actualPorts);
148 sortPorts(actualPortList);
150 List<AclInterface> expPortsList = Arrays.asList(expPorts);
151 sortPorts(expPortsList);
153 assertEquals("AclInterfaces", expPortsList, actualPortList);
156 private void assertRemoteAcls(Uuid acl, Uuid... expAcls) {
157 List<Uuid> actualAclList = getRemoteAcls(acl, DirectionIngress.class);
158 sortAcls(actualAclList);
160 List<Uuid> expAclList = Arrays.asList(expAcls);
161 sortAcls(expAclList);
163 assertEquals("Remote Acls", expAclList, actualAclList);
166 private List<Uuid> getRemoteAcls(Uuid acl, Class<? extends DirectionBase> direction) {
167 Collection<Uuid> acls = aclDataUtil.getRemoteAcl(acl, direction);
169 return new ArrayList<>(acls);
172 private static void sortPorts(List<AclInterface> ports) {
173 Collections.sort(ports, (p1, p2) -> p1.getInterfaceId().compareTo(p2.getInterfaceId()));
176 private static void sortAcls(List<Uuid> acls) {
177 Collections.sort(acls, (a1, a2) -> a1.getValue().compareTo(a2.getValue()));