Search in sources :

Example 1 with Condition

use of org.apache.nifi.update.attributes.Condition in project nifi by apache.

the class RuleResource method createCondition.

@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules/conditions")
public Response createCondition(@Context final UriInfo uriInfo, final ConditionEntity requestEntity) {
    // generate a new id
    final String uuid = UUID.randomUUID().toString();
    final Condition condition;
    try {
        // create the condition object
        final UpdateAttributeModelFactory factory = new UpdateAttributeModelFactory();
        condition = factory.createCondition(requestEntity.getCondition());
        condition.setId(uuid);
    } catch (final IllegalArgumentException iae) {
        throw new WebApplicationException(iae, badRequest(iae.getMessage()));
    }
    // build the response
    final ConditionEntity responseEntity = new ConditionEntity();
    responseEntity.setClientId(requestEntity.getClientId());
    responseEntity.setProcessorId(requestEntity.getProcessorId());
    responseEntity.setRevision(requestEntity.getRevision());
    responseEntity.setCondition(DtoFactory.createConditionDTO(condition));
    // generate the response
    final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
    final ResponseBuilder response = Response.created(uriBuilder.path(uuid).build()).entity(responseEntity);
    return noCache(response).build();
}
Also used : Condition(org.apache.nifi.update.attributes.Condition) WebApplicationException(javax.ws.rs.WebApplicationException) UpdateAttributeModelFactory(org.apache.nifi.update.attributes.UpdateAttributeModelFactory) UriBuilder(javax.ws.rs.core.UriBuilder) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) ConditionEntity(org.apache.nifi.update.attributes.entity.ConditionEntity) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 2 with Condition

use of org.apache.nifi.update.attributes.Condition in project nifi by apache.

the class RuleResource method updateRule.

@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules/{id}")
public Response updateRule(@Context final UriInfo uriInfo, @PathParam("id") final String ruleId, final RuleEntity requestEntity) {
    // get the web context
    final NiFiWebConfigurationContext nifiWebContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
    // ensure the rule has been specified
    if (requestEntity == null || requestEntity.getRule() == null) {
        throw new WebApplicationException(badRequest("The rule must be specified."));
    }
    final RuleDTO ruleDto = requestEntity.getRule();
    // ensure the id has been specified
    if (ruleDto.getId() == null) {
        throw new WebApplicationException(badRequest("The rule id must be specified."));
    }
    if (!ruleDto.getId().equals(ruleId)) {
        throw new WebApplicationException(badRequest("The rule id in the path does not equal the rule id in the request body."));
    }
    // ensure the rule name was specified
    if (ruleDto.getName() == null || ruleDto.getName().isEmpty()) {
        throw new WebApplicationException(badRequest("The rule name must be specified and cannot be blank."));
    }
    // ensure there are some conditions
    if (ruleDto.getConditions() == null || ruleDto.getConditions().isEmpty()) {
        throw new WebApplicationException(badRequest("The rule conditions must be set."));
    }
    // ensure there are some actions
    if (ruleDto.getActions() == null || ruleDto.getActions().isEmpty()) {
        throw new WebApplicationException(badRequest("The rule actions must be set."));
    }
    // build the web context config
    final NiFiWebConfigurationRequestContext requestContext = getConfigurationRequestContext(requestEntity.getProcessorId(), requestEntity.getRevision(), requestEntity.getClientId());
    // load the criteria
    final UpdateAttributeModelFactory factory = new UpdateAttributeModelFactory();
    final Criteria criteria = getCriteria(nifiWebContext, requestContext);
    // attempt to locate the rule
    Rule rule = criteria.getRule(ruleId);
    // if the rule isn't found add it
    boolean newRule = false;
    if (rule == null) {
        newRule = true;
        rule = new Rule();
        rule.setId(ruleId);
    }
    try {
        // evaluate the conditions and actions before modifying the rule
        final Set<Condition> conditions = factory.createConditions(ruleDto.getConditions());
        final Set<Action> actions = factory.createActions(ruleDto.getActions());
        // update the rule
        rule.setName(ruleDto.getName());
        rule.setConditions(conditions);
        rule.setActions(actions);
    } catch (final IllegalArgumentException iae) {
        throw new WebApplicationException(iae, badRequest(iae.getMessage()));
    }
    // add the new rule if application
    if (newRule) {
        criteria.addRule(rule);
    }
    // save the criteria
    saveCriteria(requestContext, criteria);
    // create the response entity
    final RuleEntity responseEntity = new RuleEntity();
    responseEntity.setClientId(requestEntity.getClientId());
    responseEntity.setRevision(requestEntity.getRevision());
    responseEntity.setProcessorId(requestEntity.getProcessorId());
    responseEntity.setRule(DtoFactory.createRuleDTO(rule));
    // generate the response
    final ResponseBuilder response;
    if (newRule) {
        final UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
        response = Response.created(uriBuilder.path(ruleId).build()).entity(responseEntity);
    } else {
        response = Response.ok(responseEntity);
    }
    return noCache(response).build();
}
Also used : Condition(org.apache.nifi.update.attributes.Condition) Action(org.apache.nifi.update.attributes.Action) WebApplicationException(javax.ws.rs.WebApplicationException) RuleDTO(org.apache.nifi.update.attributes.dto.RuleDTO) Criteria(org.apache.nifi.update.attributes.Criteria) NiFiWebConfigurationRequestContext(org.apache.nifi.web.NiFiWebConfigurationRequestContext) UpdateAttributeModelFactory(org.apache.nifi.update.attributes.UpdateAttributeModelFactory) Rule(org.apache.nifi.update.attributes.Rule) RuleEntity(org.apache.nifi.update.attributes.entity.RuleEntity) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) UriBuilder(javax.ws.rs.core.UriBuilder) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 3 with Condition

