Search in sources :

Example 56 with AttributeNotExistsException

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

the class urn_perun_resource_attribute_def_def_defaultHomeMountPoint method fillAttribute.

public Attribute fillAttribute(PerunSessionImpl perunSession, Resource resource, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
    Attribute resourceAttribute = null;
    try {
        resourceAttribute = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_homeMountPoints);
    } catch (AttributeNotExistsException ex) {
        throw new InternalErrorException("no homemountpoints set on this resource", ex);
    }
    Attribute retAttribute = new Attribute(attribute);
    List<?> homeMntPoints = (List<?>) resourceAttribute.getValue();
    if (homeMntPoints != null && homeMntPoints.size() > 0) {
        retAttribute.setValue(homeMntPoints.get(0));
    }
    return retAttribute;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) List(java.util.List) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Example 57 with AttributeNotExistsException

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

the class urn_perun_resource_attribute_def_def_defaultHomeMountPoint method checkAttributeValue.

/**
	 * Checks if the homemountpoint is contained in list of homemountpoint at underlying facility
	 * Allows valid unix paths
	 * @param perunSession
	 * @param resource
	 * @param attribute
	 * @throws InternalErrorException
	 * @throws WrongAttributeValueException
	 * @throws WrongReferenceAttributeValueException
	 * @throws WrongAttributeAssignmentException
	 */
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    if (attribute.getValue() == null) {
        throw new WrongAttributeValueException(attribute);
    }
    Attribute resourceAttribute = null;
    try {
        resourceAttribute = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_homeMountPoints);
    } catch (AttributeNotExistsException ex) {
        throw new InternalErrorException(ex);
    }
    if (resourceAttribute.getValue() == null)
        throw new WrongReferenceAttributeValueException(resourceAttribute);
    List<?> homeMntPoints = (List<?>) resourceAttribute.getValue();
    if (!homeMntPoints.contains(attribute.getValue())) {
        throw new WrongAttributeValueException(attribute, "Attribute value ins't defined in underlying resource. Attribute name=" + A_R_homeMountPoints);
    }
    Pattern pattern = Pattern.compile("^/[-a-zA-Z.0-9_/]*$");
    Matcher match = pattern.matcher((String) attribute.getValue());
    if (!match.matches()) {
        throw new WrongAttributeValueException(attribute, "Wrong def. mount point format");
    }
}
Also used : Pattern(java.util.regex.Pattern) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) List(java.util.List) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 58 with AttributeNotExistsException

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

the class urn_perun_resource_attribute_def_def_defaultDataLimit method checkAttributeValue.

