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;
}
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);
}
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();
}
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);
}
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();
}
Aggregations