Merge "Move adsal into its own subdirectory."
[controller.git] / opendaylight / adsal / hosttracker_new / implementation / src / main / java / org / opendaylight / controller / hosttracker / internal / DeviceIndex.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.EnumSet;
38 import java.util.Iterator;
39
40 import org.opendaylight.controller.hosttracker.Entity;
41 import org.opendaylight.controller.hosttracker.IDeviceService.DeviceField;
42
43 /**
44  * An index that maps key fields of an entity to device keys
45  */
46 public abstract class DeviceIndex {
47     /**
48      * The key fields for this index
49      */
50     protected EnumSet<DeviceField> keyFields;
51
52     /**
53      * Construct a new device index using the provided key fields
54      *
55      * @param keyFields
56      *            the key fields to use
57      */
58     public DeviceIndex(EnumSet<DeviceField> keyFields) {
59         super();
60         this.keyFields = keyFields;
61     }
62
63     /**
64      * Find all device keys in the index that match the given entity on all the
65      * key fields for this index
66      *
67      * @param e
68      *            the entity to search for
69      * @return an iterator over device keys
70      */
71     public abstract Iterator<Long> queryByEntity(Entity entity);
72
73     /**
74      * Get all device keys in the index. If certain devices exist multiple
75      * times, then these devices may be returned multiple times
76      *
77      * @return an iterator over device keys
78      */
79     public abstract Iterator<Long> getAll();
80
81     /**
82      * Attempt to update an index with the entities in the provided
83      * {@link Device}. If the update fails because of a concurrent update, will
84      * return false.
85      *
86      * @param device
87      *            the device to update
88      * @param deviceKey
89      *            the device key for the device
90      * @return true if the update succeeded, false otherwise.
91      */
92     public abstract boolean updateIndex(Device device, Long deviceKey);
93
94     /**
95      * Add a mapping from the given entity to the given device key. This update
96      * will not fail because of a concurrent update
97      *
98      * @param device
99      *            the device to update
100      * @param deviceKey
101      *            the device key for the device
102      */
103     public abstract void updateIndex(Entity entity, Long deviceKey);
104
105     /**
106      * Remove the entry for the given entity
107      *
108      * @param entity
109      *            the entity to remove
110      */
111     public abstract void removeEntity(Entity entity);
112
113     /**
114      * Remove the given device key from the index for the given entity
115      *
116      * @param entity
117      *            the entity to search for
118      * @param deviceKey
119      *            the key to remove
120      */
121     public abstract void removeEntity(Entity entity, Long deviceKey);
122
123     /**
124      * Remove the give device from the index only if this the collection of
125      * others does not contain an entity that is identical on all the key fields
126      * for this index.
127      *
128      * @param entity
129      *            the entity to search for
130      * @param deviceKey
131      *            the key to remove
132      * @param others
133      *            the others against which to check
134      */
135     public void removeEntityIfNeeded(Entity entity, Long deviceKey,
136             Collection<Entity> others) {
137         IndexedEntity ie = new IndexedEntity(keyFields, entity);
138         for (Entity o : others) {
139             IndexedEntity oio = new IndexedEntity(keyFields, o);
140             if (oio.equals(ie))
141                 return;
142         }
143
144         Iterator<Long> keyiter = this.queryByEntity(entity);
145         while (keyiter.hasNext()) {
146             Long key = keyiter.next();
147             if (key.equals(deviceKey)) {
148                 removeEntity(entity, deviceKey);
149                 break;
150             }
151         }
152     }
153
154 }