Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/incubator-brooklyn/pull/585#discussion_r28361083
--- Diff:
core/src/main/java/brooklyn/catalog/internal/BasicBrooklynCatalog.java ---
@@ -491,43 +508,127 @@ public void load() {
if (item instanceof CatalogItemDtoAbstract) return
(CatalogItemDtoAbstract<T,SpecT>) item;
throw new IllegalStateException("Cannot unwrap catalog item
'"+item+"' (type "+item.getClass()+") to restore DTO");
}
+
+ @SuppressWarnings("unchecked")
+ private <T> Maybe<T> getFirstAs(Map<?,?> map, Class<T> type, String
firstKey, String ...otherKeys) {
+ if (map==null) return Maybe.absent("No map available");
+ String foundKey = null;
+ Object value = null;
+ if (map.containsKey(firstKey)) foundKey = firstKey;
+ else for (String key: otherKeys) {
+ if (map.containsKey(key)) {
+ foundKey = key;
+ break;
+ }
+ }
+ if (foundKey==null) return Maybe.absent("Missing entry
'"+firstKey+"'");
+ value = map.get(foundKey);
+ if (type.equals(String.class) && Number.class.isInstance(value))
value = value.toString();
+ if (!type.isInstance(value))
+ throw new IllegalArgumentException("Entry for '"+firstKey+"'
should be of type "+type+", not "+value.getClass());
+ return Maybe.of((T)value);
+ }
+
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ private Maybe<Map<?,?>> getFirstAsMap(Map<?,?> map, String firstKey,
String ...otherKeys) {
+ return (Maybe<Map<?,?>>)(Maybe) getFirstAs(map, Map.class,
firstKey, otherKeys);
+ }
- private CatalogItemDtoAbstract<?,?> getAbstractCatalogItem(String
yaml) {
- DeploymentPlan plan = makePlanFromYaml(yaml);
+ private List<CatalogItemDtoAbstract<?,?>>
addAbstractCatalogItems(String yaml, Boolean whenAddingAsDtoShouldWeForce) {
+ Map<?,?> itemDef = Yamls.getAs(Yamls.parseAll(yaml), Map.class);
+ Map<?,?> catalogMetadata = getFirstAsMap(itemDef,
"brooklyn.catalog", "catalog").orNull();
+ if (catalogMetadata==null)
+ log.warn("No `brooklyn.catalog` supplied in catalog request;
using legacy mode for "+itemDef);
+ catalogMetadata = MutableMap.copyOf(catalogMetadata);
- @SuppressWarnings("rawtypes")
- Maybe<Map> possibleCatalog =
plan.getCustomAttribute("brooklyn.catalog", Map.class, true);
- MutableMap<String, Object> catalog = MutableMap.of();
- if (possibleCatalog.isPresent()) {
- @SuppressWarnings("unchecked")
- Map<String, Object> catalog2 = (Map<String, Object>)
possibleCatalog.get();
- catalog.putAll(catalog2);
+ List<CatalogItemDtoAbstract<?, ?>> result = MutableList.of();
+
+ addAbstractCatalogItems(Yamls.getTextOfYamlAtPath(yaml,
"brooklyn.catalog").getMatchedYamlTextOrWarn(),
+ catalogMetadata, result, null, whenAddingAsDtoShouldWeForce);
+
+ itemDef.remove("brooklyn.catalog");
+ catalogMetadata.remove("item");
+ catalogMetadata.remove("items");
+ if (!itemDef.isEmpty()) {
+ log.debug("Reading brooklyn.catalog peer keys as item
('top-level syntax')");
+ Map<String,?> rootItem = MutableMap.of("item", itemDef);
+ String rootItemYaml = yaml;
+ YamlExtract yamlExtract =
Yamls.getTextOfYamlAtPath(rootItemYaml, "brooklyn.catalog");
+ String match =
yamlExtract.withOriginalIndentation(true).withKeyIncluded(true).getMatchedYamlTextOrWarn();
+ if (match!=null) {
+ if (rootItemYaml.startsWith(match)) rootItemYaml =
Strings.removeFromStart(rootItemYaml, match);
+ else rootItemYaml =
Strings.replaceAllNonRegex(rootItemYaml, "\n"+match, "");
+ }
+
addAbstractCatalogItems("item:\n"+makeAsIndentedObject(rootItemYaml), rootItem,
result, catalogMetadata, whenAddingAsDtoShouldWeForce);
}
+
+ return result;
+ }
+
+ @SuppressWarnings("unchecked")
+ private void addAbstractCatalogItems(String sourceYaml, Map<?,?>
itemMetadata, List<CatalogItemDtoAbstract<?, ?>> result, Map<?,?>
parentMetadata, Boolean whenAddingAsDtoShouldWeForce) {
- Collection<CatalogBundle> libraries = Collections.emptyList();
- Maybe<Object> possibleLibraries = catalog.getMaybe("libraries");
- if (possibleLibraries.isAbsent()) possibleLibraries =
catalog.getMaybe("brooklyn.libraries");
- if (possibleLibraries.isPresentAndNonNull()) {
- if (!(possibleLibraries.get() instanceof Collection))
- throw new IllegalArgumentException("Libraries should be a
list, not "+possibleLibraries.get());
- libraries =
CatalogItemDtoAbstract.parseLibraries((Collection<?>) possibleLibraries.get());
+ if (sourceYaml==null) sourceYaml = new Yaml().dump(itemMetadata);
+
+ Map<Object,Object> catalogMetadata =
MutableMap.builder().putAll(parentMetadata).putAll(itemMetadata).build();
+
+ // libraries we treat specially, to append the list, with the
child's list preferred in classloading order
+ List<?> librariesL = MutableList.copyOf(getFirstAs(itemMetadata,
List.class, "brooklyn.libraries", "libraries").orNull())
+ .appendAll(getFirstAs(parentMetadata, List.class,
"brooklyn.libraries", "libraries").orNull());
+ if (!librariesL.isEmpty())
+ catalogMetadata.put("brooklyn.libraries", librariesL);
+ Collection<CatalogBundle> libraries =
CatalogItemDtoAbstract.parseLibraries(librariesL);
+
+ Object items = catalogMetadata.remove("items");
+ Object item = catalogMetadata.remove("item");
+
+ if (items!=null) {
+ int count = 0;
+ for (Map<?,?> i: ((List<Map<?,?>>)items)) {
+
addAbstractCatalogItems(Yamls.getTextOfYamlAtPath(sourceYaml, "items",
count).getMatchedYamlTextOrWarn(),
+ i, result, catalogMetadata,
whenAddingAsDtoShouldWeForce);
+ count++;
+ }
}
+
+ if (item==null) return;
- final String id = (String) catalog.getMaybe("id").orNull();
- final String version =
Strings.toString(catalog.getMaybe("version").orNull());
- final String symbolicName = (String)
catalog.getMaybe("symbolicName").orNull();
- final String name = (String) catalog.getMaybe("name").orNull();
- final String displayName = (String)
catalog.getMaybe("displayName").orNull();
- final String description = (String)
catalog.getMaybe("description").orNull();
- final String iconUrl = (String)
catalog.getMaybe("iconUrl").orNull();
- final String iconUrlUnderscore = (String)
catalog.getMaybe("icon_url").orNull();
- final String deprecated = (String)
catalog.getMaybe("deprecated").orNull();
+ // now look at the actual item, first correcting the sourceYaml
and interpreting the catalog metadata
+ String itemYaml = Yamls.getTextOfYamlAtPath(sourceYaml,
"item").getMatchedYamlTextOrWarn();
--- End diff --
re the UI i agree we should show catalog metadata in the catalog item view.
however i think it shouldn't be in the "plan" -- we should have a dedicated
space in the gui and rest api calls for metadata (if not already there?). i
think that doesn't need to be part of this PR.
---
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.
---