Search in sources :

Example 1 with ConfigurationCacheFileInfo

use of org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo in project nifi-minifi by apache.

the class CacheConfigurationProviderTest method testGetConfiguration.

@Test
public void testGetConfiguration() throws ConfigurationProviderException {
    int version = 99;
    ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class);
    ConfigurationCacheFileInfo configurationCacheFileInfo2 = mock(ConfigurationCacheFileInfo.class);
    WriteableConfiguration configuration = mock(WriteableConfiguration.class);
    WriteableConfiguration configuration2 = mock(WriteableConfiguration.class);
    Map<String, List<String>> parameters = mock(Map.class);
    when(configConfigurationCache.getCacheFileInfo(TEST_CONTENT_TYPE, parameters)).thenReturn(configurationCacheFileInfo);
    when(configConfigurationCache.getCacheFileInfo(TEST_CONTENT_TYPE_2, parameters)).thenReturn(configurationCacheFileInfo2);
    when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration);
    when(configurationCacheFileInfo2.getConfiguration(version)).thenReturn(configuration2);
    assertEquals(configuration, cacheConfigurationProvider.getConfiguration(TEST_CONTENT_TYPE, version, parameters));
    assertEquals(configuration2, cacheConfigurationProvider.getConfiguration(TEST_CONTENT_TYPE_2, version, parameters));
}
Also used : WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) List(java.util.List) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) Test(org.junit.Test)

Example 2 with ConfigurationCacheFileInfo

use of org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo in project nifi-minifi by apache.

the class DelegatingConfigurationProvider method getConfiguration.

@Override
public Configuration getConfiguration(String contentType, Integer version, Map<String, List<String>> parameters) throws ConfigurationProviderException {
    HttpURLConnection remoteC2ServerConnection = null;
    try {
        if (version == null) {
            remoteC2ServerConnection = getDelegateConnection(contentType, parameters);
            version = Integer.parseInt(remoteC2ServerConnection.getHeaderField("X-Content-Version"));
            if (logger.isDebugEnabled()) {
                logger.debug("Got current version " + version + " from upstream.");
            }
        }
        ConfigurationCacheFileInfo cacheFileInfo = configurationCache.getCacheFileInfo(contentType, parameters);
        WriteableConfiguration configuration = cacheFileInfo.getConfiguration(version);
        if (!configuration.exists()) {
            if (remoteC2ServerConnection == null) {
                remoteC2ServerConnection = getDelegateConnection(contentType, parameters);
            }
            try (InputStream inputStream = remoteC2ServerConnection.getInputStream();
                OutputStream outputStream = configuration.getOutputStream()) {
                IOUtils.copy(inputStream, outputStream);
            } catch (IOException e) {
                throw new ConfigurationProviderException("Unable to copy remote configuration to cache.", e);
            }
        }
        return configuration;
    } finally {
        if (remoteC2ServerConnection != null) {
            remoteC2ServerConnection.disconnect();
        }
    }
}
Also used : ConfigurationProviderException(org.apache.nifi.minifi.c2.api.ConfigurationProviderException) HttpURLConnection(java.net.HttpURLConnection) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) IOException(java.io.IOException)

Example 3 with ConfigurationCacheFileInfo

use of org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo in project nifi-minifi by apache.

the class DelegatingConfigurationProviderTest method testGetConfigurationExistsWithVersion.

@Test
public void testGetConfigurationExistsWithVersion() throws ConfigurationProviderException {
    ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class);
    WriteableConfiguration configuration = mock(WriteableConfiguration.class);
    when(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo);
    when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration);
    when(configuration.exists()).thenReturn(true);
    assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, version, parameters));
}
Also used : WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) Test(org.junit.Test)

Example 4 with ConfigurationCacheFileInfo

use of org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo in project nifi-minifi by apache.

the class DelegatingConfigurationProviderTest method testGetConfigurationDoesntExistWithVersion.

