Search in sources :

Example 1 with Rule

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

the class RuleResource method searchRules.

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules/search-results")
public Response searchRules(@QueryParam("processorId") final String processorId, @QueryParam("q") final String term) {
    // get the web context
    final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
    // build the web context config
    final NiFiWebRequestContext requestContext = getRequestContext(processorId);
    // load the criteria
    final Criteria criteria = getCriteria(configurationContext, requestContext);
    final List<Rule> rules = criteria.getRules();
    // generate the rules
    List<RuleDTO> ruleDtos = null;
    if (rules != null) {
        ruleDtos = new ArrayList<>(rules.size());
        for (final Rule rule : rules) {
            if (StringUtils.containsIgnoreCase(rule.getName(), term)) {
                final RuleDTO ruleDto = DtoFactory.createRuleDTO(rule);
                ruleDtos.add(ruleDto);
            }
        }
    }
    // sort the rules
    Collections.sort(ruleDtos, new Comparator<RuleDTO>() {

        @Override
        public int compare(RuleDTO r1, RuleDTO r2) {
            final Collator collator = Collator.getInstance(Locale.US);
            return collator.compare(r1.getName(), r2.getName());
        }
    });
    // create the response entity
    final RulesEntity responseEntity = new RulesEntity();
    responseEntity.setProcessorId(processorId);
    responseEntity.setRules(ruleDtos);
    // generate the response
    final ResponseBuilder response = Response.ok(responseEntity);
    return noCache(response).build();
}
Also used : RuleDTO(org.apache.nifi.update.attributes.dto.RuleDTO) Criteria(org.apache.nifi.update.attributes.Criteria) RulesEntity(org.apache.nifi.update.attributes.entity.RulesEntity) Collator(java.text.Collator) Rule(org.apache.nifi.update.attributes.Rule) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) NiFiWebRequestContext(org.apache.nifi.web.NiFiWebRequestContext) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 2 with Rule

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

the class RuleResource method getRules.

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules")
public Response getRules(@QueryParam("processorId") final String processorId, @DefaultValue("false") @QueryParam("verbose") final Boolean verbose) {
    // get the web context
    final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
    // build the web context config
    final NiFiWebRequestContext requestContext = getRequestContext(processorId);
    // load the criteria
    final Criteria criteria = getCriteria(configurationContext, requestContext);
    final List<Rule> rules = criteria.getRules();
    // generate the rules
    List<RuleDTO> ruleDtos = null;
    if (rules != null) {
        ruleDtos = new ArrayList<>(rules.size());
        for (final Rule rule : rules) {
            final RuleDTO ruleDto = DtoFactory.createRuleDTO(rule);
            ruleDtos.add(ruleDto);
            // prune if appropriate
            if (!verbose) {
                ruleDto.setConditions(null);
                ruleDto.setActions(null);
            }
        }
    }
    // create the response entity
    final RulesEntity responseEntity = new RulesEntity();
    responseEntity.setProcessorId(processorId);
    responseEntity.setRules(ruleDtos);
    // generate the response
    final ResponseBuilder response = Response.ok(responseEntity);
    return noCache(response).build();
}
Also used : RuleDTO(org.apache.nifi.update.attributes.dto.RuleDTO) Criteria(org.apache.nifi.update.attributes.Criteria) Rule(org.apache.nifi.update.attributes.Rule) RulesEntity(org.apache.nifi.update.attributes.entity.RulesEntity) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) NiFiWebRequestContext(org.apache.nifi.web.NiFiWebRequestContext) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with Rule

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

