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);
 }
 // gets file by name.
 private ExecutionPlanConfigurationFile getExecutionPlanConfigurationFileByPlanName(
     String name, int tenantId) {
   List<ExecutionPlanConfigurationFile> executionPlanConfigurationFiles =
       tenantSpecificExecutionPlanFiles.get(tenantId);
   if (executionPlanConfigurationFiles != null) {
     for (ExecutionPlanConfigurationFile file : executionPlanConfigurationFiles) {
       if (name.equals(file.getExecutionPlanName())
           && file.getStatus().equals(ExecutionPlanConfigurationFile.Status.DEPLOYED)) {
         return file;
       }
     }
   }
   return null;
 }
 /**
  * Just removes the configuration file
  *
  * @param fileName the filename of the {@link ExecutionPlanConfigurationFile} to be removed
  * @param tenantId the tenantId of the tenant to which this configuration file belongs
  */
 public void removeExecutionPlanConfigurationFile(String fileName, int tenantId) {
   List<ExecutionPlanConfigurationFile> executionPlanConfigurationFiles =
       tenantSpecificExecutionPlanFiles.get(tenantId);
   for (Iterator<ExecutionPlanConfigurationFile> iterator =
           executionPlanConfigurationFiles.iterator();
       iterator.hasNext(); ) {
     ExecutionPlanConfigurationFile configurationFile = iterator.next();
     if (configurationFile.getFileName().equals(fileName)) {
       if (configurationFile.getStatus().equals(ExecutionPlanConfigurationFile.Status.DEPLOYED)) {
         removeExecutionPlanConfiguration(configurationFile.getExecutionPlanName(), tenantId);
       }
       iterator.remove();
       break;
     }
   }
 }
  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);
  }
  /**
   * 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 List<ExecutionPlanConfigurationFile> getAllInactiveExecutionPlanConfiguration(
      int tenantId) {
    List<ExecutionPlanConfigurationFile> executionPlanConfigurationFiles =
        this.tenantSpecificExecutionPlanFiles.get(tenantId);

    List<ExecutionPlanConfigurationFile> files = new ArrayList<ExecutionPlanConfigurationFile>();
    if (executionPlanConfigurationFiles != null) {
      for (ExecutionPlanConfigurationFile configFile : executionPlanConfigurationFiles) {
        if (configFile.getStatus() == ExecutionPlanConfigurationFile.Status.ERROR
            || configFile.getStatus()
                == ExecutionPlanConfigurationFile.Status.WAITING_FOR_DEPENDENCY
            || configFile.getStatus()
                == ExecutionPlanConfigurationFile.Status.WAITING_FOR_OSGI_SERVICE) {
          files.add(configFile);
        }
      }
    }
    return files;
  }
  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());
        }
      }
    }
  }
  @Override
  public String getExecutionPlanStatusAsString(String filename) {
    int tenantId = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantId();
    List<ExecutionPlanConfigurationFile> executionPlanConfigurationFileList =
        tenantSpecificExecutionPlanFiles.get(tenantId);
    if (executionPlanConfigurationFileList != null) {
      for (ExecutionPlanConfigurationFile executionPlanConfigurationFile :
          executionPlanConfigurationFileList) {
        if (filename != null && filename.equals(executionPlanConfigurationFile.getFileName())) {
          String statusMsg = executionPlanConfigurationFile.getDeploymentStatusMessage();
          if (executionPlanConfigurationFile.getDependency() != null) {
            statusMsg =
                statusMsg + " [Dependency: " + executionPlanConfigurationFile.getDependency() + "]";
          }
          return statusMsg;
        }
      }
    }

    return EventProcessorConstants.NO_DEPENDENCY_INFO_MSG;
  }
 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);
   }
 }
  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;
          }
        }
      }
    }
  }