use of org.apache.nifi.update.attributes.Condition in project nifi by apache.

the class DtoFactory method createRuleDTO.

public static RuleDTO createRuleDTO(final Rule rule) {
    final RuleDTO dto = new RuleDTO();
    dto.setId(rule.getId());
    dto.setName(rule.getName());
    if (rule.getConditions() != null) {
        final Set<ConditionDTO> conditions = new TreeSet<>();
        for (final Condition condition : rule.getConditions()) {
            conditions.add(createConditionDTO(condition));
        }
        dto.setConditions(conditions);
    }
    if (rule.getActions() != null) {
        final Set<ActionDTO> actions = new TreeSet<>();
        for (final Action action : rule.getActions()) {
            actions.add(createActionDTO(action));
        }
        dto.setActions(actions);
    }
    return dto;
}
Also used : Condition(org.apache.nifi.update.attributes.Condition) Action(org.apache.nifi.update.attributes.Action) TreeSet(java.util.TreeSet)

Example 4 with Condition

use of org.apache.nifi.update.attributes.Condition in project nifi by apache.

the class UpdateAttribute method customValidate.

@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    final List<ValidationResult> reasons = new ArrayList<>(super.customValidate(context));
    if (!context.getProperty(STORE_STATE).getValue().equals(DO_NOT_STORE_STATE)) {
        String initValue = context.getProperty(STATEFUL_VARIABLES_INIT_VALUE).getValue();
        if (initValue == null) {
            reasons.add(new ValidationResult.Builder().subject(STATEFUL_VARIABLES_INIT_VALUE.getDisplayName()).valid(false).explanation("initial state value must be set if the processor is configured to store state.").build());
        }
    }
    Criteria criteria = null;
    try {
        criteria = CriteriaSerDe.deserialize(context.getAnnotationData());
    } catch (IllegalArgumentException iae) {
        reasons.add(new ValidationResult.Builder().valid(false).explanation("Unable to deserialize the update criteria." + iae.getMessage()).build());
    }
    // if there is criteria, validate it
    if (criteria != null) {
        final List<Rule> rules = criteria.getRules();
        if (rules == null) {
            reasons.add(new ValidationResult.Builder().valid(false).explanation("Update criteria has been specified by no rules were found.").build());
        } else {
            // validate the each rule
            for (final Rule rule : rules) {
                if (rule.getName() == null || rule.getName().trim().isEmpty()) {
                    reasons.add(new ValidationResult.Builder().valid(false).explanation("A rule name was not specified.").build());
                }
                // validate each condition
                final Set<Condition> conditions = rule.getConditions();
                if (conditions == null) {
                    reasons.add(new ValidationResult.Builder().valid(false).explanation(String.format("No conditions for rule '%s' found.", rule.getName())).build());
                } else {
                    for (final Condition condition : conditions) {
                        if (condition.getExpression() == null) {
                            reasons.add(new ValidationResult.Builder().valid(false).explanation(String.format("No expression for a condition in rule '%s' was found.", rule.getName())).build());
                        } else {
                            final String expression = condition.getExpression().trim();
                            reasons.add(StandardValidators.createAttributeExpressionLanguageValidator(AttributeExpression.ResultType.BOOLEAN, false).validate(String.format("Condition for rule '%s'.", rule.getName()), expression, context));
                        }
                    }
                }
                // validate each action
                final Set<Action> actions = rule.getActions();
                if (actions == null) {
                    reasons.add(new ValidationResult.Builder().valid(false).explanation(String.format("No actions for rule '%s' found.", rule.getName())).build());
                } else {
                    for (final Action action : actions) {
                        if (action.getAttribute() == null) {
                            reasons.add(new ValidationResult.Builder().valid(false).explanation(String.format("An action in rule '%s' is missing the attribute name.", rule.getName())).build());
                        } else if (action.getValue() == null) {
                            reasons.add(new ValidationResult.Builder().valid(false).explanation(String.format("No value for attribute '%s' in rule '%s' was found.", action.getAttribute(), rule.getName())).build());
                        } else {
                            reasons.add(StandardValidators.createAttributeExpressionLanguageValidator(AttributeExpression.ResultType.STRING, true).validate(String.format("Action for rule '%s'.", rule.getName()), action.getValue(), context));
                        }
                    }
                }
            }
        }
    }
    return reasons;
}
Also used : Condition(org.apache.nifi.update.attributes.Condition) Action(org.apache.nifi.update.attributes.Action) ArrayList(java.util.ArrayList) Criteria(org.apache.nifi.update.attributes.Criteria) ValidationResult(org.apache.nifi.components.ValidationResult) Rule(org.apache.nifi.update.attributes.Rule)

