Search in sources :

Example 21 with NifiProperty

use of com.thinkbiganalytics.nifi.rest.model.NifiProperty in project kylo by Teradata.

the class PropertyExpressionResolverTest method resolveExpression.

/**
 * Verifies resolving expressions in property values.
 */
@Test
public void resolveExpression() {
    final FeedMetadata metadata = new FeedMetadata();
    metadata.setSystemFeedName("myfeed");
    // Verify config variable
    final NifiProperty prop1 = createProperty("${config.test.value}");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop1));
    Assert.assertEquals("hello-world", prop1.getValue());
    // Verify metadata variable
    final NifiProperty prop2 = createProperty("${metadata.systemFeedName}");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop2));
    Assert.assertEquals("myfeed", prop2.getValue());
    // Verify static config
    final NifiProperty prop3 = createProperty(STATIC_KEY, "${metadata.systemFeedName}");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop3));
    Assert.assertEquals("myapp", prop3.getValue());
    final NifiProperty prop4 = createProperty(STATIC_KEY, "${config.test.value}");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop4));
    Assert.assertEquals("hello-world", prop4.getValue());
    final NifiProperty prop5 = createProperty(STATIC_KEY, "");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop5));
    Assert.assertEquals("myapp", prop5.getValue());
    // Verify multiple variables
    final NifiProperty prop6 = createProperty("${metadata.systemFeedName}.${config.test.value}");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop6));
    Assert.assertEquals("myfeed.hello-world", prop6.getValue());
    // Verify multiple variables
    final NifiProperty prop7 = createProperty("$${${metadata.systemFeedName}.${config.test.value}}");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop7));
    Assert.assertEquals("${myfeed.hello-world}", prop7.getValue());
    // Verify multiple variables
    final NifiProperty prop8 = createProperty("${config.${metadata.systemFeedName}.${config.test.value}}");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop8));
    Assert.assertEquals("runtime value", prop8.getValue());
    // Verify static text
    final NifiProperty prop9 = createProperty("config.test.value");
    Assert.assertFalse(resolver.resolveExpression(metadata, prop9));
    Assert.assertEquals("config.test.value", prop9.getValue());
    // verify replacement with NiFi el
    final NifiProperty prop10 = createProperty("property1", "a value");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop10));
    Assert.assertEquals("/path/to/property1,${nifi.expression.property}", prop10.getValue());
    // verify replacement without NiFi el
    final NifiProperty prop11 = createProperty("Another Processor", "property1", "a value");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop11));
    Assert.assertEquals("/path/to/another_processor/property1/location", prop11.getValue());
    // verify replacement without NiFi el using default processor type replacement
    final NifiProperty prop12 = createProperty("My New Processor", "property1", "a value");
    Assert.assertTrue(resolver.resolveExpression(metadata, prop12));
    Assert.assertEquals("/path/to/property1/location", prop12.getValue());
    // verify replacement without NiFi el using default processor type replacement
    final NifiProperty extraFiles = createProperty("extra_files", "a value");
    Assert.assertTrue(resolver.resolveExpression(metadata, extraFiles));
    Assert.assertEquals("${table_field_policy_json_file},/usr/hdp/current/spark-client/conf/hive-site.xml", extraFiles.getValue());
    Assert.assertTrue(resolver.resolveExpression(metadata, extraFiles));
    Assert.assertEquals("${table_field_policy_json_file},/usr/hdp/current/spark-client/conf/hive-site.xml", extraFiles.getValue());
    final NifiProperty hiveSchema = createProperty(STATIC_KEY, "${config.hive.schema}");
    Assert.assertTrue(resolver.resolveExpression(metadata, hiveSchema));
    Assert.assertEquals("hive", hiveSchema.getValue());
}
Also used : FeedMetadata(com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) Test(org.junit.Test)

Example 22 with NifiProperty

