Search in sources :

Example 6 with ConfigurationContext

use of org.apache.nifi.controller.ConfigurationContext in project nifi by apache.

the class StandardControllerServiceNode method disable.

/**
 * Will atomically disable this service by invoking its @OnDisabled operation.
 * It uses CAS operation on {@link #stateRef} to transition this service
 * from ENABLED to DISABLING state. If such transition succeeds the service
 * will be de-activated (see {@link ControllerServiceNode#isActive()}).
 * If such transition doesn't succeed (the service is still in ENABLING state)
 * then the service will still be transitioned to DISABLING state to ensure that
 * no other transition could happen on this service. However in such event
 * (e.g., its @OnEnabled finally succeeded), the {@link #enable(ScheduledExecutorService, long)}
 * operation will initiate service disabling javadoc for (see {@link #enable(ScheduledExecutorService, long)}
 * <br>
 * Upon successful invocation of @OnDisabled this service will be transitioned to
 * DISABLED state.
 */
@Override
public CompletableFuture<Void> disable(ScheduledExecutorService scheduler) {
    /*
         * The reason for synchronization is to ensure consistency of the
         * service state when another thread is in the middle of enabling this
         * service since it will attempt to transition service state from
         * ENABLING to ENABLED but only if it's active.
         */
    synchronized (this.active) {
        this.active.set(false);
    }
    final CompletableFuture<Void> future = new CompletableFuture<>();
    if (this.stateTransition.transitionToDisabling(ControllerServiceState.ENABLED, future)) {
        final ConfigurationContext configContext = new StandardConfigurationContext(this, this.serviceProvider, null, getVariableRegistry());
        scheduler.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    invokeDisable(configContext);
                } finally {
                    stateTransition.disable();
                }
            }
        });
    } else {
        this.stateTransition.transitionToDisabling(ControllerServiceState.ENABLING, future);
    }
    return future;
}
Also used : ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) CompletableFuture(java.util.concurrent.CompletableFuture)

Example 7 with ConfigurationContext

use of org.apache.nifi.controller.ConfigurationContext in project nifi by apache.

the class TestDataDogReportingTask method initContexts.

// init all contexts
private void initContexts() {
    configurationContext = Mockito.mock(ConfigurationContext.class);
    context = Mockito.mock(ReportingContext.class);
    Mockito.when(context.getProperty(DataDogReportingTask.ENVIRONMENT)).thenReturn(new MockPropertyValue(env, null));
    Mockito.when(context.getProperty(DataDogReportingTask.METRICS_PREFIX)).thenReturn(new MockPropertyValue(prefix, null));
    Mockito.when(context.getProperty(DataDogReportingTask.API_KEY)).thenReturn(new MockPropertyValue("agent", null));
    Mockito.when(context.getProperty(DataDogReportingTask.DATADOG_TRANSPORT)).thenReturn(new MockPropertyValue("DataDog Agent", null));
    EventAccess eventAccess = Mockito.mock(EventAccess.class);
    Mockito.when(eventAccess.getControllerStatus()).thenReturn(status);
    Mockito.when(context.getEventAccess()).thenReturn(eventAccess);
    logger = Mockito.mock(Logger.class);
    initContext = Mockito.mock(ReportingInitializationContext.class);
    Mockito.when(initContext.getIdentifier()).thenReturn(UUID.randomUUID().toString());
    // Mockito.when(initContext.getLogger()).thenReturn(logger);
    metricsMap = new ConcurrentHashMap<>();
    metricRegistry = Mockito.mock(MetricRegistry.class);
    virtualMachineMetrics = VirtualMachineMetrics.getInstance();
    metricsService = Mockito.mock(MetricsService.class);
}
Also used : EventAccess(org.apache.nifi.reporting.EventAccess) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) MetricsService(org.apache.nifi.reporting.datadog.metrics.MetricsService) MetricRegistry(com.codahale.metrics.MetricRegistry) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) Logger(java.util.logging.Logger) ReportingContext(org.apache.nifi.reporting.ReportingContext)

