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);
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_group_resource_attribute_def_def_freeipaGroupName method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl sess, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
//prepare group name and check its format
String groupName = (String) attribute.getValue();
if (groupName == null) {
throw new WrongAttributeValueException(attribute, group, "Attribute cannot be null.");
}
Matcher match = pattern.matcher(groupName);
if (!match.matches()) {
throw new WrongAttributeValueException(attribute, group, "Bad format of attribute freeipaGroupName. It has to match pattern ^[a-zA-Z0-9_.][a-zA-Z0-9_.-]{0,252}[a-zA-Z0-9_.$-]?$");
}
//Get facility for the resource
Facility facility = sess.getPerunBl().getResourcesManagerBl().getFacility(sess, resource);
// Get all resources from the facility
List<Resource> facilityResources = sess.getPerunBl().getFacilitiesManagerBl().getAssignedResources(sess, facility);
//For each resource get all groups
for (Resource rs : facilityResources) {
List<Group> resourceGroups = sess.getPerunBl().getResourcesManagerBl().getAssignedGroups(sess, rs);
//Remove our group from list of groups
if (rs.getId() == resource.getId()) {
resourceGroups.remove(group);
}
//For all groups get name and check uniqueness
for (Group gr : resourceGroups) {
Attribute freeipaGroupNameAttribute = new Attribute();
try {
freeipaGroupNameAttribute = sess.getPerunBl().getAttributesManagerBl().getAttribute(sess, rs, gr, A_GR_freeipaGroupName);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute " + A_GR_freeipaGroupName + " does not exists for group " + gr + " and resource " + rs, ex);
}
if (freeipaGroupNameAttribute.getValue() != null) {
String name = (String) freeipaGroupNameAttribute.getValue();
if (name.toLowerCase().equals(groupName.toLowerCase())) {
throw new WrongAttributeValueException(attribute, group, "Attribute has to be unique within one facility (case insensitive).");
}
}
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_group_resource_attribute_def_def_projectDataLimit method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrProjectDataQuota = null;
String projectDataQuota = null;
String projectDataLimit = null;
String projectDataQuotaNumber = null;
String projectDataQuotaLetter = null;
String projectDataLimitNumber = null;
String projectDataLimitLetter = 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, group, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
} else
return;
//Get ProjectDataQuota attribute
try {
attrProjectDataQuota = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, group, A_GR_projectDataQuota);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with projectDataQuota from resource " + resource.getId() + " and group + " + group.getId() + " could not obtain.", ex);
}
//Get ProjectDataLimit value
if (attribute.getValue() != null) {
projectDataLimit = (String) attribute.getValue();
Matcher numberMatcher = numberPattern.matcher(projectDataLimit);
Matcher letterMatcher = letterPattern.matcher(projectDataLimit);
numberMatcher.find();
letterMatcher.find();
try {
projectDataLimitNumber = projectDataLimit.substring(numberMatcher.start(), numberMatcher.end());
} catch (IllegalStateException ex) {
projectDataLimitNumber = null;
}
try {
projectDataLimitLetter = projectDataLimit.substring(letterMatcher.start(), letterMatcher.end());
} catch (IllegalStateException ex) {
projectDataLimitLetter = "G";
}
}
BigDecimal limitNumber;
if (projectDataLimitNumber != null)
limitNumber = new BigDecimal(projectDataLimitNumber.replace(',', '.'));
else
limitNumber = new BigDecimal("0");
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
throw new WrongAttributeValueException(attribute, attribute + " can't be less than 0.");
}
//Get ProjectDataQuota value
if (attrProjectDataQuota != null && attrProjectDataQuota.getValue() != null) {
projectDataQuota = (String) attrProjectDataQuota.getValue();
Matcher numberMatcher = numberPattern.matcher(projectDataQuota);
Matcher letterMatcher = letterPattern.matcher(projectDataQuota);
numberMatcher.find();
letterMatcher.find();
try {
projectDataQuotaNumber = projectDataQuota.substring(numberMatcher.start(), numberMatcher.end());
} catch (IllegalStateException ex) {
projectDataQuotaNumber = null;
}
try {
projectDataQuotaLetter = projectDataQuota.substring(letterMatcher.start(), letterMatcher.end());
} catch (IllegalStateException ex) {
projectDataQuotaLetter = "G";
}
}
BigDecimal quotaNumber;
if (projectDataQuotaNumber != null)
quotaNumber = new BigDecimal(projectDataQuotaNumber.replace(',', '.'));
else
quotaNumber = new BigDecimal("0");
if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, attrProjectDataQuota + " cant be less than 0.");
}
//Compare ProjectDataLimit with ProjectDataQuota
if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, "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 (projectDataLimitLetter.equals("K"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
else if (projectDataLimitLetter.equals("M"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
else if (projectDataLimitLetter.equals("T"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
else if (projectDataLimitLetter.equals("P"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
else if (projectDataLimitLetter.equals("E"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
else
limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
if (projectDataQuotaLetter.equals("K"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
else if (projectDataQuotaLetter.equals("M"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
else if (projectDataQuotaLetter.equals("T"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
else if (projectDataQuotaLetter.equals("P"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
else if (projectDataQuotaLetter.equals("E"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
else
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
if (limitNumber.compareTo(quotaNumber) < 0) {
throw new WrongReferenceAttributeValueException(attribute, attrProjectDataQuota, attribute + " must be more than or equals to " + attrProjectDataQuota);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_group_resource_attribute_def_def_projectDataQuota method checkAttributeValue.
@Override
public void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Attribute attrProjectDataLimit = null;
String projectDataQuota = null;
String projectDataLimit = null;
String projectDataQuotaNumber = null;
String projectDataQuotaLetter = null;
String projectDataLimitNumber = null;
String projectDataLimitLetter = 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, group, "Format of quota must be something like ex.: 1.30M or 2500K, but it is " + attribute.getValue());
}
//Get ProjectDataLimit attribute
try {
attrProjectDataLimit = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, resource, group, A_GR_projectDataLimit);
} catch (AttributeNotExistsException ex) {
throw new ConsistencyErrorException("Attribute with projectDataLimit from resource " + resource.getId() + " and group " + group.getId() + " could not obtain.", ex);
}
//Get ProjectDataQuota value
if (attribute.getValue() != null) {
projectDataQuota = (String) attribute.getValue();
Matcher numberMatcher = numberPattern.matcher(projectDataQuota);
Matcher letterMatcher = letterPattern.matcher(projectDataQuota);
numberMatcher.find();
letterMatcher.find();
try {
projectDataQuotaNumber = projectDataQuota.substring(numberMatcher.start(), numberMatcher.end());
} catch (IllegalStateException ex) {
projectDataQuotaNumber = null;
}
try {
projectDataQuotaLetter = projectDataQuota.substring(letterMatcher.start(), letterMatcher.end());
} catch (IllegalStateException ex) {
projectDataQuotaLetter = "G";
}
}
BigDecimal quotaNumber;
if (projectDataQuotaNumber != null)
quotaNumber = new BigDecimal(projectDataQuotaNumber.replace(',', '.'));
else
quotaNumber = new BigDecimal("0");
if (quotaNumber != null && quotaNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
throw new WrongAttributeValueException(attribute, attribute + " can't be less than 0.");
}
//Get ProjectDataLimit value
if (attrProjectDataLimit != null && attrProjectDataLimit.getValue() != null) {
projectDataLimit = (String) attrProjectDataLimit.getValue();
Matcher numberMatcher = numberPattern.matcher(projectDataLimit);
Matcher letterMatcher = letterPattern.matcher(projectDataLimit);
numberMatcher.find();
letterMatcher.find();
try {
projectDataLimitNumber = projectDataLimit.substring(numberMatcher.start(), numberMatcher.end());
} catch (IllegalStateException ex) {
projectDataLimitNumber = null;
}
try {
projectDataLimitLetter = projectDataLimit.substring(letterMatcher.start(), letterMatcher.end());
} catch (IllegalStateException ex) {
projectDataLimitLetter = "G";
}
}
BigDecimal limitNumber;
if (projectDataLimitNumber != null)
limitNumber = new BigDecimal(projectDataLimitNumber.replace(',', '.'));
else
limitNumber = new BigDecimal("0");
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) < 0) {
throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, attrProjectDataLimit + " cant be less than 0.");
}
//Compare ProjectDataQuota with ProjectDataLimit
if (quotaNumber == null || quotaNumber.compareTo(BigDecimal.valueOf(0)) == 0) {
if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, "Try to set unlimited quota, but limit is still " + projectDataLimitNumber + projectDataLimitLetter);
}
} else if (limitNumber != null && limitNumber.compareTo(BigDecimal.valueOf(0)) != 0) {
if (projectDataLimitLetter.equals("K"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(K));
else if (projectDataLimitLetter.equals("M"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(M));
else if (projectDataLimitLetter.equals("T"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(T));
else if (projectDataLimitLetter.equals("P"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(P));
else if (projectDataLimitLetter.equals("E"))
limitNumber = limitNumber.multiply(BigDecimal.valueOf(E));
else
limitNumber = limitNumber.multiply(BigDecimal.valueOf(G));
if (projectDataQuotaLetter.equals("K"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(K));
else if (projectDataQuotaLetter.equals("M"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(M));
else if (projectDataQuotaLetter.equals("T"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(T));
else if (projectDataQuotaLetter.equals("P"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(P));
else if (projectDataQuotaLetter.equals("E"))
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(E));
else
quotaNumber = quotaNumber.multiply(BigDecimal.valueOf(G));
if (limitNumber.compareTo(quotaNumber) < 0) {
throw new WrongReferenceAttributeValueException(attribute, attrProjectDataLimit, attribute + " must be less than or equals to " + projectDataLimit);
}
}
}
use of cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException in project perun by CESNET.
the class urn_perun_group_resource_attribute_def_def_projectDirPermissions method checkAttributeValue.
public void checkAttributeValue(PerunSessionImpl sess, Resource resource, Group group, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
Integer permissions = (Integer) attribute.getValue();
//Permissions can be null (if null, it means DEFAULT 750)
if (permissions == null)
return;
String perm = permissions.toString();
//Only 3 consecutive numbers with value >=0 and <=7 are allowed
Pattern pattern = Pattern.compile("^[01234567]{3}$");
Matcher match = pattern.matcher(perm);
if (!match.matches()) {
throw new WrongAttributeValueException(attribute, group, resource, "Bad format of attribute projectDirPermissions (expected something like '750').");
}
}
Aggregations