Exemplo n.º 1
0
  public boolean isStreamDefinitionValidForConfiguration(
      EventBuilderConfiguration eventBuilderConfiguration,
      StreamDefinition exportedStreamDefinition) {
    if (!(eventBuilderConfiguration.getToStreamName().equals(exportedStreamDefinition.getName())
        && eventBuilderConfiguration
            .getToStreamVersion()
            .equals(exportedStreamDefinition.getVersion()))) {
      return false;
    }
    if (eventBuilderConfiguration.getInputMapping() instanceof XMLInputMapping) {
      XMLInputMapping xmlInputMapping =
          (XMLInputMapping) eventBuilderConfiguration.getInputMapping();
      for (InputMappingAttribute inputMappingAttribute :
          xmlInputMapping.getInputMappingAttributes()) {
        Attribute attribute =
            new Attribute(
                inputMappingAttribute.getToElementKey(), inputMappingAttribute.getToElementType());
        if (!exportedStreamDefinition.getPayloadData().contains(attribute)) {
          return false;
        }
      }
    } else {
      return false;
    }

    return true;
  }
Exemplo n.º 2
0
  public static boolean equals(Event event1, Event event2, StreamDefinition streamDefinition) {
    if (event1 == event2) return true;

    List<Attribute> payloadDefinitions = streamDefinition.getPayloadData();
    List<Attribute> correlationDefinitions = streamDefinition.getCorrelationData();
    List<Attribute> metaDefinitions = streamDefinition.getMetaData();

    try {
      if (!(event1.getStreamId().equals(event2.getStreamId()))) {
        return false;
      }
      if (!(event1.getTimeStamp() == event2.getTimeStamp())) {
        return false;
      }

      if (payloadDefinitions != null) {
        for (int i = 0; i < payloadDefinitions.size(); i++) {
          Attribute attribute = payloadDefinitions.get(i);
          if (!compare(
              event1.getPayloadData()[i], event2.getPayloadData()[i], attribute.getType())) {
            return false;
          }
        }
      } else {
        if (!(event1.getPayloadData() == event2.getPayloadData())) return false;
      }

      if (metaDefinitions != null) {
        for (int i = 0; i < metaDefinitions.size(); i++) {
          Attribute attribute = metaDefinitions.get(i);
          if (!compare(event1.getMetaData()[i], event2.getMetaData()[i], attribute.getType())) {
            return false;
          }
        }

      } else {
        if (!(event1.getMetaData() == event2.getMetaData())) return false;
      }

      if (correlationDefinitions != null) {
        for (int i = 0; i < correlationDefinitions.size(); i++) {
          Attribute attribute = correlationDefinitions.get(i);
          if (!compare(
              event1.getCorrelationData()[i],
              event2.getCorrelationData()[i],
              attribute.getType())) {
            return false;
          }
        }
      } else {
        if (!(event1.getCorrelationData() == event2.getCorrelationData())) return false;
      }

    } catch (IOException e) {
      log.error(e.getMessage(), e);
      return false;
    }

    return true;
  }
  @Test
  public void testStreamDefinitionConversion() throws MalformedStreamDefinitionException {
    StreamDefinition streamDef = new StreamDefinition("stockStream", "1.1.0");
    List<Attribute> meta = new ArrayList<Attribute>(1);
    meta.add(new Attribute("symbol", AttributeType.STRING));
    List<Attribute> payload = new ArrayList<Attribute>(1);
    payload.add(new Attribute("price", AttributeType.DOUBLE));
    streamDef.setMetaData(meta);
    streamDef.setPayloadData(payload);

    org.wso2.siddhi.query.api.definition.StreamDefinition siddhiDefinition = null;
    try {
      siddhiDefinition =
          EventProcessorUtil.convertToSiddhiStreamDefinition(streamDef, "stockStream");
    } catch (EventStreamConfigurationException e) {
      log.error(e);
    }
    Assert.assertEquals(siddhiDefinition.getAttributeList().size(), 2);
    log.info(siddhiDefinition);

    /*
            StreamDefinition databrigeDefinition = EventProcessorUtil.convertToDatabridgeStreamDefinition(siddhiDefinition, "stockStream/1.1.0");
            Assert.assertEquals(databrigeDefinition.getPayloadData().size(), 2);
            log.info(databrigeDefinition);
    */
  }
