public static NestedState deserializeServiceDocument( Map<String, Object> value, Class<? extends ServiceDocument> type, ObjectMapper objectMapper) { Map<String, NestedState> children = deserializeChildren(value, type); ServiceDocument serviceDocument = objectMapper.convertValue(value, type); if (serviceDocument.documentSelfLink == null) { // set some documentSelfLink so that we can wire the objects together, we will remove it // before persisting serviceDocument.documentSelfLink = UUID.randomUUID().toString(); } NestedState state = new NestedState(); state.object = serviceDocument; state.children = children; return state; }
@SuppressWarnings("unchecked") public static Map<String, Object> serializeComponentTemplate( ComponentTemplate<?> componentTemplate, ObjectMapper objectMapper, ObjectWriter objectWriter) throws IOException { // We have a special deserializer for the ComponentTemplate @SuppressWarnings("rawtypes") Map serializedComponentTemplate = objectMapper .readValue(objectWriter.writeValueAsString(componentTemplate), Map.class); serializedComponentTemplate.remove("children"); if (isNullOrEmpty(componentTemplate.children)) { return serializedComponentTemplate; } NestedState state = new NestedState(); state.object = (ServiceDocument) componentTemplate.data; state.children = componentTemplate.children; Map<String, Object> serializedDataComponentTemplate = serializeNestedState(state, objectMapper, objectWriter); serializedComponentTemplate.put("data", serializedDataComponentTemplate); return serializedComponentTemplate; }
private NestedState createComponentNestedState(ComponentTemplate<?> component) { NestedState nestedState = new NestedState(); nestedState.object = (ServiceDocument) component.data; nestedState.children = component.children; ResourceType resourceType = ResourceType.fromContentType(component.type); String factoryLink = CompositeComponentRegistry .descriptionFactoryLinkByType(resourceType.getName()); nestedState.factoryLink = factoryLink; return nestedState; }
document.getClass()); NestedState result = new NestedState(); result.object = document;
private void handleBindings(Consumer<CompositeDescriptionExpanded> callbackFunction, URI uri, CompositeDescriptionExpanded description) { if (description.bindings == null) { callbackFunction.accept(description); return; } convertCompositeDescriptionToCompositeTemplate(this, description).thenApply(template -> { BindingEvaluator.evaluateBindings(template); return template; }).thenAccept(template -> DeferredResult.allOf(template.components.values().stream() .map(t -> new NestedState((ServiceDocument) t.data, t.children)) .map(n -> n.sendRequest(this, Action.PUT)) .collect(Collectors.toList()))).thenCompose(nothing -> this.sendWithDeferredResult( Operation.createGet(uri), CompositeDescriptionExpanded.class)) .whenComplete((result, ex) -> { if (ex != null) { failTask("Error while updating evaluated", ex); return; } callbackFunction.accept(result); }); }
@Test public void testEvaluateSimpleClosureTimingBindingToObject() { List<Binding> bindings = Arrays .asList(binding(Arrays.asList("inputs", "test"), "_resource~A~inputs~hostname")); Map<String, NestedState> closures = new HashMap<>(); Closure closure = new Closure(); closure.name = "Closure"; JsonPrimitive inStr = new JsonPrimitive("localhost"); closure.inputs = new HashMap<>(); closure.inputs.put("hostname", inStr); closures.put("A", new NestedState(closure)); NestedState nestedState = new NestedState(closure); NestedState evalObj = BindingEvaluator .evaluateProvisioningTimeBindings(nestedState, bindings, closures); assertNotNull(evalObj); }
@Test public void testEvaluateSimpleBindingFromObjectWithLinks() { List<Binding> bindings = Arrays .asList(binding(Arrays.asList("parentLink"), "_resource~A~networkInterfaceLinks~0~address")); Map<String, NestedState> computes = new HashMap<>(); TemplateComputeState computeStateA = new TemplateComputeState(); computeStateA.parentLink = "some-host"; computeStateA.networkInterfaceLinks = Arrays.asList("nis-link"); NetworkInterfaceState nis = new NetworkInterfaceState(); nis.documentSelfLink = "nis-link"; nis.address = "some-address"; NestedState nestedStateA = new NestedState(computeStateA); nestedStateA.children.put("nis-link", new NestedState(nis)); computes.put("A", nestedStateA); TemplateComputeState computeState = new TemplateComputeState(); NestedState nestedState = new NestedState(computeState); NestedState evalObj = BindingEvaluator .evaluateProvisioningTimeBindings(nestedState, bindings, computes); assertNotNull(evalObj); assertEquals(nis.address, ((TemplateComputeState) evalObj.object).parentLink); }
@Test public void testEvaluateSimpleBindingToObjectWithLinks() { List<Binding> bindings = Arrays .asList(binding(Arrays.asList("networks", "0", "address"), "_resource~A~parentLink")); Map<String, NestedState> computes = new HashMap<>(); TemplateComputeState computeStateA = new TemplateComputeState(); computeStateA.parentLink = "some-host"; computes.put("A", new NestedState(computeStateA)); TemplateComputeState computeState = new TemplateComputeState(); computeState.networkInterfaceLinks = Arrays.asList("nis-link"); NetworkInterfaceState nis = new NetworkInterfaceState(); nis.documentSelfLink = "nis-link"; NestedState nestedState = new NestedState(computeState); nestedState.children.put(nis.documentSelfLink, new NestedState(nis)); NestedState evalObj = BindingEvaluator .evaluateProvisioningTimeBindings(nestedState, bindings, computes); assertNotNull(evalObj); assertEquals(((NetworkInterfaceState) evalObj.children.get("nis-link").object).address, computeStateA.parentLink); }
@Test public void testEvaluateSimpleProvisioningBinding() { List<Binding> bindings = Arrays .asList(binding(Arrays.asList("parentLink"), "_resource~A~parentLink")); Map<String, NestedState> containers = new HashMap<>(); ContainerState containerState = new ContainerState(); containerState.parentLink = "some-host"; containers.put("A", new NestedState(containerState)); ContainerState containerStateE = new ContainerState(); NestedState evalObj = BindingEvaluator .evaluateProvisioningTimeBindings(new NestedState(containerStateE), bindings, containers); assertNotNull(evalObj); assertEquals(((ContainerState) evalObj.object).parentLink, containerState.parentLink); }
description.workingDir = service.working_dir; NestedState nestedState = new NestedState(); nestedState.object = description; return fromDescriptionToComponentTemplate(nestedState,