Example 5 with Condition

use of org.apache.nifi.update.attributes.Condition in project nifi by apache.

the class UpdateAttribute method search.

@Override
public Collection<SearchResult> search(final SearchContext context) {
    final String term = context.getSearchTerm();
    final Collection<SearchResult> results = new ArrayList<>();
    if (StringUtils.isBlank(context.getAnnotationData())) {
        return results;
    }
    try {
        // parse the annotation data
        final Criteria criteria = CriteriaSerDe.deserialize(context.getAnnotationData());
        // ensure there are some rules
        if (criteria.getRules() != null) {
            final FlowFilePolicy flowFilePolicy = criteria.getFlowFilePolicy();
            if (flowFilePolicy != null && StringUtils.containsIgnoreCase(flowFilePolicy.name(), term)) {
                results.add(new SearchResult.Builder().label("FlowFile policy").match(flowFilePolicy.name()).build());
            }
            for (final Rule rule : criteria.getRules()) {
                if (StringUtils.containsIgnoreCase(rule.getName(), term)) {
                    results.add(new SearchResult.Builder().label("Rule name").match(rule.getName()).build());
                }
                // ensure there are some conditions
                if (rule.getConditions() != null) {
                    for (final Condition condition : rule.getConditions()) {
                        if (StringUtils.containsIgnoreCase(condition.getExpression(), term)) {
                            results.add(new SearchResult.Builder().label(String.format("Condition in rule '%s'", rule.getName())).match(condition.getExpression()).build());
                        }
                    }
                }
                // ensure there are some actions
                if (rule.getActions() != null) {
                    for (final Action action : rule.getActions()) {
                        if (StringUtils.containsIgnoreCase(action.getAttribute(), term)) {
                            results.add(new SearchResult.Builder().label(String.format("Action in rule '%s'", rule.getName())).match(action.getAttribute()).build());
                        }
                        if (StringUtils.containsIgnoreCase(action.getValue(), term)) {
                            results.add(new SearchResult.Builder().label(String.format("Action in rule '%s'", rule.getName())).match(action.getValue()).build());
                        }
                    }
                }
            }
        }
        return results;
    } catch (Exception e) {
        return results;
    }
}
Also used : Condition(org.apache.nifi.update.attributes.Condition) Action(org.apache.nifi.update.attributes.Action) FlowFilePolicy(org.apache.nifi.update.attributes.FlowFilePolicy) ArrayList(java.util.ArrayList) SearchResult(org.apache.nifi.search.SearchResult) Criteria(org.apache.nifi.update.attributes.Criteria) Rule(org.apache.nifi.update.attributes.Rule) URISyntaxException(java.net.URISyntaxException) ProcessException(org.apache.nifi.processor.exception.ProcessException) IOException(java.io.IOException)

Aggregations

Condition (org.apache.nifi.update.attributes.Condition)5 Action (org.apache.nifi.update.attributes.Action)4 Criteria (org.apache.nifi.update.attributes.Criteria)3 Rule (org.apache.nifi.update.attributes.Rule)3 ArrayList (java.util.ArrayList)2 Consumes (javax.ws.rs.Consumes)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 WebApplicationException (javax.ws.rs.WebApplicationException)2 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 UpdateAttributeModelFactory (org.apache.nifi.update.attributes.UpdateAttributeModelFactory)2 IOException (java.io.IOException)1 URISyntaxException (java.net.URISyntaxException)1 TreeSet (java.util.TreeSet)1 POST (javax.ws.rs.POST)1 PUT (javax.ws.rs.PUT)1 ValidationResult (org.apache.nifi.components.ValidationResult)1 ProcessException (org.apache.nifi.processor.exception.ProcessException)1 SearchResult (org.apache.nifi.search.SearchResult)1