Search in sources :

Example 81 with ConsistencyErrorException

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

the class urn_perun_group_resource_attribute_def_virt_unixGID method fillAttribute.

@Override
public Attribute fillAttribute(PerunSessionImpl sess, Group group, Resource resource, AttributeDefinition attributeDefinition) throws WrongAttributeAssignmentException {
    Attribute attribute = new Attribute(attributeDefinition);
    Attribute unixGIDNamespaceAttribute;
    try {
        unixGIDNamespaceAttribute = sess.getPerunBl().getModulesUtilsBl().getUnixGIDNamespaceAttributeWithNotNullValue(sess, resource);
    } catch (WrongReferenceAttributeValueException ex) {
        return attribute;
    }
    Attribute gidAttribute;
    try {
        gidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, group, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGID-namespace:" + unixGIDNamespaceAttribute.getValue());
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
    try {
        sess.getPerunBl().getAttributesManagerBl().checkAttributeSyntax(sess, group, gidAttribute);
        sess.getPerunBl().getAttributesManagerBl().forceCheckAttributeSemantics(sess, group, gidAttribute);
        // check passed, we can use value from this physical attribute
        attribute.setValue(gidAttribute.getValue());
        return attribute;
    } catch (WrongAttributeValueException ex) {
        return attribute;
    } catch (WrongReferenceAttributeValueException ex) {
        // Physical attribute have wrong value, let's find a new one
        gidAttribute.setValue(null);
        gidAttribute = sess.getPerunBl().getAttributesManagerBl().fillAttribute(sess, group, gidAttribute);
        attribute.setValue(gidAttribute.getValue());
        return attribute;
    }
}
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) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 82 with ConsistencyErrorException

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

the class urn_perun_group_resource_attribute_def_virt_unixGID method setAttributeValue.

@Override
public boolean setAttributeValue(PerunSessionImpl sess, Group group, Resource resource, Attribute attribute) throws WrongReferenceAttributeValueException {
    Attribute unixGIDNamespaceAttribute = sess.getPerunBl().getModulesUtilsBl().getUnixGIDNamespaceAttributeWithNotNullValue(sess, resource);
    try {
        Attribute gidAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, AttributesManager.NS_GROUP_ATTR_DEF + ":unixGID-namespace:" + unixGIDNamespaceAttribute.getValue()));
        gidAttribute.setValue(attribute.getValue());
        return sess.getPerunBl().getAttributesManagerBl().setAttributeWithoutCheck(sess, group, gidAttribute);
    } catch (WrongAttributeValueException | WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) 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)

Example 83 with ConsistencyErrorException

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

the class urn_perun_resource_attribute_def_def_unixGID_namespace method fillAttribute.

@Override
public Attribute fillAttribute(PerunSessionImpl sess, Resource resource, AttributeDefinition attributeDefinition) throws WrongAttributeAssignmentException {
    Attribute attribute = new Attribute(attributeDefinition);
    String gidNamespace = attribute.getFriendlyNameParameter();
    // First I get all GroupNames of this resource (for any namespaces)
    List<Attribute> groupNamesOfResource = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, resource, A_R_unixGroupName_namespace + ":");
    // If there exist some groupName of this resource
    if (!groupNamesOfResource.isEmpty()) {
        // Get All Groups and Resources with some same GroupName in the same Namespace
        List<Group> groupsWithSameGroupNameInSameNamespace = new ArrayList<>();
        List<Resource> resourcesWithSameGroupNameInSameNamespace = new ArrayList<>();
        for (Attribute attr : groupNamesOfResource) {
            Attribute groupNameOfGroup;
            try {
                groupNameOfGroup = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_G_unixGroupName_namespace + ":" + attr.getFriendlyNameParameter()));
            } catch (AttributeNotExistsException ex) {
                throw new ConsistencyErrorException("AttributeDefinition for group_def_unixGroupName-namespace:" + attr.getFriendlyNameParameter() + " must exists", ex);
            }
            groupNameOfGroup.setValue(attr.getValue());
            // Get all resources and groups with some GroupName same with same Namespace
            groupsWithSameGroupNameInSameNamespace.addAll(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupNameOfGroup));
            resourcesWithSameGroupNameInSameNamespace.addAll(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, attr));
        }
        // Test if exists common GID for this group and other groups and resources
        Integer commonGID = sess.getPerunBl().getModulesUtilsBl().getCommonGIDOfGroupsWithSameNameInSameNamespace(sess, groupsWithSameGroupNameInSameNamespace, gidNamespace, null);
        commonGID = sess.getPerunBl().getModulesUtilsBl().getCommonGIDOfResourcesWithSameNameInSameNamespace(sess, resourcesWithSameGroupNameInSameNamespace, gidNamespace, commonGID);
        // If commonGID exists, set it
        if (commonGID != null) {
            attribute.setValue(commonGID);
            return attribute;
        }
    }
    // If commonGID not exists, try to set new one
    try {
        Integer freeGID = sess.getPerunBl().getModulesUtilsBl().getFreeGID(sess, attribute);
        if (freeGID == null) {
            // free GID not found
            log.warn("Free unix gid not found for resource:[" + resource + "] in unix group namespace " + gidNamespace);
        } else if (freeGID > 0 || freeGID < 0) {
            // free GID found
            attribute.setValue(freeGID);
        }
        return attribute;
    } 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)

