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