Search in sources :

Example 6 with WrongAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.

the class AttributesManagerBlImpl method removeAllAttributes.

public void removeAllAttributes(PerunSession sess, Host host) throws InternalErrorException, WrongAttributeValueException {
    List<Attribute> attributes = getAttributes(sess, host);
    getAttributesManagerImpl().removeAllAttributes(sess, host);
    getPerunBl().getAuditer().log(sess, "All attributes removed for {}", host);
    for (Attribute attribute : attributes) attribute.setValue(null);
    try {
        checkAttributesValue(sess, host, attributes);
        this.checkAttributesDependencies(sess, host, null, attributes);
    } catch (WrongAttributeAssignmentException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongReferenceAttributeValueException ex) {
        throw new WrongAttributeValueException(ex);
    }
//TODO HOOK FOR HOSTS
/*
			 for(Attribute attribute: attributes) {
			 try {
			 getAttributesManagerImpl().changedAttributeHook(sess, host, new Attribute(attribute));
			 } catch (WrongAttributeValueException ex) {
		//TODO better exception here
		throw new InternalErrorException(ex);
		} catch (WrongReferenceAttributeValueException ex) {
		//TODO better exception here
		throw new InternalErrorException(ex);
		}
		}*/
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 7 with WrongAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.

the class AttributesManagerBlImpl method removeAllAttributes.

public void removeAllAttributes(PerunSession sess, Resource resource) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException {
    List<Attribute> attributes = getAttributes(sess, resource);
    getAttributesManagerImpl().removeAllAttributes(sess, resource);
    //remove all virtual attributes
    /*for(Attribute attribute : getVirtualAttributes(sess, resource)) {
			getAttributesManagerImpl().removeVirtualAttribute(sess, resource, attribute);
			}*/
    getPerunBl().getAuditer().log(sess, "All attributes removed for {}", resource);
    for (Attribute attribute : attributes) attribute.setValue(null);
    try {
        checkAttributesValue(sess, resource, attributes);
        this.checkAttributesDependencies(sess, resource, null, attributes);
    } catch (WrongAttributeAssignmentException ex) {
        throw new ConsistencyErrorException(ex);
    }
    for (Attribute attribute : attributes) {
        try {
            getAttributesManagerImpl().changedAttributeHook(sess, resource, new Attribute(attribute));
        } catch (WrongAttributeValueException ex) {
            //TODO better exception here
            throw new InternalErrorException(ex);
        } catch (WrongReferenceAttributeValueException ex) {
            //TODO better exception here
            throw new InternalErrorException(ex);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) RichAttribute(cz.metacentrum.perun.core.api.RichAttribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 8 with WrongAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.

the class AttributesManagerBlImpl method removeAttributes.

public void removeAttributes(PerunSession sess, Host host, List<? extends AttributeDefinition> attributesDefinition) throws InternalErrorException, WrongAttributeAssignmentException, WrongAttributeValueException {
    getAttributesManagerImpl().checkNamespace(sess, attributesDefinition, AttributesManager.NS_HOST_ATTR);
    List<AttributeDefinition> attributesToCheck = new ArrayList<AttributeDefinition>();
    for (AttributeDefinition attribute : attributesDefinition) {
        //skip core attributes
        if (!getAttributesManagerImpl().isCoreAttribute(sess, attribute)) {
            if (removeAttributeWithoutCheck(sess, host, attribute))
                attributesToCheck.add(attribute);
        }
    }
    this.checkAttributesValue(sess, host, attributesFromDefinitions(attributesToCheck));
    try {
        this.checkAttributesDependencies(sess, host, null, attributesFromDefinitions(attributesToCheck));
    } catch (WrongReferenceAttributeValueException ex) {
        throw new WrongAttributeValueException(ex);
    }
}
Also used : WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) ArrayList(java.util.ArrayList) AttributeDefinition(cz.metacentrum.perun.core.api.AttributeDefinition) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 9 with WrongAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.

the class urn_perun_group_attribute_def_def_unixGID_namespace method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl sess, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    try {
        String gidNamespace = attribute.getFriendlyNameParameter();
        //Special behaviour if gid is null
        if (attribute.getValue() == null) {
            List<Facility> groupFacilities = new ArrayList<Facility>();
            for (Resource r : sess.getPerunBl().getResourcesManagerBl().getAssignedResources(sess, group)) {
                groupFacilities.add(sess.getPerunBl().getResourcesManagerBl().getFacility(sess, r));
            }
            Set<String> namespacesWhereGroupMustHaveGIDifItHaveUnixNameThere = sess.getPerunBl().getModulesUtilsBl().getSetOfGroupNameNamespacesWhereFacilitiesHasTheSameGIDNamespace(sess, groupFacilities, attribute);
            for (String namespace : namespacesWhereGroupMustHaveGIDifItHaveUnixNameThere) {
                Attribute unixGroupName = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, group, A_G_unixGroupName_namespace + ":" + namespace);
                if (unixGroupName.getValue() != null) {
                    throw new WrongAttributeValueException(attribute, group, "Group is propagated to the facility where it have set unix group name so it must have unix GID too.");
                }
            }
            //Group is not propagated to any facility in this GID namespace or it doesn't have set unix name there so it doesn't need to have unix GID.
            return;
        }
        //Special behaviour if gid is null
        Integer attrValue = null;
        if (attribute.getValue() == null) {
            throw new WrongAttributeValueException(attribute, group, "Unix GID must be set");
        } else {
            attrValue = (Integer) attribute.getValue();
        }
        //check if gid is not already depleted
        Attribute usedGids = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, gidNamespace, A_E_usedGids);
        //null in value means there is no depleted or used gids
        if (usedGids.getValue() != null) {
            Map<String, String> usedGidsValue = (Map<String, String>) usedGids.getValue();
            //Dx, where x is GID means depleted value for GID x
            if (usedGidsValue.containsKey("D" + attrValue.toString())) {
                throw new WrongReferenceAttributeValueException(attribute, usedGids, group, null, gidNamespace, null, "This GID is already depleted.");
            }
        }
        //Check if gid GID is within allowed range
        sess.getPerunBl().getModulesUtilsBl().checkIfGIDIsWithinRange(sess, attribute);
        //Prepare lists for all groups and resources with same GID in the same namespace
        List<Group> allGroupsWithSameGIDInSameNamespace = new ArrayList<Group>();
        List<Resource> allResourcesWithSameGIDInSameNamespace = new ArrayList<Resource>();
        //Prepare attributes for searching through groups and resources
        Attribute groupGIDAttribute = attribute;
        Attribute resourceGIDAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGID_namespace + ":" + gidNamespace));
        resourceGIDAttribute.setValue(groupGIDAttribute.getValue());
        //Fill lists of Groups and Resources by data
        allGroupsWithSameGIDInSameNamespace.addAll(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupGIDAttribute));
        allResourcesWithSameGIDInSameNamespace.addAll(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceGIDAttribute));
        //remove this group
        allGroupsWithSameGIDInSameNamespace.remove(group);
        //Prepare list of GroupName attributes of this group
        List<Attribute> groupNamesOfGroup = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, group, A_G_unixGroupName_namespace + ":");
        //Searching through groups
        if (!allGroupsWithSameGIDInSameNamespace.isEmpty()) {
            for (Group g : allGroupsWithSameGIDInSameNamespace) {
                for (Attribute a : groupNamesOfGroup) {
                    int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, a);
                    if (compare > 0) {
                        //This is problem, there is the same attribute but have other value
                        throw new WrongReferenceAttributeValueException(attribute, a, "There is a group with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + g + " " + group);
                    }
                //Other possibilities are not problem, less than 0 mean that same attribute not exists, and 0 mean that attribute exists but have same value
                }
            }
        }
        //Searching through resources
        if (!allResourcesWithSameGIDInSameNamespace.isEmpty()) {
            for (Resource r : allResourcesWithSameGIDInSameNamespace) {
                for (Attribute a : groupNamesOfGroup) {
                    //Prepare resource version of this group attribute
                    Attribute resourceGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_R_unixGroupName_namespace + ":" + a.getFriendlyNameParameter()));
                    resourceGroupName.setValue(a.getValue());
                    int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, resourceGroupName);
                    if (compare > 0) {
                        //This is problem, there is the same attribute but have other value
                        throw new WrongReferenceAttributeValueException(attribute, a, "There is a resource with same GID (namespace: " + gidNamespace + ") and different unix group name (namespace: " + a.getFriendlyNameParameter() + "). " + r + " " + group);
                    }
                //Other possibilities are not problem, less than 0 mean that same attribute not exists, and 0 mean that attribute exists but have same value
                }
            }
        }
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
}
Also used : Group(cz.metacentrum.perun.core.api.Group) ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) Resource(cz.metacentrum.perun.core.api.Resource) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with WrongAttributeValueException

