checkNotNull(reference);
CapabilityDescriptor descriptor = reference.context().descriptor();
Capability capability = reference.capability();
final CapabilityStatusXO capabilityStatus = new CapabilityStatusXO()
.withCapability(asCapability(reference))
.withTypeName(descriptor.name())
.withActive(reference.context().isActive())
.withError(reference.context().hasFailure());
try {
capabilityStatus.setDescription(capability.description());
}
catch (Throwable e) {
log.warn(
"Failed to render description of capability '{}/{}' due to {}/{}",
reference.context().type(), reference.context().id(),
e.getClass().getName(), e.getMessage(), log.isDebugEnabled() ? e : null
);
capabilityStatus.setDescription(null);
}
try {
capabilityStatus.setStatus(capability.status());
}
catch (Throwable e) {
log.warn(
"Failed to render status of capability '{}/{}' due to {}/{}",
reference.context().type(), reference.context().id(),
e.getClass().getName(), e.getMessage(), log.isDebugEnabled() ? e : null
);
capabilityStatus.setStatus(null);
}
capabilityStatus.setStateDescription(reference.context().stateDescription());
Set<Tag> tags = Sets.newHashSet();
try {
if (descriptor instanceof Taggable) {
Set<Tag> tagSet = ((Taggable) descriptor).getTags();
if (tagSet != null) {
tags.addAll(tagSet);
}
}
}
catch (Throwable e) {
log.warn(
"Failed to retrieve tags from capability descriptor '{}' due to {}/{}",
reference.context().type(),
e.getClass().getName(), e.getMessage(), log.isDebugEnabled() ? e : null
);
}
try {
if (capability instanceof Taggable) {
Set<Tag> tagSet = ((Taggable) capability).getTags();
if (tagSet != null) {
tags.addAll(tagSet);
}
}
}
catch (Throwable e) {
log.warn(
"Failed to retrieve tags from capability '{}/{}' due to {}/{}",
reference.context().type(), reference.context().id(),
e.getClass().getName(), e.getMessage(), log.isDebugEnabled() ? e : null
);
}
List<TagXO> tagXOs = Lists.transform(
Lists.newArrayList(Collections2.filter(tags, Predicates.<Tag>notNull())),
new Function<Tag, TagXO>()
{
@Override
public TagXO apply(final Tag input) {
return new TagXO().withKey(input.key()).withValue(input.value());
}
}
);
if (!tagXOs.isEmpty()) {
capabilityStatus.setTags(tagXOs);
}
return capabilityStatus;
}