Example 8 with ConfigurationContext

use of org.apache.nifi.controller.ConfigurationContext in project nifi by apache.

the class ElasticSearchClientServiceImpl method setupClient.

private void setupClient(ConfigurationContext context) throws MalformedURLException, InitializationException {
    final String hosts = context.getProperty(HTTP_HOSTS).evaluateAttributeExpressions().getValue();
    String[] hostsSplit = hosts.split(",[\\s]*");
    this.url = hostsSplit[0];
    final SSLContextService sslService = context.getProperty(PROP_SSL_CONTEXT_SERVICE).asControllerService(SSLContextService.class);
    final String username = context.getProperty(USERNAME).evaluateAttributeExpressions().getValue();
    final String password = context.getProperty(PASSWORD).evaluateAttributeExpressions().getValue();
    final Integer connectTimeout = context.getProperty(CONNECT_TIMEOUT).asInteger();
    final Integer readTimeout = context.getProperty(SOCKET_TIMEOUT).asInteger();
    final Integer retryTimeout = context.getProperty(RETRY_TIMEOUT).asInteger();
    HttpHost[] hh = new HttpHost[hostsSplit.length];
    for (int x = 0; x < hh.length; x++) {
        URL u = new URL(hostsSplit[x]);
        hh[x] = new HttpHost(u.getHost(), u.getPort(), u.getProtocol());
    }
    final SSLContext sslContext;
    try {
        sslContext = (sslService != null && sslService.isKeyStoreConfigured() && sslService.isTrustStoreConfigured()) ? buildSslContext(sslService) : null;
    } catch (IOException | CertificateException | NoSuchAlgorithmException | UnrecoverableKeyException | KeyStoreException | KeyManagementException e) {
        getLogger().error("Error building up SSL Context from the supplied configuration.", e);
        throw new InitializationException(e);
    }
    RestClientBuilder builder = RestClient.builder(hh).setHttpClientConfigCallback(httpClientBuilder -> {
        if (sslContext != null) {
            httpClientBuilder = httpClientBuilder.setSSLContext(sslContext);
        }
        if (username != null && password != null) {
            final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
            credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
            httpClientBuilder = httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
        }
        return httpClientBuilder;
    }).setRequestConfigCallback(requestConfigBuilder -> {
        requestConfigBuilder.setConnectTimeout(connectTimeout);
        requestConfigBuilder.setSocketTimeout(readTimeout);
        return requestConfigBuilder;
    }).setMaxRetryTimeoutMillis(retryTimeout);
    this.client = builder.build();
}
Also used : RestClient(org.elasticsearch.client.RestClient) SSLContext(javax.net.ssl.SSLContext) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) InitializationException(org.apache.nifi.reporting.InitializationException) URL(java.net.URL) HashMap(java.util.HashMap) KeyStoreException(java.security.KeyStoreException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) Charset(java.nio.charset.Charset) UnrecoverableKeyException(java.security.UnrecoverableKeyException) Map(java.util.Map) AbstractControllerService(org.apache.nifi.controller.AbstractControllerService) NStringEntity(org.apache.http.nio.entity.NStringEntity) OnEnabled(org.apache.nifi.annotation.lifecycle.OnEnabled) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) MalformedURLException(java.net.MalformedURLException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) SSLContextService(org.apache.nifi.ssl.SSLContextService) KeyManagerFactory(javax.net.ssl.KeyManagerFactory) IOException(java.io.IOException) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) IOUtils(org.apache.commons.io.IOUtils) List(java.util.List) AuthScope(org.apache.http.auth.AuthScope) Response(org.elasticsearch.client.Response) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHost(org.apache.http.HttpHost) Collections(java.util.Collections) OnDisabled(org.apache.nifi.annotation.lifecycle.OnDisabled) InputStream(java.io.InputStream) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) CertificateException(java.security.cert.CertificateException) SSLContext(javax.net.ssl.SSLContext) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) KeyStoreException(java.security.KeyStoreException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) InitializationException(org.apache.nifi.reporting.InitializationException) URL(java.net.URL) KeyManagementException(java.security.KeyManagementException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) UnrecoverableKeyException(java.security.UnrecoverableKeyException) HttpHost(org.apache.http.HttpHost) SSLContextService(org.apache.nifi.ssl.SSLContextService)

