Search in sources :

Example 1 with NiFiRestClient

use of com.thinkbiganalytics.nifi.rest.client.NiFiRestClient in project kylo by Teradata.

the class TemplateCreationHelperTest method updateControllerServiceReferencesWithRecursive.

/**
 * Verify recursively enabling controller services when updating processor properties.
 */
@Test
public void updateControllerServiceReferencesWithRecursive() {
    final List<ControllerServiceDTO> updatedControllerServices = new ArrayList<>();
    final List<NifiProperty> updatedProperties = new ArrayList<>();
    // Mock NiFi client
    final NiFiControllerServicesRestClient controllerServicesRestClient = Mockito.mock(NiFiControllerServicesRestClient.class);
    Mockito.when(controllerServicesRestClient.update(Mockito.any())).thenAnswer(answer -> {
        final ControllerServiceDTO controllerService = answer.getArgumentAt(0, ControllerServiceDTO.class);
        updatedControllerServices.add(controllerService);
        return controllerService;
    });
    final NiFiRestClient restClient = Mockito.mock(NiFiRestClient.class);
    Mockito.when(restClient.controllerServices()).thenReturn(controllerServicesRestClient);
    // Mock Legacy NiFi client
    final LegacyNifiRestClient legacyRestClient = Mockito.mock(LegacyNifiRestClient.class);
    Mockito.when(legacyRestClient.enableControllerServiceAndSetProperties(Mockito.any(), Mockito.any())).thenReturn(new ControllerServiceDTO());
    Mockito.when(legacyRestClient.getNiFiRestClient()).thenReturn(restClient);
    Mockito.when(legacyRestClient.getPropertyDescriptorTransform()).thenReturn(new MockNiFiPropertyDescriptorTransform());
    Mockito.doAnswer(invocation -> {
        updatedProperties.add(invocation.getArgumentAt(2, NifiProperty.class));
        return null;
    }).when(legacyRestClient).updateProcessorProperty(Mockito.isNull(String.class), Mockito.eq("P1"), Mockito.any());
    final ControllerServiceDTO service1 = new ControllerServiceDTO();
    service1.setDescriptors(Collections.singletonMap("service", newPropertyDescriptor("service", "com.example.Service2", "S2")));
    service1.setId("S1");
    service1.setName("Service1");
    service1.setProperties(newHashMap("service", "invalid"));
    service1.setState("DISABLED");
    final ControllerServiceDTO service2 = new ControllerServiceDTO();
    service2.setId("S2");
    service2.setName("Service2");
    service2.setProperties(new HashMap<>());
    service2.setState("ENABLED");
    Mockito.when(legacyRestClient.getControllerServices()).thenReturn(ImmutableSet.of(service1, service2));
    // Mock processors
    final ProcessorConfigDTO config = new ProcessorConfigDTO();
    config.setDescriptors(Collections.singletonMap("service", newPropertyDescriptor("service", "com.example.Service1", "S1")));
    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(legacyRestClient);
    helper.snapshotControllerServiceReferences();
    helper.identifyNewlyCreatedControllerServiceReferences();
    helper.updateControllerServiceReferences(Collections.singletonList(processor));
    // Verify updated properties
    Assert.assertEquals("Property 'Service' not set on controller service 'Server1'.", 1, updatedControllerServices.size());
    Assert.assertEquals("S2", updatedControllerServices.get(0).getProperties().get("service"));
    Assert.assertEquals("Property 'Service' not set on processor 'Processor1'.", 1, updatedProperties.size());
    Assert.assertEquals("S1", updatedProperties.get(0).getValue());
}
Also used : ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) ArrayList(java.util.ArrayList) LegacyNifiRestClient(com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient) NiFiControllerServicesRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiControllerServicesRestClient) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) NiFiRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiRestClient) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) Test(org.junit.Test)

Example 2 with NiFiRestClient

