Github user geomacy commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/746#discussion_r127759488
  
    --- Diff: 
rest/rest-resources/src/main/java/org/apache/brooklyn/rest/transform/CatalogTransformer.java
 ---
    @@ -67,11 +71,177 @@
     import com.google.common.collect.ImmutableSet;
     import com.google.common.collect.Sets;
     
    -@SuppressWarnings("deprecation")
     public class CatalogTransformer {
     
         private static final org.slf4j.Logger log = 
LoggerFactory.getLogger(CatalogTransformer.class);
    +    
    +    public static <T extends Entity> CatalogEntitySummary 
catalogEntitySummary(BrooklynRestResourceUtils b, RegisteredType item, 
UriBuilder ub) {
    +        Set<EntityConfigSummary> config = Sets.newLinkedHashSet();
    +        Set<SensorSummary> sensors = 
Sets.newTreeSet(SummaryComparators.nameComparator());
    +        Set<EffectorSummary> effectors = 
Sets.newTreeSet(SummaryComparators.nameComparator());
    +
    +        EntitySpec<?> spec = null;
    +
    +        try {
    +            spec = b.getTypeRegistry().createSpec(item, null, 
EntitySpec.class);
    +            EntityDynamicType typeMap = 
BrooklynTypes.getDefinedEntityType(spec.getType());
    +            EntityType type = typeMap.getSnapshot();
    +
    +            AtomicInteger paramPriorityCnt = new AtomicInteger();
    +            for (SpecParameter<?> input: spec.getParameters())
    +                config.add(EntityTransformer.entityConfigSummary(input, 
paramPriorityCnt));
    +            for (Sensor<?> x: type.getSensors())
    +                sensors.add(SensorTransformer.sensorSummaryForCatalog(x));
    +            for (Effector<?> x: type.getEffectors())
    +                
effectors.add(EffectorTransformer.effectorSummaryForCatalog(x));
    +
    +        } catch (Exception e) {
    +            Exceptions.propagateIfFatal(e);
    +            
    +            // templates with multiple entities can't have spec created in 
the manner above; just ignore
    +            if (item.getSuperTypes().contains(Entity.class)) {
    +                log.warn("Unable to create spec for "+item+": "+e, e);
    +            }
    +            if (log.isTraceEnabled()) {
    +                log.trace("Unable to create spec for "+item+": "+e, e);
    +            }
    +        }
    +        
    +        return new CatalogEntitySummary(item.getSymbolicName(), 
item.getVersion(), item.getDisplayName(),
    +            spec!=null ? spec.getType().getName() : 
item.getSuperTypes().toString(), 
    +            spec!=null ? 
    +                CatalogItemType.ofTargetClass(spec.getType()).name() : 
    +                // RegisteredTypes.isTemplate(item) ? "template" :   // 
could check this, but more reliable for clients to rely on tag 
    +                "unknown",
    +            RegisteredTypes.getImplementationDataStringForSpec(item),
    +            item.getDescription(), tidyIconLink(b, item, 
item.getIconUrl(), ub),
    +            makeTags(spec, item), config, sensors, effectors,
    +            item.isDeprecated(), makeLinks(item, ub));
    +    }
     
    +    public static CatalogItemSummary 
catalogItemSummary(BrooklynRestResourceUtils b, RegisteredType item, UriBuilder 
ub) {
    +        try {
    +            if (item.getSuperTypes().contains(Application.class) ||
    +                    item.getSuperTypes().contains(Entity.class)) {
    +                return catalogEntitySummary(b, item, ub);
    +            } else if (item.getSuperTypes().contains(Policy.class)) {
    +                return catalogPolicySummary(b, item, ub);
    +            } else if (item.getSuperTypes().contains(Enricher.class)) {
    +                return catalogEnricherSummary(b, item, ub);
    +            } else if (item.getSuperTypes().contains(Location.class)) {
    +                return catalogLocationSummary(b, item, ub);
    +            } else {
    +                log.debug("Misc catalog item type when getting self link 
(supplying generic item): "+item+" "+item.getSuperTypes());
    +            }
    +        } catch (Exception e) {
    +            Exceptions.propagateIfFatal(e);
    +            log.warn("Invalid item in catalog when converting REST 
summaries (supplying generic item), at "+item+": "+e, e);
    +        }
    +        return new CatalogItemSummary(item.getSymbolicName(), 
item.getVersion(), item.getDisplayName(),
    +            item.getSuperTypes().toString(), 
    +            item.getKind()==RegisteredTypeKind.BEAN ? "bean" : "unknown",
    +            RegisteredTypes.getImplementationDataStringForSpec(item),
    +            item.getDescription(), tidyIconLink(b, item, 
item.getIconUrl(), ub), item.getTags(), item.isDeprecated(), makeLinks(item, 
ub));
    +    }
    +
    +    public static CatalogPolicySummary 
catalogPolicySummary(BrooklynRestResourceUtils b, RegisteredType item, 
UriBuilder ub) {
    +        final Set<PolicyConfigSummary> config = Sets.newLinkedHashSet();
    +        PolicySpec<?> spec = null;
    +        try{
    +            spec = b.getTypeRegistry().createSpec(item, null, 
PolicySpec.class);
    +            for (final SpecParameter<?> input : spec.getParameters()){
    +                config.add(EntityTransformer.policyConfigSummary(input));
    +            }
    +        }catch (Exception e) {
    +            Exceptions.propagateIfFatal(e);
    +            log.trace("Unable to create policy spec for "+item+": "+e, e);
    +        }
    +        return new CatalogPolicySummary(item.getSymbolicName(), 
item.getVersion(), item.getDisplayName(),
    +                spec!=null ? spec.getType().getName() : 
item.getSuperTypes().toString(), 
    +                CatalogItemType.POLICY.toString(),
    +                RegisteredTypes.getImplementationDataStringForSpec(item),
    +                item.getDescription(), tidyIconLink(b, item, 
item.getIconUrl(), ub), config,
    +                item.getTags(), item.isDeprecated(), makeLinks(item, ub));
    +    }
    +
    +    public static CatalogEnricherSummary 
catalogEnricherSummary(BrooklynRestResourceUtils b, RegisteredType item, 
UriBuilder ub) {
    +        final Set<EnricherConfigSummary> config = Sets.newLinkedHashSet();
    +        EnricherSpec<?> spec = null;
    +        try{
    +            spec = b.getTypeRegistry().createSpec(item, null, 
EnricherSpec.class);
    +            for (final SpecParameter<?> input : spec.getParameters()){
    +                config.add(EntityTransformer.enricherConfigSummary(input));
    +            }
    +        }catch (Exception e) {
    +            Exceptions.propagateIfFatal(e);
    +            log.trace("Unable to create policy spec for "+item+": "+e, e);
    +        }
    +        return new CatalogEnricherSummary(item.getSymbolicName(), 
item.getVersion(), item.getDisplayName(),
    +                spec!=null ? spec.getType().getName() : 
item.getSuperTypes().toString(), 
    +                CatalogItemType.ENRICHER.toString(),
    +                RegisteredTypes.getImplementationDataStringForSpec(item),
    +                item.getDescription(), tidyIconLink(b, item, 
item.getIconUrl(), ub), config,
    +                item.getTags(), item.isDeprecated(), makeLinks(item, ub));
    +    }
    +
    +    public static CatalogLocationSummary 
catalogLocationSummary(BrooklynRestResourceUtils b, RegisteredType item, 
UriBuilder ub) {
    +        Set<LocationConfigSummary> config = ImmutableSet.of();
    +        return new CatalogLocationSummary(item.getSymbolicName(), 
item.getVersion(), item.getDisplayName(),
    +                item.getSuperTypes().toString(), 
    +                CatalogItemType.LOCATION.toString(),
    +                RegisteredTypes.getImplementationDataStringForSpec(item),
    +                item.getDescription(), tidyIconLink(b, item, 
item.getIconUrl(), ub), config,
    +                item.getTags(), item.isDeprecated(), makeLinks(item, ub));
    +    }
    +
    +    protected static Map<String, URI> makeLinks(RegisteredType item, 
UriBuilder ub) {
    +        return MutableMap.<String, URI>of().addIfNotNull("self", 
getSelfLink(item, ub));
    +    }
    +
    +    protected static URI getSelfLink(RegisteredType item, UriBuilder ub) {
    +        String itemId = item.getId();
    +        if (item.getSuperTypes().contains(Application.class)) {
    +            return serviceUriBuilder(ub, CatalogApi.class, 
"getApplication").build(itemId, item.getVersion());
    +        } else if (item.getSuperTypes().contains(Entity.class)) {
    +            return serviceUriBuilder(ub, CatalogApi.class, 
"getEntity").build(itemId, item.getVersion());
    +        } else if (item.getSuperTypes().contains(Policy.class)) {
    +            return serviceUriBuilder(ub, CatalogApi.class, 
"getPolicy").build(itemId, item.getVersion());
    +        } else if (item.getSuperTypes().contains(Enricher.class)) {
    +            return serviceUriBuilder(ub, CatalogApi.class, 
"getEnricher").build(itemId, item.getVersion());
    +        } else if (item.getSuperTypes().contains(Location.class)) {
    +            return serviceUriBuilder(ub, CatalogApi.class, 
"getLocation").build(itemId, item.getVersion());
    +        } else {
    +            log.warn("Unexpected catalog item type when getting self link 
(not supplying self link): "+item+" "+item.getSuperTypes());
    +            return null;
    +        }
    +    }
    +    private static String tidyIconLink(BrooklynRestResourceUtils b, 
RegisteredType item, String iconUrl, UriBuilder ub) {
    +        if (b.isUrlServerSideAndSafe(iconUrl)) {
    +            return serviceUriBuilder(ub, CatalogApi.class, 
"getIcon").build(item.getSymbolicName(), item.getVersion()).toString();
    +        }
    +        return iconUrl;
    +    }
    +
    +    private static Set<Object> makeTags(EntitySpec<?> spec, RegisteredType 
item) {
    --- End diff --
    
    This is basically a duplicate of the existing one, why not make it 
`makeTags(EntitySpec<?> spec, Set<Object> sourceTags` and call it as 
`makeTags(spec, item.getTags())` or `makeTags(spec, item.tags().getTags())` as 
appropriate.
    



---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to