Exemplo n.º 4
0
  public void createThreadDetailsStream(int tenantID) throws EmailMonitorServiceException {
    StreamDefinition streamDefinition;
    try {
      streamDefinition = eventStreamService.getStreamDefinition("threadDetails", "1.0.0", tenantID);
    } catch (EventStreamConfigurationException e) {
      logger.error(e.getMessage());
      throw new EmailMonitorServiceException("Error when getting stream definition", e);
    }

    if (streamDefinition == null) {
      try {
        streamDefinition = new StreamDefinition("threadDetails", "1.0.0");
      } catch (MalformedStreamDefinitionException e) {
        logger.error(e.getMessage());
        throw new EmailMonitorServiceException("Error defining new stream", e);
      }

      List<Attribute> payloadData = new ArrayList<Attribute>();

      Attribute payloadThreadID = new Attribute("threadID", AttributeType.LONG);
      payloadData.add(payloadThreadID);

      Attribute payloadLabels = new Attribute("labels", AttributeType.STRING);
      payloadData.add(payloadLabels);

      Attribute payloadEmailCount = new Attribute("emailCount", AttributeType.LONG);
      payloadData.add(payloadEmailCount);

      Attribute payloadTo = new Attribute("to", AttributeType.STRING);
      payloadData.add(payloadTo);

      Attribute payloadSenders = new Attribute("senders", AttributeType.STRING);
      payloadData.add(payloadSenders);

      Attribute payloadNewLabel = new Attribute("newLabel", AttributeType.STRING);
      payloadData.add(payloadNewLabel);

      streamDefinition.setPayloadData(payloadData);
      streamDefinition.setDescription("thread detail stream");
      streamDefinition.setNickName("thread detail");

      try {
        eventStreamService.addEventStreamDefinition(streamDefinition, tenantID);
      } catch (EventStreamConfigurationException e) {
        logger.error(e.getMessage());
        throw new EmailMonitorServiceException("Error when adding new stream", e);
      }
    }
  }
 @Test
 public void deleteStreamDefinitionAndId() throws StreamDefinitionStoreException {
   insertEventsFromMultipleStreams();
   insertEventsFromTwoVersions();
   Collection<StreamDefinition> allStreamDefinitionFromStore =
       cassandraConnector.getAllStreamDefinitionFromStore(getCluster());
   int i = 0;
   for (StreamDefinition streamDefinition : allStreamDefinitionFromStore) {
     cassandraConnector.deleteStreamDefinitionFromCassandra(
         getCluster(), streamDefinition.getStreamId());
     i++;
     Collection<StreamDefinition> newAllStreamDefns =
         cassandraConnector.getAllStreamDefinitionFromStore(getCluster());
     assertEquals(allStreamDefinitionFromStore.size() - i, newAllStreamDefns.size());
   }
 }
Exemplo n.º 6
0
 private void publishEvent(
     BrokerConfiguration brokerConfiguration,
     AsyncDataPublisher dataPublisher,
     Event event,
     StreamDefinition streamDefinition)
     throws BrokerEventProcessingException {
   try {
     dataPublisher.publish(streamDefinition.getName(), streamDefinition.getVersion(), event);
   } catch (AgentException ex) {
     throw new BrokerEventProcessingException(
         "Cannot publish data via DataPublisher for the broker configuration:"
             + brokerConfiguration.getName()
             + " for the  event "
             + event,
         ex);
   }
 }
Exemplo n.º 7
0
 @Override
 public void removeStream(StreamDefinition streamDefinition, Credentials credentials) {
   topicStreamDefinitionMap.remove(createTopic(streamDefinition));
   Map<String, BrokerListener> brokerListeners =
       topicBrokerListenerMap.get(createTopic(streamDefinition));
   if (brokerListeners != null) {
     for (BrokerListener brokerListener : brokerListeners.values()) {
       try {
         brokerListener.removeEventDefinition(streamDefinition);
       } catch (BrokerEventProcessingException e) {
         log.error(
             "Cannot remove Stream Definition from a brokerListener subscribed to "
                 + streamDefinition.getStreamId(),
             e);
       }
     }
   }
   streamIdBrokerListenerMap.remove(streamDefinition.getStreamId());
 }
 /**
  * Add a stream definition using the Event stream publisher.
  *
  * @param streamDefinitionBean The stream definition bean class.
  */
 public String addStreamDefinition(StreamDefinitionBean streamDefinitionBean)
     throws AnalyticsWebServiceException, MalformedStreamDefinitionException {
   StreamDefinition streamDefinition = Utils.getStreamDefinition(streamDefinitionBean);
   try {
     eventStreamService.addEventStreamDefinition(streamDefinition);
     return streamDefinition.getStreamId();
   } catch (Exception e) {
     logger.error(
         "Unable to set the stream definition: ["
             + streamDefinition.getName()
             + ":"
             + streamDefinition.getVersion()
             + "]"
             + e.getMessage(),
         e);
     throw new AnalyticsWebServiceException(
         "Unable to set the stream definition: ["
             + streamDefinition.getName()
             + ":"
             + streamDefinition.getVersion()
             + "], "
             + e.getMessage(),
         e);
   }
 }
  @Override
  public void publish(Object[] payload) {
    if (!isEnabled()) {
      throw new RuntimeException("Statistics publisher is not enabled");
    }

    Event event = new Event();
    event.setPayloadData(payload);
    event.setArbitraryDataMap(new HashMap<String, String>());

    try {
      if (log.isDebugEnabled()) {
        log.debug(
            String.format(
                "Publishing cep event: [stream] %s [version] %s",
                streamDefinition.getName(), streamDefinition.getVersion()));
      }
      asyncDataPublisher.publish(streamDefinition.getName(), streamDefinition.getVersion(), event);
    } catch (AgentException e) {
      if (log.isErrorEnabled()) {
        log.error(
            String.format(
                "Could not publish cep event: [stream] %s [version] %s",
                streamDefinition.getName(), streamDefinition.getVersion()),
            e);
      }
    }
  }
 public static void deployStreams(DeployableTemplate template) {
   if (template.getStreams() != null) {
     for (String stream : template.getStreams()) {
       StreamDefinition streamDefinition = null;
       try {
         streamDefinition = EventDefinitionConverterUtils.convertFromJson(stream);
         BatchScriptDeployerValueHolder.getEventStreamService()
             .addEventStreamDefinition(streamDefinition);
       } catch (MalformedStreamDefinitionException e) {
         log.error("Stream definition is incorrect in domain template " + stream, e);
       } catch (EventStreamConfigurationException e) {
         log.error("Exception occurred when configuring stream " + streamDefinition.getName(), e);
       } catch (StreamDefinitionAlreadyDefinedException e) {
         log.error(
             "Same template stream name "
                 + streamDefinition.getName()
                 + " has been defined for another definition ",
             e);
         throw e;
       }
     }
   }
 }