@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    Attribute attrDefaultDataQuota = null;
    String defaultDataQuota = null;
    String defaultDataLimit = null;
    String defaultDataQuotaNumber = null;
    String defaultDataQuotaLetter = null;
    String defaultDataLimitNumber = null;
    String defaultDataLimitLetter = null;
    //Check if attribute value has the right exp pattern (can be null)
    if (attribute.getValue() != null) {
        Matcher testMatcher = testingPattern.matcher((String) attribute.getValue());
        if (!testMatcher.find())
            throw new WrongAttributeValueException(attribute, resource, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
    } else
        return;
    //Get DefaultDataQuota attribute
    try {
        attrDefaultDataQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultDataQuota);
    } catch (AttributeNotExistsException ex) {
        throw new ConsistencyErrorException("Attribute with defaultDataQuota from resource " + resource.getId() + " could not obtained.", ex);
    }
    //Get DefaultDataLimit value
    if (attribute.getValue() != null) {
        defaultDataLimit = (String) attribute.getValue();
        Matcher numberMatcher = numberPattern.matcher(defaultDataLimit);
        Matcher letterMatcher = letterPattern.matcher(defaultDataLimit);
        numberMatcher.find();
        letterMatcher.find();
        try {
            defaultDataLimitNumber = defaultDataLimit.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataLimitNumber = null;
        }
        try {
            defaultDataLimitLetter = defaultDataLimit.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataLimitLetter = "G";
        }
    }
    BigDecimal limitNumber;
    if (defaultDataLimitNumber != null)
        limitNumber = new BigDecimal(defaultDataLimitNumber.replace(',', '.'));
    else
        limitNumber = new BigDecimal("0");
    if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongAttributeValueException(attribute, resource, attribute + " can't be less than 0.");
    }
    //Get DefaultDataQuota value
    if (attrDefaultDataQuota != null && attrDefaultDataQuota.getValue() != null) {
        defaultDataQuota = (String) attrDefaultDataQuota.getValue();
        Matcher numberMatcher = numberPattern.matcher(defaultDataQuota);
        Matcher letterMatcher = letterPattern.matcher(defaultDataQuota);
        numberMatcher.find();
        letterMatcher.find();
        try {
            defaultDataQuotaNumber = defaultDataQuota.substring(numberMatcher.start(), numberMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataQuotaNumber = null;
        }
        try {
            defaultDataQuotaLetter = defaultDataQuota.substring(letterMatcher.start(), letterMatcher.end());
        } catch (IllegalStateException ex) {
            defaultDataQuotaLetter = "G";
        }
    }
    BigDecimal quotaNumber;
    if (defaultDataQuotaNumber != null)
        quotaNumber = new BigDecimal(defaultDataQuotaNumber.replace(',', '.'));
    else
        quotaNumber = new BigDecimal("0");
    if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
        throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataQuota, resource, null, resource, null, attrDefaultDataQuota + " cant be less than 0.");
    }
    //Compare DefaultDataLimit with DefaultDataQuota
    if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
        if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataQuota, resource, null, resource, null, "Try to set limited limit, but there is still set unlimited Quota.");
        }
    } else if ((quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) != 0) && (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0)) {
        if (defaultDataLimitLetter.equals("K"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
        else if (defaultDataLimitLetter.equals("M"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
        else if (defaultDataLimitLetter.equals("T"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
        else if (defaultDataLimitLetter.equals("P"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
        else if (defaultDataLimitLetter.equals("E"))
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
        else
            limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
        if (defaultDataQuotaLetter.equals("K"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
        else if (defaultDataQuotaLetter.equals("M"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
        else if (defaultDataQuotaLetter.equals("T"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
        else if (defaultDataQuotaLetter.equals("P"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
        else if (defaultDataQuotaLetter.equals("E"))
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
        else
            quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
        if (limitNumber.compareTo(quotaNumber) < 0) {
            throw new WrongReferenceAttributeValueException(attribute, attrDefaultDataQuota, resource, null, resource, null, attribute + " must be more than or equals to " + attrDefaultDataQuota);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) Matcher(java.util.regex.Matcher) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException) BigDecimal(java.math.BigDecimal)

Example 59 with AttributeNotExistsException

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

the class urn_perun_resource_attribute_def_def_fairshareGroupName method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    //Null is ok, it means this resource is not fairshare group
    if (attribute.getValue() == null) {
        return;
    }
    String gName = (String) attribute.getValue();
    //Test if gName matchers regex
    Matcher matcher = pattern.matcher(gName);
    if (!matcher.matches()) {
        throw new WrongAttributeValueException(attribute, resource, "Wrong format of group fairshare name. Max length is 12, only letters are allowed.");
    }
    //On facility must be fairshare group name unique (between all resources of this facility)
    Facility facility = perunSession.getPerunBl().getResourcesManagerBl().getFacility(perunSession, resource);
    List<Resource> facilityResources = perunSession.getPerunBl().getFacilitiesManagerBl().getAssignedResources(perunSession, facility);
    facilityResources.remove(resource);
    List<String> resourcesFairshareGroupNames = new ArrayList<>();
    for (Resource res : facilityResources) {
        try {
            Attribute resFairshareName = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, res, attribute.getName());
            if (resFairshareName.getValue() == null)
                continue;
            resourcesFairshareGroupNames.add((String) resFairshareName.getValue());
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        }
    }
    if (resourcesFairshareGroupNames.contains(gName))
        throw new WrongAttributeValueException(attribute, resource, "This name is already taken (not unique). Choose another one.");
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Matcher(java.util.regex.Matcher) Attribute(cz.metacentrum.perun.core.api.Attribute) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) Resource(cz.metacentrum.perun.core.api.Resource) ArrayList(java.util.ArrayList) Facility(cz.metacentrum.perun.core.api.Facility) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 60 with AttributeNotExistsException

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

the class urn_perun_user_attribute_def_def_uid_namespace method fillAttribute.

@Override
public /**
	 * Fills the new UID for the user at the specified facility. First empty slot
	 * in range (minUID, maxUID) is returned.
	 */
Attribute fillAttribute(PerunSessionImpl sess, User user, AttributeDefinition attribute) throws InternalErrorException, WrongAttributeAssignmentException {
    String uidNamespace = attribute.getFriendlyNameParameter();
    Attribute atr = new Attribute(attribute);
    Attribute minUidAttribute = null;
    Attribute maxUidAttribute = null;
    try {
        minUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_minUID);
        maxUidAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_maxUID);
    } catch (AttributeNotExistsException e) {
        throw new ConsistencyErrorException("minUid and maxUid attributes are required", e);
    }
    Integer min = (Integer) minUidAttribute.getValue();
    Integer max = (Integer) maxUidAttribute.getValue();
    if (min == null || max == null) {
        //we couldnt determine necessary attributes for getting new uID
        return atr;
    }
    SortedSet<Integer> values = new TreeSet<Integer>();
    // Get all attributes urn:perun:member:attribute-def:def:uid-namespace:[uid-namespace], then we can get the new UID
    List<Attribute> uidAttributes = sess.getPerunBl().getAttributesManagerBl().getAttributesByAttributeDefinition(sess, attribute);
    for (Attribute uidAttribute : uidAttributes) {
        if (uidAttribute.getValue() != null)
            values.add((Integer) uidAttribute.getValue());
    }
    String uidPolicy;
    try {
        uidPolicy = (String) sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, uidNamespace, A_E_namespace_namespace_uid_policy).getValue();
    } catch (AttributeNotExistsException e) {
        throw new InternalErrorException(e);
    }
    if (UID_POLICY_INCREMENT.equals(uidPolicy)) {
        // Only use +1 for max UID
        if (values.isEmpty()) {
            atr.setValue(min);
        } else {
            atr.setValue(Collections.max(values) + 1);
        }
        return atr;
    } else {
        if (values.size() == 0) {
            atr.setValue(min);
            return atr;
        } else {
            for (int i = min; i < max; i++) {
                if (!values.contains(i)) {
                    atr.setValue(i);
                    return atr;
                }
            }
            return atr;
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) Attribute(cz.metacentrum.perun.core.api.Attribute) TreeSet(java.util.TreeSet) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException)

Aggregations

AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)138 Attribute (cz.metacentrum.perun.core.api.Attribute)120 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)94 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)81 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)75 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)64 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)59 ArrayList (java.util.ArrayList)30 Resource (cz.metacentrum.perun.core.api.Resource)25 Matcher (java.util.regex.Matcher)19 Facility (cz.metacentrum.perun.core.api.Facility)17 User (cz.metacentrum.perun.core.api.User)17 LinkedHashMap (java.util.LinkedHashMap)15 Group (cz.metacentrum.perun.core.api.Group)14 Map (java.util.Map)13 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)12 List (java.util.List)11 HashSet (java.util.HashSet)8 BigDecimal (java.math.BigDecimal)7 Pattern (java.util.regex.Pattern)6