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); }
@Override public String toString() { return siddhiRuntime.policyDef.toString(); }
public void init(AbstractPolicyDefinition policyDef) { siddhiRuntime = createSiddhiRuntime((SiddhiPolicyDefinition) policyDef); }
private SiddhiRuntime createSiddhiRuntime(SiddhiPolicyDefinition policyDef) { SiddhiManager siddhiManager = new SiddhiManager(); Map<String, InputHandler> siddhiInputHandlers = new HashMap<String, InputHandler>(); SiddhiRuntime runtime = new SiddhiRuntime(); String executionPlan = policyDef.getExpression(); if (!policyDef.isContainsDefinition()) { StringBuilder sb = new StringBuilder(); for (String sourceStream : sourceStreams) { String streamDef = SiddhiStreamMetadataUtils.convertToStreamDef(sourceStream); LOG.info("Siddhi stream definition : " + streamDef); sb.append(streamDef); String expression = policyDef.getExpression(); executionPlan = sb.toString() + " @info(name = '" + EXECUTION_PLAN_NAME + "') " + expression; executionPlanRuntime.handleExceptionWith(new SiddhiPolicyExceptionHandler()); attachCallback(runtime, executionPlanRuntime, context);
/** * 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])); } } }
public static SortedMap<String, AlertStreamSchemaEntity> getAttrMap(String streamName) { SortedMap<String, AlertStreamSchemaEntity> map = StreamMetadataManager.getInstance().getMetadataEntityMapForStream(streamName); if(map == null || map.size() == 0){ throw new IllegalStateException("Alert stream schema ["+streamName+"] should never be empty"); } return map; }
public List<AlertStreamSchemaEntity> getMetadataEntitiesForStream(String streamName){ ensureInitialized(); return getMetadataEntitiesForAllStreams().get(streamName); }
public SortedMap<String, AlertStreamSchemaEntity> getMetadataEntityMapForStream(String streamName){ ensureInitialized(); return getMetadataEntityMapForAllStreams().get(streamName); }
@SuppressWarnings("unchecked") @Override public void receive(long timeStamp, Event[] inEvents, Event[] removeEvents) { List<Object> rets = getOutputObject(inEvents[0].getData()); K alert = siddhiEvaluateContext.resultRender.render(config, rets, siddhiEvaluateContext, timeStamp); SiddhiEvaluationHandler<T, K> handler = siddhiEvaluateContext.alertExecutor; handler.onEvalEvents(siddhiEvaluateContext, Arrays.asList(alert)); } }
public static Object getAttrDefaultValue(String streamName, String attrName){ SortedMap<String, AlertStreamSchemaEntity> map = getAttrMap(streamName); AlertStreamSchemaEntity entity = map.get(attrName); if (entity.getDefaultValue() != null) { return entity.getDefaultValue(); } else { String attrType = entity.getAttrType(); if (attrType.equalsIgnoreCase(AttributeType.STRING.name())) { return "NA"; } else if (attrType.equalsIgnoreCase(AttributeType.INTEGER.name()) || attrType.equalsIgnoreCase(AttributeType.LONG.name())) { return -1; } else if (attrType.equalsIgnoreCase(AttributeType.BOOL.name())) { return true; } else { LOG.warn("AttrType is not recognized: " + attrType + ", treat it as string"); return "N/A"; } } } }
/** * @see org.wso2.siddhi.query.api.definition.Attribute.Type * make sure StreamMetadataManager.init is invoked before this method * @param streamName * @return */ public static String convertToStreamDef(String streamName){ SortedMap<String, AlertStreamSchemaEntity> map = getAttrMap(streamName); StringBuilder sb = new StringBuilder(); // sb.append(EAGLE_ALERT_CONTEXT_FIELD + " object, "); for(Map.Entry<String, AlertStreamSchemaEntity> entry : map.entrySet()){ appendAttributeNameType(sb, entry.getKey(), entry.getValue().getAttrType()); } if(sb.length() > 0){ sb.deleteCharAt(sb.length()-1); } String siddhiStreamDefFormat = "define stream " + streamName + "(" + "%s" + ");"; return String.format(siddhiStreamDefFormat, sb.toString()); }
public Map<String, SortedMap<String, AlertStreamSchemaEntity>> getMetadataEntityMapForAllStreams(){ ensureInitialized(); return UnmodifiableMap.decorate(map2); } }
/** * singleton with init would be good for unit test as well, and it ensures that * initialization happens only once before you use it. * @param config * @param dao */ public void init(Config config, AlertStreamSchemaDAO dao){ if(!initialized){ synchronized(this){ if(!initialized){ if(LOG.isDebugEnabled()) LOG.debug("Initializing ..."); internalInit(config, dao); initialized = true; LOG.info("Successfully initialized"); } } }else{ LOG.info("Already initialized, skip"); } }
private void putAttrsIntoInputStream(List<Object> input, String streamName, Set<String> metadataKeys, SortedMap dataMap) { if (!needValidation) { input.addAll(dataMap.values()); return; } // If a metadata field is not set, we put null for the field's value. for (String key : metadataKeys) { Object value = dataMap.get(key); if (value == null) { input.add(SiddhiStreamMetadataUtils.getAttrDefaultValue(streamName, key)); } else { input.add(value); } } }
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; }
private static void appendAttributeNameType(StringBuilder sb, String attrName, String attrType){ sb.append(attrName); sb.append(" "); if(attrType.equalsIgnoreCase(AttributeType.STRING.name())){ sb.append("string"); }else if(attrType.equalsIgnoreCase(AttributeType.INTEGER.name())){ sb.append("int"); }else if(attrType.equalsIgnoreCase(AttributeType.LONG.name())){ sb.append("long"); }else if(attrType.equalsIgnoreCase(AttributeType.BOOL.name())){ sb.append("bool"); }else if(attrType.equalsIgnoreCase(AttributeType.FLOAT.name())){ sb.append("float"); }else if(attrType.equalsIgnoreCase(AttributeType.DOUBLE.name())){ sb.append("double"); }else{ LOG.warn("AttrType is not recognized, ignore : " + attrType); } sb.append(","); }
public static String convertToStreamDef(String streamName, Map<String, String> eventSchema){ StringBuilder sb = new StringBuilder(); sb.append("context" + " object,"); for(Map.Entry<String, String> entry : eventSchema.entrySet()){ appendAttributeNameType(sb, entry.getKey(), entry.getValue()); } if(sb.length() > 0){ sb.deleteCharAt(sb.length()-1); } String siddhiStreamDefFormat = "define stream " + streamName + "(" + "%s" + ");"; return String.format(siddhiStreamDefFormat, sb.toString()); }
@Override public List<Module> getBindingModules() { Module module1 = new SimpleModule(Constants.POLICY_DEFINITION).registerSubtypes(new NamedType(SiddhiPolicyDefinition.class, getPolicyType())); return Arrays.asList(module1); } }
@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(); } }
public Map<String, List<AlertStreamSchemaEntity>> getMetadataEntitiesForAllStreams(){ ensureInitialized(); return UnmodifiableMap.decorate(map); }