Search in sources :

Example 21 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class ConfigSchemaFunction method apply.

@Override
public ConfigSchema apply(TemplateDTO templateDTO) {
    Map<String, Object> map = new HashMap<>();
    map.put(CommonPropertyKeys.FLOW_CONTROLLER_PROPS_KEY, flowControllerSchemaFunction.apply(templateDTO).toMap());
    FlowSnippetDTO snippet = templateDTO.getSnippet();
    addSnippet(map, snippet);
    return new ConfigSchema(map);
}
Also used : FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) HashMap(java.util.HashMap) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema)

Example 22 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class ConfigSchemaV1Test method testInvalidSourceAndDestinationNames.

@Test
public void testInvalidSourceAndDestinationNames() throws IOException, SchemaLoaderException {
    Map<String, Object> yamlAsMap = SchemaLoader.loadYamlAsMap(ConfigSchemaTest.class.getClassLoader().getResourceAsStream("config-minimal.yml"));
    List<Map<String, Object>> connections = (List<Map<String, Object>>) yamlAsMap.get(CommonPropertyKeys.CONNECTIONS_KEY);
    assertEquals(1, connections.size());
    String fakeSource = "fakeSource";
    String fakeDestination = "fakeDestination";
    Map<String, Object> connection = connections.get(0);
    connection.put(ConnectionSchemaV1.SOURCE_NAME_KEY, fakeSource);
    connection.put(ConnectionSchemaV1.DESTINATION_NAME_KEY, fakeDestination);
    ConfigSchema configSchema = new ConfigSchemaV1(yamlAsMap).convert();
    List<String> validationIssues = configSchema.getValidationIssues();
    assertEquals(4, validationIssues.size());
    assertEquals(BaseSchema.getIssueText(ConnectionSchema.DESTINATION_ID_KEY, "Connection(id: 0401b747-1dca-31c7-ab4b-cdacf7e6c44b, name: TailToSplit)", BaseSchema.IT_WAS_NOT_FOUND_AND_IT_IS_REQUIRED), validationIssues.get(0));
    assertEquals(BaseSchema.getIssueText(ConnectionSchema.SOURCE_ID_KEY, "Connection(id: 0401b747-1dca-31c7-ab4b-cdacf7e6c44b, name: TailToSplit)", BaseSchema.IT_WAS_NOT_FOUND_AND_IT_IS_REQUIRED), validationIssues.get(1));
    assertEquals(ConfigSchemaV1.CONNECTION_WITH_NAME + connection.get(NAME_KEY) + ConfigSchemaV1.HAS_INVALID_DESTINATION_NAME + fakeDestination, validationIssues.get(2));
    assertEquals(ConfigSchemaV1.CONNECTION_WITH_NAME + connection.get(NAME_KEY) + ConfigSchemaV1.HAS_INVALID_SOURCE_NAME + fakeSource, validationIssues.get(3));
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema) Test(org.junit.Test) ConfigSchemaTest(org.apache.nifi.minifi.commons.schema.ConfigSchemaTest)

Example 23 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class SchemaLoaderTest method testMinimalConfigEmptyVersion.

@Test
public void testMinimalConfigEmptyVersion() throws IOException, SchemaLoaderException {
    Map<String, Object> yamlAsMap = SchemaLoader.loadYamlAsMap(SchemaLoaderTest.class.getClassLoader().getResourceAsStream("config-minimal.yml"));
    yamlAsMap.put(ConfigSchema.VERSION, "");
    ConfigSchema configSchema = SchemaLoader.loadConfigSchemaFromYaml(yamlAsMap);
    validateMinimalConfigVersion1Parse(configSchema);
}
Also used : ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema) Test(org.junit.Test)

Example 24 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class NiFiRestConfigurationProvider method getConfiguration.