the class RuleResource method getRule.

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules/{id}")
public Response getRule(@PathParam("id") final String ruleId, @QueryParam("processorId") final String processorId, @DefaultValue("false") @QueryParam("verbose") final Boolean verbose) {
    // get the web context
    final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
    // build the web context config
    final NiFiWebRequestContext requestContext = getRequestContext(processorId);
    // load the criteria and get the rule
    final Criteria criteria = getCriteria(configurationContext, requestContext);
    final Rule rule = criteria.getRule(ruleId);
    if (rule == null) {
        throw new NotFoundException();
    }
    // convert to a dto
    final RuleDTO ruleDto = DtoFactory.createRuleDTO(rule);
    // prune if appropriate
    if (!verbose) {
        ruleDto.setConditions(null);
        ruleDto.setActions(null);
    }
    // create the response entity
    final RuleEntity responseEntity = new RuleEntity();
    responseEntity.setProcessorId(processorId);
    responseEntity.setRule(ruleDto);
    // generate the response
    final ResponseBuilder response = Response.ok(responseEntity);
    return noCache(response).build();
}
Also used : RuleDTO(org.apache.nifi.update.attributes.dto.RuleDTO) NotFoundException(javax.ws.rs.NotFoundException) Criteria(org.apache.nifi.update.attributes.Criteria) Rule(org.apache.nifi.update.attributes.Rule) RuleEntity(org.apache.nifi.update.attributes.entity.RuleEntity) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) NiFiWebRequestContext(org.apache.nifi.web.NiFiWebRequestContext) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with Rule

use of org.apache.nifi.update.attributes.Rule 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 5 with Rule

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

the class UpdateAttribute method evaluateCriteria.

// Evaluates the specified Criteria on the specified flowfile. Clones the
// specified flow file for each rule that is applied.
private boolean evaluateCriteria(final ProcessSession session, final ProcessContext context, final Criteria criteria, final FlowFile flowfile, final Map<FlowFile, List<Rule>> matchedRules, final Map<String, String> statefulAttributes) {
    final ComponentLog logger = getLogger();
    final List<Rule> rules = criteria.getRules();
    // consider each rule and hold a copy of the flowfile for each matched rule
    for (final Rule rule : rules) {
        // evaluate the rule
        if (evaluateRule(context, rule, flowfile, statefulAttributes)) {
            final FlowFile flowfileToUse;
            // determine if we should use the original flow file or clone
            if (FlowFilePolicy.USE_ORIGINAL.equals(criteria.getFlowFilePolicy()) || matchedRules.isEmpty()) {
                flowfileToUse = flowfile;
            } else {
                // clone the original for this rule
                flowfileToUse = session.clone(flowfile);
            }
            // store the flow file to use when executing this rule
            List<Rule> rulesForFlowFile = matchedRules.get(flowfileToUse);
            if (rulesForFlowFile == null) {
                rulesForFlowFile = new ArrayList<>();
                matchedRules.put(flowfileToUse, rulesForFlowFile);
            }
            rulesForFlowFile.add(rule);
            // log if appropriate
            if (debugEnabled) {
                logger.debug(this + " all conditions met for rule '" + rule.getName() + "'. Using flow file - " + flowfileToUse);
            }
        }
    }
    return !matchedRules.isEmpty();
}
Also used : FlowFile(org.apache.nifi.flowfile.FlowFile) Rule(org.apache.nifi.update.attributes.Rule) ComponentLog(org.apache.nifi.logging.ComponentLog)

Aggregations

Rule (org.apache.nifi.update.attributes.Rule)12 Criteria (org.apache.nifi.update.attributes.Criteria)10 Path (javax.ws.rs.Path)6 Produces (javax.ws.rs.Produces)6 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)6 Action (org.apache.nifi.update.attributes.Action)6 NiFiWebConfigurationContext (org.apache.nifi.web.NiFiWebConfigurationContext)6 RuleDTO (org.apache.nifi.update.attributes.dto.RuleDTO)5 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 GET (javax.ws.rs.GET)3 FlowFile (org.apache.nifi.flowfile.FlowFile)3 ComponentLog (org.apache.nifi.logging.ComponentLog)3 Condition (org.apache.nifi.update.attributes.Condition)3 RuleEntity (org.apache.nifi.update.attributes.entity.RuleEntity)3 RulesEntity (org.apache.nifi.update.attributes.entity.RulesEntity)3 NiFiWebConfigurationRequestContext (org.apache.nifi.web.NiFiWebConfigurationRequestContext)3 NiFiWebRequestContext (org.apache.nifi.web.NiFiWebRequestContext)3