Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
Srikanth Vavilapalli | 45c27c8 | 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 | */ |
| 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; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 21 | import org.apache.felix.scr.annotations.Modified; |
| 22 | import org.apache.felix.scr.annotations.Property; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 23 | import org.apache.felix.scr.annotations.Reference; |
| 24 | import org.apache.felix.scr.annotations.ReferenceCardinality; |
| 25 | import org.apache.felix.scr.annotations.Service; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 26 | import org.onlab.util.Tools; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 27 | import org.onosproject.cfg.ComponentConfigService; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 28 | import org.onosproject.core.ApplicationId; |
Andrea Campanella | 1ea1510 | 2017-09-04 16:00:09 +0200 | [diff] [blame] | 29 | import org.onosproject.mastership.MastershipService; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 30 | import org.onosproject.net.DeviceId; |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 31 | import org.onosproject.net.device.DeviceEvent; |
| 32 | import org.onosproject.net.device.DeviceListener; |
| 33 | import org.onosproject.net.device.DeviceService; |
Jordan Halterman | 59afc6a | 2017-10-30 16:12:42 -0700 | [diff] [blame] | 34 | import org.onosproject.net.driver.DriverService; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 35 | import org.onosproject.net.group.Group; |
| 36 | import org.onosproject.net.group.GroupBuckets; |
| 37 | import org.onosproject.net.group.GroupDescription; |
| 38 | import org.onosproject.net.group.GroupEvent; |
| 39 | import org.onosproject.net.group.GroupKey; |
| 40 | import org.onosproject.net.group.GroupListener; |
| 41 | import org.onosproject.net.group.GroupOperation; |
| 42 | import org.onosproject.net.group.GroupOperations; |
| 43 | import org.onosproject.net.group.GroupProvider; |
| 44 | import org.onosproject.net.group.GroupProviderRegistry; |
| 45 | import org.onosproject.net.group.GroupProviderService; |
| 46 | import org.onosproject.net.group.GroupService; |
| 47 | import org.onosproject.net.group.GroupStore; |
| 48 | import org.onosproject.net.group.GroupStore.UpdateType; |
| 49 | import org.onosproject.net.group.GroupStoreDelegate; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 50 | import org.onosproject.net.provider.AbstractListenerProviderRegistry; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 51 | import org.onosproject.net.provider.AbstractProviderService; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 52 | import org.osgi.service.component.ComponentContext; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 53 | import org.slf4j.Logger; |
| 54 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 55 | import java.util.Collection; |
| 56 | import java.util.Collections; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 57 | import java.util.Dictionary; |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 58 | import java.util.concurrent.ExecutorService; |
| 59 | import java.util.concurrent.Executors; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 60 | |
Andrea Campanella | 1ea1510 | 2017-09-04 16:00:09 +0200 | [diff] [blame] | 61 | import static com.google.common.base.Strings.isNullOrEmpty; |
| 62 | import static org.onlab.util.Tools.get; |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 63 | import static org.onlab.util.Tools.groupedThreads; |
Changhoon Yoon | 541ef71 | 2015-05-23 17:18:34 +0900 | [diff] [blame] | 64 | import static org.onosproject.security.AppGuard.checkPermission; |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 65 | import static org.onosproject.security.AppPermission.Type.GROUP_READ; |
| 66 | import static org.onosproject.security.AppPermission.Type.GROUP_WRITE; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 67 | import static org.slf4j.LoggerFactory.getLogger; |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 68 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 69 | /** |
| 70 | * Provides implementation of the group service APIs. |
| 71 | */ |
| 72 | @Component(immediate = true) |
| 73 | @Service |
| 74 | public class GroupManager |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 75 | extends AbstractListenerProviderRegistry<GroupEvent, GroupListener, |
| 76 | GroupProvider, GroupProviderService> |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 77 | implements GroupService, GroupProviderRegistry { |
| 78 | |
| 79 | private final Logger log = getLogger(getClass()); |
| 80 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 81 | private final GroupStoreDelegate delegate = new InternalGroupStoreDelegate(); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 82 | private final DeviceListener deviceListener = new InternalDeviceListener(); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 83 | |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 84 | private final GroupDriverProvider defaultProvider = new GroupDriverProvider(); |
| 85 | |
| 86 | private ExecutorService eventExecutor; |
Andrea Campanella | 1ea1510 | 2017-09-04 16:00:09 +0200 | [diff] [blame] | 87 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 88 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 89 | protected GroupStore store; |
| 90 | |
| 91 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 92 | protected DeviceService deviceService; |
| 93 | |
Jordan Halterman | 59afc6a | 2017-10-30 16:12:42 -0700 | [diff] [blame] | 94 | // Reference the DriverService to ensure the service is bound prior to initialization of the GroupDriverProvider |
| 95 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 96 | protected DriverService driverService; |
| 97 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 98 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 99 | protected ComponentConfigService cfgService; |
| 100 | |
Andrea Campanella | 1ea1510 | 2017-09-04 16:00:09 +0200 | [diff] [blame] | 101 | @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY) |
| 102 | protected MastershipService mastershipService; |
| 103 | |
| 104 | private static final int DEFAULT_POLL_FREQUENCY = 30; |
| 105 | @Property(name = "fallbackGroupPollFrequency", intValue = DEFAULT_POLL_FREQUENCY, |
| 106 | label = "Frequency (in seconds) for polling groups via fallback provider") |
| 107 | private int fallbackGroupPollFrequency = DEFAULT_POLL_FREQUENCY; |
| 108 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 109 | @Property(name = "purgeOnDisconnection", boolValue = false, |
| 110 | label = "Purge entries associated with a device when the device goes offline") |
| 111 | private boolean purgeOnDisconnection = false; |
Andrea Campanella | 1ea1510 | 2017-09-04 16:00:09 +0200 | [diff] [blame] | 112 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 113 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 114 | @Activate |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 115 | public void activate(ComponentContext context) { |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 116 | eventExecutor = Executors.newSingleThreadExecutor(groupedThreads("onos/group", "event")); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 117 | store.setDelegate(delegate); |
| 118 | eventDispatcher.addSink(GroupEvent.class, listenerRegistry); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 119 | deviceService.addListener(deviceListener); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 120 | cfgService.registerProperties(getClass()); |
| 121 | modified(context); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 122 | log.info("Started"); |
| 123 | } |
| 124 | |
| 125 | @Deactivate |
| 126 | public void deactivate() { |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 127 | eventExecutor.shutdown(); |
Andrea Campanella | 5a3c09c | 2017-12-01 13:57:48 +0100 | [diff] [blame] | 128 | defaultProvider.terminate(); |
Andrea Campanella | 3f1c61e | 2016-04-01 17:30:12 -0700 | [diff] [blame] | 129 | deviceService.removeListener(deviceListener); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 130 | cfgService.unregisterProperties(getClass(), false); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 131 | store.unsetDelegate(delegate); |
| 132 | eventDispatcher.removeSink(GroupEvent.class); |
| 133 | log.info("Stopped"); |
| 134 | } |
| 135 | |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 136 | @Modified |
| 137 | public void modified(ComponentContext context) { |
| 138 | if (context != null) { |
| 139 | readComponentConfiguration(context); |
| 140 | } |
Andrea Campanella | 1ea1510 | 2017-09-04 16:00:09 +0200 | [diff] [blame] | 141 | defaultProvider.init(deviceService, new InternalGroupProviderService(defaultProvider), |
| 142 | mastershipService, fallbackGroupPollFrequency); |
Andrea Campanella | 6ee7392 | 2016-02-03 18:00:00 -0800 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | @Override |
| 146 | protected GroupProvider defaultProvider() { |
| 147 | return defaultProvider; |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Extracts properties from the component configuration context. |
| 152 | * |
| 153 | * @param context the component context |
| 154 | */ |
| 155 | private void readComponentConfiguration(ComponentContext context) { |
| 156 | Dictionary<?, ?> properties = context.getProperties(); |
| 157 | Boolean flag; |
| 158 | |
Jian Li | d9b5f55 | 2016-03-11 18:15:31 -0800 | [diff] [blame] | 159 | flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection"); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 160 | if (flag == null) { |
| 161 | log.info("PurgeOnDisconnection is not configured, " + |
| 162 | "using current value of {}", purgeOnDisconnection); |
| 163 | } else { |
| 164 | purgeOnDisconnection = flag; |
| 165 | log.info("Configured. PurgeOnDisconnection is {}", |
| 166 | purgeOnDisconnection ? "enabled" : "disabled"); |
| 167 | } |
Andrea Campanella | 1ea1510 | 2017-09-04 16:00:09 +0200 | [diff] [blame] | 168 | String s = get(properties, "fallbackGroupPollFrequency"); |
| 169 | try { |
| 170 | fallbackGroupPollFrequency = isNullOrEmpty(s) ? DEFAULT_POLL_FREQUENCY : Integer.parseInt(s); |
| 171 | } catch (NumberFormatException e) { |
| 172 | fallbackGroupPollFrequency = DEFAULT_POLL_FREQUENCY; |
| 173 | } |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 177 | * Create a group in the specified device with the provided parameters. |
| 178 | * |
| 179 | * @param groupDesc group creation parameters |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 180 | */ |
| 181 | @Override |
| 182 | public void addGroup(GroupDescription groupDesc) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 183 | checkPermission(GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 184 | store.storeGroupDescription(groupDesc); |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * Return a group object associated to an application cookie. |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 189 | * <p> |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 190 | * NOTE1: The presence of group object in the system does not |
| 191 | * guarantee that the "group" is actually created in device. |
| 192 | * GROUP_ADDED notification would confirm the creation of |
| 193 | * this group in data plane. |
| 194 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 195 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 196 | * @param appCookie application cookie to be used for lookup |
| 197 | * @return group associated with the application cookie or |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 198 | * NULL if Group is not found for the provided cookie |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 199 | */ |
| 200 | @Override |
| 201 | public Group getGroup(DeviceId deviceId, GroupKey appCookie) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 202 | checkPermission(GROUP_READ); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 203 | return store.getGroup(deviceId, appCookie); |
| 204 | } |
| 205 | |
| 206 | /** |
| 207 | * Append buckets to existing group. The caller can optionally |
| 208 | * associate a new cookie during this updation. GROUP_UPDATED or |
| 209 | * GROUP_UPDATE_FAILED notifications would be provided along with |
| 210 | * cookie depending on the result of the operation on the device. |
| 211 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 212 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 213 | * @param oldCookie cookie to be used to retrieve the existing group |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 214 | * @param buckets immutable list of group bucket to be added |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 215 | * @param newCookie immutable cookie to be used post update operation |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 216 | * @param appId Application Id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 217 | */ |
| 218 | @Override |
| 219 | public void addBucketsToGroup(DeviceId deviceId, |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 220 | GroupKey oldCookie, |
| 221 | GroupBuckets buckets, |
| 222 | GroupKey newCookie, |
| 223 | ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 224 | checkPermission(GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 225 | store.updateGroupDescription(deviceId, |
| 226 | oldCookie, |
| 227 | UpdateType.ADD, |
| 228 | buckets, |
| 229 | newCookie); |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Remove buckets from existing group. The caller can optionally |
| 234 | * associate a new cookie during this updation. GROUP_UPDATED or |
| 235 | * GROUP_UPDATE_FAILED notifications would be provided along with |
| 236 | * cookie depending on the result of the operation on the device. |
| 237 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 238 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 239 | * @param oldCookie cookie to be used to retrieve the existing group |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 240 | * @param buckets immutable list of group bucket to be removed |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 241 | * @param newCookie immutable cookie to be used post update operation |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 242 | * @param appId Application Id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 243 | */ |
| 244 | @Override |
| 245 | public void removeBucketsFromGroup(DeviceId deviceId, |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 246 | GroupKey oldCookie, |
| 247 | GroupBuckets buckets, |
| 248 | GroupKey newCookie, |
| 249 | ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 250 | checkPermission(GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 251 | store.updateGroupDescription(deviceId, |
| 252 | oldCookie, |
| 253 | UpdateType.REMOVE, |
| 254 | buckets, |
| 255 | newCookie); |
| 256 | } |
| 257 | |
Victor Silva | 0282ab8 | 2016-11-15 16:30:27 -0300 | [diff] [blame] | 258 | /** |
| 259 | * Set buckets for an existing group. The caller can optionally |
| 260 | * associate a new cookie during this updation. GROUP_UPDATED or |
| 261 | * GROUP_UPDATE_FAILED notifications would be provided along with |
| 262 | * cookie depending on the result of the operation on the device. |
| 263 | * |
| 264 | * This operation overwrites the previous group buckets entirely. |
| 265 | * |
| 266 | * @param deviceId device identifier |
| 267 | * @param oldCookie cookie to be used to retrieve the existing group |
| 268 | * @param buckets immutable list of group buckets to be set |
| 269 | * @param newCookie immutable cookie to be used post update operation |
| 270 | * @param appId Application Id |
| 271 | */ |
| 272 | @Override |
| 273 | public void setBucketsForGroup(DeviceId deviceId, |
| 274 | GroupKey oldCookie, |
| 275 | GroupBuckets buckets, |
| 276 | GroupKey newCookie, |
| 277 | ApplicationId appId) { |
| 278 | checkPermission(GROUP_WRITE); |
| 279 | store.updateGroupDescription(deviceId, |
| 280 | oldCookie, |
| 281 | UpdateType.SET, |
| 282 | buckets, |
| 283 | newCookie); |
| 284 | } |
| 285 | |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 286 | @Override |
| 287 | public void purgeGroupEntries(DeviceId deviceId) { |
| 288 | checkPermission(GROUP_WRITE); |
| 289 | store.purgeGroupEntry(deviceId); |
| 290 | } |
| 291 | |
Victor Silva | 4e8b783 | 2016-08-17 17:11:19 -0300 | [diff] [blame] | 292 | @Override |
| 293 | public void purgeGroupEntries() { |
| 294 | checkPermission(GROUP_WRITE); |
| 295 | store.purgeGroupEntries(); |
| 296 | } |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 297 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 298 | /** |
| 299 | * Delete a group associated to an application cookie. |
| 300 | * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be |
| 301 | * provided along with cookie depending on the result of the |
| 302 | * operation on the device. |
| 303 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 304 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 305 | * @param appCookie application cookie to be used for lookup |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 306 | * @param appId Application Id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 307 | */ |
| 308 | @Override |
| 309 | public void removeGroup(DeviceId deviceId, |
| 310 | GroupKey appCookie, |
| 311 | ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 312 | checkPermission(GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 313 | store.deleteGroupDescription(deviceId, appCookie); |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Retrieve all groups created by an application in the specified device |
| 318 | * as seen by current controller instance. |
| 319 | * |
| 320 | * @param deviceId device identifier |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 321 | * @param appId application id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 322 | * @return collection of immutable group objects created by the application |
| 323 | */ |
| 324 | @Override |
| 325 | public Iterable<Group> getGroups(DeviceId deviceId, |
| 326 | ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 327 | checkPermission(GROUP_READ); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 328 | return store.getGroups(deviceId); |
| 329 | } |
| 330 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 331 | @Override |
| 332 | public Iterable<Group> getGroups(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 333 | checkPermission(GROUP_READ); |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 334 | return store.getGroups(deviceId); |
| 335 | } |
| 336 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 337 | @Override |
| 338 | protected GroupProviderService createProviderService(GroupProvider provider) { |
| 339 | return new InternalGroupProviderService(provider); |
| 340 | } |
| 341 | |
| 342 | private class InternalGroupStoreDelegate implements GroupStoreDelegate { |
| 343 | @Override |
| 344 | public void notify(GroupEvent event) { |
| 345 | final Group group = event.subject(); |
| 346 | GroupProvider groupProvider = |
| 347 | getProvider(group.deviceId()); |
| 348 | GroupOperations groupOps = null; |
| 349 | switch (event.type()) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 350 | case GROUP_ADD_REQUESTED: |
| 351 | log.debug("GROUP_ADD_REQUESTED for Group {} on device {}", |
| 352 | group.id(), group.deviceId()); |
| 353 | GroupOperation groupAddOp = GroupOperation. |
| 354 | createAddGroupOperation(group.id(), |
| 355 | group.type(), |
| 356 | group.buckets()); |
| 357 | groupOps = new GroupOperations( |
| 358 | Collections.singletonList(groupAddOp)); |
| 359 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 360 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 361 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 362 | case GROUP_UPDATE_REQUESTED: |
| 363 | log.debug("GROUP_UPDATE_REQUESTED for Group {} on device {}", |
| 364 | group.id(), group.deviceId()); |
| 365 | GroupOperation groupModifyOp = GroupOperation. |
| 366 | createModifyGroupOperation(group.id(), |
| 367 | group.type(), |
| 368 | group.buckets()); |
| 369 | groupOps = new GroupOperations( |
| 370 | Collections.singletonList(groupModifyOp)); |
| 371 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 372 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 373 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 374 | case GROUP_REMOVE_REQUESTED: |
| 375 | log.debug("GROUP_REMOVE_REQUESTED for Group {} on device {}", |
| 376 | group.id(), group.deviceId()); |
| 377 | GroupOperation groupDeleteOp = GroupOperation. |
| 378 | createDeleteGroupOperation(group.id(), |
| 379 | group.type()); |
| 380 | groupOps = new GroupOperations( |
| 381 | Collections.singletonList(groupDeleteOp)); |
| 382 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 383 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 384 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 385 | case GROUP_ADDED: |
| 386 | case GROUP_UPDATED: |
| 387 | case GROUP_REMOVED: |
| 388 | case GROUP_ADD_FAILED: |
| 389 | case GROUP_UPDATE_FAILED: |
| 390 | case GROUP_REMOVE_FAILED: |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 391 | case GROUP_BUCKET_FAILOVER: |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 392 | post(event); |
| 393 | break; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 394 | default: |
| 395 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 396 | } |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | private class InternalGroupProviderService |
| 401 | extends AbstractProviderService<GroupProvider> |
| 402 | implements GroupProviderService { |
| 403 | |
| 404 | protected InternalGroupProviderService(GroupProvider provider) { |
| 405 | super(provider); |
| 406 | } |
| 407 | |
| 408 | @Override |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 409 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 410 | store.groupOperationFailed(deviceId, operation); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 411 | } |
| 412 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 413 | @Override |
| 414 | public void pushGroupMetrics(DeviceId deviceId, |
| 415 | Collection<Group> groupEntries) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 416 | log.trace("Received group metrics from device {}", deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 417 | checkValidity(); |
| 418 | store.pushGroupMetrics(deviceId, groupEntries); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 419 | } |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 420 | |
| 421 | @Override |
| 422 | public void notifyOfFailovers(Collection<Group> failoverGroups) { |
| 423 | store.notifyOfFailovers(failoverGroups); |
| 424 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | private class InternalDeviceListener implements DeviceListener { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 428 | @Override |
| 429 | public void event(DeviceEvent event) { |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 430 | eventExecutor.execute(() -> processEventInternal(event)); |
| 431 | } |
| 432 | |
| 433 | private void processEventInternal(DeviceEvent event) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 434 | switch (event.type()) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 435 | case DEVICE_REMOVED: |
| 436 | case DEVICE_AVAILABILITY_CHANGED: |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 437 | DeviceId deviceId = event.subject().id(); |
| 438 | if (!deviceService.isAvailable(deviceId)) { |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 439 | log.debug("Device {} became unavailable; clearing initial audit status", |
| 440 | event.type(), event.subject().id()); |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 441 | store.deviceInitialAuditCompleted(event.subject().id(), false); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 442 | |
| 443 | if (purgeOnDisconnection) { |
| 444 | store.purgeGroupEntry(deviceId); |
| 445 | } |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 446 | } |
| 447 | break; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 448 | default: |
| 449 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | } |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 453 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 454 | } |