Exemplo n.º 11
0
 @Override
 public void definedStream(StreamDefinition streamDefinition, Credentials credentials) {
   topicStreamDefinitionMap.put(createTopic(streamDefinition), streamDefinition);
   Map<String, BrokerListener> brokerListeners =
       topicBrokerListenerMap.get(createTopic(streamDefinition));
   if (brokerListeners == null) {
     brokerListeners = new HashMap<String, BrokerListener>();
     topicBrokerListenerMap.put(createTopic(streamDefinition), brokerListeners);
   }
   //            inputTypeDefMap.put(streamDefinition.getName(), streamDefinition);
   for (BrokerListener brokerListener : brokerListeners.values()) {
     try {
       brokerListener.addEventDefinition(streamDefinition);
     } catch (BrokerEventProcessingException e) {
       log.error(
           "Cannot send Stream Definition to a brokerListener subscribed to "
               + streamDefinition.getStreamId(),
           e);
     }
   }
   streamIdBrokerListenerMap.put(
       streamDefinition.getStreamId(),
       topicBrokerListenerMap.get(createTopic(streamDefinition)));
 }
  public static Event convertToWSO2Event(
      org.wso2.siddhi.core.event.Event event, StreamDefinition streamDefinition) {
    int metaSize;
    int correlationSize;
    int payloadSize;

    Object[] metaAttributes = null;
    Object[] correlationAttributes = null;
    Object[] payloadAttributes = null;

    long timeStamp = event.getTimestamp();
    Object[] objArray = event.getData();

    int attributeIndex = 0;

    if (streamDefinition.getMetaData() != null) { // If there is at least 1 meta data field
      metaSize = streamDefinition.getMetaData().size();
      metaAttributes = new Object[metaSize];
      for (int i = 0; i < metaSize; i++) {
        metaAttributes[i] = objArray[attributeIndex++];
      }
    }
    if (streamDefinition.getCorrelationData()
        != null) { // If there is at least 1 correlation data field
      correlationSize = streamDefinition.getCorrelationData().size();
      correlationAttributes = new Object[correlationSize];
      for (int i = 0; i < correlationSize; i++) {
        correlationAttributes[i] = objArray[attributeIndex++];
      }
    }
    if (streamDefinition.getPayloadData() != null) { // If there is at least 1 payload data field
      payloadSize = streamDefinition.getPayloadData().size();
      payloadAttributes = new Object[payloadSize];
      for (int i = 0; i < payloadSize; i++) {
        payloadAttributes[i] = objArray[attributeIndex++];
      }
    }

    return new Event(
        streamDefinition.getStreamId(),
        timeStamp,
        metaAttributes,
        correlationAttributes,
        payloadAttributes);
  }
Exemplo n.º 13
0
 public void createMapping(
     EventBuilderConfiguration eventBuilderConfiguration,
     StreamDefinition exportedStreamDefinition,
     List<XPathData> attributeXpathList) {
   if (eventBuilderConfiguration != null
       && eventBuilderConfiguration.getInputMapping() instanceof XMLInputMapping) {
     XMLInputMapping xmlInputMapping =
         (XMLInputMapping) eventBuilderConfiguration.getInputMapping();
     XPathDefinition xPathDefinition = xmlInputMapping.getXpathDefinition();
     for (InputMappingAttribute inputMappingAttribute :
         xmlInputMapping.getInputMappingAttributes()) {
       String xpathExpr = inputMappingAttribute.getFromElementKey();
       try {
         AXIOMXPath xpath = new AXIOMXPath(xpathExpr);
         if (xPathDefinition != null && !xPathDefinition.isEmpty()) {
           xpath.addNamespace(xPathDefinition.getPrefix(), xPathDefinition.getNamespaceUri());
         }
         String type =
             EventBuilderConstants.ATTRIBUTE_TYPE_CLASS_TYPE_MAP.get(
                 inputMappingAttribute.getToElementType());
         attributeXpathList.add(
             new XPathData(xpath, type, inputMappingAttribute.getDefaultValue()));
         exportedStreamDefinition.addPayloadData(
             inputMappingAttribute.getToElementKey(), inputMappingAttribute.getToElementType());
       } catch (JaxenException e) {
         String errMsg = "Error parsing XPath expression: " + xpathExpr;
         log.error(errMsg, e);
         throw new EventBuilderConfigurationException(errMsg, e);
       }
     }
     if (!isStreamDefinitionValidForConfiguration(
         eventBuilderConfiguration, exportedStreamDefinition)) {
       throw new EventBuilderValidationException(
           "Exported stream definition does not match the specified configuration.");
     }
   }
 }