Example 9 with ConfigurationContext

use of org.apache.nifi.controller.ConfigurationContext in project nifi by apache.

the class StandardProcessorTestRunner method enableControllerService.

@Override
public void enableControllerService(final ControllerService service) {
    final ControllerServiceConfiguration configuration = context.getConfiguration(service.getIdentifier());
    if (configuration == null) {
        throw new IllegalArgumentException("Controller Service " + service + " is not known");
    }
    if (configuration.isEnabled()) {
        throw new IllegalStateException("Cannot enable Controller Service " + service + " because it is not disabled");
    }
    try {
        final ConfigurationContext configContext = new MockConfigurationContext(service, configuration.getProperties(), context, variableRegistry);
        ReflectionUtils.invokeMethodsWithAnnotation(OnEnabled.class, service, configContext);
    } catch (final InvocationTargetException ite) {
        ite.getCause().printStackTrace();
        Assert.fail("Failed to enable Controller Service " + service + " due to " + ite.getCause());
    } catch (final Exception e) {
        e.printStackTrace();
        Assert.fail("Failed to enable Controller Service " + service + " due to " + e);
    }
    configuration.setEnabled(true);
}
Also used : ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) InvocationTargetException(java.lang.reflect.InvocationTargetException) InitializationException(org.apache.nifi.reporting.InitializationException) InvocationTargetException(java.lang.reflect.InvocationTargetException) IOException(java.io.IOException)

Example 10 with ConfigurationContext

use of org.apache.nifi.controller.ConfigurationContext in project nifi by apache.

the class ITReportLineageToAtlas method test.

