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.Collections;
37 import java.util.EnumSet;
38 import java.util.Iterator;
39 import java.util.concurrent.ConcurrentHashMap;
41 import org.opendaylight.controller.hosttracker.Entity;
42 import org.opendaylight.controller.hosttracker.IDeviceService;
45 * An index that maps key fields of an entity uniquely to a device key
47 public class DeviceUniqueIndex extends DeviceIndex {
51 private final ConcurrentHashMap<IndexedEntity, Long> index;
54 * Construct a new device index using the provided key fields
57 * the key fields to use
59 public DeviceUniqueIndex(EnumSet<IDeviceService.DeviceField> keyFields) {
61 index = new ConcurrentHashMap<IndexedEntity, Long>();
69 public Iterator<Long> queryByEntity(Entity entity) {
70 final Long deviceKey = findByEntity(entity);
71 if (deviceKey != null)
72 return Collections.<Long> singleton(deviceKey).iterator();
74 return Collections.<Long> emptySet().iterator();
78 public Iterator<Long> getAll() {
79 return index.values().iterator();
83 public boolean updateIndex(Device device, Long deviceKey) {
84 for (Entity e : device.entities) {
85 IndexedEntity ie = new IndexedEntity(keyFields, e);
86 if (!ie.hasNonNullKeys())
89 Long ret = index.putIfAbsent(ie, deviceKey);
90 if (ret != null && !ret.equals(deviceKey)) {
91 // If the return value is non-null, then fail the insert
92 // (this implies that a device using this entity has
93 // already been created in another thread).
101 public void updateIndex(Entity entity, Long deviceKey) {
102 IndexedEntity ie = new IndexedEntity(keyFields, entity);
103 if (!ie.hasNonNullKeys())
105 index.put(ie, deviceKey);
109 public void removeEntity(Entity entity) {
110 IndexedEntity ie = new IndexedEntity(keyFields, entity);
115 public void removeEntity(Entity entity, Long deviceKey) {
116 IndexedEntity ie = new IndexedEntity(keyFields, entity);
117 index.remove(ie, deviceKey);
125 * Look up a {@link Device} based on the provided {@link Entity}.
128 * the entity to search for
129 * @return The key for the {@link Device} object if found
131 public Long findByEntity(Entity entity) {
132 IndexedEntity ie = new IndexedEntity(keyFields, entity);
133 Long deviceKey = index.get(ie);
134 if (deviceKey == null)