blob: c6aa9808e78a598c350ad388a7f5c06711ee8436 [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.Collections;
22import java.util.EnumSet;
23import java.util.Iterator;
24import java.util.Map;
25import java.util.concurrent.ConcurrentHashMap;
26
27import net.floodlightcontroller.devicemanager.IDeviceService.DeviceField;
28import net.floodlightcontroller.util.IterableIterator;
29
30/**
31 * An index that maps key fields of an entity to device keys, with multiple
32 * device keys allowed per entity
33 */
34public class DeviceMultiIndex extends DeviceIndex {
35 /**
36 * The index
37 */
38 private ConcurrentHashMap<IndexedEntity, Collection<Long>> index;
39
40 /**
41 * @param keyFields
42 */
43 public DeviceMultiIndex(EnumSet<DeviceField> keyFields) {
44 super(keyFields);
45 index = new ConcurrentHashMap<IndexedEntity, Collection<Long>>();
46 }
47
48 // ***********
49 // DeviceIndex
50 // ***********
51
52 @Override
53 public Iterator<Long> queryByEntity(Entity entity) {
54 IndexedEntity ie = new IndexedEntity(keyFields, entity);
55 Collection<Long> devices = index.get(ie);
56 if (devices != null)
57 return devices.iterator();
58
59 return Collections.<Long>emptySet().iterator();
60 }
61
62 @Override
63 public Iterator<Long> getAll() {
64 Iterator<Collection<Long>> iter = index.values().iterator();
65 return new IterableIterator<Long>(iter);
66 }
67
68 @Override
69 public boolean updateIndex(Device device, Long deviceKey) {
70 for (Entity e : device.entities) {
71 updateIndex(e, deviceKey);
72 }
73 return true;
74 }
75
76 @Override
77 public void updateIndex(Entity entity, Long deviceKey) {
78 Collection<Long> devices = null;
79
80 IndexedEntity ie = new IndexedEntity(keyFields, entity);
81 if (!ie.hasNonNullKeys()) return;
82
83 devices = index.get(ie);
84 if (devices == null) {
85 Map<Long,Boolean> chm = new ConcurrentHashMap<Long,Boolean>();
86 devices = Collections.newSetFromMap(chm);
87 Collection<Long> r = index.putIfAbsent(ie, devices);
88 if (r != null)
89 devices = r;
90 }
91
92 devices.add(deviceKey);
93 }
94
95 @Override
96 public void removeEntity(Entity entity) {
97 IndexedEntity ie = new IndexedEntity(keyFields, entity);
98 index.remove(ie);
99 }
100
101 @Override
102 public void removeEntity(Entity entity, Long deviceKey) {
103 IndexedEntity ie = new IndexedEntity(keyFields, entity);
104 Collection<Long> devices = index.get(ie);
105 if (devices != null)
106 devices.remove(deviceKey);
107 }
108}