blob: 0d8ea75dd42466600e9abf24637903750479985c [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
2* Copyright 2012 Big Switch Networks, Inc.
3* Originally created by David Erickson, Stanford University
4*
5* Licensed under the Apache License, Version 2.0 (the "License"); you may
6* not use this file except in compliance with the License. You may obtain
7* a copy of the License at
8*
9* http://www.apache.org/licenses/LICENSE-2.0
10*
11* Unless required by applicable law or agreed to in writing, software
12* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14* License for the specific language governing permissions and limitations
15* under the License.
16**/
17
18package net.floodlightcontroller.devicemanager.internal;
19
20import java.util.Collection;
21import java.util.EnumSet;
22import java.util.Iterator;
23
24import net.floodlightcontroller.devicemanager.IDeviceService.DeviceField;
25
26/**
27 * An index that maps key fields of an entity to device keys
28 */
29public abstract class DeviceIndex {
30 /**
31 * The key fields for this index
32 */
33 protected EnumSet<DeviceField> keyFields;
34
35 /**
36 * Construct a new device index using the provided key fields
37 * @param keyFields the key fields to use
38 */
39 public DeviceIndex(EnumSet<DeviceField> keyFields) {
40 super();
41 this.keyFields = keyFields;
42 }
43
44 /**
45 * Find all device keys in the index that match the given entity
46 * on all the key fields for this index
47 * @param e the entity to search for
48 * @return an iterator over device keys
49 */
50 public abstract Iterator<Long> queryByEntity(Entity entity);
51
52 /**
53 * Get all device keys in the index. If certain devices exist
54 * multiple times, then these devices may be returned multiple times
55 * @return an iterator over device keys
56 */
57 public abstract Iterator<Long> getAll();
58
59 /**
60 * Attempt to update an index with the entities in the provided
61 * {@link Device}. If the update fails because of a concurrent update,
62 * will return false.
63 * @param device the device to update
64 * @param deviceKey the device key for the device
65 * @return true if the update succeeded, false otherwise.
66 */
67 public abstract boolean updateIndex(Device device, Long deviceKey);
68
69 /**
70 * Add a mapping from the given entity to the given device key. This
71 * update will not fail because of a concurrent update
72 * @param device the device to update
73 * @param deviceKey the device key for the device
74 */
75 public abstract void updateIndex(Entity entity, Long deviceKey);
76
77 /**
78 * Remove the entry for the given entity
79 * @param entity the entity to remove
80 */
81 public abstract void removeEntity(Entity entity);
82
83 /**
84 * Remove the given device key from the index for the given entity
85 * @param entity the entity to search for
86 * @param deviceKey the key to remove
87 */
88 public abstract void removeEntity(Entity entity, Long deviceKey);
89
90 /**
91 * Remove the give device from the index only if this the collection
92 * of others does not contain an entity that is identical on all the key
93 * fields for this index.
94 * @param entity the entity to search for
95 * @param deviceKey the key to remove
96 * @param others the others against which to check
97 */
98 public void removeEntityIfNeeded(Entity entity, Long deviceKey,
99 Collection<Entity> others) {
100 IndexedEntity ie = new IndexedEntity(keyFields, entity);
101 for (Entity o : others) {
102 IndexedEntity oio = new IndexedEntity(keyFields, o);
103 if (oio.equals(ie)) return;
104 }
105
106 Iterator<Long> keyiter = this.queryByEntity(entity);
107 while (keyiter.hasNext()) {
108 Long key = keyiter.next();
109 if (key.equals(deviceKey)) {
110 removeEntity(entity, deviceKey);
111 break;
112 }
113 }
114 }
115
116}