use of cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException in project perun by CESNET.
the class urn_perun_resource_attribute_def_def_defaultFilesLimit method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrDefaultFilesQuota = null;
Integer defaultFilesQuota = null;
Integer defaultFilesLimit = null;
//get defaultFilesQuota attribute
try {
attrDefaultFilesQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, A_R_defaultFilesQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with defaultFilesQuota from resource " + resource.getId() + " could not obtained.", ex);
}
//get defaultFilesLimit value
if (attribute.getValue() != null) {
defaultFilesLimit = (Integer) attribute.getValue();
}
if (defaultFilesLimit != null && defaultFilesLimit < 0)
throw new WrongAttributeValueException(attribute, resource, attribute + " cannot be less than 0.");
//get defaultFilesQuota value
if (attrDefaultFilesQuota != null && attrDefaultFilesQuota.getValue() != null) {
defaultFilesQuota = (Integer) attrDefaultFilesQuota.getValue();
}
if (defaultFilesQuota != null && defaultFilesQuota < 0)
throw new ConsistencyErrorException(attrDefaultFilesQuota + " cannot be less than 0.");
//Compare defaultFilesLimit with defaultFilesQuota
if (defaultFilesQuota == null || defaultFilesQuota == 0) {
if (defaultFilesLimit != null && defaultFilesLimit != 0)
throw new WrongReferenceAttributeValueException(attribute, attrDefaultFilesQuota, resource, null, resource, null, "Try to set limited limit, but there is still set unlimited Quota.");
} else if ((defaultFilesQuota != null && defaultFilesQuota != 0) && (defaultFilesLimit != null && defaultFilesLimit != 0)) {
if (defaultFilesLimit < defaultFilesQuota)
throw new WrongReferenceAttributeValueException(attribute, attrDefaultFilesQuota, resource, null, resource, null, attribute + " must be more than or equals to " + attrDefaultFilesQuota);
}
}
use of cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException 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);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException 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.");
}
use of cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException in project perun by CESNET.
the class urn_perun_user_facility_attribute_def_virt_defaultUnixGID method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Facility facility, User user, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
if (attribute.getValue() == null)
throw new WrongAttributeValueException(attribute, user, facility, "Attribute can't be null.");
try {
Attribute defaultUnixGID = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, facility, user, AttributesManager.NS_USER_FACILITY_ATTR_DEF + ":defaultUnixGID");
defaultUnixGID.setValue(attribute.getValue());
perunSession.getPerunBl().getAttributesManagerBl().checkAttributeValue(perunSession, facility, user, defaultUnixGID);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException(ex);
}
}
use of cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException in project perun by CESNET.
the class urn_perun_user_facility_attribute_def_virt_login method checkAttributeValue.
@Override
public /**
* Calls checkAttribute on u:login-namespace:[login-namespace]
*/
void checkAttributeValue(PerunSessionImpl sess, Facility facility, User user, Attribute attribute) throws WrongAttributeValueException, WrongReferenceAttributeValueException, InternalErrorException, WrongAttributeAssignmentException {
try {
Attribute loginNamespaceAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, facility, AttributesManager.NS_FACILITY_ATTR_DEF + ":login-namespace");
Attribute loginAttribute = null;
if (loginNamespaceAttribute.getValue() != null) {
loginAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, user, AttributesManager.NS_USER_ATTR_DEF + ":login-namespace:" + (String) loginNamespaceAttribute.getValue());
if (attribute.getValue() == null)
throw new WrongAttributeValueException(loginAttribute, user, facility, "Login can't be null");
loginAttribute.setValue(attribute.getValue());
sess.getPerunBl().getAttributesManagerBl().checkAttributeValue(sess, user, loginAttribute);
} else {
throw new WrongAttributeValueException(loginNamespaceAttribute, user, facility, "Login-namespace for facility can not be empty.");
}
} catch (AttributeNotExistsException e) {
throw new ConsistencyErrorException(e);
}
}
Aggregations