Example 84 with ConsistencyErrorException

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

the class urn_perun_resource_attribute_def_def_unixGID_namespace method checkAttributeSemantics.

@Override
public void checkAttributeSemantics(PerunSessionImpl sess, Resource resource, Attribute attribute) throws WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    try {
        String gidNamespace = attribute.getFriendlyNameParameter();
        // Special behaviour if gid is null
        Integer attrValue;
        if (attribute.getValue() == null) {
            throw new WrongReferenceAttributeValueException(attribute, null, resource, null, "Unix GID must be set");
        } else {
            attrValue = attribute.valueAsInteger();
        }
        // Check if GID is within allowed range
        try {
            sess.getPerunBl().getModulesUtilsBl().checkIfGIDIsWithinRange(sess, attribute);
        } catch (WrongAttributeValueException ex) {
            throw new WrongReferenceAttributeValueException(ex);
        }
        // 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 = usedGids.valueAsMap();
            // Dx, where x is GID means depleted value for GID x
            if (usedGidsValue.containsKey("D" + attrValue.toString())) {
                throw new WrongReferenceAttributeValueException(attribute, usedGids, resource, null, gidNamespace, null, "This GID is already depleted.");
            }
        }
        // Prepare lists for all groups and resources with same GID in the same namespace
        // Prepare attributes for searching through groups and resources
        Attribute resourceGIDAttribute = attribute;
        Attribute groupGIDAttribute = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_G_unixGID_namespace + ":" + gidNamespace));
        groupGIDAttribute.setValue(resourceGIDAttribute.getValue());
        // Fill lists of Groups and Resources by data
        List<Group> allGroupsWithSameGIDInSameNamespace = new ArrayList<>(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupGIDAttribute));
        List<Resource> allResourcesWithSameGIDInSameNamespace = new ArrayList<>(sess.getPerunBl().getResourcesManagerBl().getResourcesByAttribute(sess, resourceGIDAttribute));
        // remove this resource
        allResourcesWithSameGIDInSameNamespace.remove(resource);
        // Prepare list of GroupName attributes of this resource
        List<Attribute> groupNamesOfResource = sess.getPerunBl().getAttributesManagerBl().getAllAttributesStartWithNameWithoutNullValue(sess, resource, A_R_unixGroupName_namespace + ":");
        // Searching through groups
        if (!allGroupsWithSameGIDInSameNamespace.isEmpty()) {
            for (Group g : allGroupsWithSameGIDInSameNamespace) {
                for (Attribute a : groupNamesOfResource) {
                    // Prepare group version of this group attribute
                    Attribute groupGroupName = new Attribute(sess.getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, A_G_unixGroupName_namespace + ":" + a.getFriendlyNameParameter()));
                    groupGroupName.setValue(a.getValue());
                    int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, g, groupGroupName);
                    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 + " " + resource);
                    }
                // 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 : groupNamesOfResource) {
                    int compare = sess.getPerunBl().getModulesUtilsBl().haveTheSameAttributeWithTheSameNamespace(sess, r, a);
                    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 + " " + resource);
                    }
                // 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) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 85 with ConsistencyErrorException

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

the class urn_perun_member_resource_attribute_def_virt_fileQuotas method getAttributeValue.

