blob: 4f2d3f8c4995b466f2fc5e22edbf11160fbaf3ab [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.Collections;
21import java.util.EnumSet;
22import java.util.Iterator;
23import java.util.concurrent.ConcurrentHashMap;
24
25import net.floodlightcontroller.devicemanager.IDeviceService.DeviceField;
26
27/**
28 * An index that maps key fields of an entity uniquely to a device key
29 */
30public class DeviceUniqueIndex extends DeviceIndex {
31 /**
32 * The index
33 */
34 private ConcurrentHashMap<IndexedEntity, Long> index;
35
36 /**
37 * Construct a new device index using the provided key fields
38 * @param keyFields the key fields to use
39 */
40 public DeviceUniqueIndex(EnumSet<DeviceField> keyFields) {
41 super(keyFields);
42 index = new ConcurrentHashMap<IndexedEntity, Long>();
43 }
44
45 // ***********
46 // DeviceIndex
47 // ***********
48
49 @Override
50 public Iterator<Long> queryByEntity(Entity entity) {
51 final Long deviceKey = findByEntity(entity);
52 if (deviceKey != null)
53 return Collections.<Long>singleton(deviceKey).iterator();
54
55 return Collections.<Long>emptySet().iterator();
56 }
57
58 @Override
59 public Iterator<Long> getAll() {
60 return index.values().iterator();
61 }
62
63 @Override
64 public boolean updateIndex(Device device, Long deviceKey) {
65 for (Entity e : device.entities) {
66 IndexedEntity ie = new IndexedEntity(keyFields, e);
67 if (!ie.hasNonNullKeys()) continue;
68
69 Long ret = index.putIfAbsent(ie, deviceKey);
70 if (ret != null && !ret.equals(deviceKey)) {
71 // If the return value is non-null, then fail the insert
72 // (this implies that a device using this entity has
73 // already been created in another thread).
74 return false;
75 }
76 }
77 return true;
78 }
79
80 @Override
81 public void updateIndex(Entity entity, Long deviceKey) {
82 IndexedEntity ie = new IndexedEntity(keyFields, entity);
83 if (!ie.hasNonNullKeys()) return;
84 index.put(ie, deviceKey);
85 }
86
87 @Override
88 public void removeEntity(Entity entity) {
89 IndexedEntity ie = new IndexedEntity(keyFields, entity);
90 index.remove(ie);
91 }
92
93 @Override
94 public void removeEntity(Entity entity, Long deviceKey) {
95 IndexedEntity ie = new IndexedEntity(keyFields, entity);
96 index.remove(ie, deviceKey);
97 }
98
99 // **************
100 // Public Methods
101 // **************
102
103 /**
104 * Look up a {@link Device} based on the provided {@link Entity}.
105 * @param entity the entity to search for
106 * @return The key for the {@link Device} object if found
107 */
108 public Long findByEntity(Entity entity) {
109 IndexedEntity ie = new IndexedEntity(keyFields, entity);
110 Long deviceKey = index.get(ie);
111 if (deviceKey == null)
112 return null;
113 return deviceKey;
114 }
115
116}