Srikanth Vavilapalli | 45c27c8 | 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.net.group.impl; |
| 17 | |
| 18 | import static org.slf4j.LoggerFactory.getLogger; |
| 19 | |
| 20 | import java.util.Arrays; |
| 21 | import java.util.Collection; |
| 22 | import java.util.Iterator; |
| 23 | import java.util.Set; |
| 24 | |
| 25 | import org.apache.felix.scr.annotations.Activate; |
| 26 | import org.apache.felix.scr.annotations.Component; |
| 27 | import org.apache.felix.scr.annotations.Deactivate; |
| 28 | import org.apache.felix.scr.annotations.Reference; |
| 29 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 30 | import org.apache.felix.scr.annotations.Service; |
| 31 | import org.onosproject.core.ApplicationId; |
| 32 | import org.onosproject.event.AbstractListenerRegistry; |
| 33 | import org.onosproject.event.EventDeliveryService; |
| 34 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 35 | import org.onosproject.net.device.DeviceEvent; |
| 36 | import org.onosproject.net.device.DeviceListener; |
| 37 | import org.onosproject.net.device.DeviceService; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 38 | import org.onosproject.net.group.Group; |
| 39 | import org.onosproject.net.group.GroupBuckets; |
| 40 | import org.onosproject.net.group.GroupDescription; |
| 41 | import org.onosproject.net.group.GroupEvent; |
| 42 | import org.onosproject.net.group.GroupKey; |
| 43 | import org.onosproject.net.group.GroupListener; |
| 44 | import org.onosproject.net.group.GroupOperation; |
| 45 | import org.onosproject.net.group.GroupOperations; |
| 46 | import org.onosproject.net.group.GroupProvider; |
| 47 | import org.onosproject.net.group.GroupProviderRegistry; |
| 48 | import org.onosproject.net.group.GroupProviderService; |
| 49 | import org.onosproject.net.group.GroupService; |
| 50 | import org.onosproject.net.group.GroupStore; |
| 51 | import org.onosproject.net.group.GroupStore.UpdateType; |
| 52 | import org.onosproject.net.group.GroupStoreDelegate; |
| 53 | import org.onosproject.net.provider.AbstractProviderRegistry; |
| 54 | import org.onosproject.net.provider.AbstractProviderService; |
| 55 | import org.slf4j.Logger; |
| 56 | |
| 57 | import com.google.common.collect.Sets; |
| 58 | |
| 59 | /** |
| 60 | * Provides implementation of the group service APIs. |
| 61 | */ |
| 62 | @Component(immediate = true) |
| 63 | @Service |
| 64 | public class GroupManager |
| 65 | extends AbstractProviderRegistry<GroupProvider, GroupProviderService> |
| 66 | implements GroupService, GroupProviderRegistry { |
| 67 | |
| 68 | private final Logger log = getLogger(getClass()); |
| 69 | |
| 70 | private final AbstractListenerRegistry<GroupEvent, GroupListener> |
| 71 | listenerRegistry = new AbstractListenerRegistry<>(); |
| 72 | private final GroupStoreDelegate delegate = new InternalGroupStoreDelegate(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 73 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 74 | |
| 75 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 76 | protected GroupStore store; |
| 77 | |
| 78 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 79 | protected DeviceService deviceService; |
| 80 | |
| 81 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 82 | protected EventDeliveryService eventDispatcher; |
| 83 | |
| 84 | @Activate |
| 85 | public void activate() { |
| 86 | store.setDelegate(delegate); |
| 87 | eventDispatcher.addSink(GroupEvent.class, listenerRegistry); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 88 | deviceService.addListener(deviceListener); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 89 | log.info("Started"); |
| 90 | } |
| 91 | |
| 92 | @Deactivate |
| 93 | public void deactivate() { |
| 94 | store.unsetDelegate(delegate); |
| 95 | eventDispatcher.removeSink(GroupEvent.class); |
| 96 | log.info("Stopped"); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Create a group in the specified device with the provided parameters. |
| 101 | * |
| 102 | * @param groupDesc group creation parameters |
| 103 | * |
| 104 | */ |
| 105 | @Override |
| 106 | public void addGroup(GroupDescription groupDesc) { |
| 107 | store.storeGroupDescription(groupDesc); |
| 108 | } |
| 109 | |
| 110 | /** |
| 111 | * Return a group object associated to an application cookie. |
| 112 | * |
| 113 | * NOTE1: The presence of group object in the system does not |
| 114 | * guarantee that the "group" is actually created in device. |
| 115 | * GROUP_ADDED notification would confirm the creation of |
| 116 | * this group in data plane. |
| 117 | * |
| 118 | * @param deviceId device identifier |
| 119 | * @param appCookie application cookie to be used for lookup |
| 120 | * @return group associated with the application cookie or |
| 121 | * NULL if Group is not found for the provided cookie |
| 122 | */ |
| 123 | @Override |
| 124 | public Group getGroup(DeviceId deviceId, GroupKey appCookie) { |
| 125 | return store.getGroup(deviceId, appCookie); |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Append buckets to existing group. The caller can optionally |
| 130 | * associate a new cookie during this updation. GROUP_UPDATED or |
| 131 | * GROUP_UPDATE_FAILED notifications would be provided along with |
| 132 | * cookie depending on the result of the operation on the device. |
| 133 | * |
| 134 | * @param deviceId device identifier |
| 135 | * @param oldCookie cookie to be used to retrieve the existing group |
| 136 | * @param buckets immutable list of group bucket to be added |
| 137 | * @param newCookie immutable cookie to be used post update operation |
| 138 | * @param appId Application Id |
| 139 | */ |
| 140 | @Override |
| 141 | public void addBucketsToGroup(DeviceId deviceId, |
| 142 | GroupKey oldCookie, |
| 143 | GroupBuckets buckets, |
| 144 | GroupKey newCookie, |
| 145 | ApplicationId appId) { |
| 146 | store.updateGroupDescription(deviceId, |
| 147 | oldCookie, |
| 148 | UpdateType.ADD, |
| 149 | buckets, |
| 150 | newCookie); |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Remove buckets from existing group. The caller can optionally |
| 155 | * associate a new cookie during this updation. GROUP_UPDATED or |
| 156 | * GROUP_UPDATE_FAILED notifications would be provided along with |
| 157 | * cookie depending on the result of the operation on the device. |
| 158 | * |
| 159 | * @param deviceId device identifier |
| 160 | * @param oldCookie cookie to be used to retrieve the existing group |
| 161 | * @param buckets immutable list of group bucket to be removed |
| 162 | * @param newCookie immutable cookie to be used post update operation |
| 163 | * @param appId Application Id |
| 164 | */ |
| 165 | @Override |
| 166 | public void removeBucketsFromGroup(DeviceId deviceId, |
| 167 | GroupKey oldCookie, |
| 168 | GroupBuckets buckets, |
| 169 | GroupKey newCookie, |
| 170 | ApplicationId appId) { |
| 171 | store.updateGroupDescription(deviceId, |
| 172 | oldCookie, |
| 173 | UpdateType.REMOVE, |
| 174 | buckets, |
| 175 | newCookie); |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Delete a group associated to an application cookie. |
| 180 | * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be |
| 181 | * provided along with cookie depending on the result of the |
| 182 | * operation on the device. |
| 183 | * |
| 184 | * @param deviceId device identifier |
| 185 | * @param appCookie application cookie to be used for lookup |
| 186 | * @param appId Application Id |
| 187 | */ |
| 188 | @Override |
| 189 | public void removeGroup(DeviceId deviceId, |
| 190 | GroupKey appCookie, |
| 191 | ApplicationId appId) { |
| 192 | store.deleteGroupDescription(deviceId, appCookie); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Retrieve all groups created by an application in the specified device |
| 197 | * as seen by current controller instance. |
| 198 | * |
| 199 | * @param deviceId device identifier |
| 200 | * @param appId application id |
| 201 | * @return collection of immutable group objects created by the application |
| 202 | */ |
| 203 | @Override |
| 204 | public Iterable<Group> getGroups(DeviceId deviceId, |
| 205 | ApplicationId appId) { |
| 206 | return store.getGroups(deviceId); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Adds the specified group listener. |
| 211 | * |
| 212 | * @param listener group listener |
| 213 | */ |
| 214 | @Override |
| 215 | public void addListener(GroupListener listener) { |
| 216 | listenerRegistry.addListener(listener); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Removes the specified group listener. |
| 221 | * |
| 222 | * @param listener group listener |
| 223 | */ |
| 224 | @Override |
| 225 | public void removeListener(GroupListener listener) { |
| 226 | listenerRegistry.removeListener(listener); |
| 227 | } |
| 228 | |
| 229 | @Override |
| 230 | protected GroupProviderService createProviderService(GroupProvider provider) { |
| 231 | return new InternalGroupProviderService(provider); |
| 232 | } |
| 233 | |
| 234 | private class InternalGroupStoreDelegate implements GroupStoreDelegate { |
| 235 | @Override |
| 236 | public void notify(GroupEvent event) { |
| 237 | final Group group = event.subject(); |
| 238 | GroupProvider groupProvider = |
| 239 | getProvider(group.deviceId()); |
| 240 | GroupOperations groupOps = null; |
| 241 | switch (event.type()) { |
| 242 | case GROUP_ADD_REQUESTED: |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 243 | log.debug("GROUP_ADD_REQUESTED for Group {} on device {}", |
| 244 | group.id(), group.deviceId()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 245 | GroupOperation groupAddOp = GroupOperation. |
| 246 | createAddGroupOperation(group.id(), |
| 247 | group.type(), |
| 248 | group.buckets()); |
| 249 | groupOps = new GroupOperations( |
| 250 | Arrays.asList(groupAddOp)); |
| 251 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 252 | break; |
| 253 | |
| 254 | case GROUP_UPDATE_REQUESTED: |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 255 | log.debug("GROUP_UPDATE_REQUESTED for Group {} on device {}", |
| 256 | group.id(), group.deviceId()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 257 | GroupOperation groupModifyOp = GroupOperation. |
| 258 | createModifyGroupOperation(group.id(), |
| 259 | group.type(), |
| 260 | group.buckets()); |
| 261 | groupOps = new GroupOperations( |
| 262 | Arrays.asList(groupModifyOp)); |
| 263 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 264 | break; |
| 265 | |
| 266 | case GROUP_REMOVE_REQUESTED: |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 267 | log.debug("GROUP_REMOVE_REQUESTED for Group {} on device {}", |
| 268 | group.id(), group.deviceId()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 269 | GroupOperation groupDeleteOp = GroupOperation. |
| 270 | createDeleteGroupOperation(group.id(), |
| 271 | group.type()); |
| 272 | groupOps = new GroupOperations( |
| 273 | Arrays.asList(groupDeleteOp)); |
| 274 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 275 | break; |
| 276 | |
| 277 | case GROUP_ADDED: |
| 278 | case GROUP_UPDATED: |
| 279 | case GROUP_REMOVED: |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 280 | case GROUP_ADD_FAILED: |
| 281 | case GROUP_UPDATE_FAILED: |
| 282 | case GROUP_REMOVE_FAILED: |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 283 | eventDispatcher.post(event); |
| 284 | break; |
| 285 | |
| 286 | default: |
| 287 | break; |
| 288 | } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | private class InternalGroupProviderService |
| 293 | extends AbstractProviderService<GroupProvider> |
| 294 | implements GroupProviderService { |
| 295 | |
| 296 | protected InternalGroupProviderService(GroupProvider provider) { |
| 297 | super(provider); |
| 298 | } |
| 299 | |
| 300 | @Override |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 301 | public void groupOperationFailed(DeviceId deviceId, |
| 302 | GroupOperation operation) { |
| 303 | store.groupOperationFailed(deviceId, operation); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | private void groupMissing(Group group) { |
| 307 | checkValidity(); |
| 308 | GroupProvider gp = getProvider(group.deviceId()); |
| 309 | switch (group.state()) { |
| 310 | case PENDING_DELETE: |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 311 | log.debug("Group {} delete confirmation from device {}", |
| 312 | group, group.deviceId()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 313 | store.removeGroupEntry(group); |
| 314 | break; |
| 315 | case ADDED: |
| 316 | case PENDING_ADD: |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 317 | log.debug("Group {} is in store but not on device {}", |
| 318 | group, group.deviceId()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 319 | GroupOperation groupAddOp = GroupOperation. |
| 320 | createAddGroupOperation(group.id(), |
| 321 | group.type(), |
| 322 | group.buckets()); |
| 323 | GroupOperations groupOps = new GroupOperations( |
| 324 | Arrays.asList(groupAddOp)); |
| 325 | gp.performGroupOperation(group.deviceId(), groupOps); |
| 326 | break; |
| 327 | default: |
| 328 | log.debug("Group {} has not been installed.", group); |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | |
| 333 | |
| 334 | private void extraneousGroup(Group group) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 335 | log.debug("Group {} is on device {} but not in store.", |
| 336 | group, group.deviceId()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 337 | checkValidity(); |
| 338 | store.addOrUpdateExtraneousGroupEntry(group); |
| 339 | } |
| 340 | |
| 341 | private void groupAdded(Group group) { |
| 342 | checkValidity(); |
| 343 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 344 | log.trace("Group {} Added or Updated in device {}", |
| 345 | group, group.deviceId()); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 346 | store.addOrUpdateGroupEntry(group); |
| 347 | } |
| 348 | |
| 349 | @Override |
| 350 | public void pushGroupMetrics(DeviceId deviceId, |
| 351 | Collection<Group> groupEntries) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 352 | log.trace("Received group metrics from device {}", |
| 353 | deviceId); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 354 | boolean deviceInitialAuditStatus = |
| 355 | store.deviceInitialAuditStatus(deviceId); |
| 356 | Set<Group> southboundGroupEntries = |
| 357 | Sets.newHashSet(groupEntries); |
| 358 | Set<Group> storedGroupEntries = |
| 359 | Sets.newHashSet(store.getGroups(deviceId)); |
| 360 | Set<Group> extraneousStoredEntries = |
| 361 | Sets.newHashSet(store.getExtraneousGroups(deviceId)); |
| 362 | |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 363 | log.trace("Displaying all southboundGroupEntries for device {}", deviceId); |
| 364 | for (Iterator<Group> it = southboundGroupEntries.iterator(); it.hasNext();) { |
| 365 | Group group = it.next(); |
| 366 | log.trace("Group {} in device {}", group, deviceId); |
| 367 | } |
| 368 | |
| 369 | log.trace("Displaying all stored group entries for device {}", deviceId); |
| 370 | for (Iterator<Group> it = storedGroupEntries.iterator(); it.hasNext();) { |
| 371 | Group group = it.next(); |
| 372 | log.trace("Stored Group {} for device {}", group, deviceId); |
| 373 | } |
| 374 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 375 | for (Iterator<Group> it = southboundGroupEntries.iterator(); it.hasNext();) { |
| 376 | Group group = it.next(); |
| 377 | if (storedGroupEntries.remove(group)) { |
| 378 | // we both have the group, let's update some info then. |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 379 | log.trace("Group AUDIT: group {} exists " |
| 380 | + "in both planes for device {}", |
| 381 | group.id(), deviceId); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 382 | groupAdded(group); |
| 383 | it.remove(); |
| 384 | } |
| 385 | } |
| 386 | for (Group group : southboundGroupEntries) { |
| 387 | // there are groups in the switch that aren't in the store |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 388 | log.trace("Group AUDIT: extraneous group {} exists " |
| 389 | + "in data plane for device {}", |
| 390 | group.id(), deviceId); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 391 | extraneousStoredEntries.remove(group); |
| 392 | extraneousGroup(group); |
| 393 | } |
| 394 | for (Group group : storedGroupEntries) { |
| 395 | // there are groups in the store that aren't in the switch |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 396 | log.trace("Group AUDIT: group {} missing " |
| 397 | + "in data plane for device {}", |
| 398 | group.id(), deviceId); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 399 | groupMissing(group); |
| 400 | } |
| 401 | for (Group group : extraneousStoredEntries) { |
| 402 | // there are groups in the extraneous store that |
| 403 | // aren't in the switch |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 404 | log.trace("Group AUDIT: clearing extransoeus group {} " |
| 405 | + "from store for device {}", |
| 406 | group.id(), deviceId); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 407 | store.removeExtraneousGroupEntry(group); |
| 408 | } |
| 409 | |
| 410 | if (!deviceInitialAuditStatus) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame^] | 411 | log.debug("Group AUDIT: Setting device {} initial " |
| 412 | + "AUDIT completed", deviceId); |
| 413 | store.deviceInitialAuditCompleted(deviceId, true); |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | private class InternalDeviceListener implements DeviceListener { |
| 419 | |
| 420 | @Override |
| 421 | public void event(DeviceEvent event) { |
| 422 | switch (event.type()) { |
| 423 | case DEVICE_REMOVED: |
| 424 | log.debug("Clearing device {} initial " |
| 425 | + "AUDIT completed status as device is going down", |
| 426 | event.subject().id()); |
| 427 | store.deviceInitialAuditCompleted(event.subject().id(), false); |
| 428 | break; |
| 429 | |
| 430 | default: |
| 431 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | } |
| 435 | } |