blob: d6158b51379f36368fc437dd9957004f8c3c1a88 [file] [log] [blame]
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -08001/*
2 * Copyright 2015 Open Networking Laboratory
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 */
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;
Charles Chan0c7c43b2016-01-14 17:39:20 -080026import org.onosproject.cfg.ComponentConfigService;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070027import org.onosproject.net.provider.AbstractListenerProviderRegistry;
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;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080048import org.onosproject.net.provider.AbstractProviderService;
Charles Chan0c7c43b2016-01-14 17:39:20 -080049import org.osgi.service.component.ComponentContext;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080050import org.slf4j.Logger;
51
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070052import java.util.Collection;
53import java.util.Collections;
Charles Chan0c7c43b2016-01-14 17:39:20 -080054import java.util.Dictionary;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070055
Charles Chan0c7c43b2016-01-14 17:39:20 -080056import static com.google.common.base.Strings.isNullOrEmpty;
Changhoon Yoon541ef712015-05-23 17:18:34 +090057import static org.onosproject.security.AppGuard.checkPermission;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070058import static org.slf4j.LoggerFactory.getLogger;
Changhoon Yoonb856b812015-08-10 03:47:19 +090059import static org.onosproject.security.AppPermission.Type.*;
60
Changhoon Yoon541ef712015-05-23 17:18:34 +090061
62
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080063/**
64 * Provides implementation of the group service APIs.
65 */
66@Component(immediate = true)
67@Service
68public class GroupManager
Thomas Vachuska42e8cce2015-07-29 19:25:18 -070069 extends AbstractListenerProviderRegistry<GroupEvent, GroupListener,
70 GroupProvider, GroupProviderService>
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080071 implements GroupService, GroupProviderRegistry {
72
73 private final Logger log = getLogger(getClass());
74
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080075 private final GroupStoreDelegate delegate = new InternalGroupStoreDelegate();
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080076 private final DeviceListener deviceListener = new InternalDeviceListener();
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080077
78 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
79 protected GroupStore store;
80
81 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080082 protected DeviceService deviceService;
83
Charles Chan0c7c43b2016-01-14 17:39:20 -080084 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
85 protected ComponentConfigService cfgService;
86
87 @Property(name = "purgeOnDisconnection", boolValue = false,
88 label = "Purge entries associated with a device when the device goes offline")
89 private boolean purgeOnDisconnection = false;
90
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080091 @Activate
Charles Chan0c7c43b2016-01-14 17:39:20 -080092 public void activate(ComponentContext context) {
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080093 store.setDelegate(delegate);
94 eventDispatcher.addSink(GroupEvent.class, listenerRegistry);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -080095 deviceService.addListener(deviceListener);
Charles Chan0c7c43b2016-01-14 17:39:20 -080096 cfgService.registerProperties(getClass());
97 modified(context);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -080098 log.info("Started");
99 }
100
101 @Deactivate
102 public void deactivate() {
Charles Chan0c7c43b2016-01-14 17:39:20 -0800103 cfgService.unregisterProperties(getClass(), false);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800104 store.unsetDelegate(delegate);
105 eventDispatcher.removeSink(GroupEvent.class);
106 log.info("Stopped");
107 }
108
Charles Chan0c7c43b2016-01-14 17:39:20 -0800109 @Modified
110 public void modified(ComponentContext context) {
111 if (context != null) {
112 readComponentConfiguration(context);
113 }
114 }
115
116 /**
117 * Extracts properties from the component configuration context.
118 *
119 * @param context the component context
120 */
121 private void readComponentConfiguration(ComponentContext context) {
122 Dictionary<?, ?> properties = context.getProperties();
123 Boolean flag;
124
125 flag = isPropertyEnabled(properties, "purgeOnDisconnection");
126 if (flag == null) {
127 log.info("PurgeOnDisconnection is not configured, " +
128 "using current value of {}", purgeOnDisconnection);
129 } else {
130 purgeOnDisconnection = flag;
131 log.info("Configured. PurgeOnDisconnection is {}",
132 purgeOnDisconnection ? "enabled" : "disabled");
133 }
134 }
135
136 /**
137 * Check property name is defined and set to true.
138 *
139 * @param properties properties to be looked up
140 * @param propertyName the name of the property to look up
141 * @return value when the propertyName is defined or return null
142 */
143 private static Boolean isPropertyEnabled(Dictionary<?, ?> properties,
144 String propertyName) {
145 Boolean value = null;
146 try {
147 String s = (String) properties.get(propertyName);
148 value = isNullOrEmpty(s) ? null : s.trim().equals("true");
149 } catch (ClassCastException e) {
150 // No propertyName defined.
151 value = null;
152 }
153 return value;
154 }
155
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800156 /**
157 * Create a group in the specified device with the provided parameters.
158 *
159 * @param groupDesc group creation parameters
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800160 */
161 @Override
162 public void addGroup(GroupDescription groupDesc) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900163 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800164 store.storeGroupDescription(groupDesc);
165 }
166
167 /**
168 * Return a group object associated to an application cookie.
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700169 * <p>
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800170 * NOTE1: The presence of group object in the system does not
171 * guarantee that the "group" is actually created in device.
172 * GROUP_ADDED notification would confirm the creation of
173 * this group in data plane.
174 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700175 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800176 * @param appCookie application cookie to be used for lookup
177 * @return group associated with the application cookie or
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700178 * NULL if Group is not found for the provided cookie
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800179 */
180 @Override
181 public Group getGroup(DeviceId deviceId, GroupKey appCookie) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900182 checkPermission(GROUP_READ);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800183 return store.getGroup(deviceId, appCookie);
184 }
185
186 /**
187 * Append buckets to existing group. The caller can optionally
188 * associate a new cookie during this updation. GROUP_UPDATED or
189 * GROUP_UPDATE_FAILED notifications would be provided along with
190 * cookie depending on the result of the operation on the device.
191 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700192 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800193 * @param oldCookie cookie to be used to retrieve the existing group
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700194 * @param buckets immutable list of group bucket to be added
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800195 * @param newCookie immutable cookie to be used post update operation
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700196 * @param appId Application Id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800197 */
198 @Override
199 public void addBucketsToGroup(DeviceId deviceId,
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700200 GroupKey oldCookie,
201 GroupBuckets buckets,
202 GroupKey newCookie,
203 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900204 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800205 store.updateGroupDescription(deviceId,
206 oldCookie,
207 UpdateType.ADD,
208 buckets,
209 newCookie);
210 }
211
212 /**
213 * Remove buckets from existing group. The caller can optionally
214 * associate a new cookie during this updation. GROUP_UPDATED or
215 * GROUP_UPDATE_FAILED notifications would be provided along with
216 * cookie depending on the result of the operation on the device.
217 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700218 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800219 * @param oldCookie cookie to be used to retrieve the existing group
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700220 * @param buckets immutable list of group bucket to be removed
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800221 * @param newCookie immutable cookie to be used post update operation
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700222 * @param appId Application Id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800223 */
224 @Override
225 public void removeBucketsFromGroup(DeviceId deviceId,
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700226 GroupKey oldCookie,
227 GroupBuckets buckets,
228 GroupKey newCookie,
229 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900230 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800231 store.updateGroupDescription(deviceId,
232 oldCookie,
233 UpdateType.REMOVE,
234 buckets,
235 newCookie);
236 }
237
238 /**
239 * Delete a group associated to an application cookie.
240 * GROUP_DELETED or GROUP_DELETE_FAILED notifications would be
241 * provided along with cookie depending on the result of the
242 * operation on the device.
243 *
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700244 * @param deviceId device identifier
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800245 * @param appCookie application cookie to be used for lookup
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700246 * @param appId Application Id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800247 */
248 @Override
249 public void removeGroup(DeviceId deviceId,
250 GroupKey appCookie,
251 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900252 checkPermission(GROUP_WRITE);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800253 store.deleteGroupDescription(deviceId, appCookie);
254 }
255
256 /**
257 * Retrieve all groups created by an application in the specified device
258 * as seen by current controller instance.
259 *
260 * @param deviceId device identifier
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700261 * @param appId application id
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800262 * @return collection of immutable group objects created by the application
263 */
264 @Override
265 public Iterable<Group> getGroups(DeviceId deviceId,
266 ApplicationId appId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900267 checkPermission(GROUP_READ);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800268 return store.getGroups(deviceId);
269 }
270
Jonathan Hart32600692015-03-09 10:38:40 -0700271 @Override
272 public Iterable<Group> getGroups(DeviceId deviceId) {
Changhoon Yoonb856b812015-08-10 03:47:19 +0900273 checkPermission(GROUP_READ);
Jonathan Hart32600692015-03-09 10:38:40 -0700274 return store.getGroups(deviceId);
275 }
276
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800277 @Override
278 protected GroupProviderService createProviderService(GroupProvider provider) {
279 return new InternalGroupProviderService(provider);
280 }
281
282 private class InternalGroupStoreDelegate implements GroupStoreDelegate {
283 @Override
284 public void notify(GroupEvent event) {
285 final Group group = event.subject();
286 GroupProvider groupProvider =
287 getProvider(group.deviceId());
288 GroupOperations groupOps = null;
289 switch (event.type()) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700290 case GROUP_ADD_REQUESTED:
291 log.debug("GROUP_ADD_REQUESTED for Group {} on device {}",
292 group.id(), group.deviceId());
293 GroupOperation groupAddOp = GroupOperation.
294 createAddGroupOperation(group.id(),
295 group.type(),
296 group.buckets());
297 groupOps = new GroupOperations(
298 Collections.singletonList(groupAddOp));
299 groupProvider.performGroupOperation(group.deviceId(), groupOps);
300 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800301
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700302 case GROUP_UPDATE_REQUESTED:
303 log.debug("GROUP_UPDATE_REQUESTED for Group {} on device {}",
304 group.id(), group.deviceId());
305 GroupOperation groupModifyOp = GroupOperation.
306 createModifyGroupOperation(group.id(),
307 group.type(),
308 group.buckets());
309 groupOps = new GroupOperations(
310 Collections.singletonList(groupModifyOp));
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_REMOVE_REQUESTED:
315 log.debug("GROUP_REMOVE_REQUESTED for Group {} on device {}",
316 group.id(), group.deviceId());
317 GroupOperation groupDeleteOp = GroupOperation.
318 createDeleteGroupOperation(group.id(),
319 group.type());
320 groupOps = new GroupOperations(
321 Collections.singletonList(groupDeleteOp));
322 groupProvider.performGroupOperation(group.deviceId(), groupOps);
323 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800324
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700325 case GROUP_ADDED:
326 case GROUP_UPDATED:
327 case GROUP_REMOVED:
328 case GROUP_ADD_FAILED:
329 case GROUP_UPDATE_FAILED:
330 case GROUP_REMOVE_FAILED:
331 post(event);
332 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800333
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700334 default:
335 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800336 }
337 }
338 }
339
340 private class InternalGroupProviderService
341 extends AbstractProviderService<GroupProvider>
342 implements GroupProviderService {
343
344 protected InternalGroupProviderService(GroupProvider provider) {
345 super(provider);
346 }
347
348 @Override
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700349 public void groupOperationFailed(DeviceId deviceId, GroupOperation operation) {
sangho7ff01812015-02-09 16:21:53 -0800350 store.groupOperationFailed(deviceId, operation);
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800351 }
352
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800353 @Override
354 public void pushGroupMetrics(DeviceId deviceId,
355 Collection<Group> groupEntries) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700356 log.trace("Received group metrics from device {}", deviceId);
Srikanth Vavilapalli23181912015-05-04 09:48:09 -0700357 checkValidity();
358 store.pushGroupMetrics(deviceId, groupEntries);
Srikanth Vavilapallied12ae52015-02-09 14:43:19 -0800359 }
360 }
361
362 private class InternalDeviceListener implements DeviceListener {
363
364 @Override
365 public void event(DeviceEvent event) {
366 switch (event.type()) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700367 case DEVICE_REMOVED:
368 case DEVICE_AVAILABILITY_CHANGED:
Charles Chan0c7c43b2016-01-14 17:39:20 -0800369 DeviceId deviceId = event.subject().id();
370 if (!deviceService.isAvailable(deviceId)) {
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700371 log.debug("Device {} became un available; clearing initial audit status",
372 event.type(), event.subject().id());
373 store.deviceInitialAuditCompleted(event.subject().id(), false);
Charles Chan0c7c43b2016-01-14 17:39:20 -0800374
375 if (purgeOnDisconnection) {
376 store.purgeGroupEntry(deviceId);
377 }
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700378 }
379 break;
Thomas Vachuska42e8cce2015-07-29 19:25:18 -0700380 default:
381 break;
Srikanth Vavilapalli45c27c82015-01-30 12:57:56 -0800382 }
383 }
384 }
385}