BUG 2302 : odl-clustering-test-app should not be part of the odl-restconf-all feature set
[controller.git] / opendaylight / hosttracker_new / implementation / src / test / java / org / opendaylight / controller / hosttracker / internal / DeviceUniqueIndexTest.java
1 /*
2  * Copyright (c) 2012 Big Switch Networks, Inc.
3  *
4  * Licensed under the Eclipse Public License, Version 1.0 (the
5  * "License"); you may not use this file except in compliance with the
6  * License. You may obtain a copy of the License at
7  *
8  *      http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
13  * implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16
17 package org.opendaylight.controller.hosttracker.internal;
18
19 import java.util.ArrayList;
20 import java.util.Collections;
21 import java.util.Date;
22 import java.util.EnumSet;
23 import java.util.HashSet;
24 import java.util.Iterator;
25 import java.util.List;
26 import java.util.Set;
27
28 import junit.framework.TestCase;
29
30 import org.junit.Test;
31 import org.opendaylight.controller.hosttracker.Entity;
32 import org.opendaylight.controller.hosttracker.IDeviceService.DeviceField;
33 import org.opendaylight.controller.sal.core.Node;
34 import org.opendaylight.controller.sal.core.NodeConnector;
35 import org.opendaylight.controller.sal.core.NodeConnector.NodeConnectorIDType;
36
37 /**
38  *
39  * @author gregor
40  *
41  */
42 public class DeviceUniqueIndexTest extends TestCase {
43     protected Entity e1a;
44     protected Entity e1b;
45     protected Device d1;
46     protected Entity e2;
47     protected Entity e2alt;
48     protected Entity e3;
49     protected Entity e3_ip;
50     protected Entity e4;
51
52     @Override
53     protected void setUp() throws Exception {
54         super.setUp();
55         Node n1 = new Node(Node.NodeIDType.OPENFLOW, Long.valueOf(1L));
56         NodeConnector n1_1 = new NodeConnector(NodeConnectorIDType.OPENFLOW,
57                 Short.valueOf((short) 1), n1);
58         e1a = new Entity(1L, (short) 1, 1, n1_1, new Date());
59         e1b = new Entity(1L, (short) 2, 1, n1_1, new Date());
60         List<Entity> d1Entities = new ArrayList<Entity>(2);
61         d1Entities.add(e1a);
62         d1Entities.add(e1b);
63         d1 = new Device(null, Long.valueOf(1), null, null, null, d1Entities,
64                 null);
65
66         Node n2 = new Node(Node.NodeIDType.OPENFLOW, Long.valueOf(2L));
67         NodeConnector n2_2 = new NodeConnector(NodeConnectorIDType.OPENFLOW,
68                 Short.valueOf((short) 2), n2);
69         Node n3 = new Node(Node.NodeIDType.OPENFLOW, Long.valueOf(3L));
70         NodeConnector n3_3 = new NodeConnector(NodeConnectorIDType.OPENFLOW,
71                 Short.valueOf((short) 3), n3);
72
73         // e2 and e2 alt match in MAC and VLAN
74         e2 = new Entity(2L, (short) 2, 2, n2_2, new Date());
75         e2alt = new Entity(2, (short) 2, null, null, null);
76
77         // IP is null
78         e3 = new Entity(3L, (short) 3, null, n3_3, new Date());
79         e3_ip = new Entity(3L, (short) 3, 3, n3_3, new Date());
80
81         // IP and switch and port are null
82         e4 = new Entity(4L, (short) 4, null, null, new Date());
83     }
84
85     /*
86      * Checks that the iterator it returns the elements in the Set expected
87      * Doesn't check how often an element is returned as long it's at least once
88      */
89     protected void verifyIterator(Set<Long> expected, Iterator<Long> it) {
90         HashSet<Long> actual = new HashSet<Long>();
91         while (it.hasNext()) {
92             actual.add(it.next());
93         }
94         assertEquals(expected, actual);
95     }
96
97     @Test
98     public void testDeviceUniqueIndex() {
99         DeviceUniqueIndex idx1 = new DeviceUniqueIndex(EnumSet.of(
100                 DeviceField.MAC, DeviceField.VLAN));
101
102         idx1.updateIndex(d1, d1.getDeviceKey());
103         idx1.updateIndex(e2, 2L);
104
105         // -------------
106         // Test findByEntity lookups
107         assertEquals(Long.valueOf(1L), idx1.findByEntity(e1a));
108         assertEquals(Long.valueOf(1L), idx1.findByEntity(e1b));
109         assertEquals(Long.valueOf(2L), idx1.findByEntity(e2));
110         // we didn't add e2alt but since they key fields are the same we
111         // should find it
112         assertEquals(Long.valueOf(2L), idx1.findByEntity(e2alt));
113         assertEquals(null, idx1.findByEntity(e3));
114         assertEquals(null, idx1.findByEntity(e4));
115
116         // -------------
117         // Test getAll()
118         HashSet<Long> expectedKeys = new HashSet<Long>();
119         expectedKeys.add(1L);
120         expectedKeys.add(2L);
121         verifyIterator(expectedKeys, idx1.getAll());
122
123         // -------------
124         // Test queryByEntity()
125         verifyIterator(Collections.<Long> singleton(1L),
126                 idx1.queryByEntity(e1a));
127         verifyIterator(Collections.<Long> singleton(1L),
128                 idx1.queryByEntity(e1b));
129         verifyIterator(Collections.<Long> singleton(2L), idx1.queryByEntity(e2));
130         verifyIterator(Collections.<Long> singleton(2L),
131                 idx1.queryByEntity(e2alt));
132         assertEquals(false, idx1.queryByEntity(e3).hasNext());
133         assertEquals(false, idx1.queryByEntity(e3).hasNext());
134
135         // -------------
136         // Test removal
137         idx1.removeEntity(e1a, 42L); // No-op. e1a isn't mapped to this key
138         assertEquals(Long.valueOf(1L), idx1.findByEntity(e1a));
139         idx1.removeEntity(e1a, 1L);
140         assertEquals(null, idx1.findByEntity(e1a));
141         assertEquals(Long.valueOf(1L), idx1.findByEntity(e1b));
142         assertEquals(Long.valueOf(2L), idx1.findByEntity(e2));
143         idx1.removeEntity(e2);
144         assertEquals(null, idx1.findByEntity(e2));
145         assertEquals(Long.valueOf(1L), idx1.findByEntity(e1b));
146
147         // -------------
148         // Test null keys
149         DeviceUniqueIndex idx2 = new DeviceUniqueIndex(EnumSet.of(
150                 DeviceField.IPV4, DeviceField.SWITCHPORT));
151         // only one key field is null
152         idx2.updateIndex(e3, 3L);
153         assertEquals(Long.valueOf(3L), idx2.findByEntity(e3));
154         assertEquals(null, idx2.findByEntity(e3_ip));
155         // all key fields are null
156         idx2.updateIndex(e4, 4L);
157         assertEquals(null, idx2.findByEntity(e4));
158         Device d4 = new Device(null, 4L, null, null, null,
159                 Collections.<Entity> singleton(e4), null);
160         idx2.updateIndex(d4, 4L);
161         assertEquals(null, idx2.findByEntity(e4));
162
163         // -------------
164         // entity already exists with different deviceKey
165         DeviceUniqueIndex idx3 = new DeviceUniqueIndex(EnumSet.of(
166                 DeviceField.MAC, DeviceField.VLAN));
167         idx3.updateIndex(e1a, 42L);
168         assertEquals(false, idx3.updateIndex(d1, 1L));
169         // TODO: shouldn't this fail as well so that the behavior
170         // is consistent?
171         idx3.updateIndex(e1a, 1L);
172         // anyways. We can now add d1 ;-)
173         assertEquals(true, idx3.updateIndex(d1, 1L));
174     }
175 }