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; }
public DeferredResult<Operation> sendRequest(Service sender, Service.Action action) { return sendRequest(sender, action, null); }
Map<String, Class<? extends ServiceDocument>> fields = NestedState.getLinkFields( nestedState.object.getClass());
Map<String, Class<? extends ServiceDocument>> fields = getLinkFields( document.getClass()); NestedState result = new NestedState(); result.object = document; DeferredResult<NestedState> nestedStateDeferredResult = get(sender, (String) fieldValue, fieldClass); DeferredResult<Void> childDeferredResult = get(sender, link, fieldClass) .thenAccept(child -> result.children
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); }); }
projectLink) { Map<String, Class<? extends ServiceDocument>> fields = getLinkFields(object.getClass()); .sendRequest(sender, action); .sendRequest(sender, action); String link = getLink(action);
continue; nestedResources.add(NestedState.get(this, link, meta.stateTemplateClass)); updates.add(evaluated.sendRequest(this, Action.PUT));
.map(link -> { ComponentMeta meta = metaByDescriptionLink(link); return NestedState.get(service, link, meta.descriptionClass); }).collect(Collectors.toList());
/** * Updates links that reference other components from the composition. If a link field contains * the name of a component from the composition, it is replaced with the actual component link * through a PATCH request. */ private DeferredResult<Void> updateComponentLinks( Map<String, NestedState> componentNestedStates) { Map<String, String> componentLinks = componentNestedStates.entrySet().stream().collect( Collectors.toMap(e -> e.getKey(), e -> e.getValue().object.documentSelfLink)); List<DeferredResult<Void>> updateOps = componentNestedStates.values().stream() .map(ns -> ns.updateComponentLinks(this, componentLinks)).collect(Collectors.toList()); return DeferredResult.allOf(updateOps).thenApply(ignore -> (Void) null); }
.getNestedObjectType(type, fieldName); if (childType != null) {
.forEach(ns -> drs.add(ns.updateComponentLinks(sender, componentLinks)));
NestedState.getNestedObjectType(value.getClass(), fieldName) != null;
@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; }
.map(ns -> ns.sendRequest(this, Action.POST, projectLink)) .collect(Collectors.toList())); publishComponentsDR .map(ns -> ns.sendRequest(this, Action.DELETE, projectLink)) .collect(Collectors.toList()));
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; }
@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,