use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.
the class TemplateCreationHelperTest method updateControllerServiceReferencesWithEnabled.
/**
* Verify preferring enabled controller services over disabled controller services when updating processor properties.
*/
@Test
public void updateControllerServiceReferencesWithEnabled() {
final AtomicReference<NifiProperty> updateProperty = new AtomicReference<>();
// Mock NiFi client
final LegacyNifiRestClient restClient = Mockito.mock(LegacyNifiRestClient.class);
Mockito.when(restClient.getPropertyDescriptorTransform()).thenReturn(new MockNiFiPropertyDescriptorTransform());
Mockito.doAnswer(invocation -> {
updateProperty.set(invocation.getArgumentAt(2, NifiProperty.class));
return null;
}).when(restClient).updateProcessorProperty(Mockito.isNull(String.class), Mockito.eq("P1"), Mockito.any());
final ControllerServiceDTO service1 = new ControllerServiceDTO();
service1.setId("S1");
service1.setName("Service1");
service1.setProperties(Collections.emptyMap());
service1.setState("DISABLED");
final ControllerServiceDTO service2 = new ControllerServiceDTO();
service2.setId("S2");
service2.setName("Service2");
service2.setProperties(Collections.emptyMap());
service2.setState("ENABLED");
Mockito.when(restClient.getControllerServices()).thenReturn(ImmutableSet.of(service1, service2));
// Mock processors
final ProcessorConfigDTO config = new ProcessorConfigDTO();
config.setDescriptors(Collections.singletonMap("service", newPropertyDescriptor("service", "com.example.Service", "S1", "S2")));
config.setProperties(Collections.singletonMap("service", "invalid"));
final ProcessorDTO processor = new ProcessorDTO();
processor.setId("P1");
processor.setName("Processor1");
processor.setConfig(config);
// Update processors
final TemplateCreationHelper helper = new TemplateCreationHelper(restClient);
helper.snapshotControllerServiceReferences();
helper.identifyNewlyCreatedControllerServiceReferences();
helper.updateControllerServiceReferences(Collections.singletonList(processor));
// Verify new processor properties
Assert.assertNotNull("Property 'Service' not set on processor 'Processor1'.", updateProperty.get());
Assert.assertEquals("S2", updateProperty.get().getValue());
}
use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.
the class TemplateCreationHelper method reassignControllerServiceIds.
private List<ProcessorDTO> reassignControllerServiceIds(List<ProcessorDTO> processors, TemplateInstance instance) {
Set<ProcessorDTO> updatedProcessors = new HashSet<>();
if (processors != null) {
processors.stream().forEach(processorDTO -> {
Map<String, String> updatedProcessorProperties = new HashMap<>();
processorDTO.getConfig().getDescriptors().forEach((k, v) -> {
if (v.getIdentifiesControllerService() != null) {
boolean idsMatch = getMergedControllerServices().keySet().stream().anyMatch(id -> id.equalsIgnoreCase(processorDTO.getConfig().getProperties().get(k)));
if (!idsMatch && templateProperties != null && !templateProperties.isEmpty()) {
NifiProperty matchingProperty = templateProperties.stream().filter(p -> p.getKey().equalsIgnoreCase(k) && p.getProcessorName().equalsIgnoreCase(processorDTO.getName()) && v.getIdentifiesControllerService().equalsIgnoreCase(p.getPropertyDescriptor().getIdentifiesControllerService())).findFirst().orElse(null);
if (matchingProperty != null && matchingProperty.getPropertyDescriptor() != null && matchingProperty.getPropertyDescriptor().getAllowableValues() != null) {
NiFiAllowableValue matchingValue = matchingProperty.getPropertyDescriptor().getAllowableValues().stream().filter(niFiAllowableValue -> niFiAllowableValue.getValue().equalsIgnoreCase(matchingProperty.getValue())).findFirst().orElse(null);
if (matchingValue != null) {
String name = matchingValue.getDisplayName();
String validControllerServiceId = hasMatchingService(enabledServiceNameMap, name) ? enabledServiceNameMap.get(name).get(0).getId() : hasMatchingService(serviceNameMap, name) ? serviceNameMap.get(name).get(0).getId() : null;
if (StringUtils.isNotBlank(validControllerServiceId) && (v.isRequired() || !v.isRequired() && StringUtils.isNotBlank(processorDTO.getConfig().getProperties().get(k)))) {
processorDTO.getConfig().getProperties().put(k, validControllerServiceId);
updatedProcessorProperties.put(k, validControllerServiceId);
if (!updatedProcessors.contains(processorDTO)) {
updatedProcessors.add(processorDTO);
}
}
}
}
}
// if we havent made a match attempt to see if the cs was removed
if (!updatedProcessorProperties.containsKey(k) && !idsMatch && instance != null) {
String value = processorDTO.getConfig().getProperties().get(k);
// find the correct reference from that was removed due to a matching service
ControllerServiceDTO controllerServiceDTO = instance.findMatchingControllerServoce(value);
if (controllerServiceDTO != null) {
updatedProcessorProperties.put(k, controllerServiceDTO.getId());
}
}
}
});
if (!updatedProcessorProperties.isEmpty()) {
ProcessorDTO updatedProcessor = new ProcessorDTO();
updatedProcessor.setId(processorDTO.getId());
updatedProcessor.setConfig(new ProcessorConfigDTO());
updatedProcessor.getConfig().setProperties(updatedProcessorProperties);
// update the processor
ProcessorDTO updated = restClient.updateProcessor(updatedProcessor);
updatedProcessors.add(updated);
}
});
}
// update the data back in the processors list
if (!updatedProcessors.isEmpty()) {
Map<String, ProcessorDTO> updatedMap = updatedProcessors.stream().collect(Collectors.toMap(p -> p.getId(), p -> p));
return processors.stream().map(p -> updatedMap.containsKey(p.getId()) ? updatedMap.get(p.getId()) : p).collect(Collectors.toList());
}
return processors;
}
use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.
the class TemplateCreationHelper method instantiateFlowFromTemplate.
/**
* Instantiates the specified template in the specified process group.
*
* <p>Controller services that are created under the specified process group will be moved to the root process group. This side-effect may be removed in the future.</p>
*
* @param processGroupId the process group id
* @param templateId the template id
* @return the instantiated flow
* @throws NifiComponentNotFoundException if the process group or template does not exist
*/
@Nonnull
public TemplateInstance instantiateFlowFromTemplate(@Nonnull final String processGroupId, @Nonnull final String templateId) throws NifiComponentNotFoundException {
// Instantiate template
final NiFiRestClient nifiClient = restClient.getNiFiRestClient();
final FlowSnippetDTO templateFlow = nifiClient.processGroups().instantiateTemplate(processGroupId, templateId);
TemplateInstance instance = new TemplateInstance(templateFlow);
// Move controller services to root process group (NiFi >= v1.0)
final Set<ControllerServiceDTO> groupControllerServices = nifiClient.processGroups().getControllerServices(processGroupId);
final Map<String, String> idMap = new HashMap<>(groupControllerServices.size());
groupControllerServices.stream().filter(controllerService -> controllerService.getParentGroupId().equals(processGroupId)).forEach(groupControllerService -> {
// Delete scoped service
final String oldId = groupControllerService.getId();
nifiClient.controllerServices().delete(groupControllerService.getId());
// Create root service
final ControllerServiceDTO rootControllerService = new ControllerServiceDTO();
rootControllerService.setComments(groupControllerService.getComments());
rootControllerService.setName(groupControllerService.getName());
rootControllerService.setType(groupControllerService.getType());
ControllerServiceDTO newRootService = nifiClient.processGroups().createControllerService("root", rootControllerService);
final String rootId = newRootService.getId();
// Map old ID to new ID
idMap.put(oldId, rootId);
instance.movedScopedControllerService(groupControllerService, newRootService);
});
// Set properties on root controller services
groupControllerServices.stream().filter(controllerService -> controllerService.getParentGroupId().equals(processGroupId)).forEach(groupControllerService -> {
final Map<String, String> properties = groupControllerService.getProperties();
groupControllerService.getDescriptors().values().stream().filter(descriptor -> StringUtils.isNotBlank(descriptor.getIdentifiesControllerService())).forEach(descriptor -> {
final String name = descriptor.getName();
final String oldId = properties.get(name);
properties.put(name, idMap.get(oldId));
});
final ControllerServiceDTO rootControllerService = new ControllerServiceDTO();
rootControllerService.setId(idMap.get(groupControllerService.getId()));
rootControllerService.setProperties(properties);
nifiClient.controllerServices().update(rootControllerService);
});
// Return flow
return instance;
}
use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.
the class AbstractNiFiControllerServicesRestClient method updateStateByIdWithRetries.
/**
* Sends a request to update the state of the specified controller service and waits for it to finish updating.
*
* @param id the controller service id
* @param state the new state
* @param retries number of retries, at least 0; will try {@code retries} + 1 times
* @param timeout duration to wait between retries
* @param timeUnit unit of time for {@code timeout}
* @return the controller service, if updated
* @throws NifiClientRuntimeException if the state cannot be changed
* @throws NifiComponentNotFoundException if the controller service does not exist
*/
protected ControllerServiceDTO updateStateByIdWithRetries(@Nonnull final String id, @Nonnull final String state, final int retries, final int timeout, @Nonnull final TimeUnit timeUnit) {
// Mark the Service as DISABLED
ControllerServiceDTO controllerService = new ControllerServiceDTO();
controllerService.setId(id);
controllerService.setState(state);
controllerService = update(controllerService);
// Wait for finished
for (int count = 0; isPendingState(controllerService.getState(), state) && count < retries; ++count) {
Uninterruptibles.sleepUninterruptibly(timeout, timeUnit);
controllerService = findById(id).orElseThrow(() -> new NifiComponentNotFoundException(id, NifiConstants.NIFI_COMPONENT_TYPE.CONTROLLER_SERVICE, null));
}
// Verify state change or throw bulletin message
if (state.equals(controllerService.getState())) {
return controllerService;
} else {
String msg = id;
try {
final List<BulletinDTO> bulletins = getClient().getBulletins(id);
if (!bulletins.isEmpty()) {
msg = bulletins.get(0).getMessage();
}
} catch (final ClientErrorException e) {
// ignored
}
throw new NifiClientRuntimeException("Timeout waiting for controller service to be " + state + ": " + msg);
}
}
use of org.apache.nifi.web.api.dto.ControllerServiceDTO in project kylo by Teradata.
the class LegacyNifiRestClient method getControllerServiceByName.
/**
* Returns Null if cant find it
*/
public ControllerServiceDTO getControllerServiceByName(String type, final String serviceName) {
ControllerServiceDTO controllerService = null;
Set<ControllerServiceDTO> entity = getControllerServices();
if (entity != null) {
List<ControllerServiceDTO> services = Lists.newArrayList(Iterables.filter(entity, new Predicate<ControllerServiceDTO>() {
@Override
public boolean apply(ControllerServiceDTO controllerServiceDTO) {
return controllerServiceDTO.getName().equalsIgnoreCase(serviceName);
}
}));
if (services != null) {
for (ControllerServiceDTO controllerServiceDTO : services) {
if (controllerService == null) {
controllerService = controllerServiceDTO;
}
if (controllerServiceDTO.getState().equals(NifiProcessUtil.SERVICE_STATE.ENABLED.name())) {
controllerService = controllerServiceDTO;
break;
}
}
}
}
return controllerService;
}
Aggregations