private static String[] convertDependsOn(Object dependsOn) { if (dependsOn == null) { return null; } return YamlMapper.objectMapper().convertValue(dependsOn, String[].class); }
public <T extends BaseKubernetesObject> T getKubernetesEntity(Class<T> type) throws IOException { return YamlMapper.objectMapper().readValue(kubernetesEntity, type); }
public static String fromJsonToYaml(String json) throws IOException { JsonNode jsonNode = objectMapper().readTree(json); return objectMapper().writeValueAsString(jsonNode); }
public static boolean isValidYaml(String yaml) { if (yaml == null || yaml.startsWith("{")) { return false; } try { objectMapper().readValue(yaml, Map.class); } catch (IOException ex) { return false; } return true; } }
public static String serializeKubernetesEntity(BaseKubernetesObject kubernetesEntity) throws IOException { return YamlMapper.objectMapper().setSerializationInclusion(Include.NON_NULL) .writeValueAsString(kubernetesEntity).trim(); }
public static String fromYamlToJson(String yaml) throws IOException { Object obj = objectMapper().readValue(yaml, Object.class); return Utils.toJson(obj); }
public static NestedState deserializeServiceDocument( Map<String, Object> value, Class<? extends ServiceDocument> type) { return deserializeServiceDocument(value, type, YamlMapper.objectMapper()); }
public static CompositeTemplate deserializeTemplate(Map<String, Object> initialMap) { return deserializeTemplate(initialMap, YamlMapper.objectMapper()); }
public static BaseKubernetesObject deserializeKubernetesEntity(String yaml) throws IOException { assertNotEmpty(yaml, "yaml"); BaseKubernetesObject entity; try { entity = YamlMapper.objectMapper().readValue(yaml.trim(), BaseKubernetesObject.class); if (POD_TYPE.equals(entity.kind)) { entity = YamlMapper.objectMapper().readValue(yaml.trim(), Pod.class); } else if (POD_TEMPLATE.equals(entity.kind)) { entity = YamlMapper.objectMapper().readValue(yaml.trim(), PodTemplate.class); } else if (REPLICATION_CONTROLLER_TYPE.equals(entity.kind)) { entity = YamlMapper.objectMapper().readValue(yaml.trim(), ReplicationController.class); } else if (DEPLOYMENT_TYPE.equals(entity.kind)) { entity = YamlMapper.objectMapper().readValue(yaml.trim(), Deployment.class); } else if (SERVICE_TYPE.equals(entity.kind)) { entity = YamlMapper.objectMapper().readValue(yaml.trim(), Service.class); } else { entity = YamlMapper.objectMapper().readValue(yaml.trim(), BaseKubernetesObject.class); } } catch (JsonProcessingException e) { throw new IllegalArgumentException( "Error processing Kubernetes configuration YAML content: " + e .getOriginalMessage()); } return entity; }
@SuppressWarnings("unchecked") public void merge(BaseKubernetesObject copyFrom) { Map<String, Object> copyTo; try { copyTo = YamlMapper.objectMapper().readValue(kubernetesEntity, Map.class); } catch (Exception e) { Utils.logWarning("Could not read value of kubernetes entity from yaml, reason :%s", Utils.toString(e)); return; } Map<String, Object> copyFromMap = YamlMapper.objectMapper().convertValue(copyFrom, Map.class); copyTo.putAll(copyFromMap); try { kubernetesEntity = YamlMapper.objectMapper().writeValueAsString(copyTo); } catch (JsonProcessingException e) { Utils.logWarning("Could not write of kubernetes entity to yaml, reason :%s", Utils.toString(e)); } } }
public static Map<String, Object> serializeTemplate(CompositeTemplate template) throws IOException { return serializeTemplate(template, YamlMapper.objectMapper(), YamlMapper.objectWriter()); }
public static Map<String, Object> serializeNestedState(NestedState nestedState) throws IOException { return serializeNestedState(nestedState, YamlMapper.objectMapper(), YamlMapper.objectWriter()); }
@SuppressWarnings("unchecked") private static <T> ComponentTemplate<T> createTemplate(String type, Object data) { assertNotNull(data, "data"); ComponentTemplate<T> template = new ComponentTemplate<>(); template.type = type; Class<?> descriptionClass = getDescriptionClass(type); if (descriptionClass.equals(ContainerDescription.class)) { descriptionClass = CompositeTemplateContainerDescription.class; } template.data = (T) YamlMapper.objectMapper().convertValue(data, descriptionClass); return template; }
public static DockerCompose deserializeDockerCompose(String yaml) throws IOException { assertNotEmpty(yaml, "yaml"); DockerCompose entity; try { entity = YamlMapper.objectMapper().readValue(yaml.trim(), DockerCompose.class); } catch (JsonProcessingException e) { throw new LocalizableValidationException( "Error processing Docker Compose v2 YAML content: " + e.getOriginalMessage(), "compute.template.yaml.compose2.error", e.getOriginalMessage()); } sanitizeDockerCompose(entity); return entity; }
@SuppressWarnings("rawtypes") public static void assertEqualsYamls(String expected, String actual, boolean ignoreEmptyValues) throws IOException { Map expectedMap = YamlMapper.objectMapper().readValue(expected, Map.class); Map actualMap = YamlMapper.objectMapper().readValue(actual, Map.class); if (ignoreEmptyValues) { removeEmptyValues(expectedMap); removeEmptyValues(actualMap); } Logger.getLogger(CompositeTemplateUtilTest.class.getName()).log(Level.INFO, "ignoreEmptyValues: " + ignoreEmptyValues + " \nassertEqualsYamls : expected=<" + YamlMapper.objectMapper().writeValueAsString(expectedMap) + "> and actual=<" + YamlMapper.objectMapper().writeValueAsString(actualMap) + ">"); assertEquals(expectedMap, actualMap); }
@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; }
template = YamlMapper.objectMapper().readValue(yaml, CommonDescriptionEntity.class); } catch (JsonProcessingException e) { throw new LocalizableValidationException(
@Test public void testMapper() throws Exception { URL templateUrl = getClass().getClassLoader().getResource( "WordPress_with_MySQL_containers.yaml"); ObjectMapper mapper = YamlMapper.objectMapper(); CompositeTemplate bp = mapper.readValue(templateUrl, CompositeTemplate.class); assertEquals("id", null, bp.id); assertEquals("name", "wordPressWithMySql", bp.name); assertNotNull("properties", bp.properties); assertEquals("properties.size", 1, bp.properties.size()); assertNotNull("components", bp.components); assertEquals("components.size", 2, bp.components.size()); ComponentTemplate<?> mysqlComponent = bp.components.get("mysql"); assertNotNull("components[mysql]", mysqlComponent); assertEquals("components[mysql].type", ResourceType.CONTAINER_TYPE.getContentType(), mysqlComponent.type); } }
assertTrue(resultYaml.contains("${_resource~mysql~address}:3306")); Map<String, Object> templateMap = YamlMapper.objectMapper().readValue(originalYaml.trim(), Map.class); Map<String, Object> resultMap = YamlMapper.objectMapper().readValue(resultYaml.trim(), Map.class); assertEquals(templateMap.get("components"), resultMap.get("components"));
@Test public void testMapping() throws Exception { ObjectMapper mapper = YamlMapper.objectMapper();