@Override
public Attribute getAttributeValue(PerunSessionImpl sess, Member member, Resource resource, AttributeDefinition attributeDefinition) {
    Attribute attribute = new Attribute(attributeDefinition);
    // get resource quotas
    Map<String, Pair<BigDecimal, BigDecimal>> resourceTransferedQuotas;
    Attribute resourceQuotas;
    try {
        resourceQuotas = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, resource, A_R_defaultFileQuotas);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongAttributeAssignmentException ex) {
        throw new InternalErrorException(ex);
    }
    if (resourceQuotas == null || resourceQuotas.getValue() == null)
        resourceTransferedQuotas = new HashMap<>();
    else {
        try {
            resourceTransferedQuotas = sess.getPerunBl().getModulesUtilsBl().checkAndTransferQuotas(resourceQuotas, resource, null, false);
        } catch (WrongAttributeValueException ex) {
            throw new ConsistencyErrorException("Quotas on resource " + resource + " are in bad format.", ex);
        }
    }
    // get members quotas
    Map<String, Pair<BigDecimal, BigDecimal>> memberTransferedQuotas;
    Attribute memberQuotas;
    try {
        memberQuotas = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, member, resource, A_MR_fileQuotas);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongAttributeAssignmentException | MemberResourceMismatchException ex) {
        throw new InternalErrorException(ex);
    }
    if (memberQuotas == null || memberQuotas.getValue() == null)
        memberTransferedQuotas = new HashMap<>();
    else {
        try {
            memberTransferedQuotas = sess.getPerunBl().getModulesUtilsBl().checkAndTransferQuotas(memberQuotas, resource, member, false);
        } catch (WrongAttributeValueException ex) {
            throw new ConsistencyErrorException("Quotas on resource " + resource + " for member " + member + " are in bad format.", ex);
        }
    }
    // get members quotas override
    Map<String, Pair<BigDecimal, BigDecimal>> memberTransferedQuotasOverride;
    Attribute memberQuotasOverride;
    try {
        memberQuotasOverride = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, member, resource, A_MR_fileQuotasOverride);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException(ex);
    } catch (WrongAttributeAssignmentException | MemberResourceMismatchException ex) {
        throw new InternalErrorException(ex);
    }
    if (memberQuotasOverride == null || memberQuotasOverride.getValue() == null)
        memberTransferedQuotasOverride = new HashMap<>();
    else {
        try {
            memberTransferedQuotasOverride = sess.getPerunBl().getModulesUtilsBl().checkAndTransferQuotas(memberQuotasOverride, resource, member, false);
        } catch (WrongAttributeValueException ex) {
            throw new ConsistencyErrorException("Override quotas on resource " + resource + " for member " + member + " are in bad format.", ex);
        }
    }
    // Merge quotas for member on resource
    Map<String, Pair<BigDecimal, BigDecimal>> finalMemberResourceQuotas = sess.getPerunBl().getModulesUtilsBl().mergeMemberAndResourceTransferredQuotas(resourceTransferedQuotas, memberTransferedQuotas, memberTransferedQuotasOverride);
    // set final value to attribute
    attribute.setValue(sess.getPerunBl().getModulesUtilsBl().transferQuotasBackToAttributeValue(finalMemberResourceQuotas, false));
    // return attribute
    return attribute;
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) MemberResourceMismatchException(cz.metacentrum.perun.core.api.exceptions.MemberResourceMismatchException) Attribute(cz.metacentrum.perun.core.api.Attribute) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) 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) Pair(cz.metacentrum.perun.core.api.Pair)

Aggregations

ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)281 Attribute (cz.metacentrum.perun.core.api.Attribute)212 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)162 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)120 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)111 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)102 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)84 User (cz.metacentrum.perun.core.api.User)60 ArrayList (java.util.ArrayList)51 Group (cz.metacentrum.perun.core.api.Group)44 Facility (cz.metacentrum.perun.core.api.Facility)41 Resource (cz.metacentrum.perun.core.api.Resource)37 Member (cz.metacentrum.perun.core.api.Member)30 LinkedHashMap (java.util.LinkedHashMap)23 Vo (cz.metacentrum.perun.core.api.Vo)22 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)21 GroupResourceMismatchException (cz.metacentrum.perun.core.api.exceptions.GroupResourceMismatchException)20 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)19 MemberNotExistsException (cz.metacentrum.perun.core.api.exceptions.MemberNotExistsException)17 VoNotExistsException (cz.metacentrum.perun.core.api.exceptions.VoNotExistsException)17