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