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;
}
}
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);
}
}
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);
}
}
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);
}
}
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;
}
Aggregations