public void init(AbstractPolicyDefinition policyDef) { siddhiRuntime = createSiddhiRuntime((SiddhiPolicyDefinition) policyDef); }
public Map<String, String> getAdditionalContext() { Map<String, String> context = new HashMap<String, String>(); StringBuilder sourceStreams = new StringBuilder(); for (String streamName : getStreamNames()) { sourceStreams.append(streamName + ","); } if (sourceStreams.length() > 0) { sourceStreams.deleteCharAt(sourceStreams.length() - 1); } context.put(Constants.SOURCE_STREAMS, sourceStreams.toString()); context.put(Constants.POLICY_ID, this.context.policyId); return context; }
public SiddhiPolicyEvaluator(Config config, PolicyEvaluationContext<T, K> context, AbstractPolicyDefinition policyDef, String[] sourceStreams, boolean needValidation) { this.config = config; this.context = context; this.context.evaluator = this; this.needValidation = needValidation; this.sourceStreams = sourceStreams; init(policyDef); }
/** * 1. input has 3 fields, first is siddhi context, second is streamName, the last one is map of attribute name/value * 2. runtime check for input data (This is very expensive, so we ignore for now) * the size of input map should be equal to size of attributes which stream metadata defines * the attribute names should be equal to attribute names which stream metadata defines * the input field cannot be null */ @SuppressWarnings({"rawtypes"}) @Override public void evaluate(ValuesArray data) throws Exception { if (!siddhiRuntime.markdownEnabled) { if (LOG.isDebugEnabled()) { LOG.debug("Siddhi policy evaluator consumers data :" + data); } Collector outputCollector = (Collector) data.get(0); String streamName = (String) data.get(1); SortedMap dataMap = (SortedMap) data.get(2); // Get metadata keyset for the stream. Set<String> metadataKeys = StreamMetadataManager.getInstance() .getMetadataEntityMapForStream(streamName).keySet(); validateEventInRuntime(streamName, dataMap, metadataKeys); synchronized (siddhiRuntime) { // retain the collector in the context. This assignment is idempotent context.outputCollector = outputCollector; List<Object> input = new ArrayList<Object>(); putAttrsIntoInputStream(input, streamName, metadataKeys, dataMap); siddhiRuntime.siddhiInputHandlers.get(streamName).send(input.toArray(new Object[0])); } } }
AlertAPIEntity entity = new AlertAPIEntity(); AlertContext context = new AlertContext(); String sourceStreams = evaluator.getAdditionalContext().get(Constants.SOURCE_STREAMS); String[] sourceStreamsArr = sourceStreams.split(","); List<String> attrRenameList = evaluator.getOutputStreamAttrNameList(); Map<String, String> tags = new HashMap<String, String>(); for (String sourceStream : sourceStreamsArr) { String alertEvent = sb.toString(); context.addAll(evaluator.getAdditionalContext()); String policyId = context.getProperty(Constants.POLICY_ID); String alertMessage = "The Policy \"" + policyId + "\" has been detected with the below information: " + alertEvent;
attachCallback(runtime, executionPlanRuntime, context);
@Override public void onPolicyUpdate(T newAlertDef) { AbstractPolicyDefinition policyDef = null; try { policyDef = JsonSerDeserUtils.deserialize(newAlertDef.getPolicyDef(), AbstractPolicyDefinition.class, PolicyManager.getInstance().getPolicyModules(newAlertDef.getTags().get(Constants.POLICY_TYPE))); } catch (Exception ex) { LOG.error("Initial policy def error, ", ex); } SiddhiRuntime previous = siddhiRuntime; siddhiRuntime = createSiddhiRuntime((SiddhiPolicyDefinition) policyDef); synchronized (previous) { if (!previous.markdownEnabled) // condition to check if previous SiddhiRuntime was started after policy validation previous.siddhiManager.getExecutionPlanRuntime(previous.executionPlanName).shutdown(); } }