BUG-2218: Keep existing link augmentations during discovery process
[controller.git] / opendaylight / adsal / hosttracker_new / implementation / src / main / java / org / opendaylight / controller / hosttracker / internal / DeviceMultiIndex.java
1 /*
2  * Copyright (c) 2012 Big Switch Networks, Inc.
3  *
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
7  *
8  *      http://www.eclipse.org/legal/epl-v10.html
9  *
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.
15  *
16  * This file incorporates work covered by the following copyright and
17  * permission notice:
18  *
19  *    Originally created by David Erickson, Stanford University
20  *
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
24  *
25  *         http://www.apache.org/licenses/LICENSE-2.0
26  *
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.
32  */
33
34 package org.opendaylight.controller.hosttracker.internal;
35
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.EnumSet;
39 import java.util.Iterator;
40 import java.util.Map;
41 import java.util.concurrent.ConcurrentHashMap;
42
43 import org.opendaylight.controller.hosttracker.Entity;
44 import org.opendaylight.controller.hosttracker.IDeviceService.DeviceField;
45 import org.opendaylight.controller.sal.utils.IterableIterator;
46
47 /**
48  * An index that maps key fields of an entity to device keys, with multiple
49  * device keys allowed per entity
50  */
51 public class DeviceMultiIndex extends DeviceIndex {
52     /**
53      * The index
54      */
55     private ConcurrentHashMap<IndexedEntity, Collection<Long>> index;
56
57     /**
58      * @param keyFields
59      */
60     public DeviceMultiIndex(EnumSet<DeviceField> keyFields) {
61         super(keyFields);
62         index = new ConcurrentHashMap<IndexedEntity, Collection<Long>>();
63     }
64
65     // ***********
66     // DeviceIndex
67     // ***********
68
69     @Override
70     public Iterator<Long> queryByEntity(Entity entity) {
71         IndexedEntity ie = new IndexedEntity(keyFields, entity);
72         Collection<Long> devices = index.get(ie);
73         if (devices != null)
74             return devices.iterator();
75
76         return Collections.<Long> emptySet().iterator();
77     }
78
79     @Override
80     public Iterator<Long> getAll() {
81         Iterator<Collection<Long>> iter = index.values().iterator();
82         return new IterableIterator<Long>(iter);
83     }
84
85     @Override
86     public boolean updateIndex(Device device, Long deviceKey) {
87         for (Entity e : device.entities) {
88             updateIndex(e, deviceKey);
89         }
90         return true;
91     }
92
93     @Override
94     public void updateIndex(Entity entity, Long deviceKey) {
95         Collection<Long> devices = null;
96
97         IndexedEntity ie = new IndexedEntity(keyFields, entity);
98         if (!ie.hasNonNullKeys())
99             return;
100
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);
106             if (r != null)
107                 devices = r;
108         }
109
110         devices.add(deviceKey);
111     }
112
113     @Override
114     public void removeEntity(Entity entity) {
115         IndexedEntity ie = new IndexedEntity(keyFields, entity);
116         index.remove(ie);
117     }
118
119     @Override
120     public void removeEntity(Entity entity, Long deviceKey) {
121         IndexedEntity ie = new IndexedEntity(keyFields, entity);
122         Collection<Long> devices = index.get(ie);
123         if (devices != null)
124             devices.remove(deviceKey);
125     }
126 }