Exemplo n.º 14
0
 private String createTopic(StreamDefinition streamDefinition) {
   return streamDefinition.getName() + "/" + streamDefinition.getVersion();
 }
Exemplo n.º 15
0
  public void createMailInputStream(int tenantID) throws EmailMonitorServiceException {
    StreamDefinition streamDefinition;
    try {
      streamDefinition =
          eventStreamService.getStreamDefinition("gmailInputStream", "1.0.0", tenantID);
    } catch (EventStreamConfigurationException e) {
      logger.error(e.getMessage());
      throw new EmailMonitorServiceException("Error when getting stream definition", e);
    }
    if (streamDefinition == null) {
      try {
        streamDefinition = new StreamDefinition("gmailInputStream", "1.0.0");
      } catch (MalformedStreamDefinitionException e) {
        logger.error(e.getMessage());
        throw new EmailMonitorServiceException("Error defining new stream", e);
      }

      List<Attribute> metaData = new ArrayList<Attribute>();

      Attribute metaTenantID = new Attribute("tenant_id", AttributeType.INT);
      metaData.add(metaTenantID);

      Attribute metaHttpMethod = new Attribute("http_method", AttributeType.STRING);
      metaData.add(metaHttpMethod);

      Attribute metaCharacterSet = new Attribute("character_set_encoding", AttributeType.STRING);
      metaData.add(metaCharacterSet);

      Attribute metaRemoteAddress = new Attribute("remote_address", AttributeType.STRING);
      metaData.add(metaRemoteAddress);

      Attribute metaTransportIn = new Attribute("transport_in_url", AttributeType.STRING);
      metaData.add(metaTransportIn);

      Attribute metaMessageType = new Attribute("message_type", AttributeType.STRING);
      metaData.add(metaMessageType);

      Attribute metaRemoteHost = new Attribute("remote_host", AttributeType.STRING);
      metaData.add(metaRemoteHost);

      Attribute metaServicePrefix = new Attribute("service_prefix", AttributeType.STRING);
      metaData.add(metaServicePrefix);

      Attribute metaHost = new Attribute("host", AttributeType.STRING);
      metaData.add(metaHost);

      streamDefinition.setMetaData(metaData);

      List<Attribute> correlationData = new ArrayList<Attribute>();

      Attribute correlationActivityID = new Attribute("activity_id", AttributeType.STRING);
      correlationData.add(correlationActivityID);

      streamDefinition.setCorrelationData(correlationData);

      List<Attribute> payloadData = new ArrayList<Attribute>();

      Attribute payloadMessageDirection = new Attribute("message_direction", AttributeType.STRING);
      payloadData.add(payloadMessageDirection);

      Attribute payloadServiceName = new Attribute("service_name", AttributeType.STRING);
      payloadData.add(payloadServiceName);

      Attribute payloadOperationName = new Attribute("operation_name", AttributeType.STRING);
      payloadData.add(payloadOperationName);

      Attribute payloadMessageID = new Attribute("message_id", AttributeType.STRING);
      payloadData.add(payloadMessageID);

      Attribute payloadTimestamp = new Attribute("timestamp", AttributeType.LONG);
      payloadData.add(payloadTimestamp);

      Attribute payloadEmailMessageID = new Attribute("messageID", AttributeType.LONG);
      payloadData.add(payloadEmailMessageID);

      Attribute payloadSubject = new Attribute("subject", AttributeType.STRING);
      payloadData.add(payloadSubject);

      Attribute payloadFrom = new Attribute("sender", AttributeType.STRING);
      payloadData.add(payloadFrom);

      Attribute payloadTo = new Attribute("to", AttributeType.STRING);
      payloadData.add(payloadTo);

      Attribute payloadSentDate = new Attribute("sentDate", AttributeType.LONG);
      payloadData.add(payloadSentDate);

      Attribute payloadThreadID = new Attribute("threadID", AttributeType.LONG);
      payloadData.add(payloadThreadID);

      Attribute payloadStatus = new Attribute("status", AttributeType.STRING);
      payloadData.add(payloadStatus);

      Attribute payloadContent = new Attribute("content", AttributeType.STRING);
      payloadData.add(payloadContent);

      Attribute payloadLabels = new Attribute("labels", AttributeType.STRING);
      payloadData.add(payloadLabels);

      Attribute payloadSoapHeader = new Attribute("soap_header", AttributeType.STRING);
      payloadData.add(payloadSoapHeader);

      Attribute payloadSoapBody = new Attribute("soap_body", AttributeType.STRING);
      payloadData.add(payloadSoapBody);

      streamDefinition.setPayloadData(payloadData);
      streamDefinition.setDescription("email information stream");
      streamDefinition.setNickName("gmail");

      try {
        eventStreamService.addEventStreamDefinition(streamDefinition, tenantID);
      } catch (EventStreamConfigurationException e) {
        logger.error(e.getMessage());
        throw new EmailMonitorServiceException("Error when adding new stream", e);
      }
    }
  }
