content: package alien4cloud.paas.model;
  
  import java.time.Instant;
  import java.time.OffsetDateTime;
  import java.time.ZoneId;
  import java.time.format.DateTimeFormatter;
  import java.util.Date;
  
  import org.apache.commons.lang.StringUtils;
  import org.elasticsearch.annotation.*;
  import org.elasticsearch.annotation.query.TermFilter;
  import org.elasticsearch.annotation.query.TermsFacet;
  import org.elasticsearch.mapping.IndexType;
  
  import com.fasterxml.jackson.annotation.JsonInclude;
  
  import alien4cloud.dao.EsGeneratedId;
  import lombok.AllArgsConstructor;
  import lombok.Getter;
  import lombok.NoArgsConstructor;
  import lombok.Setter;
  
  import static org.apache.commons.lang.StringUtils.defaultString;
  
  /**
   * This represents a log entry from orchestrator
   */
  @Getter
  @Setter
  @NoArgsConstructor
  @AllArgsConstructor
  @ESObject
  @JsonInclude(JsonInclude.Include.NON_NULL)
  public class PaaSDeploymentLog {
      /**
       * Unique id of the log. Note that this field value is generated by elasticsearch. Should always be null before you write it to elastic search.
       */
      @EsGeneratedId
      private String id;
  
      /**
       * Deployment id is unique for a deployment
       * It corresponds to {@link alien4cloud.model.deployment.Deployment#getId}
       * This field is mandatory
       */
      @TermFilter
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String deploymentId;
  
      /**
       * Deployment PaaS id is unique for an application on a given environment (for ex : Alien-Prod)
       * It corresponds to {@link alien4cloud.model.deployment.Deployment#getOrchestratorDeploymentId}
       * This field is mandatory
       */
      @TermFilter
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String deploymentPaaSId;
  
      /**
       * Log's level
       * This field is mandatory
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private PaaSDeploymentLogLevel level;
  
      /**
       * The type of the log entry given by the PaaS
       * For cloudify it can be a4c_workflow_event, Task sent, Task started etc ...
       * For puccini it can be operation_output, operation_info, plugin, provider, general ...
       * This field is mandatory
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String type;
  
      /**
       * Log's timestamp
       * This field is mandatory
       */
      @TermFilter
      @DateField
      @TimeStamp(format = "", index = IndexType.not_analyzed)
      private Date timestamp;
  
      /**
       * The timestamp raw value as it was provided by the orchestrator.
       */
      @TermFilter
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String rawtimestamp;
  
      /**
       * Id of the workflow that generated the log
       * This field is optional
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String workflowId;
  
      /**
       * Id of the execution that generated the log
       * This field is optional
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String executionId;
  
      /**
       * Id of the task that generated the log
       * This field is optional
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String taskId;
  
      /**
       * Id of the node that generated the log
       * This field is optional
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String nodeId;
  
      /**
       * Id of the instance that generated the log
       * This field is optional
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String instanceId;
  
      /**
       * Interface on the node that generated the log
       * This field is optional
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String interfaceName;
  
      /**
       * Operation inside the interface on the node that generated the log
       * This field is optional
       */
      @TermFilter
      @TermsFacet
      @StringField(indexType = IndexType.not_analyzed, includeInAll = false)
      private String operationName;
  
      /**
       * Finally the log's content in free text
       * This field is mandatory
       */
      @StringField
      private String content;
  
      @Override
      public String toString() {
          return "PaaSDeploymentLog{" + toCompactString() + '}';
      }
  
      private final static String COMPACT_FORMAT = "[deploymentId=%s][deploymentPaasId=%s][operationName=%s][type=%s][timestamp=%s][workflowId=%s][taskId=%s][executionId=%s][nodeId=%s] | %s";
  
      private final static String FORMAT = "[%s][%s][%s][%s][%s][%s][%s][%s][%s][%s][%s]%s\n";
  
      public String toCompactString() {
          OffsetDateTime stamp = OffsetDateTime.ofInstant(Instant.ofEpochMilli(timestamp.getTime()), ZoneId.systemDefault());
  
          return String.format(COMPACT_FORMAT,
                  defaultString(deploymentId),
                  defaultString(deploymentPaaSId),
                  defaultString(operationName),
                  defaultString(type),
                  defaultString(stamp.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME)),
                  defaultString(workflowId),
                  defaultString(taskId),
                  defaultString(executionId),
                  defaultString(nodeId),
                  defaultString(content)
          );
      }
      
      public String toFormattedString() {
          return String.format(FORMAT, defaultString(timestamp.toString()), defaultString(level.name().toUpperCase()), defaultString(deploymentPaaSId), defaultString(workflowId), defaultString(executionId), defaultString(taskId), defaultString(nodeId), defaultString(instanceId), defaultString(interfaceName), defaultString(operationName), defaultString(type), defaultString(content));
      }
      
  }
  