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 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 18 | import org.apache.felix.scr.annotations.Activate; |
| 19 | import org.apache.felix.scr.annotations.Component; |
| 20 | import org.apache.felix.scr.annotations.Deactivate; |
| 21 | import org.apache.felix.scr.annotations.Reference; |
| 22 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 23 | import org.apache.felix.scr.annotations.Service; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 24 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 25 | import org.onosproject.core.ApplicationId; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 26 | import org.onosproject.core.Permission; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 27 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 28 | import org.onosproject.net.device.DeviceEvent; |
| 29 | import org.onosproject.net.device.DeviceListener; |
| 30 | import org.onosproject.net.device.DeviceService; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 31 | import org.onosproject.net.group.Group; |
| 32 | import org.onosproject.net.group.GroupBuckets; |
| 33 | import org.onosproject.net.group.GroupDescription; |
| 34 | import org.onosproject.net.group.GroupEvent; |
| 35 | import org.onosproject.net.group.GroupKey; |
| 36 | import org.onosproject.net.group.GroupListener; |
| 37 | import org.onosproject.net.group.GroupOperation; |
| 38 | import org.onosproject.net.group.GroupOperations; |
| 39 | import org.onosproject.net.group.GroupProvider; |
| 40 | import org.onosproject.net.group.GroupProviderRegistry; |
| 41 | import org.onosproject.net.group.GroupProviderService; |
| 42 | import org.onosproject.net.group.GroupService; |
| 43 | import org.onosproject.net.group.GroupStore; |
| 44 | import org.onosproject.net.group.GroupStore.UpdateType; |
| 45 | import org.onosproject.net.group.GroupStoreDelegate; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 46 | import org.onosproject.net.provider.AbstractProviderService; |
| 47 | import org.slf4j.Logger; |
| 48 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 49 | import java.util.Collection; |
| 50 | import java.util.Collections; |
| 51 | |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 52 | import static org.onosproject.security.AppGuard.checkPermission; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 53 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 54 | |
| 55 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Provides implementation of the group service APIs. |
| 58 | */ |
| 59 | @Component(immediate = true) |
| 60 | @Service |
| 61 | public class GroupManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 62 | extends AbstractListenerProviderRegistry<GroupEvent, GroupListener, |
| 63 | GroupProvider, GroupProviderService> |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 64 | implements GroupService, GroupProviderRegistry { |
| 65 | |
| 66 | private final Logger log = getLogger(getClass()); |
| 67 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 68 | private final GroupStoreDelegate delegate = new InternalGroupStoreDelegate(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 69 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 70 | |
| 71 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 72 | protected GroupStore store; |
| 73 | |
| 74 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 75 | protected DeviceService deviceService; |
| 76 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 77 | @Activate |
| 78 | public void activate() { |
| 79 | store.setDelegate(delegate); |
| 80 | eventDispatcher.addSink(GroupEvent.class, listenerRegistry); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 81 | deviceService.addListener(deviceListener); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 82 | log.info("Started"); |
| 83 | } |
| 84 | |
| 85 | @Deactivate |
| 86 | public void deactivate() { |
| 87 | store.unsetDelegate(delegate); |
| 88 | eventDispatcher.removeSink(GroupEvent.class); |
| 89 | log.info("Stopped"); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Create a group in the specified device with the provided parameters. |
| 94 | * |
| 95 | * @param groupDesc group creation parameters |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 96 | */ |
| 97 | @Override |
| 98 | public void addGroup(GroupDescription groupDesc) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 99 | checkPermission(Permission.GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 100 | store.storeGroupDescription(groupDesc); |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Return a group object associated to an application cookie. |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 105 | * <p> |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 106 | * NOTE1: The presence of group object in the system does not |
| 107 | * guarantee that the "group" is actually created in device. |
| 108 | * GROUP_ADDED notification would confirm the creation of |
| 109 | * this group in data plane. |
| 110 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 111 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 112 | * @param appCookie application cookie to be used for lookup |
| 113 | * @return group associated with the application cookie or |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 114 | * NULL if Group is not found for the provided cookie |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 115 | */ |
| 116 | @Override |
| 117 | public Group getGroup(DeviceId deviceId, GroupKey appCookie) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 118 | checkPermission(Permission.GROUP_READ); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 119 | return store.getGroup(deviceId, appCookie); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Append buckets to existing group. The caller can optionally |
| 124 | * associate a new cookie during this updation. GROUP_UPDATED or |
| 125 | * GROUP_UPDATE_FAILED notifications would be provided along with |
| 126 | * cookie depending on the result of the operation on the device. |
| 127 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 128 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 129 | * @param oldCookie cookie to be used to retrieve the existing group |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 130 | * @param buckets immutable list of group bucket to be added |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 131 | * @param newCookie immutable cookie to be used post update operation |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 132 | * @param appId Application Id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 133 | */ |
| 134 | @Override |
| 135 | public void addBucketsToGroup(DeviceId deviceId, |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 136 | GroupKey oldCookie, |
| 137 | GroupBuckets buckets, |
| 138 | GroupKey newCookie, |
| 139 | ApplicationId appId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 140 | checkPermission(Permission.GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 141 | store.updateGroupDescription(deviceId, |
| 142 | oldCookie, |
| 143 | UpdateType.ADD, |
| 144 | buckets, |
| 145 | newCookie); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Remove buckets from existing group. The caller can optionally |
| 150 | * associate a new cookie during this updation. GROUP_UPDATED or |
| 151 | * GROUP_UPDATE_FAILED notifications would be provided along with |
| 152 | * cookie depending on the result of the operation on the device. |
| 153 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 154 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 155 | * @param oldCookie cookie to be used to retrieve the existing group |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 156 | * @param buckets immutable list of group bucket to be removed |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 157 | * @param newCookie immutable cookie to be used post update operation |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 158 | * @param appId Application Id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 159 | */ |
| 160 | @Override |
| 161 | public void removeBucketsFromGroup(DeviceId deviceId, |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 162 | GroupKey oldCookie, |
| 163 | GroupBuckets buckets, |
| 164 | GroupKey newCookie, |
| 165 | ApplicationId appId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 166 | checkPermission(Permission.GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 167 | store.updateGroupDescription(deviceId, |
| 168 | oldCookie, |
| 169 | UpdateType.REMOVE, |
| 170 | buckets, |
| 171 | newCookie); |
| 172 | } |
| 173 | |
| 174 | /** |
| 175 | * Delete a group associated to an application cookie. |
| 176 | * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be |
| 177 | * provided along with cookie depending on the result of the |
| 178 | * operation on the device. |
| 179 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 180 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 181 | * @param appCookie application cookie to be used for lookup |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 182 | * @param appId Application Id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 183 | */ |
| 184 | @Override |
| 185 | public void removeGroup(DeviceId deviceId, |
| 186 | GroupKey appCookie, |
| 187 | ApplicationId appId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 188 | checkPermission(Permission.GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 189 | store.deleteGroupDescription(deviceId, appCookie); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Retrieve all groups created by an application in the specified device |
| 194 | * as seen by current controller instance. |
| 195 | * |
| 196 | * @param deviceId device identifier |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 197 | * @param appId application id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 198 | * @return collection of immutable group objects created by the application |
| 199 | */ |
| 200 | @Override |
| 201 | public Iterable<Group> getGroups(DeviceId deviceId, |
| 202 | ApplicationId appId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 203 | checkPermission(Permission.GROUP_READ); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 204 | return store.getGroups(deviceId); |
| 205 | } |
| 206 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 207 | @Override |
| 208 | public Iterable<Group> getGroups(DeviceId deviceId) { |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 209 | checkPermission(Permission.GROUP_READ); |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 210 | return store.getGroups(deviceId); |
| 211 | } |
| 212 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 213 | @Override |
| 214 | protected GroupProviderService createProviderService(GroupProvider provider) { |
| 215 | return new InternalGroupProviderService(provider); |
| 216 | } |
| 217 | |
| 218 | private class InternalGroupStoreDelegate implements GroupStoreDelegate { |
| 219 | @Override |
| 220 | public void notify(GroupEvent event) { |
| 221 | final Group group = event.subject(); |
| 222 | GroupProvider groupProvider = |
| 223 | getProvider(group.deviceId()); |
| 224 | GroupOperations groupOps = null; |
| 225 | switch (event.type()) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 226 | case GROUP_ADD_REQUESTED: |
| 227 | log.debug("GROUP_ADD_REQUESTED for Group {} on device {}", |
| 228 | group.id(), group.deviceId()); |
| 229 | GroupOperation groupAddOp = GroupOperation. |
| 230 | createAddGroupOperation(group.id(), |
| 231 | group.type(), |
| 232 | group.buckets()); |
| 233 | groupOps = new GroupOperations( |
| 234 | Collections.singletonList(groupAddOp)); |
| 235 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 236 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 237 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 238 | case GROUP_UPDATE_REQUESTED: |
| 239 | log.debug("GROUP_UPDATE_REQUESTED for Group {} on device {}", |
| 240 | group.id(), group.deviceId()); |
| 241 | GroupOperation groupModifyOp = GroupOperation. |
| 242 | createModifyGroupOperation(group.id(), |
| 243 | group.type(), |
| 244 | group.buckets()); |
| 245 | groupOps = new GroupOperations( |
| 246 | Collections.singletonList(groupModifyOp)); |
| 247 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 248 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 249 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 250 | case GROUP_REMOVE_REQUESTED: |
| 251 | log.debug("GROUP_REMOVE_REQUESTED for Group {} on device {}", |
| 252 | group.id(), group.deviceId()); |
| 253 | GroupOperation groupDeleteOp = GroupOperation. |
| 254 | createDeleteGroupOperation(group.id(), |
| 255 | group.type()); |
| 256 | groupOps = new GroupOperations( |
| 257 | Collections.singletonList(groupDeleteOp)); |
| 258 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 259 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 260 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 261 | case GROUP_ADDED: |
| 262 | case GROUP_UPDATED: |
| 263 | case GROUP_REMOVED: |
| 264 | case GROUP_ADD_FAILED: |
| 265 | case GROUP_UPDATE_FAILED: |
| 266 | case GROUP_REMOVE_FAILED: |
| 267 | post(event); |
| 268 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 269 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 270 | default: |
| 271 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | private class InternalGroupProviderService |
| 277 | extends AbstractProviderService<GroupProvider> |
| 278 | implements GroupProviderService { |
| 279 | |
| 280 | protected InternalGroupProviderService(GroupProvider provider) { |
| 281 | super(provider); |
| 282 | } |
| 283 | |
| 284 | @Override |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 285 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 286 | store.groupOperationFailed(deviceId, operation); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 287 | } |
| 288 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 289 | @Override |
| 290 | public void pushGroupMetrics(DeviceId deviceId, |
| 291 | Collection<Group> groupEntries) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 292 | log.trace("Received group metrics from device {}", deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 293 | checkValidity(); |
| 294 | store.pushGroupMetrics(deviceId, groupEntries); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 295 | } |
| 296 | } |
| 297 | |
| 298 | private class InternalDeviceListener implements DeviceListener { |
| 299 | |
| 300 | @Override |
| 301 | public void event(DeviceEvent event) { |
| 302 | switch (event.type()) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 303 | case DEVICE_REMOVED: |
| 304 | case DEVICE_AVAILABILITY_CHANGED: |
| 305 | if (!deviceService.isAvailable(event.subject().id())) { |
| 306 | log.debug("Device {} became un available; clearing initial audit status", |
| 307 | event.type(), event.subject().id()); |
| 308 | store.deviceInitialAuditCompleted(event.subject().id(), false); |
| 309 | } |
| 310 | break; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 311 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 312 | default: |
| 313 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 314 | } |
| 315 | } |
| 316 | } |
| 317 | } |