blob: 091265ae7cc7ab880cb1a32486a8b07e1ab94393 [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
227 /**
228 * Delete a group associated to an application cookie.
229 * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be
230 * provided along with cookie depending on the result of the
231 * operation on the device.
232 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700233 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800234 * @param appCookie application cookie to be used for lookup
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700235 * @param appId Application Id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800236 */
237 @Override
238 public void removeGroup(DeviceId deviceId,
239 GroupKey appCookie,
240 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900241 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800242 store.deleteGroupDescription(deviceId, appCookie);
243 }
244
245 /**
246 * Retrieve all groups created by an application in the specified device
247 * as seen by current controller instance.
248 *
249 * @param deviceId device identifier
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700250 * @param appId application id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800251 * @return collection of immutable group objects created by the application
252 */
253 @Override
254 public Iterable<Group> getGroups(DeviceId deviceId,
255 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900256 checkPermission(GROUP_READ);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800257 return store.getGroups(deviceId);
258 }
259
Jonathan Hart32600692015-03-09 10:38:40 -0700260 @Override
261 public Iterable<Group> getGroups(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900262 checkPermission(GROUP_READ);
Jonathan Hart32600692015-03-09 10:38:40 -0700263 return store.getGroups(deviceId);
264 }
265
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800266 @Override
267 protected GroupProviderService createProviderService(GroupProvider provider) {
268 return new InternalGroupProviderService(provider);
269 }
270
271 private class InternalGroupStoreDelegate implements GroupStoreDelegate {
272 @Override
273 public void notify(GroupEvent event) {
274 final Group group = event.subject();
275 GroupProvider groupProvider =
276 getProvider(group.deviceId());
277 GroupOperations groupOps = null;
278 switch (event.type()) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700279 case GROUP_ADD_REQUESTED:
280 log.debug("GROUP_ADD_REQUESTED for Group {} on device {}",
281 group.id(), group.deviceId());
282 GroupOperation groupAddOp = GroupOperation.
283 createAddGroupOperation(group.id(),
284 group.type(),
285 group.buckets());
286 groupOps = new GroupOperations(
287 Collections.singletonList(groupAddOp));
288 groupProvider.performGroupOperation(group.deviceId(), groupOps);
289 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800290
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700291 case GROUP_UPDATE_REQUESTED:
292 log.debug("GROUP_UPDATE_REQUESTED for Group {} on device {}",
293 group.id(), group.deviceId());
294 GroupOperation groupModifyOp = GroupOperation.
295 createModifyGroupOperation(group.id(),
296 group.type(),
297 group.buckets());
298 groupOps = new GroupOperations(
299 Collections.singletonList(groupModifyOp));
300 groupProvider.performGroupOperation(group.deviceId(), groupOps);
301 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800302
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700303 case GROUP_REMOVE_REQUESTED:
304 log.debug("GROUP_REMOVE_REQUESTED for Group {} on device {}",
305 group.id(), group.deviceId());
306 GroupOperation groupDeleteOp = GroupOperation.
307 createDeleteGroupOperation(group.id(),
308 group.type());
309 groupOps = new GroupOperations(
310 Collections.singletonList(groupDeleteOp));
311 groupProvider.performGroupOperation(group.deviceId(), groupOps);
312 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800313
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700314 case GROUP_ADDED:
315 case GROUP_UPDATED:
316 case GROUP_REMOVED:
317 case GROUP_ADD_FAILED:
318 case GROUP_UPDATE_FAILED:
319 case GROUP_REMOVE_FAILED:
320 post(event);
321 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800322
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700323 default:
324 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800325 }
326 }
327 }
328
329 private class InternalGroupProviderService
330 extends AbstractProviderService<GroupProvider>
331 implements GroupProviderService {
332
333 protected InternalGroupProviderService(GroupProvider provider) {
334 super(provider);
335 }
336
337 @Override
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700338 public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) {
sangho7ff01812015-02-09 16:21:53 -0800339 store.groupOperationFailed(deviceId, operation);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800340 }
341
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800342 @Override
343 public void pushGroupMetrics(DeviceId deviceId,
344 Collection<Group> groupEntries) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700345 log.trace("Received group metrics from device {}", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700346 checkValidity();
347 store.pushGroupMetrics(deviceId, groupEntries);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800348 }
349 }
350
351 private class InternalDeviceListener implements DeviceListener {
352
353 @Override
354 public void event(DeviceEvent event) {
355 switch (event.type()) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700356 case DEVICE_REMOVED:
357 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan0c7c43b2016-01-14 17:39:20 -0800358 DeviceId deviceId = event.subject().id();
359 if (!deviceService.isAvailable(deviceId)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700360 log.debug("Device {} became un available; clearing initial audit status",
361 event.type(), event.subject().id());
362 store.deviceInitialAuditCompleted(event.subject().id(), false);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800363
364 if (purgeOnDisconnection) {
365 store.purgeGroupEntry(deviceId);
366 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700367 }
368 break;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700369 default:
370 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800371 }
372 }
373 }
374}