Search in sources :

Example 11 with Criteria

use of org.apache.nifi.update.attributes.Criteria 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)

Example 12 with Criteria

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

the class UpdateAttribute method onScheduled.

@OnScheduled
public void onScheduled(final ProcessContext context) throws IOException {
    criteriaCache.set(CriteriaSerDe.deserialize(context.getAnnotationData()));
    propertyValues.clear();
    if (stateful) {
        StateManager stateManager = context.getStateManager();
        StateMap state = stateManager.getState(Scope.LOCAL);
        HashMap<String, String> tempMap = new HashMap<>();
        tempMap.putAll(state.toMap());
        String initValue = context.getProperty(STATEFUL_VARIABLES_INIT_VALUE).getValue();
        // Initialize the stateful default actions
        for (PropertyDescriptor entry : context.getProperties().keySet()) {
            if (entry.isDynamic()) {
                if (!tempMap.containsKey(entry.getName())) {
                    tempMap.put(entry.getName(), initValue);
                }
            }
        }
        // Initialize the stateful actions if the criteria exists
        final Criteria criteria = criteriaCache.get();
        if (criteria != null) {
            for (Rule rule : criteria.getRules()) {
                for (Action action : rule.getActions()) {
                    if (!tempMap.containsKey(action.getAttribute())) {
                        tempMap.put(action.getAttribute(), initValue);
                    }
                }
            }
        }
        context.getStateManager().setState(tempMap, Scope.LOCAL);
    }
    defaultActions = getDefaultActions(context.getProperties());
    debugEnabled = getLogger().isDebugEnabled();
}
Also used : Action(org.apache.nifi.update.attributes.Action) StateManager(org.apache.nifi.components.state.StateManager) PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) StateMap(org.apache.nifi.components.state.StateMap) Criteria(org.apache.nifi.update.attributes.Criteria) Rule(org.apache.nifi.update.attributes.Rule) OnScheduled(org.apache.nifi.annotation.lifecycle.OnScheduled)

Example 13 with Criteria

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

the class RuleResource method createRule.

@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules")
public Response createRule(@Context final UriInfo uriInfo, final RuleEntity requestEntity) {
    // get the web context
    final NiFiWebConfigurationContext configurationContext = (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 hasn't been specified
    if (ruleDto.getId() != null) {
        throw new WebApplicationException(badRequest("The rule id cannot be specified."));
    }
    // 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."));
    }
    // generate a new id
    final String uuid = UUID.randomUUID().toString();
    // build the request context
    final NiFiWebConfigurationRequestContext requestContext = getConfigurationRequestContext(requestEntity.getProcessorId(), requestEntity.getRevision(), requestEntity.getClientId());
    // load the criteria
    final Criteria criteria = getCriteria(configurationContext, requestContext);
    final UpdateAttributeModelFactory factory = new UpdateAttributeModelFactory();
    // create the new rule
    final Rule rule;
    try {
        rule = factory.createRule(ruleDto);
        rule.setId(uuid);
    } catch (final IllegalArgumentException iae) {
        throw new WebApplicationException(iae, badRequest(iae.getMessage()));
    }
    // add the rule
    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 UriBuilder uriBuilder = uriInfo.getAbsolutePathBuilder();
    final ResponseBuilder response = Response.created(uriBuilder.path(uuid).build()).entity(responseEntity);
    return noCache(response).build();
}
Also used : WebApplicationException(javax.ws.rs.WebApplicationException) RuleDTO(org.apache.nifi.update.attributes.dto.RuleDTO) NiFiWebConfigurationRequestContext(org.apache.nifi.web.NiFiWebConfigurationRequestContext) UpdateAttributeModelFactory(org.apache.nifi.update.attributes.UpdateAttributeModelFactory) Criteria(org.apache.nifi.update.attributes.Criteria) Rule(org.apache.nifi.update.attributes.Rule) RuleEntity(org.apache.nifi.update.attributes.entity.RuleEntity) UriBuilder(javax.ws.rs.core.UriBuilder) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) NiFiWebConfigurationContext(org.apache.nifi.web.NiFiWebConfigurationContext) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 14 with Criteria

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

the class RuleResource method deleteRule.

@DELETE
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Path("/rules/{id}")
public Response deleteRule(@PathParam("id") final String ruleId, @QueryParam("processorId") final String processorId, @QueryParam("clientId") final String clientId, @QueryParam("revision") final Long revision) {
    // get the web context
    final NiFiWebConfigurationContext configurationContext = (NiFiWebConfigurationContext) servletContext.getAttribute("nifi-web-configuration-context");
    // build the web context config
    final NiFiWebConfigurationRequestContext requestContext = getConfigurationRequestContext(processorId, revision, clientId);
    // 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();
    }
    // delete the rule
    criteria.deleteRule(rule);
    // save the criteria
    saveCriteria(requestContext, criteria);
    // create the response entity
    final RulesEntity responseEntity = new RulesEntity();
    responseEntity.setClientId(clientId);
    responseEntity.setRevision(revision);
    responseEntity.setProcessorId(processorId);
    // generate the response
    final ResponseBuilder response = Response.ok(responseEntity);
    return noCache(response).build();
}
Also used : NiFiWebConfigurationRequestContext(org.apache.nifi.web.NiFiWebConfigurationRequestContext) NotFoundException(javax.ws.rs.NotFoundException) 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) Path(javax.ws.rs.Path) DELETE(javax.ws.rs.DELETE) Produces(javax.ws.rs.Produces)

Aggregations

Criteria (org.apache.nifi.update.attributes.Criteria)14 Rule (org.apache.nifi.update.attributes.Rule)10 Path (javax.ws.rs.Path)8 Produces (javax.ws.rs.Produces)8 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)8 NiFiWebConfigurationContext (org.apache.nifi.web.NiFiWebConfigurationContext)8 Action (org.apache.nifi.update.attributes.Action)5 RuleDTO (org.apache.nifi.update.attributes.dto.RuleDTO)5 GET (javax.ws.rs.GET)4 WebApplicationException (javax.ws.rs.WebApplicationException)4 NiFiWebConfigurationRequestContext (org.apache.nifi.web.NiFiWebConfigurationRequestContext)4 NiFiWebRequestContext (org.apache.nifi.web.NiFiWebRequestContext)4 ArrayList (java.util.ArrayList)3 Consumes (javax.ws.rs.Consumes)3 NotFoundException (javax.ws.rs.NotFoundException)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 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2