2 * Copyright (c) 2012 Big Switch Networks, Inc.
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
8 * http://www.eclipse.org/legal/epl-v10.html
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.
16 * This file incorporates work covered by the following copyright and
19 * Originally created by David Erickson, Stanford University
21 * Licensed under the Apache License, Version 2.0 (the "License");
22 * you may not use this file except in compliance with the
23 * License. You may obtain a copy of the License at
25 * http://www.apache.org/licenses/LICENSE-2.0
27 * Unless required by applicable law or agreed to in writing,
28 * software distributed under the License is distributed on an "AS
29 * IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
30 * express or implied. See the License for the specific language
31 * governing permissions and limitations under the License.
34 package org.opendaylight.controller.hosttracker.internal;
36 import java.util.Collection;
37 import java.util.EnumSet;
38 import java.util.Iterator;
40 import org.opendaylight.controller.hosttracker.Entity;
41 import org.opendaylight.controller.hosttracker.IDeviceService.DeviceField;
44 * An index that maps key fields of an entity to device keys
46 public abstract class DeviceIndex {
48 * The key fields for this index
50 protected EnumSet<DeviceField> keyFields;
53 * Construct a new device index using the provided key fields
56 * the key fields to use
58 public DeviceIndex(EnumSet<DeviceField> keyFields) {
60 this.keyFields = keyFields;
64 * Find all device keys in the index that match the given entity on all the
65 * key fields for this index
68 * the entity to search for
69 * @return an iterator over device keys
71 public abstract Iterator<Long> queryByEntity(Entity entity);
74 * Get all device keys in the index. If certain devices exist multiple
75 * times, then these devices may be returned multiple times
77 * @return an iterator over device keys
79 public abstract Iterator<Long> getAll();
82 * Attempt to update an index with the entities in the provided
83 * {@link Device}. If the update fails because of a concurrent update, will
87 * the device to update
89 * the device key for the device
90 * @return true if the update succeeded, false otherwise.
92 public abstract boolean updateIndex(Device device, Long deviceKey);
95 * Add a mapping from the given entity to the given device key. This update
96 * will not fail because of a concurrent update
99 * the device to update
101 * the device key for the device
103 public abstract void updateIndex(Entity entity, Long deviceKey);
106 * Remove the entry for the given entity
109 * the entity to remove
111 public abstract void removeEntity(Entity entity);
114 * Remove the given device key from the index for the given entity
117 * the entity to search for
121 public abstract void removeEntity(Entity entity, Long deviceKey);
124 * Remove the give device from the index only if this the collection of
125 * others does not contain an entity that is identical on all the key fields
129 * the entity to search for
133 * the others against which to check
135 public void removeEntityIfNeeded(Entity entity, Long deviceKey,
136 Collection<Entity> others) {
137 IndexedEntity ie = new IndexedEntity(keyFields, entity);
138 for (Entity o : others) {
139 IndexedEntity oio = new IndexedEntity(keyFields, o);
144 Iterator<Long> keyiter = this.queryByEntity(entity);
145 while (keyiter.hasNext()) {
146 Long key = keyiter.next();
147 if (key.equals(deviceKey)) {
148 removeEntity(entity, deviceKey);