Make sure invokeOperation is set once
[controller.git] / opendaylight / adsal / hosttracker_new / implementation / src / main / java / org / opendaylight / controller / hosttracker / internal / DeviceUniqueIndex.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.Collections;
37 import java.util.EnumSet;
38 import java.util.Iterator;
39 import java.util.concurrent.ConcurrentHashMap;
40
41 import org.opendaylight.controller.hosttracker.Entity;
42 import org.opendaylight.controller.hosttracker.IDeviceService;
43
44 /**
45  * An index that maps key fields of an entity uniquely to a device key
46  */
47 public class DeviceUniqueIndex extends DeviceIndex {
48     /**
49      * The index
50      */
51     private final ConcurrentHashMap<IndexedEntity, Long> index;
52
53     /**
54      * Construct a new device index using the provided key fields
55      *
56      * @param keyFields
57      *            the key fields to use
58      */
59     public DeviceUniqueIndex(EnumSet<IDeviceService.DeviceField> keyFields) {
60         super(keyFields);
61         index = new ConcurrentHashMap<IndexedEntity, Long>();
62     }
63
64     // ***********
65     // DeviceIndex
66     // ***********
67
68     @Override
69     public Iterator<Long> queryByEntity(Entity entity) {
70         final Long deviceKey = findByEntity(entity);
71         if (deviceKey != null)
72             return Collections.<Long> singleton(deviceKey).iterator();
73
74         return Collections.<Long> emptySet().iterator();
75     }
76
77     @Override
78     public Iterator<Long> getAll() {
79         return index.values().iterator();
80     }
81
82     @Override
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())
87                 continue;
88
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).
94                 return false;
95             }
96         }
97         return true;
98     }
99
100     @Override
101     public void updateIndex(Entity entity, Long deviceKey) {
102         IndexedEntity ie = new IndexedEntity(keyFields, entity);
103         if (!ie.hasNonNullKeys())
104             return;
105         index.put(ie, deviceKey);
106     }
107
108     @Override
109     public void removeEntity(Entity entity) {
110         IndexedEntity ie = new IndexedEntity(keyFields, entity);
111         index.remove(ie);
112     }
113
114     @Override
115     public void removeEntity(Entity entity, Long deviceKey) {
116         IndexedEntity ie = new IndexedEntity(keyFields, entity);
117         index.remove(ie, deviceKey);
118     }
119
120     // **************
121     // Public Methods
122     // **************
123
124     /**
125      * Look up a {@link Device} based on the provided {@link Entity}.
126      *
127      * @param entity
128      *            the entity to search for
129      * @return The key for the {@link Device} object if found
130      */
131     public Long findByEntity(Entity entity) {
132         IndexedEntity ie = new IndexedEntity(keyFields, entity);
133         Long deviceKey = index.get(ie);
134         if (deviceKey == null)
135             return null;
136         return deviceKey;
137     }
138
139 }