blob: 1150d056968a44b5f47c0c9978cfc12d14e6d1a4 [file] [log] [blame]
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -08003 *
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 */
16package org.onosproject.net.group.impl;
17
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080018import org.apache.felix.scr.annotations.Activate;
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Deactivate;
Charles Chan0c7c43b2016-01-14 17:39:20 -080021import org.apache.felix.scr.annotations.Modified;
22import org.apache.felix.scr.annotations.Property;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080023import org.apache.felix.scr.annotations.Reference;
24import org.apache.felix.scr.annotations.ReferenceCardinality;
25import org.apache.felix.scr.annotations.Service;
Jian Lid9b5f552016-03-11 18:15:31 -080026import org.onlab.util.Tools;
Charles Chan0c7c43b2016-01-14 17:39:20 -080027import org.onosproject.cfg.ComponentConfigService;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080028import org.onosproject.core.ApplicationId;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080029import org.onosproject.net.DeviceId;
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080030import org.onosproject.net.device.DeviceEvent;
31import org.onosproject.net.device.DeviceListener;
32import org.onosproject.net.device.DeviceService;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080033import org.onosproject.net.group.Group;
34import org.onosproject.net.group.GroupBuckets;
35import org.onosproject.net.group.GroupDescription;
36import org.onosproject.net.group.GroupEvent;
37import org.onosproject.net.group.GroupKey;
38import org.onosproject.net.group.GroupListener;
39import org.onosproject.net.group.GroupOperation;
40import org.onosproject.net.group.GroupOperations;
41import org.onosproject.net.group.GroupProvider;
42import org.onosproject.net.group.GroupProviderRegistry;
43import org.onosproject.net.group.GroupProviderService;
44import org.onosproject.net.group.GroupService;
45import org.onosproject.net.group.GroupStore;
46import org.onosproject.net.group.GroupStore.UpdateType;
47import org.onosproject.net.group.GroupStoreDelegate;
Jian Lid9b5f552016-03-11 18:15:31 -080048import org.onosproject.net.provider.AbstractListenerProviderRegistry;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080049import org.onosproject.net.provider.AbstractProviderService;
Charles Chan0c7c43b2016-01-14 17:39:20 -080050import org.osgi.service.component.ComponentContext;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080051import org.slf4j.Logger;
52
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070053import java.util.Collection;
54import java.util.Collections;
Charles Chan0c7c43b2016-01-14 17:39:20 -080055import java.util.Dictionary;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070056
Changhoon Yoon541ef712015-05-23 17:18:34 +090057import static org.onosproject.security.AppGuard.checkPermission;
Jian Lid9b5f552016-03-11 18:15:31 -080058import static org.onosproject.security.AppPermission.Type.GROUP_READ;
59import static org.onosproject.security.AppPermission.Type.GROUP_WRITE;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070060import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090061
Changhoon Yoon541ef712015-05-23 17:18:34 +090062
63
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080064/**
65 * Provides implementation of the group service APIs.
66 */
67@Component(immediate = true)
68@Service
69public class GroupManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070070 extends AbstractListenerProviderRegistry<GroupEvent, GroupListener,
71 GroupProvider, GroupProviderService>
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080072 implements GroupService, GroupProviderRegistry {
73
74 private final Logger log = getLogger(getClass());
75
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080076 private final GroupStoreDelegate delegate = new InternalGroupStoreDelegate();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080077 private final DeviceListener deviceListener = new InternalDeviceListener();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080078
79 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
80 protected GroupStore store;
81
82 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080083 protected DeviceService deviceService;
84
Charles Chan0c7c43b2016-01-14 17:39:20 -080085 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
86 protected ComponentConfigService cfgService;
87
88 @Property(name = "purgeOnDisconnection", boolValue = false,
89 label = "Purge entries associated with a device when the device goes offline")
90 private boolean purgeOnDisconnection = false;
Andrea Campanella6ee73922016-02-03 18:00:00 -080091 private final GroupDriverProvider defaultProvider = new GroupDriverProvider();
Charles Chan0c7c43b2016-01-14 17:39:20 -080092
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080093 @Activate
Charles Chan0c7c43b2016-01-14 17:39:20 -080094 public void activate(ComponentContext context) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080095 store.setDelegate(delegate);
96 eventDispatcher.addSink(GroupEvent.class, listenerRegistry);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080097 deviceService.addListener(deviceListener);
Charles Chan0c7c43b2016-01-14 17:39:20 -080098 cfgService.registerProperties(getClass());
99 modified(context);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800100 log.info("Started");
101 }
102
103 @Deactivate
104 public void deactivate() {
Andrea Campanella3f1c61e2016-04-01 17:30:12 -0700105 deviceService.removeListener(deviceListener);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800106 cfgService.unregisterProperties(getClass(), false);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800107 store.unsetDelegate(delegate);
108 eventDispatcher.removeSink(GroupEvent.class);
109 log.info("Stopped");
110 }
111
Charles Chan0c7c43b2016-01-14 17:39:20 -0800112 @Modified
113 public void modified(ComponentContext context) {
114 if (context != null) {
115 readComponentConfiguration(context);
116 }
Andrea Campanella6ee73922016-02-03 18:00:00 -0800117 defaultProvider.init(deviceService);
118 }
119
120 @Override
121 protected GroupProvider defaultProvider() {
122 return defaultProvider;
Charles Chan0c7c43b2016-01-14 17:39:20 -0800123 }
124
125 /**
126 * Extracts properties from the component configuration context.
127 *
128 * @param context the component context
129 */
130 private void readComponentConfiguration(ComponentContext context) {
131 Dictionary<?, ?> properties = context.getProperties();
132 Boolean flag;
133
Jian Lid9b5f552016-03-11 18:15:31 -0800134 flag = Tools.isPropertyEnabled(properties, "purgeOnDisconnection");
Charles Chan0c7c43b2016-01-14 17:39:20 -0800135 if (flag == null) {
136 log.info("PurgeOnDisconnection is not configured, " +
137 "using current value of {}", purgeOnDisconnection);
138 } else {
139 purgeOnDisconnection = flag;
140 log.info("Configured. PurgeOnDisconnection is {}",
141 purgeOnDisconnection ? "enabled" : "disabled");
142 }
143 }
144
145 /**
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800146 * Create a group in the specified device with the provided parameters.
147 *
148 * @param groupDesc group creation parameters
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800149 */
150 @Override
151 public void addGroup(GroupDescription groupDesc) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900152 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800153 store.storeGroupDescription(groupDesc);
154 }
155
156 /**
157 * Return a group object associated to an application cookie.
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700158 * <p>
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800159 * NOTE1: The presence of group object in the system does not
160 * guarantee that the "group" is actually created in device.
161 * GROUP_ADDED notification would confirm the creation of
162 * this group in data plane.
163 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700164 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800165 * @param appCookie application cookie to be used for lookup
166 * @return group associated with the application cookie or
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700167 * NULL if Group is not found for the provided cookie
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800168 */
169 @Override
170 public Group getGroup(DeviceId deviceId, GroupKey appCookie) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900171 checkPermission(GROUP_READ);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800172 return store.getGroup(deviceId, appCookie);
173 }
174
175 /**
176 * Append buckets to existing group. The caller can optionally
177 * associate a new cookie during this updation. GROUP_UPDATED or
178 * GROUP_UPDATE_FAILED notifications would be provided along with
179 * cookie depending on the result of the operation on the device.
180 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700181 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800182 * @param oldCookie cookie to be used to retrieve the existing group
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700183 * @param buckets immutable list of group bucket to be added
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800184 * @param newCookie immutable cookie to be used post update operation
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700185 * @param appId Application Id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800186 */
187 @Override
188 public void addBucketsToGroup(DeviceId deviceId,
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700189 GroupKey oldCookie,
190 GroupBuckets buckets,
191 GroupKey newCookie,
192 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900193 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800194 store.updateGroupDescription(deviceId,
195 oldCookie,
196 UpdateType.ADD,
197 buckets,
198 newCookie);
199 }
200
201 /**
202 * Remove buckets from existing group. The caller can optionally
203 * associate a new cookie during this updation. GROUP_UPDATED or
204 * GROUP_UPDATE_FAILED notifications would be provided along with
205 * cookie depending on the result of the operation on the device.
206 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700207 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800208 * @param oldCookie cookie to be used to retrieve the existing group
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700209 * @param buckets immutable list of group bucket to be removed
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800210 * @param newCookie immutable cookie to be used post update operation
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700211 * @param appId Application Id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800212 */
213 @Override
214 public void removeBucketsFromGroup(DeviceId deviceId,
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700215 GroupKey oldCookie,
216 GroupBuckets buckets,
217 GroupKey newCookie,
218 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900219 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800220 store.updateGroupDescription(deviceId,
221 oldCookie,
222 UpdateType.REMOVE,
223 buckets,
224 newCookie);
225 }
226
Kavitha Alagesanc69c66a2016-06-15 14:26:04 +0530227 @Override
228 public void purgeGroupEntries(DeviceId deviceId) {
229 checkPermission(GROUP_WRITE);
230 store.purgeGroupEntry(deviceId);
231 }
232
233
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800234 /**
235 * Delete a group associated to an application cookie.
236 * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be
237 * provided along with cookie depending on the result of the
238 * operation on the device.
239 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700240 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800241 * @param appCookie application cookie to be used for lookup
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700242 * @param appId Application Id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800243 */
244 @Override
245 public void removeGroup(DeviceId deviceId,
246 GroupKey appCookie,
247 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900248 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800249 store.deleteGroupDescription(deviceId, appCookie);
250 }
251
252 /**
253 * Retrieve all groups created by an application in the specified device
254 * as seen by current controller instance.
255 *
256 * @param deviceId device identifier
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700257 * @param appId application id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800258 * @return collection of immutable group objects created by the application
259 */
260 @Override
261 public Iterable<Group> getGroups(DeviceId deviceId,
262 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900263 checkPermission(GROUP_READ);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800264 return store.getGroups(deviceId);
265 }
266
Jonathan Hart32600692015-03-09 10:38:40 -0700267 @Override
268 public Iterable<Group> getGroups(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900269 checkPermission(GROUP_READ);
Jonathan Hart32600692015-03-09 10:38:40 -0700270 return store.getGroups(deviceId);
271 }
272
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800273 @Override
274 protected GroupProviderService createProviderService(GroupProvider provider) {
275 return new InternalGroupProviderService(provider);
276 }
277
278 private class InternalGroupStoreDelegate implements GroupStoreDelegate {
279 @Override
280 public void notify(GroupEvent event) {
281 final Group group = event.subject();
282 GroupProvider groupProvider =
283 getProvider(group.deviceId());
284 GroupOperations groupOps = null;
285 switch (event.type()) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700286 case GROUP_ADD_REQUESTED:
287 log.debug("GROUP_ADD_REQUESTED for Group {} on device {}",
288 group.id(), group.deviceId());
289 GroupOperation groupAddOp = GroupOperation.
290 createAddGroupOperation(group.id(),
291 group.type(),
292 group.buckets());
293 groupOps = new GroupOperations(
294 Collections.singletonList(groupAddOp));
295 groupProvider.performGroupOperation(group.deviceId(), groupOps);
296 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800297
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700298 case GROUP_UPDATE_REQUESTED:
299 log.debug("GROUP_UPDATE_REQUESTED for Group {} on device {}",
300 group.id(), group.deviceId());
301 GroupOperation groupModifyOp = GroupOperation.
302 createModifyGroupOperation(group.id(),
303 group.type(),
304 group.buckets());
305 groupOps = new GroupOperations(
306 Collections.singletonList(groupModifyOp));
307 groupProvider.performGroupOperation(group.deviceId(), groupOps);
308 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800309
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700310 case GROUP_REMOVE_REQUESTED:
311 log.debug("GROUP_REMOVE_REQUESTED for Group {} on device {}",
312 group.id(), group.deviceId());
313 GroupOperation groupDeleteOp = GroupOperation.
314 createDeleteGroupOperation(group.id(),
315 group.type());
316 groupOps = new GroupOperations(
317 Collections.singletonList(groupDeleteOp));
318 groupProvider.performGroupOperation(group.deviceId(), groupOps);
319 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800320
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700321 case GROUP_ADDED:
322 case GROUP_UPDATED:
323 case GROUP_REMOVED:
324 case GROUP_ADD_FAILED:
325 case GROUP_UPDATE_FAILED:
326 case GROUP_REMOVE_FAILED:
327 post(event);
328 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800329
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700330 default:
331 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800332 }
333 }
334 }
335
336 private class InternalGroupProviderService
337 extends AbstractProviderService<GroupProvider>
338 implements GroupProviderService {
339
340 protected InternalGroupProviderService(GroupProvider provider) {
341 super(provider);
342 }
343
344 @Override
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700345 public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) {
sangho7ff01812015-02-09 16:21:53 -0800346 store.groupOperationFailed(deviceId, operation);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800347 }
348
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800349 @Override
350 public void pushGroupMetrics(DeviceId deviceId,
351 Collection<Group> groupEntries) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700352 log.trace("Received group metrics from device {}", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700353 checkValidity();
354 store.pushGroupMetrics(deviceId, groupEntries);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800355 }
356 }
357
358 private class InternalDeviceListener implements DeviceListener {
359
360 @Override
361 public void event(DeviceEvent event) {
362 switch (event.type()) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700363 case DEVICE_REMOVED:
364 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan0c7c43b2016-01-14 17:39:20 -0800365 DeviceId deviceId = event.subject().id();
366 if (!deviceService.isAvailable(deviceId)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700367 log.debug("Device {} became un available; clearing initial audit status",
368 event.type(), event.subject().id());
369 store.deviceInitialAuditCompleted(event.subject().id(), false);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800370
371 if (purgeOnDisconnection) {
372 store.purgeGroupEntry(deviceId);
373 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700374 }
375 break;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700376 default:
377 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800378 }
379 }
380 }
381}