/**
   * Activate Inactive Execution Plan Configurations
   *
   * @param tenantId the tenant id of the tenant which triggered this call
   * @param resolvedDependencyId the id of the dependency that was resolved which resulted in
   *     triggering this method call
   */
  public void activateInactiveExecutionPlanConfigurations(
      ExecutionPlanConfigurationFile.Status status, String resolvedDependencyId, int tenantId)
      throws ExecutionPlanConfigurationException {

    List<ExecutionPlanConfigurationFile> reloadFileList =
        new ArrayList<ExecutionPlanConfigurationFile>();

    if (tenantSpecificExecutionPlanFiles != null && tenantSpecificExecutionPlanFiles.size() > 0) {
      List<ExecutionPlanConfigurationFile> executionPlanConfigurationFiles =
          tenantSpecificExecutionPlanFiles.get(tenantId);

      if (executionPlanConfigurationFiles != null) {
        for (ExecutionPlanConfigurationFile executionPlanConfigurationFile :
            executionPlanConfigurationFiles) {
          if ((executionPlanConfigurationFile.getStatus().equals(status))
              && resolvedDependencyId.equalsIgnoreCase(
                  executionPlanConfigurationFile.getDependency())) {
            reloadFileList.add(executionPlanConfigurationFile);
          }
        }
      }
    }
    for (ExecutionPlanConfigurationFile executionPlanConfigurationFile : reloadFileList) {
      try {
        EventProcessorConfigurationFilesystemInvoker.reload(
            executionPlanConfigurationFile.getFileName(),
            executionPlanConfigurationFile.getAxisConfiguration());
      } catch (Exception e) {
        log.error(
            "Exception occurred while trying to deploy the Execution Plan configuration file : "
                + new File(executionPlanConfigurationFile.getFileName()).getName());
      }
    }
  }
  @Override
  public void undeployInactiveExecutionPlanConfiguration(
      String filename, AxisConfiguration axisConfiguration)
      throws ExecutionPlanConfigurationException {

    EventProcessorConfigurationFilesystemInvoker.delete(filename, axisConfiguration);
  }
 public void editInactiveExecutionPlanConfiguration(
     String executionPlanConfiguration, String filename, AxisConfiguration axisConfiguration)
     throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
   try {
     int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
     OMElement omElement = AXIOMUtil.stringToOM(executionPlanConfiguration);
     EventProcessorConfigurationHelper.validateExecutionPlanConfiguration(omElement, tenantId);
     ExecutionPlanConfiguration config = EventProcessorConfigurationHelper.fromOM(omElement);
     EventProcessorConfigurationFilesystemInvoker.delete(filename, axisConfiguration);
     EventProcessorConfigurationFilesystemInvoker.save(
         executionPlanConfiguration, config.getName(), filename, axisConfiguration);
   } catch (XMLStreamException e) {
     log.error("Error while creating the xml object");
     throw new ExecutionPlanConfigurationException("Not a valid xml object ", e);
   }
 }
  private void editExecutionPlanConfiguration(
      ExecutionPlanConfiguration executionPlanConfiguration,
      String executionPlanName,
      int tenantId,
      AxisConfiguration axisConfiguration)
      throws ExecutionPlanConfigurationException {

    ExecutionPlanConfigurationFile configFile =
        getExecutionPlanConfigurationFileByPlanName(executionPlanName, tenantId);
    String fileName = configFile.getFileName();
    EventProcessorConfigurationFilesystemInvoker.delete(
        configFile.getFileName(), axisConfiguration);
    OMElement omElement = EventProcessorConfigurationHelper.toOM(executionPlanConfiguration);
    EventProcessorConfigurationFilesystemInvoker.save(
        omElement, executionPlanName, fileName, axisConfiguration);
  }
 @Override
 public void undeployActiveExecutionPlanConfiguration(
     String name, AxisConfiguration axisConfiguration) throws ExecutionPlanConfigurationException {
   int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
   EventProcessorConfigurationFilesystemInvoker.delete(
       getExecutionPlanConfigurationFileByPlanName(name, tenantId).getFileName(),
       axisConfiguration);
 }
  public static void save(
      OMElement executionPlanOM,
      String executionPlanName,
      String fileName,
      AxisConfiguration axisConfiguration)
      throws ExecutionPlanConfigurationException {

    EventProcessorConfigurationFilesystemInvoker.save(
        executionPlanOM.toString(), executionPlanName, fileName, axisConfiguration);
  }
 public String getActiveExecutionPlanConfigurationContent(
     String planName, AxisConfiguration axisConfiguration)
     throws ExecutionPlanConfigurationException {
   int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
   ExecutionPlanConfigurationFile configFile =
       getExecutionPlanConfigurationFileByPlanName(planName, tenantId);
   if (configFile == null) {
     throw new ExecutionPlanConfigurationException(
         "Configuration file for " + planName + "doesn't exist.");
   }
   return EventProcessorConfigurationFilesystemInvoker.readExecutionPlanConfigFile(
       configFile.getFileName(), axisConfiguration);
 }
 public void editActiveExecutionPlanConfiguration(
     String executionPlanConfiguration,
     String executionPlanName,
     AxisConfiguration axisConfiguration)
     throws ExecutionPlanConfigurationException, ExecutionPlanDependencyValidationException {
   int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
   try {
     OMElement omElement = AXIOMUtil.stringToOM(executionPlanConfiguration);
     EventProcessorConfigurationHelper.validateExecutionPlanConfiguration(omElement, tenantId);
     ExecutionPlanConfiguration executionPlanConfigurationObject =
         EventProcessorConfigurationHelper.fromOM(omElement);
     if (!(executionPlanConfigurationObject.getName().equals(executionPlanName))) {
       if (!(checkExecutionPlanValidity(executionPlanConfigurationObject.getName(), tenantId))) {
         throw new ExecutionPlanConfigurationException(
             executionPlanConfigurationObject.getName()
                 + " already registered as an execution in this tenant");
       }
     }
     if (executionPlanName != null && executionPlanName.length() > 0) {
       String fileName;
       ExecutionPlanConfigurationFile file =
           getExecutionPlanConfigurationFileByPlanName(executionPlanName, tenantId);
       if (file == null) {
         fileName = executionPlanName + EventProcessorConstants.EP_CONFIG_FILE_EXTENSION_WITH_DOT;
       } else {
         fileName = file.getFileName();
       }
       EventProcessorConfigurationFilesystemInvoker.delete(fileName, axisConfiguration);
       EventProcessorConfigurationFilesystemInvoker.save(
           executionPlanConfiguration, executionPlanName, fileName, axisConfiguration);
     } else {
       throw new ExecutionPlanConfigurationException(
           "Invalid configuration provided, No execution plan name.");
     }
   } catch (XMLStreamException e) {
     log.error("Error while creating the xml object");
     throw new ExecutionPlanConfigurationException("Not a valid xml object, ", e);
   }
 }
  @Override
  public void deployExecutionPlanConfiguration(
      String executionPlanConfigurationXml, AxisConfiguration axisConfiguration)
      throws ExecutionPlanDependencyValidationException, ExecutionPlanConfigurationException {

    OMElement omElement = null;
    try {
      omElement = AXIOMUtil.stringToOM(executionPlanConfigurationXml);
    } catch (XMLStreamException e) {
      throw new ExecutionPlanConfigurationException(
          "Cannot parse execution plan configuration XML:" + e.getMessage(), e);
    }
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    EventProcessorConfigurationHelper.validateExecutionPlanConfiguration(omElement, tenantId);
    String executionPlanName = EventProcessorConfigurationHelper.getExecutionPlanName(omElement);

    File directory = new File(axisConfiguration.getRepository().getPath());
    if (!directory.exists()) {
      if (directory.mkdir()) {
        throw new ExecutionPlanConfigurationException(
            "Cannot create directory to add tenant specific execution plan : " + executionPlanName);
      }
    }
    directory =
        new File(
            directory.getAbsolutePath()
                + File.separator
                + EventProcessorConstants.EP_ELE_DIRECTORY);
    if (!directory.exists()) {
      if (!directory.mkdir()) {
        throw new ExecutionPlanConfigurationException(
            "Cannot create directory "
                + EventProcessorConstants.EP_ELE_DIRECTORY
                + " to add tenant specific  execution plan :"
                + executionPlanName);
      }
    }
    validateToRemoveInactiveExecutionPlanConfiguration(executionPlanName, axisConfiguration);
    EventProcessorConfigurationFilesystemInvoker.save(
        omElement,
        executionPlanName,
        executionPlanName + EventProcessorConstants.XML_EXTENSION,
        axisConfiguration);
  }
  public void deactivateActiveExecutionPlanConfigurations(String streamId, int tenantId) {

    List<String> toDeactivateExecutionPlan = new ArrayList<String>();
    Map<String, ExecutionPlan> executionPlanMap = tenantSpecificExecutionPlans.get(tenantId);
    if (executionPlanMap != null) {
      for (ExecutionPlan executionPlan : executionPlanMap.values()) {
        boolean done = false;
        for (EventProducer eventProducer : executionPlan.getEventProducers()) {
          if (eventProducer.getStreamId().equals(streamId)) {
            toDeactivateExecutionPlan.add(executionPlan.getName());
            done = true;
            break;
          }
        }

        if (!done) {
          for (SiddhiEventConsumer eventConsumer : executionPlan.getSiddhiEventConsumers()) {
            if (eventConsumer.getStreamId().equals(streamId)) {
              toDeactivateExecutionPlan.add(executionPlan.getName());
              break;
            }
          }
        }
      }
    }
    if (toDeactivateExecutionPlan.size() > 0) {
      for (String name : toDeactivateExecutionPlan) {
        ExecutionPlanConfigurationFile executionPlanConfigurationFile =
            getExecutionPlanConfigurationFileByPlanName(name, tenantId);
        try {
          EventProcessorConfigurationFilesystemInvoker.reload(
              executionPlanConfigurationFile.getFileName(),
              executionPlanConfigurationFile.getAxisConfiguration());
        } catch (Exception e) {
          log.error(
              "Exception occurred while trying to deploy the Execution Plan configuration file : "
                  + new File(executionPlanConfigurationFile.getFileName()).getName());
        }
      }
    }
  }
  private void validateToRemoveInactiveExecutionPlanConfiguration(
      String executionPlanName, AxisConfiguration axisConfiguration)
      throws ExecutionPlanConfigurationException {
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();

    String fileName = executionPlanName + EventProcessorConstants.EP_CONFIG_FILE_EXTENSION_WITH_DOT;
    List<ExecutionPlanConfigurationFile> executionPlanConfigurationFiles =
        tenantSpecificExecutionPlanFiles.get(tenantId);
    if (executionPlanConfigurationFiles != null) {
      for (ExecutionPlanConfigurationFile executionPlanConfigurationFile :
          executionPlanConfigurationFiles) {
        if ((executionPlanConfigurationFile.getFileName().equals(fileName))) {
          if (!(executionPlanConfigurationFile
              .getStatus()
              .equals(ExecutionPlanConfigurationFile.Status.DEPLOYED))) {
            EventProcessorConfigurationFilesystemInvoker.delete(fileName, axisConfiguration);
            break;
          }
        }
      }
    }
  }
 public String getInactiveExecutionPlanConfigurationContent(
     String filename, AxisConfiguration axisConfiguration)
     throws ExecutionPlanConfigurationException {
   return EventProcessorConfigurationFilesystemInvoker.readExecutionPlanConfigFile(
       filename, axisConfiguration);
 }