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