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