All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ing.baker.runtime.serialization.InteractionExecution.scala Maven / Gradle / Ivy

package com.ing.baker.runtime.serialization

import com.ing.baker.runtime.common.InteractionExecutionFailureReason
import com.ing.baker.runtime.scaladsl.{EventInstance, IngredientInstance, InteractionExecutionResult, InteractionInstanceInput}
import com.ing.baker.runtime.serialization.InteractionExecution.{Descriptor, ExecutionRequest, ExecutionResult, Failure, FailureReason, Interactions, Success}
import com.ing.baker.types.Type
import io.circe.generic.semiauto.{deriveCodec, deriveDecoder, deriveEncoder}
import io.circe.{Codec, Decoder, Encoder}

import scala.collection.immutable.Seq

/**
  * Protocol executed after a match between a QuestMandate and InteractionAgent has been made and after both
  * have committed.
  *
  * A simple request from the manager to the agent for execution with specific ingredients is done using the
  * ExecuteInstance message, the outcome comes in the form of either the response messages InstanceExecutedSuccessfully,
  * InstanceExecutionFailed or InvalidExecution
  *
  */

object InteractionExecutionJsonCodecs {
  import com.ing.baker.runtime.serialization.JsonCodec._
  import com.ing.baker.runtime.serialization.JsonDecoders._
  import com.ing.baker.runtime.serialization.JsonEncoders._

  implicit val interactionsCodec: Codec[Interactions] = deriveCodec[Interactions]
  implicit val interactionExecutionRequestCodec: Codec[ExecutionRequest] = deriveCodec[ExecutionRequest]
  implicit val interactionExecutionDescriptorEncoder: Encoder[Descriptor] = deriveEncoder[Descriptor].mapJsonObject(_.filter(removeNulls))
  implicit val interactionExecutionDescriptorDecoder: Decoder[Descriptor] = deriveDecoder[Descriptor]
  implicit val failureReasonCodec: Codec[FailureReason] = deriveCodec[FailureReason]
  implicit val successCodec: Codec[Success] = deriveCodec[Success]
  implicit val failureCodec: Codec[Failure] = deriveCodec[Failure]
  implicit val eitherFailureOrSuccessCodec: Codec[Either[Failure, Success]] = deriveCodec[Either[Failure, Success]]
  implicit val executionResultCodec: Codec[ExecutionResult] = deriveCodec[ExecutionResult]
}

object InteractionExecution {

  case class Interactions(startedAt: Long, interactions: List[Descriptor])
  case class Descriptor(id: String, name: String, input: Seq[InteractionInstanceInput], output: Option[Map[String, Map[String, Type]]])
  case class ExecutionRequest(id: String, ingredients: List[IngredientInstance], metaData: Option[Map[String, String]])
  case class ExecutionResult(outcome: Either[Failure, Success])

  sealed trait Result
  case class Success(result: Option[EventInstance]) extends Result {
    def toBakerInteractionExecutionSuccess: InteractionExecutionResult.Success =
      InteractionExecutionResult.Success(result)
  }
  case class Failure(reason: FailureReason) extends Result {
    def toBakerInteractionExecutionFailure: InteractionExecutionResult.Failure = reason match {
      case InteractionError(interactionName, message) => InteractionExecutionResult.Failure(InteractionExecutionFailureReason.INTERACTION_NOT_FOUND, Some(interactionName), Some(message))
      case NoInstanceFound => InteractionExecutionResult.Failure(InteractionExecutionFailureReason.INTERACTION_NOT_FOUND, None, None)
      case Timeout => InteractionExecutionResult.Failure(InteractionExecutionFailureReason.TIMEOUT, None, None)
      case BadIngredients => InteractionExecutionResult.Failure(InteractionExecutionFailureReason.BAD_INGREDIENTS, None, None)
    }

  }

  sealed trait FailureReason
  case class InteractionError(interactionName: String, message: String)  extends FailureReason
  case object NoInstanceFound extends FailureReason
  case object Timeout extends FailureReason
  case object BadIngredients extends FailureReason

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy