1 package org.opendaylight.controller.forwardingrulesmanager.consumer.impl;
3 import java.util.ArrayList;
4 import java.util.Collection;
5 import java.util.Collections;
6 import java.util.EnumSet;
7 import java.util.HashMap;
8 import java.util.HashSet;
9 import java.util.Iterator;
10 import java.util.List;
12 import java.util.Map.Entry;
14 import java.util.concurrent.ConcurrentHashMap;
15 import java.util.concurrent.ConcurrentMap;
17 import org.opendaylight.controller.clustering.services.CacheConfigException;
18 import org.opendaylight.controller.clustering.services.CacheExistException;
19 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
20 import org.opendaylight.controller.clustering.services.IClusterServices;
21 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler;
22 import org.opendaylight.controller.md.sal.common.api.data.DataCommitHandler.DataCommitTransaction;
23 import org.opendaylight.controller.md.sal.common.api.data.DataModification;
24 import org.opendaylight.controller.sal.common.util.Rpcs;
25 import org.opendaylight.controller.sal.core.IContainer;
26 import org.opendaylight.controller.sal.core.Node;
27 import org.opendaylight.controller.sal.utils.GlobalConstants;
28 import org.opendaylight.controller.sal.utils.Status;
29 import org.opendaylight.controller.sal.utils.StatusCode;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.config.rev131024.Groups;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.config.rev131024.groups.Group;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.config.rev131024.groups.GroupKey;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.AddGroupInputBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAdded;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInputBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupListener;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInputBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.group.update.UpdatedGroupBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
45 import org.opendaylight.yangtools.concepts.Registration;
46 import org.opendaylight.yangtools.yang.binding.DataObject;
47 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
48 import org.opendaylight.yangtools.yang.binding.NotificationListener;
49 import org.opendaylight.yangtools.yang.common.RpcError;
50 import org.opendaylight.yangtools.yang.common.RpcResult;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 @SuppressWarnings("unused")
55 public class GroupConsumerImpl implements IForwardingRulesManager {
57 protected static final Logger logger = LoggerFactory.getLogger(GroupConsumerImpl.class);
58 private final GroupEventListener groupEventListener = new GroupEventListener();
59 private Registration<NotificationListener> groupListener;
60 private SalGroupService groupService;
61 private GroupDataCommitHandler commitHandler;
63 private ConcurrentMap<GroupKey, Group> originalSwGroupView;
64 private ConcurrentMap<GroupKey, Group> installedSwGroupView;
66 private ConcurrentMap<Node, List<Group>> nodeGroups;
67 private ConcurrentMap<GroupKey, Group> inactiveGroups;
69 private IClusterContainerServices clusterGroupContainerService = null;
70 private IContainer container;
72 public GroupConsumerImpl() {
74 InstanceIdentifier<? extends DataObject> path = InstanceIdentifier.builder(Groups.class).toInstance();
75 groupService = FRMConsumerImpl.getProviderSession().getRpcService(SalGroupService.class);
77 clusterGroupContainerService = FRMConsumerImpl.getClusterContainerService();
78 container = FRMConsumerImpl.getContainer();
80 if (!(cacheStartup())) {
81 logger.error("Unanle to allocate/retrieve group cache");
82 System.out.println("Unable to allocate/retrieve group cache");
85 if (null == groupService) {
86 logger.error("Consumer SAL Group Service is down or NULL. FRM may not function as intended");
87 System.out.println("Consumer SAL Group Service is down or NULL.");
92 groupListener = FRMConsumerImpl.getNotificationService().registerNotificationListener(groupEventListener);
94 if (null == groupListener) {
95 logger.error("Listener to listen on group data modifcation events");
96 System.out.println("Listener to listen on group data modifcation events.");
100 commitHandler = new GroupDataCommitHandler();
101 FRMConsumerImpl.getDataProviderService().registerCommitHandler(path, commitHandler);
104 private boolean allocateGroupCaches() {
105 if (this.clusterGroupContainerService == null) {
106 logger.warn("Group: Un-initialized clusterGroupContainerService, can't create cache");
111 clusterGroupContainerService.createCache("frm.originalSwGroupView",
112 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
114 clusterGroupContainerService.createCache("frm.installedSwGroupView",
115 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
117 clusterGroupContainerService.createCache("frm.inactiveGroups",
118 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
120 clusterGroupContainerService.createCache("frm.nodeGroups",
121 EnumSet.of(IClusterServices.cacheMode.TRANSACTIONAL));
123 // TODO for cluster mode
125 * clusterGroupContainerService.createCache(WORK_STATUS_CACHE,
126 * EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL,
127 * IClusterServices.cacheMode.ASYNC));
129 * clusterGroupContainerService.createCache(WORK_ORDER_CACHE,
130 * EnumSet.of(IClusterServices.cacheMode.NON_TRANSACTIONAL,
131 * IClusterServices.cacheMode.ASYNC));
134 } catch (CacheConfigException cce) {
135 logger.error("Group CacheConfigException");
138 } catch (CacheExistException cce) {
139 logger.error(" Group CacheExistException");
145 private void nonClusterGroupObjectCreate() {
146 originalSwGroupView = new ConcurrentHashMap<GroupKey, Group>();
147 installedSwGroupView = new ConcurrentHashMap<GroupKey, Group>();
148 nodeGroups = new ConcurrentHashMap<Node, List<Group>>();
149 inactiveGroups = new ConcurrentHashMap<GroupKey, Group>();
152 @SuppressWarnings({ "unchecked" })
153 private boolean retrieveGroupCaches() {
154 ConcurrentMap<?, ?> map;
156 if (this.clusterGroupContainerService == null) {
157 logger.warn("Group: un-initialized clusterGroupContainerService, can't retrieve cache");
158 nonClusterGroupObjectCreate();
162 map = clusterGroupContainerService.getCache("frm.originalSwGroupView");
164 originalSwGroupView = (ConcurrentMap<GroupKey, Group>) map;
166 logger.error("Retrieval of cache(originalSwGroupView) failed");
170 map = clusterGroupContainerService.getCache("frm.installedSwGroupView");
172 installedSwGroupView = (ConcurrentMap<GroupKey, Group>) map;
174 logger.error("Retrieval of cache(installedSwGroupView) failed");
178 map = clusterGroupContainerService.getCache("frm.inactiveGroups");
180 inactiveGroups = (ConcurrentMap<GroupKey, Group>) map;
182 logger.error("Retrieval of cache(inactiveGroups) failed");
186 map = clusterGroupContainerService.getCache("frm.nodeGroups");
188 nodeGroups = (ConcurrentMap<Node, List<Group>>) map;
190 logger.error("Retrieval of cache(nodeGroup) failed");
197 private boolean cacheStartup() {
198 if (allocateGroupCaches()) {
199 if (retrieveGroupCaches()) {
207 public Status validateGroup(Group group, FRMUtil.operation operation) {
208 String containerName;
210 Iterator<Bucket> bucketIterator;
211 boolean returnResult;
212 Buckets groupBuckets;
215 containerName = group.getContainerName();
217 if (null == containerName) {
218 containerName = GlobalConstants.DEFAULT.toString();
219 } else if (!FRMUtil.isNameValid(containerName)) {
220 logger.error("Container Name is invalid %s" + containerName);
221 return new Status(StatusCode.BADREQUEST, "Container Name is invalid");
224 groupName = group.getGroupName();
225 if (!FRMUtil.isNameValid(groupName)) {
226 logger.error("Group Name is invalid %s" + groupName);
227 return new Status(StatusCode.BADREQUEST, "Group Name is invalid");
230 /* returnResult = doesGroupEntryExists(group.getKey(), groupName, containerName);
232 if (FRMUtil.operation.ADD == operation && returnResult) {
233 logger.error("Record with same Group Name exists");
234 return new Status(StatusCode.BADREQUEST, "Group record exists");
235 } else if (!returnResult) {
236 logger.error("Group record does not exist");
237 return new Status(StatusCode.BADREQUEST, "Group record does not exist");
240 if (!(group.getGroupType().getIntValue() >= GroupTypes.GroupAll.getIntValue() && group.getGroupType()
241 .getIntValue() <= GroupTypes.GroupFf.getIntValue())) {
242 logger.error("Invalid Group type %d" + group.getGroupType().getIntValue());
243 return new Status(StatusCode.BADREQUEST, "Invalid Group type");
246 groupBuckets = group.getBuckets();
248 if (null != groupBuckets && null != groupBuckets.getBucket()) {
249 bucketIterator = groupBuckets.getBucket().iterator();
251 while (bucketIterator.hasNext()) {
252 if (!(FRMUtil.validateActions(bucketIterator.next().getAction()))) {
253 logger.error("Error in action bucket");
254 return new Status(StatusCode.BADREQUEST, "Invalid Group bucket contents");
260 return new Status(StatusCode.SUCCESS);
264 /* private boolean doesGroupEntryExists(GroupKey key, String groupName, String containerName) {
265 if (!originalSwGroupView.containsKey(key)) {
269 for (ConcurrentMap.Entry<GroupKey, Group> entry : originalSwGroupView.entrySet()) {
270 if (entry.getValue().getGroupName().equals(groupName)) {
271 if (entry.getValue().getContainerName().equals(containerName)) {
280 * Update Group entries to the southbound plugin/inventory and our internal
286 private Status updateGroup(InstanceIdentifier<?> path, Group groupUpdateDataObject) {
287 GroupKey groupKey = groupUpdateDataObject.getKey();
288 UpdatedGroupBuilder updateGroupBuilder = null;
290 Status groupOperationStatus = validateGroup(groupUpdateDataObject, FRMUtil.operation.UPDATE);
292 if (!groupOperationStatus.isSuccess()) {
293 logger.error("Group data object validation failed %s" + groupUpdateDataObject.getGroupName());
294 return groupOperationStatus;
297 /*if (originalSwGroupView.containsKey(groupKey)) {
298 originalSwGroupView.remove(groupKey);
299 originalSwGroupView.put(groupKey, groupUpdateDataObject);
302 if (groupUpdateDataObject.isInstall()) {
303 UpdateGroupInputBuilder groupData = new UpdateGroupInputBuilder();
304 updateGroupBuilder = new UpdatedGroupBuilder();
305 updateGroupBuilder.fieldsFrom(groupUpdateDataObject);
306 groupData.setUpdatedGroup(updateGroupBuilder.build());
307 // TODO how to get original group and modified group.
309 /* if (installedSwGroupView.containsKey(groupKey)) {
310 installedSwGroupView.remove(groupKey);
311 installedSwGroupView.put(groupKey, groupUpdateDataObject);
314 groupService.updateGroup(groupData.build());
317 return groupOperationStatus;
321 * Adds Group to the southbound plugin and our internal database
326 private Status addGroup(InstanceIdentifier<?> path, Group groupAddDataObject) {
327 GroupKey groupKey = groupAddDataObject.getKey();
328 Status groupOperationStatus = validateGroup(groupAddDataObject, FRMUtil.operation.ADD);
330 if (!groupOperationStatus.isSuccess()) {
331 logger.error("Group data object validation failed %s" + groupAddDataObject.getGroupName());
332 return groupOperationStatus;
335 //originalSwGroupView.put(groupKey, groupAddDataObject);
337 if (groupAddDataObject.isInstall()) {
338 AddGroupInputBuilder groupData = new AddGroupInputBuilder();
339 groupData.setBuckets(groupAddDataObject.getBuckets());
340 groupData.setContainerName(groupAddDataObject.getContainerName());
341 groupData.setGroupId(groupAddDataObject.getGroupId());
342 groupData.setGroupType(groupAddDataObject.getGroupType());
343 groupData.setNode(groupAddDataObject.getNode());
344 // installedSwGroupView.put(groupKey, groupAddDataObject);
345 groupService.addGroup(groupData.build());
348 return groupOperationStatus;
352 * Remove Group to the southbound plugin and our internal database
357 private Status removeGroup(InstanceIdentifier<?> path, Group groupRemoveDataObject) {
358 GroupKey groupKey = groupRemoveDataObject.getKey();
359 Status groupOperationStatus = validateGroup(groupRemoveDataObject, FRMUtil.operation.ADD);
361 if (!groupOperationStatus.isSuccess()) {
362 logger.error("Group data object validation failed %s" + groupRemoveDataObject.getGroupName());
363 return groupOperationStatus;
365 //originalSwGroupView.put(groupKey, groupAddDataObject);
367 if (groupRemoveDataObject.isInstall()) {
368 RemoveGroupInputBuilder groupData = new RemoveGroupInputBuilder();
369 groupData.setBuckets(groupRemoveDataObject.getBuckets());
370 groupData.setContainerName(groupRemoveDataObject.getContainerName());
371 groupData.setGroupId(groupRemoveDataObject.getGroupId());
372 groupData.setGroupType(groupRemoveDataObject.getGroupType());
373 groupData.setNode(groupRemoveDataObject.getNode());
374 // installedSwGroupView.put(groupKey, groupAddDataObject);
375 groupService.removeGroup(groupData.build());
378 return groupOperationStatus;
381 private RpcResult<Void> commitToPlugin(InternalTransaction transaction) {
382 for (Entry<InstanceIdentifier<?>, Group> entry : transaction.additions.entrySet()) {
384 if (!addGroup(entry.getKey(), entry.getValue()).isSuccess()) {
385 transaction.additions.remove(entry.getKey());
386 return Rpcs.getRpcResult(false, null, Collections.<RpcError>emptySet());
390 for (Entry<InstanceIdentifier<?>, Group> entry : transaction.updates.entrySet()) {
392 if (!updateGroup(entry.getKey(), entry.getValue()).isSuccess()) {
393 transaction.updates.remove(entry.getKey());
394 return Rpcs.getRpcResult(false, null, Collections.<RpcError>emptySet());
398 for (InstanceIdentifier<?> groupId : transaction.removals) {
399 DataObject removeValue = transaction.getModification().getOriginalConfigurationData().get(groupId);
401 if(removeValue instanceof Group) {
402 if(!removeGroup(groupId, (Group)removeValue).isSuccess()) {
403 return Rpcs.getRpcResult(false, null, Collections.<RpcError>emptySet());
408 return Rpcs.getRpcResult(true, null, null);
411 private final class GroupDataCommitHandler implements DataCommitHandler<InstanceIdentifier<?>, DataObject> {
414 public DataCommitTransaction<InstanceIdentifier<?>, DataObject> requestCommit(
415 DataModification<InstanceIdentifier<?>, DataObject> modification) {
416 // We should verify transaction
417 System.out.println("Coming in GroupDatacommitHandler");
418 InternalTransaction transaction = new InternalTransaction(modification);
419 transaction.prepareUpdate();
424 private final class InternalTransaction implements DataCommitTransaction<InstanceIdentifier<?>, DataObject> {
426 private final DataModification<InstanceIdentifier<?>, DataObject> modification;
429 public DataModification<InstanceIdentifier<?>, DataObject> getModification() {
433 public InternalTransaction(DataModification<InstanceIdentifier<?>, DataObject> modification) {
434 this.modification = modification;
437 Map<InstanceIdentifier<?>, Group> additions = new HashMap<>();
438 Map<InstanceIdentifier<?>, Group> updates = new HashMap<>();
439 Set<InstanceIdentifier<?>> removals = new HashSet<>();
442 * We create a plan which flows will be added, which will be updated and
443 * which will be removed based on our internal state.
446 void prepareUpdate() {
448 Set<Entry<InstanceIdentifier<?>, DataObject>> groupAdded = modification.getCreatedConfigurationData().entrySet();
449 for (Entry<InstanceIdentifier<?>, DataObject> entry : groupAdded) {
450 if (entry.getValue() instanceof Group) {
451 Group group = (Group) entry.getValue();
452 additions.put(entry.getKey(), group);
457 Set<Entry<InstanceIdentifier<?>, DataObject>> groupUpdate = modification.getUpdatedConfigurationData().entrySet();
458 for (Entry<InstanceIdentifier<?>, DataObject> entry : groupUpdate) {
459 if (entry.getValue() instanceof Group) {
460 Group group = (Group) entry.getValue();
461 ///will be fixed once getUpdatedConfigurationData returns only updated data not created data with it.
462 if (additions.containsKey(entry.getKey())) {
463 updates.put(entry.getKey(), group);
469 removals = modification.getRemovedConfigurationData();
473 * We are OK to go with execution of plan
477 public RpcResult<Void> finish() throws IllegalStateException {
479 RpcResult<Void> rpcStatus = commitToPlugin(this);
480 // We return true if internal transaction is successful.
481 // return Rpcs.getRpcResult(true, null, Collections.emptySet());
487 * We should rollback our preparation
491 public RpcResult<Void> rollback() throws IllegalStateException {
492 // NOOP - we did not modified any internal state during
493 // requestCommit phase
494 // return Rpcs.getRpcResult(true, null, Collections.emptySet());
495 return Rpcs.getRpcResult(true, null, Collections.<RpcError>emptySet());
501 final class GroupEventListener implements SalGroupListener {
503 List<GroupAdded> addedGroups = new ArrayList<>();
504 List<GroupRemoved> removedGroups = new ArrayList<>();
505 List<GroupUpdated> updatedGroups = new ArrayList<>();
508 public void onGroupAdded(GroupAdded notification) {
509 System.out.println("added Group..........................");
510 addedGroups.add(notification);
514 public void onGroupRemoved(GroupRemoved notification) {
515 // TODO Auto-generated method stub
520 public void onGroupUpdated(GroupUpdated notification) {
521 // TODO Auto-generated method stub
527 public List<DataObject> get() {
529 List<DataObject> orderedList = new ArrayList<DataObject>();
530 Collection<Group> groupList = originalSwGroupView.values();
531 for (Iterator<Group> iterator = groupList.iterator(); iterator.hasNext();) {
532 orderedList.add(iterator.next());
538 public DataObject getWithName(String name, Node n) {
540 if (this instanceof GroupConsumerImpl) {
541 Collection<Group> groupList = originalSwGroupView.values();
542 for (Iterator<Group> iterator = groupList.iterator(); iterator.hasNext();) {
543 Group group = iterator.next();
544 if (group.getNode().equals(n) && group.getGroupName().equals(name)) {