Search in sources :

Example 1 with BaseResource

use of org.ovirt.engine.api.model.BaseResource in project ovirt-engine by oVirt.

the class LinkFollower method fetchData.

/**
 * For the provided collection-type entity (e.g: Nics), follow the link represented by the
 * provided node. Do not follow child-links of this node.
 *
 * For example, for a Nics object:
 *
 *   nics [nic1, nic2]
 *
 * and the tree:
 *
 *   vnicprofiles
 *        networkfilter
 *        qos
 *
 * This method fetches all vnicprofiles of nic1, nic2, and sets them in these Nic objects.
 * The method will return the fetched vnic-profile objects. The child links networkfilter, qos
 * are purposely ignored.
 */
@SuppressWarnings("unchecked")
private List<ActionableResource> fetchData(BaseResources collectionEntity, LinksTreeNode node) {
    List<ActionableResource> results = new LinkedList<>();
    Method collectionGetter = EntityHelper.getCollectionGetter(collectionEntity);
    try {
        // get the actual list of entities in the collection-type, e.g for Nics get List<Nic>
        // (by invoking nics.getNics() using reflection)
        List<BaseResource> entities = (List<BaseResource>) collectionGetter.invoke(collectionEntity);
        // for each entity in the list, fetch link data.
        for (BaseResource entity : entities) {
            results.add(fetchData(entity, node));
        }
    } catch (Exception e) {
        throw new IllegalStateException("Problem following '" + node.getElement() + "' link in " + collectionEntity.getClass().getSimpleName() + " entity.", e);
    }
    return results;
}
Also used : BaseResource(org.ovirt.engine.api.model.BaseResource) ActionableResource(org.ovirt.engine.api.model.ActionableResource) List(java.util.List) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) LinkedList(java.util.LinkedList) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with BaseResource

use of org.ovirt.engine.api.model.BaseResource in project ovirt-engine by oVirt.

the class LinkHelper method getPathConsideringParent.

private static String getPathConsideringParent(BaseResource entity, ApiLocationMetadata locationMetadata) {
    BaseResource parent = getParent(entity, locationMetadata.getParentType());
    if (parent == null) {
        return null;
    }
    ApiLocationMetadata parentLocationMetadata = getLocationMetadata(parent);
    if (parentLocationMetadata == null) {
        return null;
    }
    String parentPath = getPath(parent);
    if (parentPath == null) {
        return null;
    }
    String relativePath = getRelativePath(locationMetadata.getCollectionServiceClass(), parentLocationMetadata.getEntityServiceClass());
    return String.join("/", parentPath, relativePath, entity.getId());
}
Also used : BaseResource(org.ovirt.engine.api.model.BaseResource)

Example 3 with BaseResource

use of org.ovirt.engine.api.model.BaseResource in project ovirt-engine by oVirt.

the class LinkHelper method getInlineResources.

/**
 * Obtain a set of inline BaseResource objects from @obj
 *
 * i.e. return the value of any properties on @obj which are a
 * sub-type of BaseResource
 *
 * @param obj the object to check
 * @return    a list of any inline BaseResource objects
 */
@SuppressWarnings("unchecked")
private static List<BaseResource> getInlineResources(Object obj) {
    ArrayList<BaseResource> ret = new ArrayList<>();
    for (Method method : getRelevantMethods(obj.getClass())) {
        // We need to recursively scan everything that is in the model package, as there may be references
        // to resources deeply nested:
        Object inline = null;
        try {
            inline = method.invoke(obj);
        } catch (Exception e) {
        // invocation target exception should not occur on simple getter
        }
        if (inline != null) {
            if (inline instanceof BaseResource) {
                ret.add((BaseResource) inline);
            } else if (inline instanceof BaseResources) {
                BaseResources entities = (BaseResources) inline;
                Method getter = EntityHelper.getCollectionGetter(entities);
                try {
                    List<BaseResource> entitiesList = (List<BaseResource>) getter.invoke(entities);
                    for (BaseResource entity : entitiesList) {
                        ret.add(entity);
                    }
                } catch (Exception e) {
                    log.error("Error invoking method '{}' on class '{}'.", method.getName(), entities.getClass().getSimpleName());
                    log.error("Exception", e);
                }
            } else {
                ret.addAll(getInlineResources(inline));
            }
        }
    }
    return ret;
}
Also used : BaseResource(org.ovirt.engine.api.model.BaseResource) ArrayList(java.util.ArrayList) BaseResources(org.ovirt.engine.api.model.BaseResources) ArrayList(java.util.ArrayList) List(java.util.List) Method(java.lang.reflect.Method)

Example 4 with BaseResource

use of org.ovirt.engine.api.model.BaseResource in project ovirt-engine by oVirt.

the class LinkFollower method getHref.

/**
 * Returns the 'href' string - the actual link that should be followed.
 * There are two possible locations for the href string:
 * 1) For sub-collections, (e.g: disk_attachments of a VM) the href will
 *    appear under the list of links (vm.getLinks()...)
 * 2) For referenced single entities (e.g Template of a VM) the href will appear
 *    in the member itself (vm.getTemplate().getHref()).
 */
private String getHref(BaseResource entity, String link) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
    Optional<Link> optional = entity.getLinks().stream().filter(x -> x.getRel().equals(toRelFormat(link))).findFirst();
    if (optional.isPresent()) {
        return optional.get().getHref();
    } else {
        // assume this is not a sub-collection, since it wasn't found among links.
        Method getter = ReflectionHelper.getGetter(entity, underscoreToCamelCase(link));
        BaseResource member = (BaseResource) getter.invoke(entity);
        return member.getHref();
    }
}
Also used : Arrays(java.util.Arrays) GET(javax.ws.rs.GET) EntityHelper(org.ovirt.engine.api.utils.EntityHelper) InvocationTargetException(java.lang.reflect.InvocationTargetException) ActionableResource(org.ovirt.engine.api.model.ActionableResource) List(java.util.List) BaseBackendResource(org.ovirt.engine.api.restapi.resource.BaseBackendResource) Link(org.ovirt.engine.api.model.Link) ResourceLocator(org.ovirt.engine.api.restapi.resource.ResourceLocator) Optional(java.util.Optional) BaseResource(org.ovirt.engine.api.model.BaseResource) BaseResources(org.ovirt.engine.api.model.BaseResources) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) ReflectionHelper(org.ovirt.engine.api.utils.ReflectionHelper) BaseResource(org.ovirt.engine.api.model.BaseResource) Method(java.lang.reflect.Method) Link(org.ovirt.engine.api.model.Link)

Aggregations

BaseResource (org.ovirt.engine.api.model.BaseResource)4 Method (java.lang.reflect.Method)3 List (java.util.List)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 LinkedList (java.util.LinkedList)2 ActionableResource (org.ovirt.engine.api.model.ActionableResource)2 BaseResources (org.ovirt.engine.api.model.BaseResources)2 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Optional (java.util.Optional)1 GET (javax.ws.rs.GET)1 Link (org.ovirt.engine.api.model.Link)1 BaseBackendResource (org.ovirt.engine.api.restapi.resource.BaseBackendResource)1 ResourceLocator (org.ovirt.engine.api.restapi.resource.ResourceLocator)1 EntityHelper (org.ovirt.engine.api.utils.EntityHelper)1 ReflectionHelper (org.ovirt.engine.api.utils.ReflectionHelper)1