Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 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 | */ |
Thomas Vachuska | c97aa61 | 2015-06-23 16:00:18 -0700 | [diff] [blame] | 16 | package org.onosproject.store.trivial; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 17 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.ArrayList; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 21 | import java.util.Collection; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 22 | import java.util.HashMap; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 23 | import java.util.Iterator; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 24 | import java.util.List; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 25 | import java.util.Map; |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 26 | import java.util.Optional; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 27 | import java.util.Set; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 28 | import java.util.concurrent.ConcurrentHashMap; |
| 29 | import java.util.concurrent.ConcurrentMap; |
| 30 | import java.util.concurrent.atomic.AtomicInteger; |
| 31 | |
| 32 | import org.apache.felix.scr.annotations.Activate; |
| 33 | import org.apache.felix.scr.annotations.Component; |
| 34 | import org.apache.felix.scr.annotations.Deactivate; |
| 35 | import org.apache.felix.scr.annotations.Service; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 36 | import org.onosproject.core.DefaultGroupId; |
| 37 | import org.onosproject.core.GroupId; |
| 38 | import org.onosproject.net.DeviceId; |
| 39 | import org.onosproject.net.group.DefaultGroup; |
| 40 | import org.onosproject.net.group.DefaultGroupDescription; |
| 41 | import org.onosproject.net.group.Group; |
| 42 | import org.onosproject.net.group.Group.GroupState; |
| 43 | import org.onosproject.net.group.GroupBucket; |
| 44 | import org.onosproject.net.group.GroupBuckets; |
| 45 | import org.onosproject.net.group.GroupDescription; |
| 46 | import org.onosproject.net.group.GroupEvent; |
| 47 | import org.onosproject.net.group.GroupEvent.Type; |
| 48 | import org.onosproject.net.group.GroupKey; |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 49 | import org.onosproject.net.group.GroupOperation; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 50 | import org.onosproject.net.group.GroupStore; |
| 51 | import org.onosproject.net.group.GroupStoreDelegate; |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 52 | import org.onosproject.net.group.StoredGroupBucketEntry; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 53 | import org.onosproject.net.group.StoredGroupEntry; |
| 54 | import org.onosproject.store.AbstractStore; |
| 55 | import org.slf4j.Logger; |
| 56 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 57 | import com.google.common.collect.FluentIterable; |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 58 | import com.google.common.collect.Sets; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 59 | |
| 60 | /** |
| 61 | * Manages inventory of group entries using trivial in-memory implementation. |
| 62 | */ |
| 63 | @Component(immediate = true) |
| 64 | @Service |
| 65 | public class SimpleGroupStore |
| 66 | extends AbstractStore<GroupEvent, GroupStoreDelegate> |
| 67 | implements GroupStore { |
| 68 | |
| 69 | private final Logger log = getLogger(getClass()); |
| 70 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 71 | private final int dummyId = 0xffffffff; |
| 72 | private final GroupId dummyGroupId = new DefaultGroupId(dummyId); |
| 73 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 74 | // inner Map is per device group table |
| 75 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupKey, StoredGroupEntry>> |
| 76 | groupEntriesByKey = new ConcurrentHashMap<>(); |
| 77 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupId, StoredGroupEntry>> |
| 78 | groupEntriesById = new ConcurrentHashMap<>(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 79 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupKey, StoredGroupEntry>> |
| 80 | pendingGroupEntriesByKey = new ConcurrentHashMap<>(); |
| 81 | private final ConcurrentMap<DeviceId, ConcurrentMap<GroupId, Group>> |
| 82 | extraneousGroupEntriesById = new ConcurrentHashMap<>(); |
| 83 | |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 84 | private final HashMap<DeviceId, Boolean> deviceAuditStatus = new HashMap<>(); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 85 | |
| 86 | private final AtomicInteger groupIdGen = new AtomicInteger(); |
| 87 | |
| 88 | @Activate |
| 89 | public void activate() { |
| 90 | log.info("Started"); |
| 91 | } |
| 92 | |
| 93 | @Deactivate |
| 94 | public void deactivate() { |
| 95 | groupEntriesByKey.clear(); |
| 96 | groupEntriesById.clear(); |
| 97 | log.info("Stopped"); |
| 98 | } |
| 99 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 100 | /** |
| 101 | * Returns the group key table for specified device. |
| 102 | * |
| 103 | * @param deviceId identifier of the device |
| 104 | * @return Map representing group key table of given device. |
| 105 | */ |
| 106 | private ConcurrentMap<GroupKey, StoredGroupEntry> getGroupKeyTable(DeviceId deviceId) { |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 107 | return groupEntriesByKey.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>()); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Returns the group id table for specified device. |
| 112 | * |
| 113 | * @param deviceId identifier of the device |
| 114 | * @return Map representing group key table of given device. |
| 115 | */ |
| 116 | private ConcurrentMap<GroupId, StoredGroupEntry> getGroupIdTable(DeviceId deviceId) { |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 117 | return groupEntriesById.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>()); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | /** |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 121 | * Returns the pending group key table for specified device. |
| 122 | * |
| 123 | * @param deviceId identifier of the device |
| 124 | * @return Map representing group key table of given device. |
| 125 | */ |
| 126 | private ConcurrentMap<GroupKey, StoredGroupEntry> |
| 127 | getPendingGroupKeyTable(DeviceId deviceId) { |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 128 | return pendingGroupEntriesByKey.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Returns the extraneous group id table for specified device. |
| 133 | * |
| 134 | * @param deviceId identifier of the device |
| 135 | * @return Map representing group key table of given device. |
| 136 | */ |
| 137 | private ConcurrentMap<GroupId, Group> |
| 138 | getExtraneousGroupIdTable(DeviceId deviceId) { |
Yuta HIGUCHI | c2e6815 | 2016-08-16 13:47:36 -0700 | [diff] [blame] | 139 | return extraneousGroupEntriesById.computeIfAbsent(deviceId, k -> new ConcurrentHashMap<>()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | /** |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 143 | * Returns the number of groups for the specified device in the store. |
| 144 | * |
| 145 | * @return number of groups for the specified device |
| 146 | */ |
| 147 | @Override |
| 148 | public int getGroupCount(DeviceId deviceId) { |
| 149 | return (groupEntriesByKey.get(deviceId) != null) ? |
| 150 | groupEntriesByKey.get(deviceId).size() : 0; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Returns the groups associated with a device. |
| 155 | * |
| 156 | * @param deviceId the device ID |
| 157 | * |
| 158 | * @return the group entries |
| 159 | */ |
| 160 | @Override |
| 161 | public Iterable<Group> getGroups(DeviceId deviceId) { |
| 162 | // flatten and make iterator unmodifiable |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 163 | return FluentIterable.from(getGroupKeyTable(deviceId).values()) |
Sho SHIMIZU | 7462641 | 2015-09-11 11:46:27 -0700 | [diff] [blame] | 164 | .transform(input -> input); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns the stored group entry. |
| 169 | * |
| 170 | * @param deviceId the device ID |
| 171 | * @param appCookie the group key |
| 172 | * |
| 173 | * @return a group associated with the key |
| 174 | */ |
| 175 | @Override |
| 176 | public Group getGroup(DeviceId deviceId, GroupKey appCookie) { |
| 177 | return (groupEntriesByKey.get(deviceId) != null) ? |
| 178 | groupEntriesByKey.get(deviceId).get(appCookie) : |
| 179 | null; |
| 180 | } |
| 181 | |
Srikanth Vavilapalli | 717361f | 2015-03-16 12:06:04 -0700 | [diff] [blame] | 182 | @Override |
| 183 | public Group getGroup(DeviceId deviceId, GroupId groupId) { |
| 184 | return (groupEntriesById.get(deviceId) != null) ? |
| 185 | groupEntriesById.get(deviceId).get(groupId) : |
| 186 | null; |
| 187 | } |
| 188 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 189 | private int getFreeGroupIdValue(DeviceId deviceId) { |
| 190 | int freeId = groupIdGen.incrementAndGet(); |
| 191 | |
| 192 | while (true) { |
| 193 | Group existing = ( |
| 194 | groupEntriesById.get(deviceId) != null) ? |
| 195 | groupEntriesById.get(deviceId).get(new DefaultGroupId(freeId)) : |
| 196 | null; |
| 197 | if (existing == null) { |
| 198 | existing = ( |
| 199 | extraneousGroupEntriesById.get(deviceId) != null) ? |
| 200 | extraneousGroupEntriesById.get(deviceId). |
| 201 | get(new DefaultGroupId(freeId)) : |
| 202 | null; |
| 203 | } |
| 204 | if (existing != null) { |
| 205 | freeId = groupIdGen.incrementAndGet(); |
| 206 | } else { |
| 207 | break; |
| 208 | } |
| 209 | } |
| 210 | return freeId; |
| 211 | } |
| 212 | |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 213 | /** |
| 214 | * Stores a new group entry using the information from group description. |
| 215 | * |
| 216 | * @param groupDesc group description to be used to create group entry |
| 217 | */ |
| 218 | @Override |
| 219 | public void storeGroupDescription(GroupDescription groupDesc) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 220 | // Check if a group is existing with the same key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 221 | if (getGroup(groupDesc.deviceId(), groupDesc.appCookie()) != null) { |
| 222 | return; |
| 223 | } |
| 224 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 225 | if (deviceAuditStatus.get(groupDesc.deviceId()) == null) { |
| 226 | // Device group audit has not completed yet |
| 227 | // Add this group description to pending group key table |
| 228 | // Create a group entry object with Dummy Group ID |
| 229 | StoredGroupEntry group = new DefaultGroup(dummyGroupId, groupDesc); |
| 230 | group.setState(GroupState.WAITING_AUDIT_COMPLETE); |
| 231 | ConcurrentMap<GroupKey, StoredGroupEntry> pendingKeyTable = |
| 232 | getPendingGroupKeyTable(groupDesc.deviceId()); |
| 233 | pendingKeyTable.put(groupDesc.appCookie(), group); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | storeGroupDescriptionInternal(groupDesc); |
| 238 | } |
| 239 | |
| 240 | private void storeGroupDescriptionInternal(GroupDescription groupDesc) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 241 | // Check if a group is existing with the same key |
| 242 | if (getGroup(groupDesc.deviceId(), groupDesc.appCookie()) != null) { |
| 243 | return; |
| 244 | } |
| 245 | |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 246 | GroupId id = null; |
| 247 | if (groupDesc.givenGroupId() == null) { |
| 248 | // Get a new group identifier |
| 249 | id = new DefaultGroupId(getFreeGroupIdValue(groupDesc.deviceId())); |
| 250 | } else { |
| 251 | id = new DefaultGroupId(groupDesc.givenGroupId()); |
| 252 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 253 | // Create a group entry object |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 254 | StoredGroupEntry group = new DefaultGroup(id, groupDesc); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 255 | // Insert the newly created group entry into concurrent key and id maps |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 256 | ConcurrentMap<GroupKey, StoredGroupEntry> keyTable = |
| 257 | getGroupKeyTable(groupDesc.deviceId()); |
| 258 | keyTable.put(groupDesc.appCookie(), group); |
| 259 | ConcurrentMap<GroupId, StoredGroupEntry> idTable = |
| 260 | getGroupIdTable(groupDesc.deviceId()); |
| 261 | idTable.put(id, group); |
| 262 | notifyDelegate(new GroupEvent(GroupEvent.Type.GROUP_ADD_REQUESTED, |
| 263 | group)); |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * Updates the existing group entry with the information |
| 268 | * from group description. |
| 269 | * |
| 270 | * @param deviceId the device ID |
| 271 | * @param oldAppCookie the current group key |
| 272 | * @param type update type |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 273 | * @param newBuckets group buckets for updates |
| 274 | * @param newAppCookie optional new group key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 275 | */ |
| 276 | @Override |
| 277 | public void updateGroupDescription(DeviceId deviceId, |
| 278 | GroupKey oldAppCookie, |
| 279 | UpdateType type, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 280 | GroupBuckets newBuckets, |
| 281 | GroupKey newAppCookie) { |
| 282 | // Check if a group is existing with the provided key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 283 | Group oldGroup = getGroup(deviceId, oldAppCookie); |
| 284 | if (oldGroup == null) { |
| 285 | return; |
| 286 | } |
| 287 | |
| 288 | List<GroupBucket> newBucketList = getUpdatedBucketList(oldGroup, |
| 289 | type, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 290 | newBuckets); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 291 | if (newBucketList != null) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 292 | // Create a new group object from the old group |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 293 | GroupBuckets updatedBuckets = new GroupBuckets(newBucketList); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 294 | GroupKey newCookie = (newAppCookie != null) ? newAppCookie : oldAppCookie; |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 295 | GroupDescription updatedGroupDesc = new DefaultGroupDescription( |
| 296 | oldGroup.deviceId(), |
| 297 | oldGroup.type(), |
| 298 | updatedBuckets, |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 299 | newCookie, |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 300 | oldGroup.givenGroupId(), |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 301 | oldGroup.appId()); |
| 302 | StoredGroupEntry newGroup = new DefaultGroup(oldGroup.id(), |
| 303 | updatedGroupDesc); |
| 304 | newGroup.setState(GroupState.PENDING_UPDATE); |
| 305 | newGroup.setLife(oldGroup.life()); |
| 306 | newGroup.setPackets(oldGroup.packets()); |
| 307 | newGroup.setBytes(oldGroup.bytes()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 308 | // 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] | 309 | ConcurrentMap<GroupKey, StoredGroupEntry> keyTable = |
| 310 | getGroupKeyTable(oldGroup.deviceId()); |
| 311 | ConcurrentMap<GroupId, StoredGroupEntry> idTable = |
| 312 | getGroupIdTable(oldGroup.deviceId()); |
| 313 | keyTable.remove(oldGroup.appCookie()); |
| 314 | idTable.remove(oldGroup.id()); |
| 315 | keyTable.put(newGroup.appCookie(), newGroup); |
| 316 | idTable.put(newGroup.id(), newGroup); |
| 317 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_REQUESTED, newGroup)); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | private List<GroupBucket> getUpdatedBucketList(Group oldGroup, |
| 322 | UpdateType type, |
| 323 | GroupBuckets buckets) { |
| 324 | GroupBuckets oldBuckets = oldGroup.buckets(); |
Sho SHIMIZU | 7a4087b | 2015-09-10 09:23:16 -0700 | [diff] [blame] | 325 | List<GroupBucket> newBucketList = new ArrayList<>(oldBuckets.buckets()); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 326 | boolean groupDescUpdated = false; |
| 327 | |
| 328 | if (type == UpdateType.ADD) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 329 | // Check if the any of the new buckets are part of |
| 330 | // the old bucket list |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 331 | for (GroupBucket addBucket:buckets.buckets()) { |
| 332 | if (!newBucketList.contains(addBucket)) { |
| 333 | newBucketList.add(addBucket); |
| 334 | groupDescUpdated = true; |
| 335 | } |
| 336 | } |
| 337 | } else if (type == UpdateType.REMOVE) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 338 | // Check if the to be removed buckets are part of the |
| 339 | // old bucket list |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 340 | for (GroupBucket removeBucket:buckets.buckets()) { |
| 341 | if (newBucketList.contains(removeBucket)) { |
| 342 | newBucketList.remove(removeBucket); |
| 343 | groupDescUpdated = true; |
| 344 | } |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | if (groupDescUpdated) { |
| 349 | return newBucketList; |
| 350 | } else { |
| 351 | return null; |
| 352 | } |
| 353 | } |
| 354 | |
| 355 | /** |
| 356 | * Triggers deleting the existing group entry. |
| 357 | * |
| 358 | * @param deviceId the device ID |
| 359 | * @param appCookie the group key |
| 360 | */ |
| 361 | @Override |
| 362 | public void deleteGroupDescription(DeviceId deviceId, |
| 363 | GroupKey appCookie) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 364 | // Check if a group is existing with the provided key |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 365 | StoredGroupEntry existing = (groupEntriesByKey.get(deviceId) != null) ? |
| 366 | groupEntriesByKey.get(deviceId).get(appCookie) : |
| 367 | null; |
| 368 | if (existing == null) { |
| 369 | return; |
| 370 | } |
| 371 | |
| 372 | synchronized (existing) { |
| 373 | existing.setState(GroupState.PENDING_DELETE); |
| 374 | } |
| 375 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, existing)); |
| 376 | } |
| 377 | |
| 378 | /** |
| 379 | * Stores a new group entry, or updates an existing entry. |
| 380 | * |
| 381 | * @param group group entry |
| 382 | */ |
| 383 | @Override |
| 384 | public void addOrUpdateGroupEntry(Group group) { |
| 385 | // check if this new entry is an update to an existing entry |
| 386 | StoredGroupEntry existing = (groupEntriesById.get( |
| 387 | group.deviceId()) != null) ? |
| 388 | groupEntriesById.get(group.deviceId()).get(group.id()) : |
| 389 | null; |
| 390 | GroupEvent event = null; |
| 391 | |
| 392 | if (existing != null) { |
| 393 | synchronized (existing) { |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 394 | for (GroupBucket bucket:group.buckets().buckets()) { |
Sho SHIMIZU | 30d639b | 2015-05-05 09:30:35 -0700 | [diff] [blame] | 395 | Optional<GroupBucket> matchingBucket = |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 396 | existing.buckets().buckets() |
| 397 | .stream() |
| 398 | .filter((existingBucket)->(existingBucket.equals(bucket))) |
| 399 | .findFirst(); |
| 400 | if (matchingBucket.isPresent()) { |
| 401 | ((StoredGroupBucketEntry) matchingBucket. |
| 402 | get()).setPackets(bucket.packets()); |
| 403 | ((StoredGroupBucketEntry) matchingBucket. |
| 404 | get()).setBytes(bucket.bytes()); |
| 405 | } else { |
| 406 | log.warn("addOrUpdateGroupEntry: No matching " |
| 407 | + "buckets to update stats"); |
| 408 | } |
| 409 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 410 | existing.setLife(group.life()); |
| 411 | existing.setPackets(group.packets()); |
| 412 | existing.setBytes(group.bytes()); |
| 413 | if (existing.state() == GroupState.PENDING_ADD) { |
| 414 | existing.setState(GroupState.ADDED); |
| 415 | event = new GroupEvent(Type.GROUP_ADDED, existing); |
| 416 | } else { |
| 417 | if (existing.state() == GroupState.PENDING_UPDATE) { |
Srikanth Vavilapalli | 10e75cd | 2015-04-13 16:21:24 -0700 | [diff] [blame] | 418 | existing.setState(GroupState.ADDED); |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 419 | } |
| 420 | event = new GroupEvent(Type.GROUP_UPDATED, existing); |
| 421 | } |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | if (event != null) { |
| 426 | notifyDelegate(event); |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | /** |
| 431 | * Removes the group entry from store. |
| 432 | * |
| 433 | * @param group group entry |
| 434 | */ |
| 435 | @Override |
| 436 | public void removeGroupEntry(Group group) { |
| 437 | StoredGroupEntry existing = (groupEntriesById.get( |
| 438 | group.deviceId()) != null) ? |
| 439 | groupEntriesById.get(group.deviceId()).get(group.id()) : |
| 440 | null; |
| 441 | |
| 442 | if (existing != null) { |
| 443 | ConcurrentMap<GroupKey, StoredGroupEntry> keyTable = |
| 444 | getGroupKeyTable(existing.deviceId()); |
| 445 | ConcurrentMap<GroupId, StoredGroupEntry> idTable = |
| 446 | getGroupIdTable(existing.deviceId()); |
| 447 | idTable.remove(existing.id()); |
| 448 | keyTable.remove(existing.appCookie()); |
| 449 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVED, existing)); |
| 450 | } |
| 451 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 452 | |
| 453 | @Override |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 454 | public void purgeGroupEntry(DeviceId deviceId) { |
| 455 | Set<Map.Entry<GroupId, StoredGroupEntry>> entryPendingRemove = |
| 456 | groupEntriesById.get(deviceId).entrySet(); |
| 457 | |
| 458 | groupEntriesById.remove(deviceId); |
| 459 | groupEntriesByKey.remove(deviceId); |
| 460 | |
| 461 | entryPendingRemove.forEach(entry -> { |
| 462 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVED, entry.getValue())); |
| 463 | }); |
| 464 | } |
| 465 | |
| 466 | @Override |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 467 | public void deviceInitialAuditCompleted(DeviceId deviceId, |
| 468 | boolean completed) { |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 469 | synchronized (deviceAuditStatus) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 470 | if (completed) { |
| 471 | log.debug("deviceInitialAuditCompleted: AUDIT " |
| 472 | + "completed for device {}", deviceId); |
| 473 | deviceAuditStatus.put(deviceId, true); |
| 474 | // Execute all pending group requests |
| 475 | ConcurrentMap<GroupKey, StoredGroupEntry> pendingGroupRequests = |
| 476 | getPendingGroupKeyTable(deviceId); |
| 477 | for (Group group:pendingGroupRequests.values()) { |
| 478 | GroupDescription tmp = new DefaultGroupDescription( |
| 479 | group.deviceId(), |
| 480 | group.type(), |
| 481 | group.buckets(), |
| 482 | group.appCookie(), |
Saurav Das | 100e3b8 | 2015-04-30 11:12:10 -0700 | [diff] [blame] | 483 | group.givenGroupId(), |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 484 | group.appId()); |
| 485 | storeGroupDescriptionInternal(tmp); |
| 486 | } |
| 487 | getPendingGroupKeyTable(deviceId).clear(); |
| 488 | } else { |
| 489 | if (deviceAuditStatus.get(deviceId)) { |
| 490 | log.debug("deviceInitialAuditCompleted: Clearing AUDIT " |
| 491 | + "status for device {}", deviceId); |
| 492 | deviceAuditStatus.put(deviceId, false); |
| 493 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 494 | } |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 495 | } |
| 496 | } |
| 497 | |
| 498 | @Override |
| 499 | public boolean deviceInitialAuditStatus(DeviceId deviceId) { |
| 500 | synchronized (deviceAuditStatus) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 501 | return (deviceAuditStatus.get(deviceId) != null) |
| 502 | ? deviceAuditStatus.get(deviceId) : false; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
| 506 | @Override |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 507 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
| 508 | |
| 509 | StoredGroupEntry existing = (groupEntriesById.get( |
| 510 | deviceId) != null) ? |
| 511 | groupEntriesById.get(deviceId).get(operation.groupId()) : |
| 512 | null; |
| 513 | |
| 514 | if (existing == null) { |
| 515 | log.warn("No group entry with ID {} found ", operation.groupId()); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | switch (operation.opType()) { |
| 520 | case ADD: |
| 521 | notifyDelegate(new GroupEvent(Type.GROUP_ADD_FAILED, existing)); |
| 522 | break; |
| 523 | case MODIFY: |
| 524 | notifyDelegate(new GroupEvent(Type.GROUP_UPDATE_FAILED, existing)); |
| 525 | break; |
| 526 | case DELETE: |
| 527 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_FAILED, existing)); |
| 528 | break; |
| 529 | default: |
| 530 | log.warn("Unknown group operation type {}", operation.opType()); |
| 531 | } |
| 532 | |
| 533 | ConcurrentMap<GroupKey, StoredGroupEntry> keyTable = |
| 534 | getGroupKeyTable(existing.deviceId()); |
| 535 | ConcurrentMap<GroupId, StoredGroupEntry> idTable = |
| 536 | getGroupIdTable(existing.deviceId()); |
| 537 | idTable.remove(existing.id()); |
| 538 | keyTable.remove(existing.appCookie()); |
| 539 | } |
| 540 | |
| 541 | @Override |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 542 | public void addOrUpdateExtraneousGroupEntry(Group group) { |
| 543 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 544 | getExtraneousGroupIdTable(group.deviceId()); |
| 545 | extraneousIdTable.put(group.id(), group); |
| 546 | // Check the reference counter |
| 547 | if (group.referenceCount() == 0) { |
| 548 | notifyDelegate(new GroupEvent(Type.GROUP_REMOVE_REQUESTED, group)); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | @Override |
| 553 | public void removeExtraneousGroupEntry(Group group) { |
| 554 | ConcurrentMap<GroupId, Group> extraneousIdTable = |
| 555 | getExtraneousGroupIdTable(group.deviceId()); |
| 556 | extraneousIdTable.remove(group.id()); |
| 557 | } |
| 558 | |
| 559 | @Override |
| 560 | public Iterable<Group> getExtraneousGroups(DeviceId deviceId) { |
| 561 | // flatten and make iterator unmodifiable |
| 562 | return FluentIterable.from( |
| 563 | getExtraneousGroupIdTable(deviceId).values()); |
| 564 | } |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 565 | |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 566 | @Override |
| 567 | public void pushGroupMetrics(DeviceId deviceId, |
| 568 | Collection<Group> groupEntries) { |
| 569 | boolean deviceInitialAuditStatus = |
| 570 | deviceInitialAuditStatus(deviceId); |
| 571 | Set<Group> southboundGroupEntries = |
| 572 | Sets.newHashSet(groupEntries); |
| 573 | Set<Group> storedGroupEntries = |
| 574 | Sets.newHashSet(getGroups(deviceId)); |
| 575 | Set<Group> extraneousStoredEntries = |
| 576 | Sets.newHashSet(getExtraneousGroups(deviceId)); |
| 577 | |
| 578 | log.trace("pushGroupMetrics: Displaying all ({}) " |
| 579 | + "southboundGroupEntries for device {}", |
| 580 | southboundGroupEntries.size(), |
| 581 | deviceId); |
Sho SHIMIZU | 1578042 | 2016-08-12 15:59:20 -0700 | [diff] [blame] | 582 | for (Group group : southboundGroupEntries) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 583 | log.trace("Group {} in device {}", group, deviceId); |
| 584 | } |
| 585 | |
| 586 | log.trace("Displaying all ({}) stored group entries for device {}", |
| 587 | storedGroupEntries.size(), |
| 588 | deviceId); |
Sho SHIMIZU | 1578042 | 2016-08-12 15:59:20 -0700 | [diff] [blame] | 589 | for (Group group : storedGroupEntries) { |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 590 | log.trace("Stored Group {} for device {}", group, deviceId); |
| 591 | } |
| 592 | |
| 593 | for (Iterator<Group> it2 = southboundGroupEntries.iterator(); it2.hasNext();) { |
| 594 | Group group = it2.next(); |
| 595 | if (storedGroupEntries.remove(group)) { |
| 596 | // we both have the group, let's update some info then. |
| 597 | log.trace("Group AUDIT: group {} exists " |
| 598 | + "in both planes for device {}", |
| 599 | group.id(), deviceId); |
| 600 | groupAdded(group); |
| 601 | it2.remove(); |
| 602 | } |
| 603 | } |
| 604 | for (Group group : southboundGroupEntries) { |
| 605 | if (getGroup(group.deviceId(), group.id()) != null) { |
| 606 | // There is a group existing with the same id |
| 607 | // It is possible that group update is |
| 608 | // in progress while we got a stale info from switch |
| 609 | if (!storedGroupEntries.remove(getGroup( |
| 610 | group.deviceId(), group.id()))) { |
| 611 | log.warn("Group AUDIT: Inconsistent state:" |
| 612 | + "Group exists in ID based table while " |
| 613 | + "not present in key based table"); |
| 614 | } |
| 615 | } else { |
| 616 | // there are groups in the switch that aren't in the store |
| 617 | log.trace("Group AUDIT: extraneous group {} exists " |
| 618 | + "in data plane for device {}", |
| 619 | group.id(), deviceId); |
| 620 | extraneousStoredEntries.remove(group); |
| 621 | extraneousGroup(group); |
| 622 | } |
| 623 | } |
| 624 | for (Group group : storedGroupEntries) { |
| 625 | // there are groups in the store that aren't in the switch |
| 626 | log.trace("Group AUDIT: group {} missing " |
| 627 | + "in data plane for device {}", |
| 628 | group.id(), deviceId); |
| 629 | groupMissing(group); |
| 630 | } |
| 631 | for (Group group : extraneousStoredEntries) { |
| 632 | // there are groups in the extraneous store that |
| 633 | // aren't in the switch |
| 634 | log.trace("Group AUDIT: clearing extransoeus group {} " |
| 635 | + "from store for device {}", |
| 636 | group.id(), deviceId); |
| 637 | removeExtraneousGroupEntry(group); |
| 638 | } |
| 639 | |
| 640 | if (!deviceInitialAuditStatus) { |
| 641 | log.debug("Group AUDIT: Setting device {} initial " |
| 642 | + "AUDIT completed", deviceId); |
| 643 | deviceInitialAuditCompleted(deviceId, true); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | private void groupMissing(Group group) { |
| 648 | switch (group.state()) { |
| 649 | case PENDING_DELETE: |
| 650 | log.debug("Group {} delete confirmation from device {}", |
| 651 | group, group.deviceId()); |
| 652 | removeGroupEntry(group); |
| 653 | break; |
| 654 | case ADDED: |
| 655 | case PENDING_ADD: |
| 656 | case PENDING_UPDATE: |
| 657 | log.debug("Group {} is in store but not on device {}", |
| 658 | group, group.deviceId()); |
| 659 | StoredGroupEntry existing = (groupEntriesById.get( |
| 660 | group.deviceId()) != null) ? |
| 661 | groupEntriesById.get(group.deviceId()).get(group.id()) : |
| 662 | null; |
| 663 | log.trace("groupMissing: group " |
| 664 | + "entry {} in device {} moving " |
| 665 | + "from {} to PENDING_ADD", |
| 666 | existing.id(), |
| 667 | existing.deviceId(), |
| 668 | existing.state()); |
| 669 | existing.setState(Group.GroupState.PENDING_ADD); |
| 670 | notifyDelegate(new GroupEvent(GroupEvent.Type.GROUP_ADD_REQUESTED, |
| 671 | group)); |
| 672 | break; |
| 673 | default: |
| 674 | log.debug("Group {} has not been installed.", group); |
| 675 | break; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | private void extraneousGroup(Group group) { |
| 680 | log.debug("Group {} is on device {} but not in store.", |
| 681 | group, group.deviceId()); |
| 682 | addOrUpdateExtraneousGroupEntry(group); |
| 683 | } |
| 684 | |
| 685 | private void groupAdded(Group group) { |
| 686 | log.trace("Group {} Added or Updated in device {}", |
| 687 | group, group.deviceId()); |
| 688 | addOrUpdateGroupEntry(group); |
| 689 | } |
Srikanth Vavilapalli | 0599d51 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 690 | } |