Search in sources :

Example 26 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class AvroSchemaRegistry method customValidate.

@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    Set<ValidationResult> results = new HashSet<>();
    boolean strict = validationContext.getProperty(VALIDATE_FIELD_NAMES).asBoolean();
    // Iterate over dynamic properties, validating the schemas, and adding results
    validationContext.getProperties().entrySet().stream().filter(entry -> entry.getKey().isDynamic()).forEach(entry -> {
        String subject = entry.getKey().getDisplayName();
        String input = entry.getValue();
        try {
            final Schema avroSchema = new Schema.Parser().setValidate(strict).parse(input);
            AvroTypeUtil.createSchema(avroSchema, input, SchemaIdentifier.EMPTY);
        } catch (final Exception e) {
            results.add(new ValidationResult.Builder().input(input).subject(subject).valid(false).explanation("Not a valid Avro Schema: " + e.getMessage()).build());
        }
    });
    return results;
}
Also used : StandardValidators(org.apache.nifi.processor.util.StandardValidators) CapabilityDescription(org.apache.nifi.annotation.documentation.CapabilityDescription) ValidationContext(org.apache.nifi.components.ValidationContext) InitializationException(org.apache.nifi.reporting.InitializationException) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) SchemaIdentifier(org.apache.nifi.serialization.record.SchemaIdentifier) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) ControllerServiceInitializationContext(org.apache.nifi.controller.ControllerServiceInitializationContext) AbstractControllerService(org.apache.nifi.controller.AbstractControllerService) ValidationResult(org.apache.nifi.components.ValidationResult) EnumSet(java.util.EnumSet) Schema(org.apache.avro.Schema) AvroTypeUtil(org.apache.nifi.avro.AvroTypeUtil) SchemaField(org.apache.nifi.schema.access.SchemaField) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) IOException(java.io.IOException) List(java.util.List) Optional(java.util.Optional) Tags(org.apache.nifi.annotation.documentation.Tags) Collections(java.util.Collections) RecordSchema(org.apache.nifi.serialization.record.RecordSchema) Schema(org.apache.avro.Schema) ValidationResult(org.apache.nifi.components.ValidationResult) InitializationException(org.apache.nifi.reporting.InitializationException) SchemaNotFoundException(org.apache.nifi.schema.access.SchemaNotFoundException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 27 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class NotificationServiceManager method loadNotificationServices.

/**
 * Loads the Notification Services from the given XML configuration file.
 *
 * File is expected to have the following format:
 *
 * <pre>
 * &lt;services&gt;
 *   &lt;service&gt;
 *     &lt;id&gt;service-identifier&lt;/id&gt;
 *     &lt;class&gt;org.apache.nifi.MyNotificationService&lt;/class&gt;
 *     &lt;property name="My First Property"&gt;Property Value&lt;/property&gt;
 *   &lt;/service&gt;
 *   &lt;service&gt;
 *     &lt;id&gt;other-service&lt;/id&gt;
 *     &lt;class&gt;org.apache.nifi.MyOtherNotificationService&lt;/class&gt;
 *     &lt;property name="Another Property"&gt;Property Value 2&lt;/property&gt;
 *   &lt;/service&gt;
 *   ...
 *   &lt;service&gt;
 *     &lt;id&gt;service-identifier-2&lt;/id&gt;
 *     &lt;class&gt;org.apache.nifi.FinalNotificationService&lt;/class&gt;
 *     &lt;property name="Yet Another Property"&gt;3rd Prop Value&lt;/property&gt;
 *   &lt;/service&gt;
 * &lt;/services&gt;
 * </pre>
 *
 * Note that as long as the file can be interpreted properly, a misconfigured service will result in a warning
 * or error being logged and the service will be unavailable but will not prevent the rest of the services from loading.
 *
 * @param servicesFile the XML file to load services from.
 * @throws IOException if unable to read from the given file
 * @throws ParserConfigurationException if unable to parse the given file as XML properly
 * @throws SAXException if unable to parse the given file properly
 */
public void loadNotificationServices(final File servicesFile) throws IOException, ParserConfigurationException, SAXException {
    final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
    docBuilderFactory.setNamespaceAware(false);
    final DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
    final Map<String, ConfiguredNotificationService> serviceMap = new HashMap<>();
    try (final InputStream fis = new FileInputStream(servicesFile);
        final InputStream in = new BufferedInputStream(fis)) {
        final Document doc = docBuilder.parse(new InputSource(in));
        final List<Element> serviceElements = getChildElementsByTagName(doc.getDocumentElement(), "service");
        logger.debug("Found {} service elements", serviceElements.size());
        for (final Element serviceElement : serviceElements) {
            final ConfiguredNotificationService config = createService(serviceElement);
            final NotificationService service = config.getService();
            if (service == null) {
                // reason will have already been logged, so just move on.
                continue;
            }
            final String id = service.getIdentifier();
            if (serviceMap.containsKey(id)) {
                logger.error("Found two different Notification Services configured with the same ID: '{}'. Loaded the first service.", id);
                continue;
            }
            // Check if the service is valid; if not, warn now so that users know this before they fail to receive notifications
            final ValidationContext validationContext = new NotificationValidationContext(buildNotificationContext(config), variableRegistry);
            final Collection<ValidationResult> validationResults = service.validate(validationContext);
            final List<String> invalidReasons = new ArrayList<>();
            for (final ValidationResult result : validationResults) {
                if (!result.isValid()) {
                    invalidReasons.add(result.toString());
                }
            }
            if (!invalidReasons.isEmpty()) {
                logger.warn("Configured Notification Service {} is not valid for the following reasons: {}", service, invalidReasons);
            }
            serviceMap.put(id, config);
        }
    }
    logger.info("Successfully loaded the following {} services: {}", serviceMap.size(), serviceMap.keySet());
    servicesById.clear();
    servicesById.putAll(serviceMap);
}
Also used : InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) HashMap(java.util.HashMap) NotificationValidationContext(org.apache.nifi.bootstrap.notification.NotificationValidationContext) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) NotificationService(org.apache.nifi.bootstrap.notification.NotificationService) Document(org.w3c.dom.Document) ValidationResult(org.apache.nifi.components.ValidationResult) FileInputStream(java.io.FileInputStream) NotificationValidationContext(org.apache.nifi.bootstrap.notification.NotificationValidationContext) ValidationContext(org.apache.nifi.components.ValidationContext) DocumentBuilder(javax.xml.parsers.DocumentBuilder) BufferedInputStream(java.io.BufferedInputStream)

