@SuppressWarnings("unchecked") private static List<Binding> extractBindings(String currentComponent, Object value) { LinkedList<String> targetPath = new LinkedList<>(); if (value instanceof Map) { return extractBindings(targetPath, (Map<String, Object>) value); } return null; }
private static List<Binding> extractBindings(LinkedList<String> targetPath, String fieldName, Map<String, Object> map) { if (!DATA.equals(fieldName)) { targetPath.add(fieldName); } List<Binding> bindings = extractBindings(targetPath, map); if (!DATA.equals(fieldName)) { targetPath.pollLast(); } return bindings; }
@SuppressWarnings("unchecked") public static List<Binding.ComponentBinding> extractBindings( Map<String, Object> initialMap) { List<Binding.ComponentBinding> bindingsPerComponent = new LinkedList<>(); for (Entry<String, Object> e : initialMap.entrySet()) { if (e.getKey().equals(COMPONENTS)) { Map<String, Object> components = (Map<String, Object>) e.getValue(); for (Entry<String, Object> ce : components.entrySet()) { List<Binding> bindings = extractBindings(ce.getKey(), ce.getValue()); if (bindings != null && !bindings.isEmpty()) { bindingsPerComponent.add(new ComponentBinding(ce.getKey(), bindings)); } } } } return bindingsPerComponent; // return initialMap.entrySet().stream() // .filter(e -> e.getKey().equals(COMPONENTS)) // .flatMap(e -> ((Map<String, Object>) e.getValue()).entrySet().stream()) // .map(e -> new Binding.ComponentBinding(e.getKey(), // extractBindings(e.getKey(), e.getValue()))) // .filter(b -> (b.bindings != null && !b.bindings.isEmpty())) // .collect(Collectors.toList()); }
listMap.put(String.valueOf(i), list.get(i)); bindings.addAll(extractBindings(targetPath, fieldName, listMap)); list = listMap.values().stream().collect(Collectors.toList()); entry.setValue(list); } else if (value instanceof Map) { bindings.addAll( extractBindings(targetPath, fieldName, (Map<String, Object>) value));
@SuppressWarnings("unchecked") public static CompositeTemplate deserializeCompositeTemplate(String yaml) throws IOException { assertNotEmpty(yaml, "yaml"); CompositeTemplate entity; try { Map<String, Object> deserialized = YamlMapper.objectMapper().readValue(yaml.trim(), Map.class); List<Binding.ComponentBinding> componentBindings = BindingUtils .extractBindings(deserialized); entity = TemplateSerializationUtils.deserializeTemplate(deserialized); entity.bindings = new ArrayList<>(componentBindings); } catch (JsonProcessingException e) { String format = "Error processing Blueprint YAML content: %s"; Utils.logWarning(format, e.getMessage()); throw new LocalizableValidationException(String.format(format, e.getOriginalMessage()), "compute.template.yaml.error", e.getOriginalMessage()); } sanitizeCompositeTemplate(entity, false); return entity; }
@Test public void extractBindingsSimple() { final String componentName = "component-name"; //CHECKSTYLE:OFF Map<String, Object> input = new HashMap<String, Object>() {{ put(BindingUtils.COMPONENTS, new HashMap<String, Object>() {{ put(componentName, new HashMap<String, Object>() {{ put(BindingUtils.DATA, new HashMap<String, Object>() {{ put("field1", 3); put("field2", "${component1~field1}"); }}); }}); }}); }}; //CHECKSTYLE:ON List<Binding.ComponentBinding> componentBindings = BindingUtils.extractBindings(input); assertEquals(1, componentBindings.size()); //the binding should be removed from the map assertEquals(1, ((Map) ((Map) ((Map) input.get(BindingUtils.COMPONENTS)).get(componentName)) .get(BindingUtils.DATA)) .size()); assertEquals(componentName, componentBindings.get(0).componentName); assertEquals("component1~field1", componentBindings.get(0).bindings.get(0).placeholder.bindingExpression); }
@Test public void extractBindingsNested() { final String componentName = "component-name"; //CHECKSTYLE:OFF Map<String, Object> input = new HashMap<String, Object>() {{ put(BindingUtils.COMPONENTS, new HashMap<String, Object>() {{ put(componentName, new HashMap<String, Object>() {{ put(BindingUtils.DATA, new HashMap<String, Object>() {{ put("field1", 3); put("field2", new HashMap<String, Object>() {{ put("field21", "${component1~field1}"); }}); }}); }}); }}); }}; //CHECKSTYLE:ON List<Binding.ComponentBinding> componentBindings = BindingUtils.extractBindings(input); assertEquals(1, componentBindings.size()); //the binding should be removed from the map assertEquals(0, ((Map) ((Map) ((Map) ((Map) input.get(BindingUtils.COMPONENTS)).get(componentName)) .get(BindingUtils.DATA)).get("field2")).size()); assertEquals(componentName, componentBindings.get(0).componentName); assertEquals("component1~field1", componentBindings.get(0).bindings.get(0).placeholder.bindingExpression); assertEquals(Arrays.asList("field2", "field21"), componentBindings.get(0).bindings.get(0).targetFieldPath); }
List<Binding.ComponentBinding> componentBindings = BindingUtils.extractBindings(input); assertEquals(1, componentBindings.size());
@Test public void extractBindingsSimpleProvisioningTimeBinding() { final String componentName = "component-name"; //CHECKSTYLE:OFF Map<String, Object> input = new HashMap<String, Object>() {{ put(BindingUtils.COMPONENTS, new HashMap<String, Object>() {{ put(componentName, new HashMap<String, Object>() {{ put(BindingUtils.DATA, new HashMap<String, Object>() {{ put("field1", 3); put("field2", "${_resource~component1~field1}"); }}); }}); }}); }}; //CHECKSTYLE:ON List<Binding.ComponentBinding> componentBindings = BindingUtils.extractBindings(input); assertEquals(1, componentBindings.size()); //the binding should be removed from the map assertEquals(1, ((Map) ((Map) ((Map) input.get(BindingUtils.COMPONENTS)).get(componentName)) .get(BindingUtils.DATA)) .size()); assertEquals(componentName, componentBindings.get(0).componentName); assertEquals("_resource~component1~field1", componentBindings.get(0).bindings.get(0).placeholder.bindingExpression); assertTrue(componentBindings.get(0).bindings.get(0).isProvisioningTimeBinding()); }