@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 void evaluateProvisioningTimeBinding(Binding binding, Map<String, NestedState> provisionedResources, Map<String, Object> evaluatedBindings) throws ReflectiveOperationException, IOException { String componentName = BindingUtils .extractComponentNameFromBindingExpression(binding.placeholder.bindingExpression); NestedState provisionedResource = provisionedResources.get(componentName); if (provisionedResource == null) { return; } Object value = getFieldValueByPath( BindingUtils.convertToFieldPath(binding.placeholder.bindingExpression), provisionedResource); value = BindingUtils.valueForBinding(binding, value); evaluatedBindings.put(binding.placeholder.bindingExpression, value); }
public boolean isProvisioningTimeBinding() { return (this.placeholder != null) && BindingUtils.isProvisioningTimeBinding(this.placeholder.bindingExpression); } }
Object value = entry.getValue(); Binding binding = toBinding(targetPath, fieldName, value); 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));
@Override public Map<String, AffinityConstraint> getAffinityConstraints() { return componentDescription.bindings.stream() .filter(b -> b.isProvisioningTimeBinding()) .map(b -> BindingUtils .extractComponentNameFromBindingExpression( b.placeholder.bindingExpression)) .distinct() .collect(Collectors.toMap( Function.identity(), AffinityConstraint::new)); }
int startIndex = strVal.indexOf(PLACEHOLDER_PREFIX); if (startIndex != -1) { int endIndex = findPlaceholderEndIndex(result, startIndex); if (endIndex != -1) { String placeholder = result.substring(startIndex + PLACEHOLDER_PREFIX.length(),
.extractComponentNameFromBindingExpression( binding.placeholder.bindingExpression); ComponentDescription sourceDescription = nameToComponent.get(sourceComponentName);
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; }
List<String> sourceFieldPath = BindingUtils.convertToFieldPath(bindingExpression); String sourceComponentName = BindingUtils .extractComponentNameFromBindingExpression(bindingExpression); return BindingUtils.valueForBinding(binding, rootSourceValue);
public static String extractComponentNameFromBindingExpression(String bindingExpression) { String[] split = bindingExpression.split(FIELD_SEPARATOR); if (isProvisioningTimeBinding(bindingExpression)) { return split[1]; } else { return split[0]; } }
@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()); }
public static List<String> convertToFieldPath(String bindingExpression) { String[] split = bindingExpression.split(FIELD_SEPARATOR); if (isProvisioningTimeBinding(bindingExpression)) { return new ArrayList<>(Arrays.asList(Arrays.copyOfRange(split, 2, split.length))); } else { return new ArrayList<>(Arrays.asList(Arrays.copyOfRange(split, 1, split.length))); } }
@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); }
List<Binding.ComponentBinding> componentBindings = BindingUtils.extractBindings(input); assertEquals(1, componentBindings.size());
@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); }
@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()); }