private void test(TestConfiguration tc) throws InitializationException, IOException {
    final ReportLineageToAtlas reportingTask = new ReportLineageToAtlas();
    final MockComponentLog logger = new MockComponentLog("reporting-task-id", reportingTask);
    final ReportingInitializationContext initializationContext = mock(ReportingInitializationContext.class);
    when(initializationContext.getLogger()).thenReturn(logger);
    final ConfigurationContext configurationContext = new MockConfigurationContext(tc.properties, null);
    final ValidationContext validationContext = mock(ValidationContext.class);
    when(validationContext.getProperty(any())).then(invocation -> new MockPropertyValue(tc.properties.get(invocation.getArguments()[0])));
    final ReportingContext reportingContext = mock(ReportingContext.class);
    final MockStateManager stateManager = new MockStateManager(reportingTask);
    final EventAccess eventAccess = mock(EventAccess.class);
    when(reportingContext.getProperties()).thenReturn(tc.properties);
    when(reportingContext.getProperty(any())).then(invocation -> new MockPropertyValue(tc.properties.get(invocation.getArguments()[0])));
    when(reportingContext.getStateManager()).thenReturn(stateManager);
    when(reportingContext.getEventAccess()).thenReturn(eventAccess);
    when(eventAccess.getGroupStatus(eq("root"))).thenReturn(tc.rootPgStatus);
    final ProvenanceRepository provenanceRepository = mock(ProvenanceRepository.class);
    when(eventAccess.getControllerStatus()).thenReturn(tc.rootPgStatus);
    when(eventAccess.getProvenanceRepository()).thenReturn(provenanceRepository);
    when(eventAccess.getProvenanceEvents(eq(-1L), anyInt())).thenReturn(tc.provenanceRecords);
    when(provenanceRepository.getMaxEventId()).thenReturn((long) tc.provenanceRecords.size() - 1);
    when(provenanceRepository.getEvent(anyLong())).then(invocation -> tc.provenanceRecords.get(((Long) invocation.getArguments()[0]).intValue()));
    // To mock this async method invocations, keep the requested event ids in a stack.
    final ComputeLineageSubmission lineageComputationSubmission = mock(ComputeLineageSubmission.class);
    when(provenanceRepository.submitLineageComputation(anyLong(), any())).thenAnswer(invocation -> {
        requestedLineageComputationIds.push((Long) invocation.getArguments()[0]);
        return lineageComputationSubmission;
    });
    when(lineageComputationSubmission.getResult()).then(invocation -> tc.lineageResults.get(requestedLineageComputationIds.pop()));
    final ComputeLineageSubmission expandParentsSubmission = mock(ComputeLineageSubmission.class);
    when(provenanceRepository.submitExpandParents(anyLong(), any())).thenAnswer(invocation -> {
        requestedExpandParentsIds.push(((Long) invocation.getArguments()[0]));
        return expandParentsSubmission;
    });
    when(expandParentsSubmission.getResult()).then(invocation -> tc.parentLineageResults.get(requestedExpandParentsIds.pop()));
    tc.properties.put(ATLAS_NIFI_URL, "http://localhost:8080/nifi");
    tc.properties.put(ATLAS_URLS, TARGET_ATLAS_URL);
    tc.properties.put(ATLAS_USER, "admin");
    tc.properties.put(ATLAS_PASSWORD, "admin");
    tc.properties.put(new PropertyDescriptor.Builder().name("hostnamePattern.example").dynamic(true).build(), ".*");
    reportingTask.initialize(initializationContext);
    reportingTask.validate(validationContext);
    reportingTask.setup(configurationContext);
    reportingTask.onTrigger(reportingContext);
    reportingTask.onUnscheduled();
    reportingTask.onStopped();
}
Also used : EventAccess(org.apache.nifi.reporting.EventAccess) ConfigurationContext(org.apache.nifi.controller.ConfigurationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) MockComponentLog(org.apache.nifi.util.MockComponentLog) ComputeLineageSubmission(org.apache.nifi.provenance.lineage.ComputeLineageSubmission) MockPropertyValue(org.apache.nifi.util.MockPropertyValue) ValidationContext(org.apache.nifi.components.ValidationContext) ReportingContext(org.apache.nifi.reporting.ReportingContext) ReportingInitializationContext(org.apache.nifi.reporting.ReportingInitializationContext) MockConfigurationContext(org.apache.nifi.util.MockConfigurationContext) MockStateManager(org.apache.nifi.state.MockStateManager) Matchers.anyLong(org.mockito.Matchers.anyLong) ProvenanceRepository(org.apache.nifi.provenance.ProvenanceRepository)

Aggregations

ConfigurationContext (org.apache.nifi.controller.ConfigurationContext)13 HashMap (java.util.HashMap)6 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)6 Test (org.junit.Test)5 IOException (java.io.IOException)4 Map (java.util.Map)4 RecordSchema (org.apache.nifi.serialization.record.RecordSchema)4 MockConfigurationContext (org.apache.nifi.util.MockConfigurationContext)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 List (java.util.List)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 NarCloseable (org.apache.nifi.nar.NarCloseable)3 EventAccess (org.apache.nifi.reporting.EventAccess)3 ReportingContext (org.apache.nifi.reporting.ReportingContext)3 ReportingInitializationContext (org.apache.nifi.reporting.ReportingInitializationContext)3 SchemaIdentifier (org.apache.nifi.serialization.record.SchemaIdentifier)3 MockPropertyValue (org.apache.nifi.util.MockPropertyValue)3 SchemaMetadata (com.hortonworks.registries.schemaregistry.SchemaMetadata)2 SchemaMetadataInfo (com.hortonworks.registries.schemaregistry.SchemaMetadataInfo)2 SchemaVersionInfo (com.hortonworks.registries.schemaregistry.SchemaVersionInfo)2