@Override
public Configuration getConfiguration(String contentType, Integer version, Map<String, List<String>> parameters) throws ConfigurationProviderException {
    if (!CONTENT_TYPE.equals(contentType)) {
        throw new ConfigurationProviderException("Unsupported content type: " + contentType + " supported value is " + CONTENT_TYPE);
    }
    String filename = templateNamePattern;
    for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
        if (entry.getValue().size() != 1) {
            throw new InvalidParameterException("Multiple values for same parameter not supported in this provider.");
        }
        filename = filename.replaceAll(Pattern.quote("${" + entry.getKey() + "}"), entry.getValue().get(0));
    }
    int index = filename.indexOf("${");
    while (index != -1) {
        int endIndex = filename.indexOf("}", index);
        if (endIndex == -1) {
            break;
        }
        String variable = filename.substring(index + 2, endIndex);
        if (!"version".equals(variable)) {
            throw new InvalidParameterException("Found unsubstituted parameter " + variable);
        }
        index = endIndex + 1;
    }
    String id = null;
    if (version == null) {
        String filenamePattern = Arrays.stream(filename.split(Pattern.quote("${version}"), -1)).map(Pattern::quote).collect(Collectors.joining("([0-9+])"));
        Pair<String, Integer> maxIdAndVersion = getMaxIdAndVersion(filenamePattern);
        id = maxIdAndVersion.getFirst();
        version = maxIdAndVersion.getSecond();
    }
    filename = filename.replaceAll(Pattern.quote("${version}"), Integer.toString(version));
    WriteableConfiguration configuration = configurationCache.getCacheFileInfo(contentType, parameters).getConfiguration(version);
    if (configuration.exists()) {
        if (logger.isDebugEnabled()) {
            logger.debug("Configuration " + configuration + " exists and can be served from configurationCache.");
        }
    } else {
        if (logger.isDebugEnabled()) {
            logger.debug("Configuration " + configuration + " doesn't exist, will need to download and convert template.");
        }
        if (id == null) {
            try {
                String tmpFilename = templateNamePattern;
                for (Map.Entry<String, List<String>> entry : parameters.entrySet()) {
                    if (entry.getValue().size() != 1) {
                        throw new InvalidParameterException("Multiple values for same parameter not supported in this provider.");
                    }
                    tmpFilename = tmpFilename.replaceAll(Pattern.quote("${" + entry.getKey() + "}"), entry.getValue().get(0));
                }
                Pair<Stream<Pair<String, String>>, Closeable> streamCloseablePair = getIdAndFilenameStream();
                try {
                    String finalFilename = filename;
                    id = streamCloseablePair.getFirst().filter(p -> finalFilename.equals(p.getSecond())).map(Pair::getFirst).findFirst().orElseThrow(() -> new InvalidParameterException("Unable to find template named " + finalFilename));
                } finally {
                    streamCloseablePair.getSecond().close();
                }
            } catch (IOException | TemplatesIteratorException e) {
                throw new ConfigurationProviderException("Unable to retrieve template list", e);
            }
        }
        HttpURLConnection urlConnection = httpConnector.get("/templates/" + id + "/download");
        try (InputStream inputStream = urlConnection.getInputStream()) {
            ConfigSchema configSchema = ConfigMain.transformTemplateToSchema(inputStream);
            SchemaSaver.saveConfigSchema(configSchema, configuration.getOutputStream());
        } catch (IOException e) {
            throw new ConfigurationProviderException("Unable to download template from url " + urlConnection.getURL(), e);
        } catch (JAXBException e) {
            throw new ConfigurationProviderException("Unable to convert template to yaml", e);
        } finally {
            urlConnection.disconnect();
        }
    }
    return configuration;
}
Also used : InputStream(java.io.InputStream) Closeable(java.io.Closeable) JAXBException(javax.xml.bind.JAXBException) IOException(java.io.IOException) ConfigurationProviderException(org.apache.nifi.minifi.c2.api.ConfigurationProviderException) InvalidParameterException(org.apache.nifi.minifi.c2.api.InvalidParameterException) HttpURLConnection(java.net.HttpURLConnection) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) List(java.util.List) Stream(java.util.stream.Stream) InputStream(java.io.InputStream) Map(java.util.Map) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema)

Example 25 with ConfigSchema

use of org.apache.nifi.minifi.commons.schema.ConfigSchema in project nifi-minifi by apache.

the class ConfigTransformer method transformConfigFile.

public static void transformConfigFile(InputStream sourceStream, String destPath) throws Exception {
    ConvertableSchema<ConfigSchema> convertableSchema = throwIfInvalid(SchemaLoader.loadConvertableSchemaFromYaml(sourceStream));
    ConfigSchema configSchema = throwIfInvalid(convertableSchema.convert());
    // Create nifi.properties and flow.xml.gz in memory
    ByteArrayOutputStream nifiPropertiesOutputStream = new ByteArrayOutputStream();
    writeNiFiProperties(configSchema, nifiPropertiesOutputStream);
    writeFlowXmlFile(configSchema, destPath);
    // Write nifi.properties and flow.xml.gz
    writeNiFiPropertiesFile(nifiPropertiesOutputStream, destPath);
}
Also used : ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) ConfigSchema(org.apache.nifi.minifi.commons.schema.ConfigSchema)

Aggregations

ConfigSchema (org.apache.nifi.minifi.commons.schema.ConfigSchema)31 Test (org.junit.Test)20 InputStream (java.io.InputStream)7 HashMap (java.util.HashMap)6 Map (java.util.Map)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 IOException (java.io.IOException)5 FileInputStream (java.io.FileInputStream)4 List (java.util.List)4 JAXBException (javax.xml.bind.JAXBException)4 FileNotFoundException (java.io.FileNotFoundException)3 OutputStream (java.io.OutputStream)3 ArrayList (java.util.ArrayList)3 Properties (java.util.Properties)3 ConfigSchemaTest (org.apache.nifi.minifi.commons.schema.ConfigSchemaTest)3 ConnectionSchema (org.apache.nifi.minifi.commons.schema.ConnectionSchema)3 HttpURLConnection (java.net.HttpURLConnection)2 ByteBuffer (java.nio.ByteBuffer)2 HashSet (java.util.HashSet)2