Exemplo n.º 16
0
  private void sendEvent(Object obj) {
    if (obj instanceof OMElement) {
      OMElement eventOMElement = (OMElement) obj;
      if (eventBuilderConfiguration.isTraceEnabled()) {
        trace.info("[Event-Builder] Received event as OMElement.\n" + eventOMElement.toString());
      }

      OMNamespace omNamespace = null;
      if (this.xPathDefinition == null || this.xPathDefinition.isEmpty()) {
        omNamespace = eventOMElement.getNamespace();
      }
      List<Object> objList = new ArrayList<Object>();
      for (XPathData xpathData : attributeXpathList) {
        AXIOMXPath xpath = xpathData.getXpath();
        OMElement omElementResult = null;
        String type = xpathData.getType();
        try {
          if (omNamespace != null) {
            xpath.addNamespaces(eventOMElement);
          }
          omElementResult = (OMElement) xpath.selectSingleNode(eventOMElement);
          Class<?> beanClass = Class.forName(type);
          Object returnedObj = null;
          if (omElementResult != null) {
            returnedObj =
                BeanUtil.deserialize(
                    beanClass, omElementResult, reflectionBasedObjectSupplier, null);
          } else if (xpathData.getDefaultValue() != null) {
            if (!beanClass.equals(String.class)) {
              Class<?> stringClass = String.class;
              Method valueOfMethod = beanClass.getMethod("valueOf", stringClass);
              returnedObj = valueOfMethod.invoke(null, xpathData.getDefaultValue());
            } else {
              returnedObj = xpathData.getDefaultValue();
            }
            log.warn("Unable to parse XPath to retrieve required attribute. Sending defaults.");
          } else {
            log.warn(
                "Unable to parse XPath to retrieve required attribute. Skipping to next attribute.");
          }
          objList.add(returnedObj);
        } catch (JaxenException e) {
          throw new EventBuilderConfigurationException("Error parsing xpath for " + xpath, e);
        } catch (ClassNotFoundException e) {
          throw new EventBuilderConfigurationException(
              "Cannot find specified class for type " + type);
        } catch (AxisFault axisFault) {
          throw new EventBuilderConfigurationException(
              "Error deserializing OMElement " + omElementResult, axisFault);
        } catch (NoSuchMethodException e) {
          throw new EventBuilderConfigurationException(
              "Error trying to convert default value to specified target type.", e);
        } catch (InvocationTargetException e) {
          throw new EventBuilderConfigurationException(
              "Error trying to convert default value to specified target type.", e);
        } catch (IllegalAccessException e) {
          throw new EventBuilderConfigurationException(
              "Error trying to convert default value to specified target type.", e);
        }
      }
      Object[] objArray = objList.toArray(new Object[objList.size()]);
      if (!this.basicEventListeners.isEmpty()) {
        if (eventBuilderConfiguration.isTraceEnabled()) {
          trace.info(
              "[Event-Builder] Sending event object array "
                  + Arrays.toString(objArray)
                  + " to all registered basic event listeners");
        }
        for (BasicEventListener basicEventListener : basicEventListeners) {
          basicEventListener.onEvent(objArray);
        }
      }
      if (!this.wso2EventListeners.isEmpty()) {
        Event event =
            new Event(
                exportedStreamDefinition.getStreamId(),
                System.currentTimeMillis(),
                null,
                null,
                objArray);
        if (eventBuilderConfiguration.isTraceEnabled()) {
          trace.info(
              "[Event-Builder] Sending event "
                  + event.toString()
                  + " to all registered wso2 event listeners");
        }
        for (Wso2EventListener wso2EventListener : wso2EventListeners) {
          wso2EventListener.onEvent(event);
        }
      }
    }
  }
  public void addExecutionPlanConfiguration(
      ExecutionPlanConfiguration executionPlanConfiguration, AxisConfiguration axisConfiguration)
      throws ExecutionPlanDependencyValidationException, ExecutionPlanConfigurationException,
          ServiceDependencyValidationException {
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    Map<String, ExecutionPlan> tenantExecutionPlans = tenantSpecificExecutionPlans.get(tenantId);
    if (tenantExecutionPlans == null) {
      tenantExecutionPlans = new ConcurrentHashMap<String, ExecutionPlan>();
      tenantSpecificExecutionPlans.put(tenantId, tenantExecutionPlans);
    } else if (tenantExecutionPlans.get(executionPlanConfiguration.getName()) != null) {
      // if an execution plan with the same name already exists, we are not going to override it
      // with this plan.
      throw new ExecutionPlanConfigurationException(
          "Execution plan with the same name already exists. Please remove it and retry.");
    }

    // This iteration exists only as a check. Actual usage of imported stream configs is further
    // down
    for (StreamConfiguration streamConfiguration :
        executionPlanConfiguration.getImportedStreams()) {
      try {
        StreamDefinition streamDefinition =
            EventProcessorValueHolder.getEventStreamService()
                .getStreamDefinition(streamConfiguration.getStreamId(), tenantId);
        if (streamDefinition == null) {
          throw new ExecutionPlanDependencyValidationException(
              streamConfiguration.getStreamId(),
              "Imported Stream " + streamConfiguration.getStreamId() + " does not exist");
        }
      } catch (EventStreamConfigurationException e) {
        throw new ExecutionPlanConfigurationException(
            "Error in retrieving stream ID : " + streamConfiguration.getStreamId());
      }
    }

    // This iteration exists only as a check. Actual usage of exported stream configs is further
    // down
    for (StreamConfiguration streamConfiguration :
        executionPlanConfiguration.getExportedStreams()) {
      try {
        StreamDefinition streamDefinition =
            EventProcessorValueHolder.getEventStreamService()
                .getStreamDefinition(streamConfiguration.getStreamId(), tenantId);
        if (streamDefinition == null) {
          throw new ExecutionPlanDependencyValidationException(
              streamConfiguration.getStreamId(),
              "Exported Stream " + streamConfiguration.getStreamId() + " does not exist");
        }
      } catch (EventStreamConfigurationException e) {
        throw new ExecutionPlanConfigurationException(
            "Error in retrieving stream ID : " + streamConfiguration.getStreamId());
      }
    }

    Map<String, InputHandler> inputHandlerMap =
        new ConcurrentHashMap<String, InputHandler>(
            executionPlanConfiguration.getImportedStreams().size());

    SiddhiConfiguration siddhiConfig =
        getSiddhiConfigurationFor(executionPlanConfiguration, tenantId);
    SiddhiManager siddhiManager =
        getSiddhiManagerFor(executionPlanConfiguration, siddhiConfig, inputHandlerMap);

    for (StreamConfiguration importedStreamConfiguration :
        executionPlanConfiguration.getImportedStreams()) {
      org.wso2.siddhi.query.api.definition.StreamDefinition siddhiStreamDefinition =
          new org.wso2.siddhi.query.api.definition.StreamDefinition();
      siddhiStreamDefinition.name(importedStreamConfiguration.getSiddhiStreamName());
      StreamDefinition streamDefinition = null;
      try {
        streamDefinition =
            EventProcessorValueHolder.getEventStreamService()
                .getStreamDefinition(importedStreamConfiguration.getStreamId(), tenantId);

        populateAttributes(
            siddhiStreamDefinition,
            streamDefinition.getMetaData(),
            EventProcessorConstants.META + EventProcessorConstants.ATTRIBUTE_SEPARATOR);
        populateAttributes(
            siddhiStreamDefinition,
            streamDefinition.getCorrelationData(),
            EventProcessorConstants.CORRELATION + EventProcessorConstants.ATTRIBUTE_SEPARATOR);
        populateAttributes(siddhiStreamDefinition, streamDefinition.getPayloadData(), "");
        InputHandler inputHandler = siddhiManager.defineStream(siddhiStreamDefinition);
        inputHandlerMap.put(streamDefinition.getStreamId(), inputHandler);
        log.debug("input handler created for " + siddhiStreamDefinition.getStreamId());
      } catch (EventStreamConfigurationException e) {
        // ignored as this will not happen
      }
    }

    for (StreamConfiguration exportedStreamConfiguration :
        executionPlanConfiguration.getExportedStreams()) {
      org.wso2.siddhi.query.api.definition.StreamDefinition siddhiStreamDefinition =
          new org.wso2.siddhi.query.api.definition.StreamDefinition();
      siddhiStreamDefinition.name(exportedStreamConfiguration.getSiddhiStreamName());
      StreamDefinition streamDefinition = null;
      try {
        streamDefinition =
            EventProcessorValueHolder.getEventStreamService()
                .getStreamDefinition(exportedStreamConfiguration.getStreamId(), tenantId);

        populateAttributes(
            siddhiStreamDefinition,
            streamDefinition.getMetaData(),
            EventProcessorConstants.META + EventProcessorConstants.ATTRIBUTE_SEPARATOR);
        populateAttributes(
            siddhiStreamDefinition,
            streamDefinition.getCorrelationData(),
            EventProcessorConstants.CORRELATION + EventProcessorConstants.ATTRIBUTE_SEPARATOR);
        populateAttributes(siddhiStreamDefinition, streamDefinition.getPayloadData(), "");
        siddhiManager.defineStream(siddhiStreamDefinition);
        log.debug("stream defined for " + siddhiStreamDefinition.getStreamId());
      } catch (EventStreamConfigurationException e) {
        // ignored as this will not happen
      }
    }

    HAManager haManager = null;
    String isDistributedProcessingEnabledString =
        executionPlanConfiguration
            .getSiddhiConfigurationProperties()
            .get(EventProcessorConstants.SIDDHI_DISTRIBUTED_PROCESSING);
    if (isDistributedProcessingEnabledString != null
        && isDistributedProcessingEnabledString.equalsIgnoreCase("RedundantNode")) {
      haManager =
          new HAManager(
              EventProcessorValueHolder.getHazelcastInstance(),
              executionPlanConfiguration.getName(),
              tenantId,
              siddhiManager,
              inputHandlerMap.size(),
              currentCepMembershipInfo);
    }

    try {
      siddhiManager.addExecutionPlan(executionPlanConfiguration.getQueryExpressions());
    } catch (Exception e) {
      throw new ExecutionPlanConfigurationException(
          "Invalid query specified, " + e.getMessage(), e);
    }

    ExecutionPlan executionPlan =
        new ExecutionPlan(
            executionPlanConfiguration.getName(),
            siddhiManager,
            executionPlanConfiguration,
            haManager);
    tenantExecutionPlans.put(executionPlanConfiguration.getName(), executionPlan);

    // subscribe output to junction
    SindhiStormOutputEventListener stormOutputListener = null;
    if (isRunningOnStorm) {
      stormOutputListener =
          new SindhiStormOutputEventListener(executionPlanConfiguration, tenantId);
    }
    for (StreamConfiguration exportedStreamConfiguration :
        executionPlanConfiguration.getExportedStreams()) {

      SiddhiOutputStreamListener streamCallback;

      if (haManager != null) {
        streamCallback =
            new SiddhiHAOutputStreamListener(
                exportedStreamConfiguration.getSiddhiStreamName(),
                exportedStreamConfiguration.getStreamId(),
                executionPlanConfiguration,
                tenantId);
        haManager.addStreamCallback((SiddhiHAOutputStreamListener) streamCallback);
      } else {
        streamCallback =
            new SiddhiOutputStreamListener(
                exportedStreamConfiguration.getSiddhiStreamName(),
                exportedStreamConfiguration.getStreamId(),
                executionPlanConfiguration,
                tenantId);

        if (isRunningOnStorm) {
          stormOutputListener.registerOutputStreamListener(
              exportedStreamConfiguration.getSiddhiStreamName(), streamCallback);
        }
      }
      siddhiManager.addCallback(exportedStreamConfiguration.getSiddhiStreamName(), streamCallback);
      try {
        EventProcessorValueHolder.getEventStreamService().subscribe(streamCallback, tenantId);
      } catch (EventStreamConfigurationException e) {
        // ignored as this will never happen
      }
      executionPlan.addProducer(streamCallback);
    }

    // subscribe input to junction
    for (StreamConfiguration importedStreamConfiguration :
        executionPlanConfiguration.getImportedStreams()) {
      InputHandler inputHandler = inputHandlerMap.get(importedStreamConfiguration.getStreamId());

      AbstractSiddhiInputEventDispatcher eventDispatcher = null;
      if (haManager != null) {
        eventDispatcher =
            new SiddhiHAInputEventDispatcher(
                importedStreamConfiguration.getStreamId(),
                inputHandler,
                executionPlanConfiguration,
                tenantId,
                haManager.getProcessThreadPoolExecutor(),
                haManager.getThreadBarrier());
        haManager.addInputEventDispatcher(
            importedStreamConfiguration.getStreamId(),
            (SiddhiHAInputEventDispatcher) eventDispatcher);
      } else if (isRunningOnStorm) {
        StreamDefinition streamDefinition = null;
        try {
          streamDefinition =
              EventProcessorValueHolder.getEventStreamService()
                  .getStreamDefinition(importedStreamConfiguration.getStreamId(), tenantId);
        } catch (EventStreamConfigurationException e) {
          // Ignore as this would never happen
        }
        eventDispatcher =
            new SiddhiStormInputEventDispatcher(
                streamDefinition,
                importedStreamConfiguration.getSiddhiStreamName(),
                executionPlanConfiguration,
                tenantId);
      } else {
        eventDispatcher =
            new SiddhiInputEventDispatcher(
                importedStreamConfiguration.getStreamId(),
                inputHandler,
                executionPlanConfiguration,
                tenantId);
      }

      try {
        EventProcessorValueHolder.getEventStreamService().subscribe(eventDispatcher, tenantId);
        executionPlan.addConsumer(eventDispatcher);

      } catch (EventStreamConfigurationException e) {
        // ignored as this will never happen
      }
    }

    if (haManager != null) {
      haManager.init();
    }
  }
 private static boolean validateSiddhiStreamWithDatabridgeStream(
     String streamName,
     String streamVersion,
     org.wso2.siddhi.query.api.definition.StreamDefinition siddhiStreamDefinition)
     throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
   if (siddhiStreamDefinition == null) {
     throw new ExecutionPlanDependencyValidationException(
         streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
         "Cannot validate null Siddhi stream for the stream: "
             + streamName
             + EventProcessorConstants.STREAM_SEPARATOR
             + streamVersion
             + " ");
   }
   EventStreamService eventStreamService = EventProcessorValueHolder.getEventStreamService();
   try {
     StreamDefinition streamDefinition =
         eventStreamService.getStreamDefinition(streamName, streamVersion);
     if (streamDefinition != null) {
       String siddhiAttributeName;
       int attributeCount = 0;
       int streamSize =
           (streamDefinition.getMetaData() == null ? 0 : streamDefinition.getMetaData().size())
               + (streamDefinition.getCorrelationData() == null
                   ? 0
                   : streamDefinition.getCorrelationData().size())
               + (streamDefinition.getPayloadData() == null
                   ? 0
                   : streamDefinition.getPayloadData().size());
       if (siddhiStreamDefinition.getAttributeList().size() != streamSize) {
         throw new ExecutionPlanDependencyValidationException(
             streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
             "No of attributes in stream "
                 + streamName
                 + EventProcessorConstants.STREAM_SEPARATOR
                 + streamVersion
                 + " do not match the no of attributes in Siddhi stream");
       }
       if (streamDefinition.getMetaData() != null) {
         for (Attribute attribute : streamDefinition.getMetaData()) {
           siddhiAttributeName = EventProcessorConstants.META_PREFIX + attribute.getName();
           org.wso2.siddhi.query.api.definition.Attribute.Type type =
               siddhiStreamDefinition.getAttributeType(siddhiAttributeName);
           // null check for type not required since an exception is thrown by Siddhi
           // StreamDefinition.getAttributeType() method for non-existent attributes
           if (siddhiStreamDefinition.getAttributePosition(siddhiAttributeName)
               != attributeCount++) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Attribute positions do not match for attribute: "
                     + attribute.getName());
           }
           if (!isMatchingType(type, attribute.getType())) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Type mismatch for attribute: "
                     + attribute.getName());
           }
         }
       }
       if (streamDefinition.getCorrelationData() != null) {
         for (Attribute attribute : streamDefinition.getCorrelationData()) {
           siddhiAttributeName = EventProcessorConstants.CORRELATION_PREFIX + attribute.getName();
           org.wso2.siddhi.query.api.definition.Attribute.Type type =
               siddhiStreamDefinition.getAttributeType(siddhiAttributeName);
           // null check for type not required since an exception is thrown by Siddhi
           // StreamDefinition.getAttributeType() method for non-existent attributes
           if (siddhiStreamDefinition.getAttributePosition(siddhiAttributeName)
               != attributeCount++) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Attribute positions do not match for attribute: "
                     + attribute.getName());
           }
           if (!isMatchingType(type, attribute.getType())) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Type mismatch for attribute: "
                     + attribute.getName());
           }
         }
       }
       if (streamDefinition.getPayloadData() != null) {
         for (Attribute attribute : streamDefinition.getPayloadData()) {
           siddhiAttributeName = attribute.getName();
           org.wso2.siddhi.query.api.definition.Attribute.Type type =
               siddhiStreamDefinition.getAttributeType(siddhiAttributeName);
           // null check for type not required since an exception is thrown by Siddhi
           // StreamDefinition.getAttributeType() method for non-existent attributes
           if (siddhiStreamDefinition.getAttributePosition(siddhiAttributeName)
               != attributeCount++) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Attribute positions do not match for attribute: "
                     + attribute.getName());
           }
           if (!isMatchingType(type, attribute.getType())) {
             throw new ExecutionPlanDependencyValidationException(
                 streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
                 "Stream "
                     + streamName
                     + EventProcessorConstants.STREAM_SEPARATOR
                     + streamVersion
                     + "; Type mismatch for attribute: "
                     + attribute.getName());
           }
         }
       }
       return true;
     }
   } catch (EventStreamConfigurationException e) {
     throw new ExecutionPlanConfigurationException(
         "Error while validating stream definition with store : " + e.getMessage(), e);
   } catch (AttributeNotExistException e) {
     throw new ExecutionPlanDependencyValidationException(
         streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion, e.getMessage());
   }
   throw new ExecutionPlanDependencyValidationException(
       streamName + EventProcessorConstants.STREAM_SEPARATOR + streamVersion,
       "Stream "
           + streamName
           + EventProcessorConstants.STREAM_SEPARATOR
           + streamVersion
           + " does not exist");
 }