Change-Id: I25b5a588648afcb79c56a26545ecb785afd0d3a3
Signed-off-by: Yevgeny Khodorkovsky <ykhodork@cisco.com>
import org.opendaylight.controller.sal.utils.Status;
import org.opendaylight.controller.sal.utils.StatusCode;
import org.opendaylight.controller.usermanager.IUserManager;
import org.opendaylight.controller.sal.utils.Status;
import org.opendaylight.controller.sal.utils.StatusCode;
import org.opendaylight.controller.usermanager.IUserManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
protected Status createRoleInternal(String role, AppRoleLevel level) {
roles.put(role, level);
groupsAuthorizations.put(role, new HashSet<ResourceGroup>());
protected Status createRoleInternal(String role, AppRoleLevel level) {
roles.put(role, level);
groupsAuthorizations.put(role, new HashSet<ResourceGroup>());
- return new Status(StatusCode.SUCCESS, null);
+ return new Status(StatusCode.SUCCESS);
protected Status removeRoleInternal(String role) {
groupsAuthorizations.remove(role);
roles.remove(role);
protected Status removeRoleInternal(String role) {
groupsAuthorizations.remove(role);
roles.remove(role);
- return new Status(StatusCode.SUCCESS, null);
+ return new Status(StatusCode.SUCCESS);
}
//verify group name is unique
if (resourceGroups.containsKey(groupName)) {
}
//verify group name is unique
if (resourceGroups.containsKey(groupName)) {
- return new Status(StatusCode.CONFLICT, "Group name already exists");
+ return new Status(StatusCode.CONFLICT, "Group name already exists");
}
//try adding resources, discard if not of type T
}
//try adding resources, discard if not of type T
- /*if (toBeAdded.size() == 0){ // TODO andrekim - we should have the ability to create a group with no resources (so we can add and delete entries)
- return new Status(StatusCode.NOTACCEPTABLE, "Group not created. No valid resources specified");
- }*/
resourceGroups.put(groupName, toBeAdded);
return (allAdded ? new Status(StatusCode.SUCCESS, "All resources added succesfully") :
new Status(StatusCode.SUCCESS, "One or more resources couldn't be added"));
resourceGroups.put(groupName, toBeAdded);
return (allAdded ? new Status(StatusCode.SUCCESS, "All resources added succesfully") :
new Status(StatusCode.SUCCESS, "One or more resources couldn't be added"));
Set<T> group = resourceGroups.get(groupName);
if (group != null && resource != null) {
group.add(resource);
Set<T> group = resourceGroups.get(groupName);
if (group != null && resource != null) {
group.add(resource);
+ // Update cluster
+ resourceGroups.put(groupName, group);
return new Status(StatusCode.SUCCESS, "Resource added successfully");
}
return new Status(StatusCode.SUCCESS, "Resource added successfully");
}
public Status removeRoleResourceGroupMapping(String groupName) {
List<String> affectedRoles = new ArrayList<String>();
Status result;
public Status removeRoleResourceGroupMapping(String groupName) {
List<String> affectedRoles = new ArrayList<String>();
Status result;
- for (Entry<String, Set<ResourceGroup>> pairs : groupsAuthorizations
- .entrySet()) {
+ for (Entry<String, Set<ResourceGroup>> pairs : groupsAuthorizations.entrySet()) {
String role = pairs.getKey();
Set<ResourceGroup> groups = pairs.getValue();
for (ResourceGroup group : groups) {
String role = pairs.getKey();
Set<ResourceGroup> groups = pairs.getValue();
for (ResourceGroup group : groups) {
- if (group.getGroupName().equals(groupName)) {
- affectedRoles.add(role);
- break;
- }
+ if (group.getGroupName().equals(groupName)) {
+ affectedRoles.add(role);
+ break;
+ }
}
}
StringBuffer msg = new StringBuffer();
for (String role : affectedRoles) {
}
}
StringBuffer msg = new StringBuffer();
for (String role : affectedRoles) {
- result = unassignResourceGroupFromRole(groupName, role);
- if (!result.isSuccess()) {
+ result = unassignResourceGroupFromRole(groupName, role);
+ if (!result.isSuccess()) {
msg.append(result.getDescription());
msg.append(' ');
msg.append(result.getDescription());
msg.append(' ');
- return new Status(StatusCode.BADREQUEST, msg.toString());
+ return new Status(StatusCode.BADREQUEST, msg.toString());
- return new Status(StatusCode.SUCCESS);
+ return new Status(StatusCode.SUCCESS);
return new Status(StatusCode.NOTALLOWED,
"All resource group cannot be removed");
}
return new Status(StatusCode.NOTALLOWED,
"All resource group cannot be removed");
}
-
- Status result = removeRoleResourceGroupMapping(groupName);
-
resourceGroups.remove(groupName);
resourceGroups.remove(groupName);
+ Status result = removeRoleResourceGroupMapping(groupName);
- if (!result.isSuccess()) {
- return result;
- }
-
- return new Status(StatusCode.SUCCESS, null);
+ return result.isSuccess() ? result :
+ new Status(StatusCode.SUCCESS, "Failed removing group from: " + result.getDescription());
Set<T> group = resourceGroups.get(groupName);
if (group != null && group.remove(resource)) {
Set<T> group = resourceGroups.get(groupName);
if (group != null && group.remove(resource)) {
+ // Update cluster
+ resourceGroups.put(groupName, group);
return new Status(StatusCode.SUCCESS, "Resource removed successfully");
}
return new Status(StatusCode.SUCCESS, "Resource removed successfully");
}
return assignResourceGroupToRoleInternal(group, privilege, role);
}
return assignResourceGroupToRoleInternal(group, privilege, role);
}
- protected Status assignResourceGroupToRoleInternal(String group,
- Privilege privilege, String role) {
- groupsAuthorizations.get(role).add(new ResourceGroup(group, privilege));
- return new Status(StatusCode.SUCCESS, null);
+ protected Status assignResourceGroupToRoleInternal(String group, Privilege privilege, String role) {
+ Set<ResourceGroup> roleGroups = groupsAuthorizations.get(role);
+ roleGroups.add(new ResourceGroup(group, privilege));
+ // Update cluster
+ groupsAuthorizations.put(role, roleGroups);
+ return new Status(StatusCode.SUCCESS);
return unassignResourceGroupFromRoleInternal(group, role);
}
return unassignResourceGroupFromRoleInternal(group, role);
}
- protected Status unassignResourceGroupFromRoleInternal(String group,
- String role) {
+ protected Status unassignResourceGroupFromRoleInternal(String group, String role) {
ResourceGroup target = null;
for (ResourceGroup rGroup : groupsAuthorizations.get(role)) {
if (rGroup.getGroupName().equals(group)) {
ResourceGroup target = null;
for (ResourceGroup rGroup : groupsAuthorizations.get(role)) {
if (rGroup.getGroupName().equals(group)) {
}
}
if (target == null) {
}
}
if (target == null) {
- return new Status(StatusCode.SUCCESS, "Group " + group
- + " was not assigned to " + role);
+ return new Status(StatusCode.SUCCESS, "Group " + group + " was not assigned to " + role);
- groupsAuthorizations.get(role).remove(target);
- return new Status(StatusCode.SUCCESS);
+ Set<ResourceGroup> groups = groupsAuthorizations.get(role);
+ groups.remove(target);
+ // Update cluster
+ groupsAuthorizations.put(role, groups);
+ return new Status(StatusCode.SUCCESS);
@Override
public List<Object> getResources(String groupName) {
return (resourceGroups.containsKey(groupName)) ? new ArrayList<Object>(
@Override
public List<Object> getResources(String groupName) {
return (resourceGroups.containsKey(groupName)) ? new ArrayList<Object>(
- resourceGroups.get(groupName)) : new ArrayList<Object>();
+ resourceGroups.get(groupName)) : new ArrayList<Object>(0);
public boolean isApplicationRole(String roleName);
/**
public boolean isApplicationRole(String roleName);
/**
- * Create a resource group for application
+ * Create a resource group for application.
- * @param groupName the name for the resource group
- * @param resources the list of resources for the group
+ * NOTE: Resource addition is "best effort", if an object is not of correct type,
+ * it is discarded.
+ *
+ * @param groupName
+ * the name for the resource group
+ * @param resources
+ * the list of resources for the group
* @return the status of the request
*/
public Status createResourceGroup(String groupName, List<Object> resources);
* @return the status of the request
*/
public Status createResourceGroup(String groupName, List<Object> resources);