Merge "Bump model revisions to 2010.09.24.2-SNAPSHOT"
[controller.git] / opendaylight / md-sal / forwardingrules-manager / src / main / java / org / opendaylight / controller / forwardingrulesmanager / consumer / impl / GroupConsumerImpl.java
index 851e7d9b2685420b5093d8771770a64a3fe52250..7d16cb5b428452dfa25e110ebbc7ad795c298a18 100644 (file)
@@ -2,6 +2,7 @@ package org.opendaylight.controller.forwardingrulesmanager.consumer.impl;
 
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.EnumSet;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -33,17 +34,19 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.Add
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupAdded;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupRemoved;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.GroupUpdated;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.RemoveGroupInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupListener;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.SalGroupService;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.UpdateGroupInputBuilder;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.service.rev130918.group.update.UpdatedGroupBuilder;
-import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes.GroupType;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.GroupTypes;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.Buckets;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.group.types.rev131018.group.buckets.Bucket;
 import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.opendaylight.yangtools.yang.binding.NotificationListener;
+import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -55,7 +58,7 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
     private final GroupEventListener groupEventListener = new GroupEventListener();
     private Registration<NotificationListener> groupListener;
     private SalGroupService groupService;
-    private GroupDataCommitHandler commitHandler;
+    private GroupDataCommitHandler groupCommitHandler;
 
     private ConcurrentMap<GroupKey, Group> originalSwGroupView;
     private ConcurrentMap<GroupKey, Group> installedSwGroupView;
@@ -68,13 +71,9 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
 
     public GroupConsumerImpl() {
 
-        InstanceIdentifier<? extends DataObject> path = InstanceIdentifier.builder(Groups.class).child(Group.class)
-                .toInstance();
+        InstanceIdentifier<? extends DataObject> path = InstanceIdentifier.builder(Groups.class).toInstance();
         groupService = FRMConsumerImpl.getProviderSession().getRpcService(SalGroupService.class);
 
-        clusterGroupContainerService = FRMConsumerImpl.getClusterContainerService();
-        container = FRMConsumerImpl.getContainer();
-
         if (!(cacheStartup())) {
             logger.error("Unanle to allocate/retrieve group cache");
             System.out.println("Unable to allocate/retrieve group cache");
@@ -95,8 +94,10 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
             return;
         }
 
-        commitHandler = new GroupDataCommitHandler();
-        FRMConsumerImpl.getDataProviderService().registerCommitHandler(path, commitHandler);
+        groupCommitHandler = new GroupDataCommitHandler();
+        FRMConsumerImpl.getDataProviderService().registerCommitHandler(path, groupCommitHandler);
+        clusterGroupContainerService = FRMConsumerImpl.getClusterContainerService();
+        container = FRMConsumerImpl.getContainer();
     }
 
     private boolean allocateGroupCaches() {
@@ -218,25 +219,15 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
                 logger.error("Container Name is invalid %s" + containerName);
                 return new Status(StatusCode.BADREQUEST, "Container Name is invalid");
             }
-
+            
             groupName = group.getGroupName();
             if (!FRMUtil.isNameValid(groupName)) {
                 logger.error("Group Name is invalid %s" + groupName);
                 return new Status(StatusCode.BADREQUEST, "Group Name is invalid");
             }
-
-            returnResult = doesGroupEntryExists(group.getKey(), groupName, containerName);
-
-            if (FRMUtil.operation.ADD == operation && returnResult) {
-                logger.error("Record with same Group Name exists");
-                return new Status(StatusCode.BADREQUEST, "Group record exists");
-            } else if (!returnResult) {
-                logger.error("Group record does not exist");
-                return new Status(StatusCode.BADREQUEST, "Group record does not exist");
-            }
-
-            if (!(group.getGroupType().getIntValue() >= GroupType.GroupAll.getIntValue() && group.getGroupType()
-                    .getIntValue() <= GroupType.GroupFf.getIntValue())) {
+            
+            if (!(group.getGroupType().getIntValue() >= GroupTypes.GroupAll.getIntValue() && group.getGroupType()
+                    .getIntValue() <= GroupTypes.GroupFf.getIntValue())) {
                 logger.error("Invalid Group type %d" + group.getGroupType().getIntValue());
                 return new Status(StatusCode.BADREQUEST, "Invalid Group type");
             }
@@ -256,22 +247,6 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
         }
 
         return new Status(StatusCode.SUCCESS);
