use of org.apache.nifi.minifi.commons.schema.exception.SchemaLoaderException in project nifi-minifi by apache.
the class SchemaLoader method loadYamlAsMap.
public static Map<String, Object> loadYamlAsMap(InputStream sourceStream) throws IOException, SchemaLoaderException {
try {
Yaml yaml = new Yaml();
// Parse the YAML file
final Object loadedObject = yaml.load(sourceStream);
// Verify the parsed object is a Map structure
if (loadedObject instanceof Map) {
return (Map<String, Object>) loadedObject;
} else {
throw new SchemaLoaderException("Provided YAML configuration is not a Map");
}
} catch (YAMLException e) {
throw new IOException(e);
} finally {
sourceStream.close();
}
}
use of org.apache.nifi.minifi.commons.schema.exception.SchemaLoaderException in project nifi-minifi by apache.
the class ConfigMainTest method testV1YmlIfPresent.
private void testV1YmlIfPresent(String name, Map<String, Object> yamlMap) throws IOException, SchemaLoaderException {
InputStream upgradedInputStream = upgradeAndReturn(name + "-v1.yml");
if (upgradedInputStream != null) {
ConvertableSchema<ConfigSchema> configSchemaConvertableSchema = SchemaLoader.loadConvertableSchemaFromYaml(upgradedInputStream);
ConfigSchema configSchemaUpgradedFromV1 = configSchemaConvertableSchema.convert();
assertTrue(configSchemaUpgradedFromV1.isValid());
assertEquals(configSchemaConvertableSchema, configSchemaUpgradedFromV1);
ConfigSchema configSchemaFromCurrent = new ConfigSchema(yamlMap);
List<ProcessorSchema> currentProcessors = configSchemaFromCurrent.getProcessGroupSchema().getProcessors();
List<ProcessorSchema> v1Processors = configSchemaUpgradedFromV1.getProcessGroupSchema().getProcessors();
assertEquals(currentProcessors.size(), v1Processors.size());
// V1 doesn't have ids so we need to map the autogenerated ones to the ones from the template
Map<String, String> v1IdToCurrentIdMap = new HashMap<>();
for (int i = 0; i < currentProcessors.size(); i++) {
ProcessorSchema currentProcessor = currentProcessors.get(i);
ProcessorSchema v1Processor = v1Processors.get(i);
assertEquals(currentProcessor.getName(), v1Processor.getName());
v1IdToCurrentIdMap.put(v1Processor.getId(), currentProcessor.getId());
v1Processor.setId(currentProcessor.getId());
}
List<RemoteProcessGroupSchema> currentRPGs = configSchemaFromCurrent.getProcessGroupSchema().getRemoteProcessGroups();
List<RemoteProcessGroupSchema> v1RPGs = configSchemaUpgradedFromV1.getProcessGroupSchema().getRemoteProcessGroups();
// V1 doesn't have ids so we need to map the autogenerated ones to the ones from the template
for (int i = 0; i < currentRPGs.size(); i++) {
RemoteProcessGroupSchema currentRPG = currentRPGs.get(i);
RemoteProcessGroupSchema v1RPG = v1RPGs.get(i);
assertEquals(currentRPG.getName(), v1RPG.getName());
v1IdToCurrentIdMap.put(v1RPG.getId(), currentRPG.getId());
v1RPG.setId(currentRPG.getId());
}
configSchemaUpgradedFromV1.getProcessGroupSchema().getRemoteProcessGroups().stream().flatMap(g -> g.getInputPorts().stream()).map(RemotePortSchema::getId).sequential().forEach(id -> v1IdToCurrentIdMap.put(id, id));
List<ConnectionSchema> currentConnections = configSchemaFromCurrent.getProcessGroupSchema().getConnections();
List<ConnectionSchema> v1Connections = configSchemaUpgradedFromV1.getProcessGroupSchema().getConnections();
// Update source and dest ids, can set connection id equal because it isn't referenced elsewhere
assertEquals(currentConnections.size(), v1Connections.size());
for (int i = 0; i < currentConnections.size(); i++) {
ConnectionSchema currentConnection = currentConnections.get(i);
ConnectionSchema v1Connection = v1Connections.get(i);
assertEquals(currentConnection.getName(), v1Connection.getName());
v1Connection.setId(currentConnection.getId());
v1Connection.setSourceId(v1IdToCurrentIdMap.get(v1Connection.getSourceId()));
v1Connection.setDestinationId(v1IdToCurrentIdMap.get(v1Connection.getDestinationId()));
}
ConfigSchema.getAllProcessGroups(configSchemaFromCurrent.getProcessGroupSchema()).stream().flatMap(p -> p.getRemoteProcessGroups().stream()).forEach(r -> {
clearV3orLaterProperties(r);
r.setTransportProtocol(RemoteProcessGroupSchema.TransportProtocolOptions.RAW.name());
});
Map<String, Object> v1YamlMap = configSchemaUpgradedFromV1.toMap();
assertNoMapDifferences(v1YamlMap, configSchemaFromCurrent.toMap());
}
}
use of org.apache.nifi.minifi.commons.schema.exception.SchemaLoaderException in project nifi-minifi by apache.
the class ConfigMainTest method testV2YmlIfPresent.
private void testV2YmlIfPresent(String name, Map<String, Object> yamlMap) throws IOException, SchemaLoaderException {
InputStream upgradedInputStream = upgradeAndReturn(name + "-v2.yml");
if (upgradedInputStream != null) {
ConvertableSchema<ConfigSchema> configSchemaConvertableSchema = SchemaLoader.loadConvertableSchemaFromYaml(upgradedInputStream);
ConfigSchema configSchemaUpgradedFromV2 = configSchemaConvertableSchema.convert();
ConfigSchema configSchemaFromCurrent = new ConfigSchema(yamlMap);
ConfigSchema.getAllProcessGroups(configSchemaFromCurrent.getProcessGroupSchema()).stream().flatMap(p -> p.getRemoteProcessGroups().stream()).forEach(r -> {
clearV3orLaterProperties(r);
});
assertNoMapDifferences(configSchemaUpgradedFromV2.toMap(), configSchemaFromCurrent.toMap());
}
}
Aggregations