Search in sources :

Example 26 with Attribute

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

the class urn_perun_member_attribute_def_def_phone method changedAttributeHook.

@Override
public void changedAttributeHook(PerunSessionImpl session, Member member, Attribute attribute) throws InternalErrorException, WrongReferenceAttributeValueException {
    User user = session.getPerunBl().getUsersManagerBl().getUserByMember(session, member);
    if (attribute.getValue() != null) {
        Attribute userPhone = null;
        try {
            userPhone = session.getPerunBl().getAttributesManagerBl().getAttribute(session, user, A_U_phone);
            if (userPhone.getValue() == null) {
                userPhone.setValue(attribute.getValue());
                session.getPerunBl().getAttributesManagerBl().setAttribute(session, user, userPhone);
            }
        } catch (WrongAttributeAssignmentException ex) {
            throw new InternalErrorException(ex);
        } catch (AttributeNotExistsException ex) {
            throw new ConsistencyErrorException(ex);
        } catch (WrongAttributeValueException ex) {
            throw new WrongReferenceAttributeValueException(attribute, userPhone, "Mismatch in checking of member phone and user phone (different checking rules)", ex);
        }
    }
}
Also used : ConsistencyErrorException(cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException) User(cz.metacentrum.perun.core.api.User) Attribute(cz.metacentrum.perun.core.api.Attribute) WrongAttributeAssignmentException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) 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 27 with Attribute

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

the class urn_perun_resource_attribute_def_def_shells method checkAttributeValue.

@Override
public /**
	 * Checks the attribute with all available shells from resource's facility
	 */
void checkAttributeValue(PerunSessionImpl perunSession, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    List<String> shells = (List<String>) attribute.getValue();
    if (shells == null) {
        throw new WrongAttributeValueException(attribute, "Attribute was not filled, therefore there is nothing to be checked.");
    }
    if (!shells.isEmpty()) {
        for (String st : shells) {
            perunSession.getPerunBl().getModulesUtilsBl().checkFormatOfShell(st, attribute);
        }
    }
    Facility facility = perunSession.getPerunBl().getResourcesManagerBl().getFacility(perunSession, resource);
    Attribute allShellsPerFacility;
    try {
        allShellsPerFacility = perunSession.getPerunBl().getAttributesManagerBl().getAttribute(perunSession, facility, A_F_shells);
    } catch (AttributeNotExistsException ex) {
        throw new InternalErrorException("Attribute with list of shells from facility " + facility.getId() + " could not obtained.", ex);
    }
    if (allShellsPerFacility.getValue() == null) {
        throw new WrongReferenceAttributeValueException(attribute, allShellsPerFacility);
    } else {
        if (!((List<String>) allShellsPerFacility.getValue()).containsAll(shells)) {
            throw new WrongAttributeValueException(attribute, "Some shells from specified resource are not at home facility " + facility.getId());
        }
    }
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) WrongReferenceAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException) AttributeNotExistsException(cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException) ArrayList(java.util.ArrayList) List(java.util.List) Facility(cz.metacentrum.perun.core.api.Facility) InternalErrorException(cz.metacentrum.perun.core.api.exceptions.InternalErrorException) WrongAttributeValueException(cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)

Example 28 with Attribute

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

the class urn_perun_resource_attribute_def_def_unixGID_namespace method fillAttribute.

public Attribute fillAttribute(PerunSessionImpl sess, Resource resource, AttributeDefinition attributeDefinition) throws InternalErrorException, 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<Group>();
        List<Resource> resourcesWithSameGroupNameInSameNamespace = new ArrayList<Resource>();
        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));
        }
        //Prepare variable for commonGID
        Integer commonGID = null;
        //Test if exists common GID for this group and other groups and resources
        commonGID = sess.getPerunBl().getModulesUtilsBl().getCommonGIDOfGroupsWithSameNameInSameNamespace(sess, groupsWithSameGroupNameInSameNamespace, gidNamespace, commonGID);
        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 29 with Attribute

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

the class urn_perun_resource_attribute_def_def_unixGID_namespace method checkAttributeValue.

public void checkAttributeValue(PerunSessionImpl sess, Resource resource, Attribute attribute) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, WrongAttributeAssignmentException {
    try {
        String gidNamespace = attribute.getFriendlyNameParameter();
        //Special behaviour if gid is null
        Integer attrValue = null;
        if (attribute.getValue() == null) {
            throw new WrongAttributeValueException(attribute, resource, "Unix GID must be set");
        } else {
            attrValue = (Integer) attribute.getValue();
        }
        //Check if GID is within allowed range
        sess.getPerunBl().getModulesUtilsBl().checkIfGIDIsWithinRange(sess, attribute);
        //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, resource, null, gidNamespace, null, "This GID is already depleted.");
            }
        }
        //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 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
        allGroupsWithSameGIDInSameNamespace.addAll(sess.getPerunBl().getGroupsManagerBl().getGroupsByAttribute(sess, groupGIDAttribute));
        allResourcesWithSameGIDInSameNamespace.addAll(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) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 30 with Attribute

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

the class urn_perun_resource_attribute_def_virt_voShortName method getAttributeValue.

public Attribute getAttributeValue(PerunSessionImpl sess, Resource resource, AttributeDefinition attributeDefinition) throws InternalErrorException {
    Attribute attribute = new Attribute(attributeDefinition);
    Vo vo = sess.getPerunBl().getResourcesManagerBl().getVo(sess, resource);
    attribute.setValue(vo.getShortName());
    return attribute;
}
Also used : Attribute(cz.metacentrum.perun.core.api.Attribute) Vo(cz.metacentrum.perun.core.api.Vo)

Aggregations

Attribute (cz.metacentrum.perun.core.api.Attribute)668 RichAttribute (cz.metacentrum.perun.core.api.RichAttribute)240 Test (org.junit.Test)178 AttributeDefinition (cz.metacentrum.perun.core.api.AttributeDefinition)157 ArrayList (java.util.ArrayList)150 AbstractPerunIntegrationTest (cz.metacentrum.perun.core.AbstractPerunIntegrationTest)134 User (cz.metacentrum.perun.core.api.User)131 AttributeNotExistsException (cz.metacentrum.perun.core.api.exceptions.AttributeNotExistsException)121 InternalErrorException (cz.metacentrum.perun.core.api.exceptions.InternalErrorException)121 ConsistencyErrorException (cz.metacentrum.perun.core.api.exceptions.ConsistencyErrorException)110 WrongReferenceAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongReferenceAttributeValueException)102 WrongAttributeValueException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeValueException)101 WrongAttributeAssignmentException (cz.metacentrum.perun.core.api.exceptions.WrongAttributeAssignmentException)96 Resource (cz.metacentrum.perun.core.api.Resource)82 Facility (cz.metacentrum.perun.core.api.Facility)79 Group (cz.metacentrum.perun.core.api.Group)34 PerunSession (cz.metacentrum.perun.core.api.PerunSession)33 LinkedHashMap (java.util.LinkedHashMap)24 PerunSessionImpl (cz.metacentrum.perun.core.impl.PerunSessionImpl)22 Vo (cz.metacentrum.perun.core.api.Vo)21