use of com.thinkbiganalytics.nifi.rest.client.NiFiRestClient 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;
}
Also used : Iterables(com.google.common.collect.Iterables) VersionedProcessGroup(com.thinkbiganalytics.nifi.rest.model.VersionedProcessGroup) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) NifiError(com.thinkbiganalytics.nifi.rest.model.NifiError) LoggerFactory(org.slf4j.LoggerFactory) ConnectionDTO(org.apache.nifi.web.api.dto.ConnectionDTO) NiFiPropertyDescriptor(com.thinkbiganalytics.nifi.rest.model.NiFiPropertyDescriptor) NifiProcessUtil(com.thinkbiganalytics.nifi.rest.support.NifiProcessUtil) HashMap(java.util.HashMap) StringUtils(org.apache.commons.lang3.StringUtils) NiFiPropertyDescriptorTransform(com.thinkbiganalytics.nifi.rest.model.NiFiPropertyDescriptorTransform) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) NifiClientRuntimeException(com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException) NiFiAllowableValue(com.thinkbiganalytics.nifi.rest.model.NiFiAllowableValue) Lists(com.google.common.collect.Lists) ProcessGroupDTO(org.apache.nifi.web.api.dto.ProcessGroupDTO) PropertyDescriptorDTO(org.apache.nifi.web.api.dto.PropertyDescriptorDTO) NifiConnectionUtil(com.thinkbiganalytics.nifi.rest.support.NifiConnectionUtil) NiFiObjectCache(com.thinkbiganalytics.nifi.rest.NiFiObjectCache) NifiPropertyUtil(com.thinkbiganalytics.nifi.rest.support.NifiPropertyUtil) Map(java.util.Map) NiFiRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiRestClient) NifiComponentNotFoundException(com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException) Nonnull(javax.annotation.Nonnull) NifiConstants(com.thinkbiganalytics.nifi.rest.support.NifiConstants) Nullable(javax.annotation.Nullable) NifiTemplateNameUtil(com.thinkbiganalytics.nifi.rest.support.NifiTemplateNameUtil) Logger(org.slf4j.Logger) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) Set(java.util.Set) ComparisonChain(com.google.common.collect.ComparisonChain) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) List(java.util.List) Predicate(com.google.common.base.Predicate) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) Optional(java.util.Optional) WebApplicationException(javax.ws.rs.WebApplicationException) Collections(java.util.Collections) LegacyNifiRestClient(com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient) NifiProcessGroup(com.thinkbiganalytics.nifi.rest.model.NifiProcessGroup) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) FlowSnippetDTO(org.apache.nifi.web.api.dto.FlowSnippetDTO) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) NiFiRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiRestClient) HashMap(java.util.HashMap) Nonnull(javax.annotation.Nonnull)

Example 3 with NiFiRestClient

use of com.thinkbiganalytics.nifi.rest.client.NiFiRestClient in project kylo by Teradata.

the class NifiRestTest method setupRestClient.

@Before
public void setupRestClient() {
    restClient = new LegacyNifiRestClient();
    NifiRestClientConfig clientConfig = new NifiRestClientConfig();
    // clientConfig.setHost("localhost");
    clientConfig.setHost("34.208.236.190");
    clientConfig.setPort(8079);
    NiFiRestClient c = new NiFiRestClientV1(clientConfig);
    restClient.setClient(c);
    nifiFlowCache = new NifiFlowCacheImpl();
    propertyDescriptorTransform = new NiFiPropertyDescriptorTransformV1();
    createFeedBuilderCache = new NiFiObjectCache();
    createFeedBuilderCache.setRestClient(restClient);
    templateConnectionUtil = new TemplateConnectionUtil();
    templateConnectionUtil.setRestClient(restClient);
    templateConnectionUtil.setNifiFlowCache(nifiFlowCache);
    templateConnectionUtil.setNiFiObjectCache(createFeedBuilderCache);
    templateConnectionUtil.setPropertyDescriptorTransform(propertyDescriptorTransform);
}
Also used : NiFiRestClient(com.thinkbiganalytics.nifi.rest.client.NiFiRestClient) NiFiPropertyDescriptorTransformV1(com.thinkbiganalytics.nifi.v1.rest.model.NiFiPropertyDescriptorTransformV1) NifiRestClientConfig(com.thinkbiganalytics.nifi.rest.client.NifiRestClientConfig) TemplateConnectionUtil(com.thinkbiganalytics.feedmgr.nifi.TemplateConnectionUtil) LegacyNifiRestClient(com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient) NiFiRestClientV1(com.thinkbiganalytics.nifi.v1.rest.client.NiFiRestClientV1) NifiFlowCacheImpl(com.thinkbiganalytics.feedmgr.nifi.cache.NifiFlowCacheImpl) Before(org.junit.Before)

Aggregations

LegacyNifiRestClient (com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient)3 NiFiRestClient (com.thinkbiganalytics.nifi.rest.client.NiFiRestClient)3 NifiProperty (com.thinkbiganalytics.nifi.rest.model.NifiProperty)2 ArrayList (java.util.ArrayList)2 Predicate (com.google.common.base.Predicate)1 ComparisonChain (com.google.common.collect.ComparisonChain)1 Iterables (com.google.common.collect.Iterables)1 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 TemplateConnectionUtil (com.thinkbiganalytics.feedmgr.nifi.TemplateConnectionUtil)1 NifiFlowCacheImpl (com.thinkbiganalytics.feedmgr.nifi.cache.NifiFlowCacheImpl)1 NiFiObjectCache (com.thinkbiganalytics.nifi.rest.NiFiObjectCache)1 NiFiControllerServicesRestClient (com.thinkbiganalytics.nifi.rest.client.NiFiControllerServicesRestClient)1 NifiClientRuntimeException (com.thinkbiganalytics.nifi.rest.client.NifiClientRuntimeException)1 NifiComponentNotFoundException (com.thinkbiganalytics.nifi.rest.client.NifiComponentNotFoundException)1 NifiRestClientConfig (com.thinkbiganalytics.nifi.rest.client.NifiRestClientConfig)1 NiFiAllowableValue (com.thinkbiganalytics.nifi.rest.model.NiFiAllowableValue)1 NiFiPropertyDescriptor (com.thinkbiganalytics.nifi.rest.model.NiFiPropertyDescriptor)1 NiFiPropertyDescriptorTransform (com.thinkbiganalytics.nifi.rest.model.NiFiPropertyDescriptorTransform)1 NifiError (com.thinkbiganalytics.nifi.rest.model.NifiError)1