Example 28 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class StandardProcessorTestRunner method assertValid.

@Override
public void assertValid(final ControllerService service) {
    final StateManager serviceStateManager = controllerServiceStateManagers.get(service.getIdentifier());
    if (serviceStateManager == null) {
        throw new IllegalStateException("Controller Service has not been added to this TestRunner via the #addControllerService method");
    }
    final ValidationContext validationContext = new MockValidationContext(context, serviceStateManager, variableRegistry).getControllerServiceValidationContext(service);
    final Collection<ValidationResult> results = context.getControllerService(service.getIdentifier()).validate(validationContext);
    for (final ValidationResult result : results) {
        if (!result.isValid()) {
            Assert.fail("Expected Controller Service to be valid but it is invalid due to: " + result.toString());
        }
    }
}
Also used : MockStateManager(org.apache.nifi.state.MockStateManager) StateManager(org.apache.nifi.components.state.StateManager) ValidationResult(org.apache.nifi.components.ValidationResult) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 29 with ValidationContext

use of org.apache.nifi.components.ValidationContext 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)

Example 30 with ValidationContext

use of org.apache.nifi.components.ValidationContext in project nifi by apache.

the class StandardValidators method createDataSizeBoundsValidator.

public static Validator createDataSizeBoundsValidator(final long minBytesInclusive, final long maxBytesInclusive) {
    return new Validator() {

        @Override
        public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
            if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
                return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
            }
            final ValidationResult vr = DATA_SIZE_VALIDATOR.validate(subject, input, context);
            if (!vr.isValid()) {
                return vr;
            }
            final long dataSizeBytes = DataUnit.parseDataSize(input, DataUnit.B).longValue();
            if (dataSizeBytes < minBytesInclusive) {
                return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Cannot be smaller than " + minBytesInclusive + " bytes").build();
            }
            if (dataSizeBytes > maxBytesInclusive) {
                return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Cannot be larger than " + maxBytesInclusive + " bytes").build();
            }
            return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
        }
    };
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

Aggregations

ValidationContext (org.apache.nifi.components.ValidationContext)40 ValidationResult (org.apache.nifi.components.ValidationResult)37 Validator (org.apache.nifi.components.Validator)18 Test (org.junit.Test)18 ArrayList (java.util.ArrayList)9 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)7 Collection (java.util.Collection)6 File (java.io.File)5 HashMap (java.util.HashMap)5 List (java.util.List)5 Map (java.util.Map)5 Collections (java.util.Collections)4 Set (java.util.Set)4 PropertyValue (org.apache.nifi.components.PropertyValue)4 MockStateManager (org.apache.nifi.state.MockStateManager)4 IOException (java.io.IOException)3 MalformedURLException (java.net.MalformedURLException)3 CapabilityDescription (org.apache.nifi.annotation.documentation.CapabilityDescription)3 Relationship (org.apache.nifi.processor.Relationship)3 FileInputStream (java.io.FileInputStream)2