Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2015 Open Networking Laboratory |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 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 implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.store.trivial.impl; |
| 17 | |
| 18 | import static org.apache.commons.lang3.concurrent.ConcurrentUtils.createIfAbsentUnchecked; |
| 19 | import static org.slf4j.LoggerFactory.getLogger; |
| 20 | |
| 21 | import java.util.ArrayList; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 22 | import java.util.HashMap; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 23 | import java.util.List; |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 24 | import java.util.Optional; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 25 | import java.util.concurrent.ConcurrentHashMap; |
| 26 | import java.util.concurrent.ConcurrentMap; |
| 27 | import java.util.concurrent.atomic.AtomicInteger; |
| 28 | |
| 29 | import org.apache.felix.scr.annotations.Activate; |
| 30 | import org.apache.felix.scr.annotations.Component; |
| 31 | import org.apache.felix.scr.annotations.Deactivate; |
| 32 | import org.apache.felix.scr.annotations.Service; |
| 33 | import org.onlab.util.NewConcurrentHashMap; |
| 34 | import org.onosproject.core.DefaultGroupId; |
| 35 | import org.onosproject.core.GroupId; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.net.group.DefaultGroup; |
| 38 | import org.onosproject.net.group.DefaultGroupDescription; |
| 39 | import org.onosproject.net.group.Group; |
| 40 | import org.onosproject.net.group.Group.GroupState; |
| 41 | import org.onosproject.net.group.GroupBucket; |
| 42 | import org.onosproject.net.group.GroupBuckets; |
| 43 | import org.onosproject.net.group.GroupDescription; |
| 44 | import org.onosproject.net.group.GroupEvent; |
| 45 | import org.onosproject.net.group.GroupEvent.Type; |
| 46 | import org.onosproject.net.group.GroupKey; |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 47 | import org.onosproject.net.group.GroupOperation; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 48 | import org.onosproject.net.group.GroupStore; |
| 49 | import org.onosproject.net.group.GroupStoreDelegate; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 50 | import org.onosproject.net.group.StoredGroupBucketEntry; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 51 | import org.onosproject.net.group.StoredGroupEntry; |
| 52 | import org.onosproject.store.AbstractStore; |
| 53 | import org.slf4j.Logger; |
| 54 | |
| 55 | import com.google.common.base.Function; |
| 56 | import com.google.common.collect.FluentIterable; |
| 57 | |
| 58 | /** |
| 59 | * Manages inventory of group entries using trivial in-memory implementation. |
| 60 | */ |
| 61 | @Component(immediate = true) |
| 62 | @Service |
| 63 | public class SimpleGroupStore |
| 64 | extends AbstractStore<GroupEvent, GroupStoreDelegate> |
| 65 | implements GroupStore { |
| 66 | |
| 67 | private final Logger log = getLogger(getClass()); |
| 68 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 69 | private final int dummyId = 0xffffffff; |
| 70 | private final GroupId dummyGroupId = new DefaultGroupId(dummyId); |
| 71 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 72 | // inner Map is per device group table |
| 73 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupKey, StoredGroupEntry>> |
| 74 | groupEntriesByKey = new ConcurrentHashMap<>(); |
| 75 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupId, StoredGroupEntry>> |
| 76 | groupEntriesById = new ConcurrentHashMap<>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 77 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupKey, StoredGroupEntry>> |
| 78 | pendingGroupEntriesByKey = new ConcurrentHashMap<>(); |
| 79 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupId, Group>> |
| 80 | extraneousGroupEntriesById = new ConcurrentHashMap<>(); |
| 81 | |
| 82 | private final HashMap<DeviceId, Boolean> deviceAuditStatus = |
| 83 | new HashMap<DeviceId, Boolean>(); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 84 | |
| 85 | private final AtomicInteger groupIdGen = new AtomicInteger(); |
| 86 | |
| 87 | @Activate |
| 88 | public void activate() { |
| 89 | log.info("Started"); |
| 90 | } |
| 91 | |
| 92 | @Deactivate |
| 93 | public void deactivate() { |
| 94 | groupEntriesByKey.clear(); |
| 95 | groupEntriesById.clear(); |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 99 | private static NewConcurrentHashMap<GroupKey, StoredGroupEntry> |
| 100 | lazyEmptyGroupKeyTable() { |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 101 | return NewConcurrentHashMap.<GroupKey, StoredGroupEntry>ifNeeded(); |
| 102 | } |
| 103 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 104 | private static NewConcurrentHashMap<GroupId, StoredGroupEntry> |
| 105 | lazyEmptyGroupIdTable() { |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 106 | return NewConcurrentHashMap.<GroupId, StoredGroupEntry>ifNeeded(); |
| 107 | } |
| 108 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 109 | private static NewConcurrentHashMap<GroupKey, StoredGroupEntry> |
| 110 | lazyEmptyPendingGroupKeyTable() { |
| 111 | return NewConcurrentHashMap.<GroupKey, StoredGroupEntry>ifNeeded(); |
| 112 | } |
| 113 | |
| 114 | private static NewConcurrentHashMap<GroupId, Group> |
| 115 | lazyEmptyExtraneousGroupIdTable() { |
| 116 | return NewConcurrentHashMap.<GroupId, Group>ifNeeded(); |
| 117 | } |
| 118 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 119 | /** |
| 120 | * Returns the group key table for specified device. |
| 121 | * |
| 122 | * @param deviceId identifier of the device |
| 123 | * @return Map representing group key table of given device. |
| 124 | */ |
| 125 | private ConcurrentMap<GroupKey, StoredGroupEntry> getGroupKeyTable(DeviceId deviceId) { |
| 126 | return createIfAbsentUnchecked(groupEntriesByKey, |
| 127 | deviceId, lazyEmptyGroupKeyTable()); |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * Returns the group id table for specified device. |
| 132 | * |
| 133 | * @param deviceId identifier of the device |
| 134 | * @return Map representing group key table of given device. |
| 135 | */ |
| 136 | private ConcurrentMap<GroupId, StoredGroupEntry> getGroupIdTable(DeviceId deviceId) { |
| 137 | return createIfAbsentUnchecked(groupEntriesById, |
| 138 | deviceId, lazyEmptyGroupIdTable()); |
| 139 | } |
| 140 | |
| 141 | /** |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 142 | * Returns the pending group key table for specified device. |
| 143 | * |
| 144 | * @param deviceId identifier of the device |
| 145 | * @return Map representing group key table of given device. |
| 146 | */ |
| 147 | private ConcurrentMap<GroupKey, StoredGroupEntry> |
| 148 | getPendingGroupKeyTable(DeviceId deviceId) { |
| 149 | return createIfAbsentUnchecked(pendingGroupEntriesByKey, |
| 150 | deviceId, lazyEmptyPendingGroupKeyTable()); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the extraneous group id table for specified device. |
| 155 | * |
| 156 | * @param deviceId identifier of the device |
| 157 | * @return Map representing group key table of given device. |
| 158 | */ |
| 159 | private ConcurrentMap<GroupId, Group> |
| 160 | getExtraneousGroupIdTable(DeviceId deviceId) { |
| 161 | return createIfAbsentUnchecked(extraneousGroupEntriesById, |
| 162 | deviceId, |
| 163 | lazyEmptyExtraneousGroupIdTable()); |
| 164 | } |
| 165 | |
| 166 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 167 | * Returns the number of groups for the specified device in the store. |
| 168 | * |
| 169 | * @return number of groups for the specified device |
| 170 | */ |
| 171 | @Override |
| 172 | public int getGroupCount(DeviceId deviceId) { |
| 173 | return (groupEntriesByKey.get(deviceId) != null) ? |
| 174 | groupEntriesByKey.get(deviceId).size() : 0; |
| 175 | } |
| 176 | |
| 177 | /** |
| 178 | * Returns the groups associated with a device. |
| 179 | * |
| 180 | * @param deviceId the device ID |
| 181 | * |
| 182 | * @return the group entries |
| 183 | */ |
| 184 | @Override |
| 185 | public Iterable<Group> getGroups(DeviceId deviceId) { |
| 186 | // flatten and make iterator unmodifiable |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 187 | return FluentIterable.from(getGroupKeyTable(deviceId).values()) |
| 188 | .transform( |
| 189 | new Function<StoredGroupEntry, Group>() { |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 190 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 191 | @Override |
| 192 | public Group apply( |
| 193 | StoredGroupEntry input) { |
| 194 | return input; |
| 195 | } |
| 196 | }); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Returns the stored group entry. |
| 201 | * |
| 202 | * @param deviceId the device ID |
| 203 | * @param appCookie the group key |
| 204 | * |
| 205 | * @return a group associated with the key |
| 206 | */ |
| 207 | @Override |
| 208 | public Group getGroup(DeviceId deviceId, GroupKey appCookie) { |
| 209 | return (groupEntriesByKey.get(deviceId) != null) ? |
| 210 | groupEntriesByKey.get(deviceId).get(appCookie) : |
| 211 | null; |
| 212 | } |
| 213 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 214 | @Override |
| 215 | public Group getGroup(DeviceId deviceId, GroupId groupId) { |
| 216 | return (groupEntriesById.get(deviceId) != null) ? |
| 217 | groupEntriesById.get(deviceId).get(groupId) : |
| 218 | null; |
| 219 | } |
| 220 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 221 | private int getFreeGroupIdValue(DeviceId deviceId) { |
| 222 | int freeId = groupIdGen.incrementAndGet(); |
| 223 | |
| 224 | while (true) { |
| 225 | Group existing = ( |
| 226 | groupEntriesById.get(deviceId) != null) ? |
| 227 | groupEntriesById.get(deviceId).get(new DefaultGroupId(freeId)) : |
| 228 | null; |
| 229 | if (existing == null) { |
| 230 | existing = ( |
| 231 | extraneousGroupEntriesById.get(deviceId) != null) ? |
| 232 | extraneousGroupEntriesById.get(deviceId). |
| 233 | get(new DefaultGroupId(freeId)) : |
| 234 | null; |
| 235 | } |
| 236 | if (existing != null) { |
| 237 | freeId = groupIdGen.incrementAndGet(); |
| 238 | } else { |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | return freeId; |
| 243 | } |
| 244 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 245 | /** |
| 246 | * Stores a new group entry using the information from group description. |
| 247 | * |
| 248 | * @param groupDesc group description to be used to create group entry |
| 249 | */ |
| 250 | @Override |
| 251 | public void storeGroupDescription(GroupDescription groupDesc) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 252 | // Check if a group is existing with the same key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 253 | if (getGroup(groupDesc.deviceId(), groupDesc.appCookie()) != null) { |
| 254 | return; |
| 255 | } |
| 256 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 257 | if (deviceAuditStatus.get(groupDesc.deviceId()) == null) { |
| 258 | // Device group audit has not completed yet |
| 259 | // Add this group description to pending group key table |
| 260 | // Create a group entry object with Dummy Group ID |
| 261 | StoredGroupEntry group = new DefaultGroup(dummyGroupId, groupDesc); |
| 262 | group.setState(GroupState.WAITING_AUDIT_COMPLETE); |
| 263 | ConcurrentMap<GroupKey, StoredGroupEntry> pendingKeyTable = |
| 264 | getPendingGroupKeyTable(groupDesc.deviceId()); |
| 265 | pendingKeyTable.put(groupDesc.appCookie(), group); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | storeGroupDescriptionInternal(groupDesc); |
| 270 | } |
| 271 | |
| 272 | private void storeGroupDescriptionInternal(GroupDescription groupDesc) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 273 | // Check if a group is existing with the same key |
| 274 | if (getGroup(groupDesc.deviceId(), groupDesc.appCookie()) != null) { |
| 275 | return; |
| 276 | } |
| 277 | |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 278 | GroupId id = null; |
| 279 | if (groupDesc.givenGroupId() == null) { |
| 280 | // Get a new group identifier |
| 281 | id = new DefaultGroupId(getFreeGroupIdValue(groupDesc.deviceId())); |
| 282 | } else { |
| 283 | id = new DefaultGroupId(groupDesc.givenGroupId()); |
| 284 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 285 | // Create a group entry object |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 286 | StoredGroupEntry group = new DefaultGroup(id, groupDesc); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 287 | // Insert the newly created group entry into concurrent key and id maps |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 288 | ConcurrentMap<GroupKey, StoredGroupEntry> keyTable = |
| 289 | getGroupKeyTable(groupDesc.deviceId()); |
| 290 | keyTable.put(groupDesc.appCookie(), group); |
| 291 | ConcurrentMap<GroupId, StoredGroupEntry> idTable = |
| 292 | getGroupIdTable(groupDesc.deviceId()); |
| 293 | idTable.put(id, group); |
| 294 | notifyDelegate(new GroupEvent(GroupEvent.Type.GROUP_ADD_REQUESTED, |
| 295 | group)); |
| 296 | } |
| 297 | |
| 298 | /** |
| 299 | * Updates the existing group entry with the information |
| 300 | * from group description. |
| 301 | * |
| 302 | * @param deviceId the device ID |
| 303 | * @param oldAppCookie the current group key |
| 304 | * @param type update type |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 305 | * @param newBuckets group buckets for updates |
| 306 | * @param newAppCookie optional new group key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 307 | */ |
| 308 | @Override |
| 309 | public void updateGroupDescription(DeviceId deviceId, |
| 310 | GroupKey oldAppCookie, |
| 311 | UpdateType type, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 312 | GroupBuckets newBuckets, |
| 313 | GroupKey newAppCookie) { |
| 314 | // Check if a group is existing with the provided key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 315 | Group oldGroup = getGroup(deviceId, oldAppCookie); |
| 316 | if (oldGroup == null) { |
| 317 | return; |
| 318 | } |
| 319 | |
| 320 | List<GroupBucket> newBucketList = getUpdatedBucketList(oldGroup, |
| 321 | type, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 322 | newBuckets); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 323 | if (newBucketList != null) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 324 | // Create a new group object from the old group |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 325 | GroupBuckets updatedBuckets = new GroupBuckets(newBucketList); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 326 | GroupKey newCookie = (newAppCookie != null) ? newAppCookie : oldAppCookie; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 327 | GroupDescription updatedGroupDesc = new DefaultGroupDescription( |
| 328 | oldGroup.deviceId(), |
| 329 | oldGroup.type(), |
| 330 | updatedBuckets, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 331 | newCookie, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 332 | oldGroup.givenGroupId(), |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 333 | oldGroup.appId()); |
| 334 | StoredGroupEntry newGroup = new DefaultGroup(oldGroup.id(), |
| 335 | updatedGroupDesc); |
| 336 | newGroup.setState(GroupState.PENDING_UPDATE); |
| 337 | newGroup.setLife(oldGroup.life()); |
| 338 | newGroup.setPackets(oldGroup.packets()); |
| 339 | newGroup.setBytes(oldGroup.bytes()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 340 | // Remove the old entry from maps and add new entry using new key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 341 | ConcurrentMap<GroupKey, StoredGroupEntry> keyTable = |
| 342 | getGroupKeyTable(oldGroup.deviceId()); |
| 343 | ConcurrentMap<GroupId, StoredGroupEntry> idTable = |
| 344 | getGroupIdTable(oldGroup.deviceId()); |
| 345 | keyTable.remove(oldGroup.appCookie()); |
| 346 | idTable.remove(oldGroup.id()); |
| 347 | keyTable.put(newGroup.appCookie(), newGroup); |
| 348 | idTable.put(newGroup.id(), newGroup); |
| 349 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_REQUESTED, newGroup)); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | private List<GroupBucket> getUpdatedBucketList(Group oldGroup, |
| 354 | UpdateType type, |
| 355 | GroupBuckets buckets) { |
| 356 | GroupBuckets oldBuckets = oldGroup.buckets(); |
| 357 | List<GroupBucket> newBucketList = new ArrayList<GroupBucket>( |
| 358 | oldBuckets.buckets()); |
| 359 | boolean groupDescUpdated = false; |
| 360 | |
| 361 | if (type == UpdateType.ADD) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 362 | // Check if the any of the new buckets are part of |
| 363 | // the old bucket list |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 364 | for (GroupBucket addBucket:buckets.buckets()) { |
| 365 | if (!newBucketList.contains(addBucket)) { |
| 366 | newBucketList.add(addBucket); |
| 367 | groupDescUpdated = true; |
| 368 | } |
| 369 | } |
| 370 | } else if (type == UpdateType.REMOVE) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 371 | // Check if the to be removed buckets are part of the |
| 372 | // old bucket list |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 373 | for (GroupBucket removeBucket:buckets.buckets()) { |
| 374 | if (newBucketList.contains(removeBucket)) { |
| 375 | newBucketList.remove(removeBucket); |
| 376 | groupDescUpdated = true; |
| 377 | } |
| 378 | } |
| 379 | } |
| 380 | |
| 381 | if (groupDescUpdated) { |
| 382 | return newBucketList; |
| 383 | } else { |
| 384 | return null; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Triggers deleting the existing group entry. |
| 390 | * |
| 391 | * @param deviceId the device ID |
| 392 | * @param appCookie the group key |
| 393 | */ |
| 394 | @Override |
| 395 | public void deleteGroupDescription(DeviceId deviceId, |
| 396 | GroupKey appCookie) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 397 | // Check if a group is existing with the provided key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 398 | StoredGroupEntry existing = (groupEntriesByKey.get(deviceId) != null) ? |
| 399 | groupEntriesByKey.get(deviceId).get(appCookie) : |
| 400 | null; |
| 401 | if (existing == null) { |
| 402 | return; |
| 403 | } |
| 404 | |
| 405 | synchronized (existing) { |
| 406 | existing.setState(GroupState.PENDING_DELETE); |
| 407 | } |
| 408 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, existing)); |
| 409 | } |
| 410 | |
| 411 | /** |
| 412 | * Stores a new group entry, or updates an existing entry. |
| 413 | * |
| 414 | * @param group group entry |
| 415 | */ |
| 416 | @Override |
| 417 | public void addOrUpdateGroupEntry(Group group) { |
| 418 | // check if this new entry is an update to an existing entry |
| 419 | StoredGroupEntry existing = (groupEntriesById.get( |
| 420 | group.deviceId()) != null) ? |
| 421 | groupEntriesById.get(group.deviceId()).get(group.id()) : |
| 422 | null; |
| 423 | GroupEvent event = null; |
| 424 | |
| 425 | if (existing != null) { |
| 426 | synchronized (existing) { |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 427 | for (GroupBucket bucket:group.buckets().buckets()) { |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 428 | Optional<GroupBucket> matchingBucket = |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 429 | existing.buckets().buckets() |
| 430 | .stream() |
| 431 | .filter((existingBucket)->(existingBucket.equals(bucket))) |
| 432 | .findFirst(); |
| 433 | if (matchingBucket.isPresent()) { |
| 434 | ((StoredGroupBucketEntry) matchingBucket. |
| 435 | get()).setPackets(bucket.packets()); |
| 436 | ((StoredGroupBucketEntry) matchingBucket. |
| 437 | get()).setBytes(bucket.bytes()); |
| 438 | } else { |
| 439 | log.warn("addOrUpdateGroupEntry: No matching " |
| 440 | + "buckets to update stats"); |
| 441 | } |
| 442 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 443 | existing.setLife(group.life()); |
| 444 | existing.setPackets(group.packets()); |
| 445 | existing.setBytes(group.bytes()); |
| 446 | if (existing.state() == GroupState.PENDING_ADD) { |
| 447 | existing.setState(GroupState.ADDED); |
| 448 | event = new GroupEvent(Type.GROUP_ADDED, existing); |
| 449 | } else { |
| 450 | if (existing.state() == GroupState.PENDING_UPDATE) { |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 451 | existing.setState(GroupState.ADDED); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 452 | } |
| 453 | event = new GroupEvent(Type.GROUP_UPDATED, existing); |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | |
| 458 | if (event != null) { |
| 459 | notifyDelegate(event); |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Removes the group entry from store. |
| 465 | * |
| 466 | * @param group group entry |
| 467 | */ |
| 468 | @Override |
| 469 | public void removeGroupEntry(Group group) { |
| 470 | StoredGroupEntry existing = (groupEntriesById.get( |
| 471 | group.deviceId()) != null) ? |
| 472 | groupEntriesById.get(group.deviceId()).get(group.id()) : |
| 473 | null; |
| 474 | |
| 475 | if (existing != null) { |
| 476 | ConcurrentMap<GroupKey, StoredGroupEntry> keyTable = |
| 477 | getGroupKeyTable(existing.deviceId()); |
| 478 | ConcurrentMap<GroupId, StoredGroupEntry> idTable = |
| 479 | getGroupIdTable(existing.deviceId()); |
| 480 | idTable.remove(existing.id()); |
| 481 | keyTable.remove(existing.appCookie()); |
| 482 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVED, existing)); |
| 483 | } |
| 484 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 485 | |
| 486 | @Override |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 487 | public void deviceInitialAuditCompleted(DeviceId deviceId, |
| 488 | boolean completed) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 489 | synchronized (deviceAuditStatus) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 490 | if (completed) { |
| 491 | log.debug("deviceInitialAuditCompleted: AUDIT " |
| 492 | + "completed for device {}", deviceId); |
| 493 | deviceAuditStatus.put(deviceId, true); |
| 494 | // Execute all pending group requests |
| 495 | ConcurrentMap<GroupKey, StoredGroupEntry> pendingGroupRequests = |
| 496 | getPendingGroupKeyTable(deviceId); |
| 497 | for (Group group:pendingGroupRequests.values()) { |
| 498 | GroupDescription tmp = new DefaultGroupDescription( |
| 499 | group.deviceId(), |
| 500 | group.type(), |
| 501 | group.buckets(), |
| 502 | group.appCookie(), |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 503 | group.givenGroupId(), |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 504 | group.appId()); |
| 505 | storeGroupDescriptionInternal(tmp); |
| 506 | } |
| 507 | getPendingGroupKeyTable(deviceId).clear(); |
| 508 | } else { |
| 509 | if (deviceAuditStatus.get(deviceId)) { |
| 510 | log.debug("deviceInitialAuditCompleted: Clearing AUDIT " |
| 511 | + "status for device {}", deviceId); |
| 512 | deviceAuditStatus.put(deviceId, false); |
| 513 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 514 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 515 | } |
| 516 | } |
| 517 | |
| 518 | @Override |
| 519 | public boolean deviceInitialAuditStatus(DeviceId deviceId) { |
| 520 | synchronized (deviceAuditStatus) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 521 | return (deviceAuditStatus.get(deviceId) != null) |
| 522 | ? deviceAuditStatus.get(deviceId) : false; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
| 526 | @Override |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 527 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
| 528 | |
| 529 | StoredGroupEntry existing = (groupEntriesById.get( |
| 530 | deviceId) != null) ? |
| 531 | groupEntriesById.get(deviceId).get(operation.groupId()) : |
| 532 | null; |
| 533 | |
| 534 | if (existing == null) { |
| 535 | log.warn("No group entry with ID {} found ", operation.groupId()); |
| 536 | return; |
| 537 | } |
| 538 | |
| 539 | switch (operation.opType()) { |
| 540 | case ADD: |
| 541 | notifyDelegate(new GroupEvent(Type.GROUP_ADD_FAILED, existing)); |
| 542 | break; |
| 543 | case MODIFY: |
| 544 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_FAILED, existing)); |
| 545 | break; |
| 546 | case DELETE: |
| 547 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_FAILED, existing)); |
| 548 | break; |
| 549 | default: |
| 550 | log.warn("Unknown group operation type {}", operation.opType()); |
| 551 | } |
| 552 | |
| 553 | ConcurrentMap<GroupKey, StoredGroupEntry> keyTable = |
| 554 | getGroupKeyTable(existing.deviceId()); |
| 555 | ConcurrentMap<GroupId, StoredGroupEntry> idTable = |
| 556 | getGroupIdTable(existing.deviceId()); |
| 557 | idTable.remove(existing.id()); |
| 558 | keyTable.remove(existing.appCookie()); |
| 559 | } |
| 560 | |
| 561 | @Override |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 562 | public void addOrUpdateExtraneousGroupEntry(Group group) { |
| 563 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 564 | getExtraneousGroupIdTable(group.deviceId()); |
| 565 | extraneousIdTable.put(group.id(), group); |
| 566 | // Check the reference counter |
| 567 | if (group.referenceCount() == 0) { |
| 568 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, group)); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | @Override |
| 573 | public void removeExtraneousGroupEntry(Group group) { |
| 574 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 575 | getExtraneousGroupIdTable(group.deviceId()); |
| 576 | extraneousIdTable.remove(group.id()); |
| 577 | } |
| 578 | |
| 579 | @Override |
| 580 | public Iterable<Group> getExtraneousGroups(DeviceId deviceId) { |
| 581 | // flatten and make iterator unmodifiable |
| 582 | return FluentIterable.from( |
| 583 | getExtraneousGroupIdTable(deviceId).values()); |
| 584 | } |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 585 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 586 | } |