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 | ad95aaf | 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 | ad95aaf | 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 | ad95aaf | 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 | ad95aaf | 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 |
| 308 | public void purgeGroupEntries() { |
| 309 | checkPermission(GROUP_WRITE); |
| 310 | store.purgeGroupEntries(); |
| 311 | } |
Kavitha Alagesan | c69c66a | 2016-06-15 14:26:04 +0530 | [diff] [blame] | 312 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 313 | /** |
| 314 | * Delete a group associated to an application cookie. |
| 315 | * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be |
| 316 | * provided along with cookie depending on the result of the |
| 317 | * operation on the device. |
| 318 | * |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 319 | * @param deviceId device identifier |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 320 | * @param appCookie application cookie to be used for lookup |
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 | */ |
| 323 | @Override |
| 324 | public void removeGroup(DeviceId deviceId, |
| 325 | GroupKey appCookie, |
| 326 | ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 327 | checkPermission(GROUP_WRITE); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 328 | store.deleteGroupDescription(deviceId, appCookie); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Retrieve all groups created by an application in the specified device |
| 333 | * as seen by current controller instance. |
| 334 | * |
| 335 | * @param deviceId device identifier |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 336 | * @param appId application id |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 337 | * @return collection of immutable group objects created by the application |
| 338 | */ |
| 339 | @Override |
| 340 | public Iterable<Group> getGroups(DeviceId deviceId, |
| 341 | ApplicationId appId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 342 | checkPermission(GROUP_READ); |
Carmelo Cascone | bc7eb85 | 2019-04-17 23:10:51 -0700 | [diff] [blame] | 343 | return Iterables.filter( |
| 344 | store.getGroups(deviceId), |
| 345 | g -> g != null && Objects.equals(g.appId(), appId)); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 348 | @Override |
| 349 | public Iterable<Group> getGroups(DeviceId deviceId) { |
Changhoon Yoon | b856b81 | 2015-08-10 03:47:19 +0900 | [diff] [blame] | 350 | checkPermission(GROUP_READ); |
Jonathan Hart | 3260069 | 2015-03-09 10:38:40 -0700 | [diff] [blame] | 351 | return store.getGroups(deviceId); |
| 352 | } |
| 353 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 354 | @Override |
| 355 | protected GroupProviderService createProviderService(GroupProvider provider) { |
| 356 | return new InternalGroupProviderService(provider); |
| 357 | } |
| 358 | |
| 359 | private class InternalGroupStoreDelegate implements GroupStoreDelegate { |
| 360 | @Override |
| 361 | public void notify(GroupEvent event) { |
| 362 | final Group group = event.subject(); |
| 363 | GroupProvider groupProvider = |
| 364 | getProvider(group.deviceId()); |
| 365 | GroupOperations groupOps = null; |
| 366 | switch (event.type()) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 367 | case GROUP_ADD_REQUESTED: |
| 368 | log.debug("GROUP_ADD_REQUESTED for Group {} on device {}", |
| 369 | group.id(), group.deviceId()); |
| 370 | GroupOperation groupAddOp = GroupOperation. |
| 371 | createAddGroupOperation(group.id(), |
| 372 | group.type(), |
| 373 | group.buckets()); |
| 374 | groupOps = new GroupOperations( |
| 375 | Collections.singletonList(groupAddOp)); |
| 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_UPDATE_REQUESTED: |
| 380 | log.debug("GROUP_UPDATE_REQUESTED for Group {} on device {}", |
| 381 | group.id(), group.deviceId()); |
| 382 | GroupOperation groupModifyOp = GroupOperation. |
| 383 | createModifyGroupOperation(group.id(), |
| 384 | group.type(), |
| 385 | group.buckets()); |
| 386 | groupOps = new GroupOperations( |
| 387 | Collections.singletonList(groupModifyOp)); |
| 388 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 389 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 390 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 391 | case GROUP_REMOVE_REQUESTED: |
| 392 | log.debug("GROUP_REMOVE_REQUESTED for Group {} on device {}", |
| 393 | group.id(), group.deviceId()); |
| 394 | GroupOperation groupDeleteOp = GroupOperation. |
| 395 | createDeleteGroupOperation(group.id(), |
| 396 | group.type()); |
| 397 | groupOps = new GroupOperations( |
| 398 | Collections.singletonList(groupDeleteOp)); |
| 399 | groupProvider.performGroupOperation(group.deviceId(), groupOps); |
| 400 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 401 | |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 402 | case GROUP_ADDED: |
| 403 | case GROUP_UPDATED: |
| 404 | case GROUP_REMOVED: |
| 405 | case GROUP_ADD_FAILED: |
| 406 | case GROUP_UPDATE_FAILED: |
| 407 | case GROUP_REMOVE_FAILED: |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 408 | case GROUP_BUCKET_FAILOVER: |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 409 | post(event); |
| 410 | break; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 411 | default: |
| 412 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | private class InternalGroupProviderService |
| 418 | extends AbstractProviderService<GroupProvider> |
| 419 | implements GroupProviderService { |
| 420 | |
| 421 | protected InternalGroupProviderService(GroupProvider provider) { |
| 422 | super(provider); |
| 423 | } |
| 424 | |
| 425 | @Override |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 426 | public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) { |
sangho | 7ff0181 | 2015-02-09 16:21:53 -0800 | [diff] [blame] | 427 | store.groupOperationFailed(deviceId, operation); |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 428 | } |
| 429 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 430 | @Override |
| 431 | public void pushGroupMetrics(DeviceId deviceId, |
| 432 | Collection<Group> groupEntries) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 433 | log.trace("Received group metrics from device {}", deviceId); |
Srikanth Vavilapalli | 2318191 | 2015-05-04 09:48:09 -0700 | [diff] [blame] | 434 | checkValidity(); |
| 435 | store.pushGroupMetrics(deviceId, groupEntries); |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 436 | } |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 437 | |
| 438 | @Override |
| 439 | public void notifyOfFailovers(Collection<Group> failoverGroups) { |
| 440 | store.notifyOfFailovers(failoverGroups); |
| 441 | } |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | private class InternalDeviceListener implements DeviceListener { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 445 | @Override |
| 446 | public void event(DeviceEvent event) { |
Charles Chan | 07f15f2 | 2018-05-08 21:35:50 -0700 | [diff] [blame] | 447 | eventExecutor.execute(() -> processEventInternal(event)); |
| 448 | } |
| 449 | |
| 450 | private void processEventInternal(DeviceEvent event) { |
Srikanth Vavilapalli | ed12ae5 | 2015-02-09 14:43:19 -0800 | [diff] [blame] | 451 | switch (event.type()) { |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 452 | case DEVICE_REMOVED: |
| 453 | case DEVICE_AVAILABILITY_CHANGED: |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 454 | DeviceId deviceId = event.subject().id(); |
| 455 | if (!deviceService.isAvailable(deviceId)) { |
Tian Jian | 9cb2e9e | 2018-10-23 16:32:51 +0800 | [diff] [blame] | 456 | log.debug("Device {} became unavailable for {}; clearing initial audit status", |
| 457 | deviceId, event.type()); |
| 458 | store.deviceInitialAuditCompleted(deviceId, false); |
Andrea Campanella | ad95aaf | 2020-03-27 12:53:46 +0100 | [diff] [blame^] | 459 | BasicDeviceConfig cfg = netCfgService.getConfig(deviceId, BasicDeviceConfig.class); |
| 460 | //if purgeOnDisconnection is set for the device or it's a global configuration |
| 461 | // lets remove the groups. |
| 462 | boolean purge = cfg != null && cfg.isPurgeOnDisconnectionConfigured() ? |
| 463 | cfg.purgeOnDisconnection() : purgeOnDisconnection; |
| 464 | if (purge) { |
| 465 | log.info("PurgeOnDisconnection is requested for device {}, " + |
| 466 | "removing groups", deviceId); |
Charles Chan | 0c7c43b | 2016-01-14 17:39:20 -0800 | [diff] [blame] | 467 | store.purgeGroupEntry(deviceId); |
| 468 | } |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 469 | } |
| 470 | break; |
Thomas Vachuska | 42e8cce | 2015-07-29 19:25:18 -0700 | [diff] [blame] | 471 | default: |
| 472 | break; |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 473 | } |
| 474 | } |
| 475 | } |
helenyrwu | 89470f1 | 2016-08-12 13:18:10 -0700 | [diff] [blame] | 476 | |
Srikanth Vavilapalli | 45c27c8 | 2015-01-30 12:57:56 -0800 | [diff] [blame] | 477 | } |