@Test
public void testGetConfigurationDoesntExistWithVersion() throws ConfigurationProviderException, IOException {
    ConfigurationCacheFileInfo configurationCacheFileInfo = mock(ConfigurationCacheFileInfo.class);
    WriteableConfiguration configuration = mock(WriteableConfiguration.class);
    byte[] payload = "payload".getBytes(StandardCharsets.UTF_8);
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    when(httpURLConnection.getInputStream()).thenReturn(new ByteArrayInputStream(payload));
    when(configuration.getOutputStream()).thenReturn(output);
    when(configurationCache.getCacheFileInfo(contentType, parameters)).thenReturn(configurationCacheFileInfo);
    when(configurationCacheFileInfo.getConfiguration(version)).thenReturn(configuration);
    when(configuration.exists()).thenReturn(false);
    assertEquals(configuration, delegatingConfigurationProvider.getConfiguration(contentType, version, parameters));
    assertArrayEquals(payload, output.toByteArray());
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) WriteableConfiguration(org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration) ConfigurationCacheFileInfo(org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.Test)

Example 5 with ConfigurationCacheFileInfo

use of org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo in project nifi-minifi by apache.

the class FileSystemConfigurationCache method getCacheFileInfo.

@Override
public ConfigurationCacheFileInfo getCacheFileInfo(String contentType, Map<String, List<String>> parameters) throws InvalidParameterException {
    String pathString = pathPattern;
    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.");
        }
        pathString = pathString.replaceAll(Pattern.quote("${" + entry.getKey() + "}"), entry.getValue().get(0));
    }
    pathString = pathString + "." + contentType.replace('/', '.');
    String[] split = pathString.split("/");
    for (String s1 : split) {
        int openBrace = s1.indexOf("${");
        if (openBrace >= 0 && openBrace < s1.length() + 2) {
            int closeBrace = s1.indexOf("}", openBrace + 2);
            if (closeBrace >= 0) {
                throw new InvalidParameterException("Found unsubstituted variable " + s1.substring(openBrace + 2, closeBrace));
            }
        }
    }
    String[] splitPath = split;
    Path path = pathRoot.toAbsolutePath();
    for (int i = 0; i < splitPath.length - 1; i++) {
        String s = splitPath[i];
        path = resolveChildAndVerifyParent(path, s);
    }
    Pair<Path, String> dirPathAndFilename = new Pair<>(path, splitPath[splitPath.length - 1]);
    if (logger.isDebugEnabled()) {
        StringBuilder message = new StringBuilder("Parameters {");
        message.append(parameters.entrySet().stream().map(e -> e.getKey() + ": [" + String.join(", ", e.getValue()) + "]").collect(Collectors.joining(", ")));
        message.append("} -> ");
        message.append(dirPathAndFilename.getFirst().resolve(dirPathAndFilename.getSecond()).toAbsolutePath());
        logger.debug(message.toString());
    }
    return new FileSystemCacheFileInfoImpl(this, dirPathAndFilename.getFirst(), dirPathAndFilename.getSecond() + ".v");
}
Also used : Path(java.nio.file.Path) InvalidParameterException(org.apache.nifi.minifi.c2.api.InvalidParameterException) List(java.util.List) Map(java.util.Map) Pair(org.apache.nifi.minifi.c2.api.util.Pair)

Aggregations

ConfigurationCacheFileInfo (org.apache.nifi.minifi.c2.api.cache.ConfigurationCacheFileInfo)9 WriteableConfiguration (org.apache.nifi.minifi.c2.api.cache.WriteableConfiguration)9 Test (org.junit.Test)8 List (java.util.List)5 HashMap (java.util.HashMap)3 FileSystemConfigurationCache (org.apache.nifi.minifi.c2.cache.filesystem.FileSystemConfigurationCache)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 Path (java.nio.file.Path)1 Map (java.util.Map)1 ConfigurationProviderException (org.apache.nifi.minifi.c2.api.ConfigurationProviderException)1 InvalidParameterException (org.apache.nifi.minifi.c2.api.InvalidParameterException)1 Pair (org.apache.nifi.minifi.c2.api.util.Pair)1