-
-    }
-
-    private boolean doesGroupEntryExists(GroupKey key, String groupName, String containerName) {
-        if (!originalSwGroupView.containsKey(key)) {
-            return false;
-        }
-
-        for (ConcurrentMap.Entry<GroupKey, Group> entry : originalSwGroupView.entrySet()) {
-            if (entry.getValue().getGroupName().equals(groupName)) {
-                if (entry.getValue().getContainerName().equals(containerName)) {
-                    return true;
-                }
-            }
-        }
-        return false;
     }
 
     /**
@@ -291,27 +266,12 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
             logger.error("Group data object validation failed %s" + groupUpdateDataObject.getGroupName());
             return groupOperationStatus;
         }
-
-        if (originalSwGroupView.containsKey(groupKey)) {
-            originalSwGroupView.remove(groupKey);
-            originalSwGroupView.put(groupKey, groupUpdateDataObject);
-        }
-
-        if (groupUpdateDataObject.isInstall()) {
-            UpdateGroupInputBuilder groupData = new UpdateGroupInputBuilder();
-            updateGroupBuilder = new UpdatedGroupBuilder();
-            updateGroupBuilder.fieldsFrom(groupUpdateDataObject);
-            groupData.setUpdatedGroup(updateGroupBuilder.build());
-            // TODO how to get original group and modified group.
-
-            if (installedSwGroupView.containsKey(groupKey)) {
-                installedSwGroupView.remove(groupKey);
-                installedSwGroupView.put(groupKey, groupUpdateDataObject);
-            }
-
-            groupService.updateGroup(groupData.build());
-        }
-
+        
+        UpdateGroupInputBuilder groupData = new UpdateGroupInputBuilder();
+        updateGroupBuilder = new UpdatedGroupBuilder();
+        updateGroupBuilder.fieldsFrom(groupUpdateDataObject);
+        groupData.setUpdatedGroup(updateGroupBuilder.build());        
+        groupService.updateGroup(groupData.build());
         return groupOperationStatus;
     }
 
@@ -329,29 +289,48 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
             logger.error("Group data object validation failed %s" + groupAddDataObject.getGroupName());
             return groupOperationStatus;
         }
+        
+        AddGroupInputBuilder groupData = new AddGroupInputBuilder();
+        groupData.setBuckets(groupAddDataObject.getBuckets());
+        groupData.setContainerName(groupAddDataObject.getContainerName());
+        groupData.setGroupId(groupAddDataObject.getGroupId());
+        groupData.setGroupType(groupAddDataObject.getGroupType());
+        groupData.setNode(groupAddDataObject.getNode());    
+        groupService.addGroup(groupData.build());
+        return groupOperationStatus;
+    }
 
-        originalSwGroupView.put(groupKey, groupAddDataObject);
-
-        if (groupAddDataObject.isInstall()) {
-            AddGroupInputBuilder groupData = new AddGroupInputBuilder();
-            groupData.setBuckets(groupAddDataObject.getBuckets());
-            groupData.setContainerName(groupAddDataObject.getContainerName());
-            groupData.setGroupId(groupAddDataObject.getGroupId());
-            groupData.setGroupType(groupAddDataObject.getGroupType());
-            groupData.setNode(groupAddDataObject.getNode());
-            installedSwGroupView.put(groupKey, groupAddDataObject);
-            groupService.addGroup(groupData.build());
-        }
+    /**
+     * Remove Group to the southbound plugin and our internal database
+     *
+     * @param path
+     * @param dataObject
+     */
+    private Status removeGroup(InstanceIdentifier<?> path, Group groupRemoveDataObject) {
+        GroupKey groupKey = groupRemoveDataObject.getKey();
+        Status groupOperationStatus = validateGroup(groupRemoveDataObject, FRMUtil.operation.ADD);
 
+        if (!groupOperationStatus.isSuccess()) {
+            logger.error("Group data object validation failed %s" + groupRemoveDataObject.getGroupName());
+            return groupOperationStatus;
+        }
+       
+        RemoveGroupInputBuilder groupData = new RemoveGroupInputBuilder();
+        groupData.setBuckets(groupRemoveDataObject.getBuckets());
+        groupData.setContainerName(groupRemoveDataObject.getContainerName());
+        groupData.setGroupId(groupRemoveDataObject.getGroupId());
+        groupData.setGroupType(groupRemoveDataObject.getGroupType());
+        groupData.setNode(groupRemoveDataObject.getNode());    
+        groupService.removeGroup(groupData.build());  
         return groupOperationStatus;
     }
