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