use of com.thinkbiganalytics.nifi.rest.model.NifiProperty 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());
}
Also used : ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) LegacyNifiRestClient(com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 23 with NifiProperty

use of com.thinkbiganalytics.nifi.rest.model.NifiProperty in project kylo by Teradata.

the class NifiPropertyUtil method groupPropertiesByProcessGroupAndProcessor.

/**
 * Return a map of processGroupId to a map of that groups processors and its respective propeties
 *
 * @param properties the properties to inspect
 * @return a map with the key being the processGroupId and the value being a map of properties with its key being the processorId
 */
public static Map<String, Map<String, List<NifiProperty>>> groupPropertiesByProcessGroupAndProcessor(List<NifiProperty> properties) {
    Map<String, Map<String, List<NifiProperty>>> processGroupProperties = new HashMap();
    for (NifiProperty property : properties) {
        String processGroup = property.getProcessGroupId();
        String processorId = property.getProcessorId();
        if (!processGroupProperties.containsKey(processGroup)) {
            processGroupProperties.put(processGroup, new HashMap<String, List<NifiProperty>>());
        }
        if (!processGroupProperties.get(processGroup).containsKey(processorId)) {
            processGroupProperties.get(processGroup).put(processorId, new ArrayList<NifiProperty>());
        }
        processGroupProperties.get(processGroup).get(processorId).add(property);
    }
    return processGroupProperties;
}
Also used : HashMap(java.util.HashMap) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 24 with NifiProperty

use of com.thinkbiganalytics.nifi.rest.model.NifiProperty in project kylo by Teradata.

the class NifiPropertyUtil method matchPropertyByProcessorName.

/**
 * update the templateProperties with those that match the nifiProperty using the {@link NifiProperty#getProcessorName()}  to make the match
 *
 * @param templateProperties the properties to update
 * @param nifiProperty       the property to check
 * @param updateMode         a mode to update
 * @return the property, or updated property if matched
 */
private static NifiProperty matchPropertyByProcessorName(Collection<NifiProperty> templateProperties, final NifiProperty nifiProperty, PROPERTY_MATCH_AND_UPDATE_MODE updateMode) {
    NifiProperty matchingProperty = findPropertyByProcessorName(templateProperties, nifiProperty);
    if (matchingProperty != null) {
        // 
        if (PROPERTY_MATCH_AND_UPDATE_MODE.FEED_DETAILS_MATCH_TEMPLATE.equals(updateMode)) {
            // copy the property
            NifiProperty copy = new NifiProperty(matchingProperty);
            templateProperties.remove(matchingProperty);
            templateProperties.add(copy);
            matchingProperty = copy;
        }
        updateMatchingProperty(matchingProperty, nifiProperty, updateMode);
    }
    return matchingProperty;
}
Also used : NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty)

Example 25 with NifiProperty

use of com.thinkbiganalytics.nifi.rest.model.NifiProperty 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;
}
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) 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) 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) Comparator(java.util.Comparator) 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) ControllerServiceDTO(org.apache.nifi.web.api.dto.ControllerServiceDTO) HashMap(java.util.HashMap) ProcessorConfigDTO(org.apache.nifi.web.api.dto.ProcessorConfigDTO) ProcessorDTO(org.apache.nifi.web.api.dto.ProcessorDTO) NiFiAllowableValue(com.thinkbiganalytics.nifi.rest.model.NiFiAllowableValue) NifiProperty(com.thinkbiganalytics.nifi.rest.model.NifiProperty) HashSet(java.util.HashSet)

Aggregations

NifiProperty (com.thinkbiganalytics.nifi.rest.model.NifiProperty)47 ArrayList (java.util.ArrayList)22 FeedMetadata (com.thinkbiganalytics.feedmgr.rest.model.FeedMetadata)19 Map (java.util.Map)15 RegisteredTemplate (com.thinkbiganalytics.feedmgr.rest.model.RegisteredTemplate)13 HashMap (java.util.HashMap)13 List (java.util.List)13 HashSet (java.util.HashSet)12 Collectors (java.util.stream.Collectors)12 Nonnull (javax.annotation.Nonnull)12 StringUtils (org.apache.commons.lang3.StringUtils)12 Set (java.util.Set)11 ProcessorDTO (org.apache.nifi.web.api.dto.ProcessorDTO)11 NifiProcessGroup (com.thinkbiganalytics.nifi.rest.model.NifiProcessGroup)10 Optional (java.util.Optional)10 TemplateDTO (org.apache.nifi.web.api.dto.TemplateDTO)10 LegacyNifiRestClient (com.thinkbiganalytics.nifi.rest.client.LegacyNifiRestClient)8 NifiError (com.thinkbiganalytics.nifi.rest.model.NifiError)8 Collections (java.util.Collections)7 ControllerServiceDTO (org.apache.nifi.web.api.dto.ControllerServiceDTO)7