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.Collections;
38 import java.util.EnumSet;
39 import java.util.Iterator;
41 import java.util.concurrent.ConcurrentHashMap;
43 import org.opendaylight.controller.hosttracker.Entity;
44 import org.opendaylight.controller.hosttracker.IDeviceService.DeviceField;
45 import org.opendaylight.controller.sal.utils.IterableIterator;
48 * An index that maps key fields of an entity to device keys, with multiple
49 * device keys allowed per entity
51 public class DeviceMultiIndex extends DeviceIndex {
55 private ConcurrentHashMap<IndexedEntity, Collection<Long>> index;
60 public DeviceMultiIndex(EnumSet<DeviceField> keyFields) {
62 index = new ConcurrentHashMap<IndexedEntity, Collection<Long>>();
70 public Iterator<Long> queryByEntity(Entity entity) {
71 IndexedEntity ie = new IndexedEntity(keyFields, entity);
72 Collection<Long> devices = index.get(ie);
74 return devices.iterator();
76 return Collections.<Long> emptySet().iterator();
80 public Iterator<Long> getAll() {
81 Iterator<Collection<Long>> iter = index.values().iterator();
82 return new IterableIterator<Long>(iter);
86 public boolean updateIndex(Device device, Long deviceKey) {
87 for (Entity e : device.entities) {
88 updateIndex(e, deviceKey);
94 public void updateIndex(Entity entity, Long deviceKey) {
95 Collection<Long> devices = null;
97 IndexedEntity ie = new IndexedEntity(keyFields, entity);
98 if (!ie.hasNonNullKeys())
101 devices = index.get(ie);
102 if (devices == null) {
103 Map<Long, Boolean> chm = new ConcurrentHashMap<Long, Boolean>();
104 devices = Collections.newSetFromMap(chm);
105 Collection<Long> r = index.putIfAbsent(ie, devices);
110 devices.add(deviceKey);
114 public void removeEntity(Entity entity) {
115 IndexedEntity ie = new IndexedEntity(keyFields, entity);
120 public void removeEntity(Entity entity, Long deviceKey) {
121 IndexedEntity ie = new IndexedEntity(keyFields, entity);
122 Collection<Long> devices = index.get(ie);
124 devices.remove(deviceKey);