-
-    private RpcResult<Void> commitToPlugin(internalTransaction transaction) {
+    
+    private RpcResult<Void> commitToPlugin(InternalTransaction transaction) {
         for (Entry<InstanceIdentifier<?>, Group> entry : transaction.additions.entrySet()) {
 
             if (!addGroup(entry.getKey(), entry.getValue()).isSuccess()) {
                 transaction.additions.remove(entry.getKey());
-                return Rpcs.getRpcResult(false, null, null);
+                return Rpcs.getRpcResult(false, null, Collections.<RpcError>emptySet());
             }
         }
 
@@ -359,32 +338,37 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
 
             if (!updateGroup(entry.getKey(), entry.getValue()).isSuccess()) {
                 transaction.updates.remove(entry.getKey());
-                return Rpcs.getRpcResult(false, null, null);
+                return Rpcs.getRpcResult(false, null, Collections.<RpcError>emptySet());
             }
         }
 
-        for (InstanceIdentifier<?> removal : transaction.removals) {
-            // removeFlow(removal);
+        for (InstanceIdentifier<?> groupId : transaction.removals) {
+                       DataObject removeValue = transaction.getModification().getOriginalConfigurationData().get(groupId);     
+                
+                       if(removeValue instanceof Group) {
+                           if(!removeGroup(groupId, (Group)removeValue).isSuccess()) {
+                               return Rpcs.getRpcResult(false, null, Collections.<RpcError>emptySet()); 
+                           }             
+                       }
         }
 
-        return Rpcs.getRpcResult(true, null, null);
+        return Rpcs.getRpcResult(true, null, Collections.<RpcError>emptySet());
     }
 
     private final class GroupDataCommitHandler implements DataCommitHandler<InstanceIdentifier<?>, DataObject> {
 
-        @SuppressWarnings("unchecked")
         @Override
         public DataCommitTransaction<InstanceIdentifier<?>, DataObject> requestCommit(
                 DataModification<InstanceIdentifier<?>, DataObject> modification) {
             // We should verify transaction
             System.out.println("Coming in GroupDatacommitHandler");
-            internalTransaction transaction = new internalTransaction(modification);
+            InternalTransaction transaction = new InternalTransaction(modification);
             transaction.prepareUpdate();
             return transaction;
         }
     }
 
-    private final class internalTransaction implements DataCommitTransaction<InstanceIdentifier<?>, DataObject> {
+    private final class InternalTransaction implements DataCommitTransaction<InstanceIdentifier<?>, DataObject> {
 
         private final DataModification<InstanceIdentifier<?>, DataObject> modification;
 
@@ -393,7 +377,7 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
             return modification;
         }
 
-        public internalTransaction(DataModification<InstanceIdentifier<?>, DataObject> modification) {
+        public InternalTransaction(DataModification<InstanceIdentifier<?>, DataObject> modification) {
             this.modification = modification;
         }
 
@@ -407,33 +391,31 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
          *
          */
         void prepareUpdate() {
-
-            Set<Entry<InstanceIdentifier<?>, DataObject>> puts = modification.getUpdatedConfigurationData().entrySet();
-            for (Entry<InstanceIdentifier<?>, DataObject> entry : puts) {
+               
+                Set<Entry<InstanceIdentifier<?>, DataObject>> groupAdded = modification.getCreatedConfigurationData().entrySet();
+             for (Entry<InstanceIdentifier<?>, DataObject> entry : groupAdded) {
+                 if (entry.getValue() instanceof Group) {
+                     Group group = (Group) entry.getValue();
+                     additions.put(entry.getKey(), group);                     
+                 }
+
+             }
+
+            Set<Entry<InstanceIdentifier<?>, DataObject>> groupUpdate = modification.getUpdatedConfigurationData().entrySet();
+            for (Entry<InstanceIdentifier<?>, DataObject> entry : groupUpdate) {
                 if (entry.getValue() instanceof Group) {
                     Group group = (Group) entry.getValue();
-                    preparePutEntry(entry.getKey(), group);
+                    ///will be fixed once getUpdatedConfigurationData returns only updated data not created data with it.
+                    if (additions.containsKey(entry.getKey())) {
+                       updates.put(entry.getKey(), group);
+                    }
                 }
 
             }
 
             removals = modification.getRemovedConfigurationData();
         }
-
-        private void preparePutEntry(InstanceIdentifier<?> key, Group group) {
-
-            Group original = originalSwGroupView.get(key);
-            if (original != null) {
-                // It is update for us
-
-                updates.put(key, group);
-            } else {
-                // It is addition for us
-
-                additions.put(key, group);
-            }
-        }
-
+        
         /**
          * We are OK to go with execution of plan
          *
@@ -457,7 +439,7 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
             // NOOP - we did not modified any internal state during
             // requestCommit phase
             // return Rpcs.getRpcResult(true, null, Collections.emptySet());
-            return Rpcs.getRpcResult(true, null, null);
+            return Rpcs.getRpcResult(true, null, Collections.<RpcError>emptySet());
 
         }
 
@@ -471,7 +453,6 @@ public class GroupConsumerImpl implements IForwardingRulesManager {
 
         @Override
         public void onGroupAdded(GroupAdded notification) {
-            System.out.println("added Group..........................");
             addedGroups.add(notification);
         }