use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.

the class urn_perun_group_attribute_def_def_unixGID_namespace method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, Group group, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    String gidNamespace = attribute.getFriendlyNameParameter();
    //get attribute with usedGids for update
    //IMPORTANT: for update lock row in table of attr values, be careful when using
    Attribute usedGids;
    try {
        usedGids = session.getPerunBl().getAttributesManagerBl().getEntitylessAttributeForUpdate(session, gidNamespace, A_E_usedGids);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
    //Get Map of gids (if there is no value, use empty map
    Map<String, String> usedGidsValue = new LinkedHashMap<>();
    if (usedGids.getValue() != null)
        usedGidsValue = (Map<String, String>) usedGids.getValue();
    //initial settings
    String key = "G" + group.getId();
    String oldGid = usedGidsValue.get(key);
    //for removing gid
    if (attribute.getValue() == null) {
        //remove record from map
        if (oldGid != null) {
            usedGidsValue.remove(key);
            //looking for another oldGid value, if not exists, add depleted record
            if (!usedGidsValue.containsValue(oldGid)) {
                usedGidsValue.put("D" + oldGid, oldGid);
            }
        }
    //for setting gid
    } else {
        String newUnixGid = ((Integer) attribute.getValue()).toString();
        //add new record to map
        usedGidsValue.put(key, newUnixGid);
        //looking for another oldGid value, if not exists, add depleted record
        if (oldGid != null && !usedGidsValue.containsValue(oldGid)) {
            usedGidsValue.put("D" + oldGid, oldGid);
        }
    }
    //set new attribute value for usedGids
    usedGids.setValue(usedGidsValue);
    try {
        session.getPerunBl().getAttributesManagerBl().setAttribute(session, gidNamespace, usedGids);
    } catch (WrongAttributeValueException ex) {
        throw new WrongReferenceAttributeValueException(attribute, usedGids, ex);
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)135 Attribute (cz.metacentrum.perun.core.api.Attribute)100 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)83 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)82 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)75 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)66 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)54 Matcher (java.util.regex.Matcher)31 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)27 ArrayList (java.util.ArrayList)17 LinkedHashMap (java.util.LinkedHashMap)16 Facility (cz.metacentrum.perun.core.api.Facility)14 Map (java.util.Map)14 Resource (cz.metacentrum.perun.core.api.Resource)12 User (cz.metacentrum.perun.core.api.User)11 Group (cz.metacentrum.perun.core.api.Group)9 Pattern (java.util.regex.Pattern)8 BigDecimal (java.math.BigDecimal)7 List (java.util.List)7 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)5