{
  "Comment": "Handles parsing a DDEX document and ingesting information from it into the Orchard",
  "StartAt": "CheckForSentinelFile",
  "States": {
    "CheckForSentinelFile": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.detail.requestParameters.key",
          "StringMatches": "*/${filename_pattern}",
          "Next": "ParseDDEX"
        }
      ],
      "Default": "FailState"
    },
    "ParseDDEX": {
      "Type": "Task",
      "Resource": "${lambda_parse_ddex_arn}",
      "Parameters": {
        "bucket.$": "$.detail.requestParameters.bucketName",
        "key.$": "$.detail.requestParameters.key",
        "execution_name.$": "$$.Execution.Name"
      },
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "SendFailureXML",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "LookupSwitchboardDeal"
    },
    "LookupSwitchboardDeal": {
      "Type": "Pass",
      "Next": "CheckPurgedRelease"
    },
    "CheckPurgedRelease": {
      "Type": "Choice",
      "Choices": [
        {
          "Variable": "$.is_purged_release",
          "BooleanEquals": true,
          "Next": "HandlePurgedRelease"
        },
        {
          "Variable": "$.product.release_type",
          "StringMatches": "VideoSingle",
          "Next": "LookupVideoProduct"
        }
      ],
      "Default": "GetOrchardProductMetadata"
    },
    "LookupVideoProduct": {
      "Type": "Task",
      "Resource": "${lambda_lookup_video_product_arn}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "EmailErrors",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "ProcessVideoProduct"
    },
    "ProcessVideoProduct": {
      "Type": "Parallel",
      "Branches": [
        {
          "StartAt": "ProcessVideos",
          "States": {
            "ProcessVideos": {
              "Type": "Map",
              "MaxConcurrency": 20,
              "ItemsPath": "$.videos",
              "Parameters": {
                "context.$": "$",
                "track.$": "$$.Map.Item.Value"
              },
              "Iterator": {
                "StartAt": "ParallelIterator",
                "States": {
                  "ParallelIterator": {
                    "Type": "Parallel",
                    "Branches": [
                      {
                        "StartAt": "VideoAssetFound",
                        "States": {
                          "VideoAssetFound": {
                            "Type": "Choice",
                            "Choices": [
                              {
                                "Variable": "$.video.asset",
                                "IsNull": false,
                                "Next": "HandleVideoAsset"
                              }
                            ],
                            "Default": "MissingVideoAsset"
                          },
                          "HandleVideoAsset": {
                            "Type": "Task",
                            "Resource": "${lambda_handle_video_asset_arn}:qa",
                            "HeartbeatSeconds": 60,
                            "TimeoutSeconds": 3600,
                            "Catch": [
                              {
                                "ErrorEquals": [
                                  "States.ALL"
                                ],
                                "Next": "CatchHandleVideoAssetError",
                                "ResultPath": "$.errors"
                              }
                            ],
                            "Retry": [
                              {
                                "ErrorEquals": [
                                  "States.Timeout",
                                  "Lambda.ServiceException",
                                  "Lambda.AWSLambdaException",
                                  "Lambda.SdkClientException",
                                  "Lambda.TooManyRequestsException"
                                ],
                                "IntervalSeconds": 5,
                                "MaxAttempts": 5,
                                "BackoffRate": 3
                              }
                            ],
                            "Next": "PollVideoAssetStatus"
                          },
                          "PollVideoAssetStatus": {
                            "Type": "Task",
                            "Resource": "${lambda_poll_video_asset_status_arn}:qa",
                            "HeartbeatSeconds": 60,
                            "TimeoutSeconds": 158400,
                            "Catch": [
                              {
                                "ErrorEquals": [
                                  "States.ALL"
                                ],
                                "Next": "CatchHandleVideoAssetError",
                                "ResultPath": "$.errors"
                              }
                            ],
                            "Retry": [
                              {
                                "ErrorEquals": [
                                  "VideoException"
                                ],
                                "IntervalSeconds": 60,
                                "MaxAttempts": 120,
                                "BackoffRate": 1
                              },
                              {
                                "ErrorEquals": [
                                  "States.Timeout",
                                  "Lambda.ServiceException",
                                  "Lambda.AWSLambdaException",
                                  "Lambda.SdkClientException",
                                  "Lambda.TooManyRequestsException"
                                ],
                                "IntervalSeconds": 5,
                                "MaxAttempts": 5,
                                "BackoffRate": 3
                              }
                            ],
                            "End": true
                          },
                          "CatchHandleVideoAssetError": {
                            "Type": "Succeed"
                          },
                          "MissingVideoAsset": {
                            "Type": "Succeed"
                          }
                        }
                      }
                    ],
                    "End": true
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "ProcessVideoCarveouts",
          "States": {
            "ProcessVideoCarveouts": {
              "Type": "Task",
              "Resource": "${lambda_set_carveouts_arn}",
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": 5,
                  "MaxAttempts": 5,
                  "BackoffRate": 3
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "Next": "CatchVideoCarveoutErrors",
                  "ResultPath": "$.errors"
                }
              ],
              "End": true
            },
            "CatchVideoCarveoutErrors": {
              "Type": "Succeed"
            }
          }
        },
        {
          "StartAt": "ProcessVideoPricing",
          "States": {
            "ProcessVideoPricing": {
              "Type": "Task",
              "Resource": "${lambda_set_pricing_arn}",
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": 5,
                  "MaxAttempts": 5,
                  "BackoffRate": 3
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.errors",
                  "Next": "CatchVideoPricingErrors"
                }
              ],
              "End": true
            },
            "CatchVideoPricingErrors": {
              "Type": "Succeed"
            }
          }
        },
        {
          "StartAt": "SetVideoReleaseDate",
          "States": {
            "SetVideoReleaseDate": {
              "Type": "Task",
              "Resource": "${lambda_set_main_release_date_arn}",
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": 5,
                  "MaxAttempts": 5,
                  "BackoffRate": 3
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "Next": "CatchVideoReleaseDateErrors",
                  "ResultPath": "$.errors"
                }
              ],
              "End": true
            },
            "CatchVideoReleaseDateErrors": {
              "Type": "Succeed"
            }
          }
        }
      ],
      "Next": "DetectErrors"
    },
    "HandlePurgedRelease": {
      "Type": "Task",
      "Resource": "${lambda_handle_purged_release_arn}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "EmailErrors",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "SendSuccessXML"
    },
    "GetOrchardProductMetadata": {
      "Type": "Task",
      "Resource": "${lambda_retrieve_orchard_metadata_arn}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "EmailErrors",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "ProcessProductData"
    },
    "ProcessProductData": {
      "Type": "Parallel",
      "Branches": [
        {
          "StartAt": "ProcessTracks",
          "States": {
            "ProcessTracks": {
              "Type": "Map",
              "MaxConcurrency": 20,
              "ItemsPath": "$.tracks",
              "Parameters": {
                "context.$": "$",
                "track.$": "$$.Map.Item.Value"
              },
              "Iterator": {
                "StartAt": "AssetFound",
                "States": {
                  "AssetFound": {
                    "Type": "Choice",
                    "Choices": [
                      {
                        "Variable": "$.track.asset",
                        "IsNull": false,
                        "Next": "HandleAudioAsset"
                      }
                    ],
                    "Default": "MissingAsset"
                  },
                  "HandleAudioAsset": {
                    "Type": "Task",
                    "Resource": "${lambda_handle_audio_asset_arn}",
                    "HeartbeatSeconds": 60,
                    "TimeoutSeconds": 3600,
                    "Catch": [
                      {
                        "ErrorEquals": [
                          "States.ALL"
                        ],
                        "Next": "CatchHandleAudioAssetError",
                        "ResultPath": "$.errors"
                      }
                    ],
                    "Retry": [
                      {
                        "ErrorEquals": [
                          "States.Timeout",
                          "Lambda.ServiceException",
                          "Lambda.AWSLambdaException",
                          "Lambda.SdkClientException",
                          "Lambda.TooManyRequestsException"
                        ],
                        "IntervalSeconds": 5,
                        "MaxAttempts": 5,
                        "BackoffRate": 3
                      }
                    ],
                    "Next": "PollAudioAssetStatus"
                  },
                  "PollAudioAssetStatus": {
                    "Type": "Task",
                    "Resource": "${lambda_poll_audio_asset_status_arn}",
                    "HeartbeatSeconds": 60,
                    "TimeoutSeconds": 3600,
                    "Catch": [
                      {
                        "ErrorEquals": [
                          "States.ALL"
                        ],
                        "Next": "CatchHandleAudioAssetError",
                        "ResultPath": "$.errors"
                      }
                    ],
                    "Retry": [
                      {
                        "ErrorEquals": [
                          "AudioException"
                        ],
                        "IntervalSeconds": 60,
                        "MaxAttempts": 30,
                        "BackoffRate": 1
                      },
                      {
                        "ErrorEquals": [
                          "States.Timeout",
                          "Lambda.ServiceException",
                          "Lambda.AWSLambdaException",
                          "Lambda.SdkClientException",
                          "Lambda.TooManyRequestsException"
                        ],
                        "IntervalSeconds": 5,
                        "MaxAttempts": 5,
                        "BackoffRate": 3
                      }
                    ],
                    "End": true
                  },
                  "CatchHandleAudioAssetError": {
                    "Type": "Succeed"
                  },
                  "MissingAsset": {
                    "Type": "Succeed"
                  }
                }
              },
              "End": true
            }
          }
        },
        {
          "StartAt": "ProcessCarveouts",
          "States": {
            "ProcessCarveouts": {
              "Type": "Task",
              "Resource": "${lambda_set_carveouts_arn}",
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": 5,
                  "MaxAttempts": 5,
                  "BackoffRate": 3
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "Next": "CatchCarveoutErrors",
                  "ResultPath": "$.errors"
                }
              ],
              "End": true
            },
            "CatchCarveoutErrors": {
              "Type": "Succeed"
            }
          }
        },
        {
          "StartAt": "ArtworkFound",
          "States": {
            "ArtworkFound": {
              "Type": "Choice",
              "Choices": [
                {
                  "Variable": "$.product.artwork",
                  "IsNull": false,
                  "Next": "HandleArtwork"
                }
              ],
              "Default": "MissingArtwork"
            },
            "HandleArtwork": {
              "Type": "Task",
              "Resource": "${lambda_handle_artwork_arn}",
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": 5,
                  "MaxAttempts": 5,
                  "BackoffRate": 3
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "Next": "CatchHandleArtworkErrors",
                  "ResultPath": "$.errors"
                }
              ],
              "Next": "PollArtworkAssetStatus"
            },
            "PollArtworkAssetStatus": {
              "Type": "Task",
              "Resource": "${lambda_poll_artwork_asset_status_arn}",
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "Retry": [
                {
                  "ErrorEquals": [
                    "ArtworkException"
                  ],
                  "IntervalSeconds": 60,
                  "MaxAttempts": 30,
                  "BackoffRate": 1
                },
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": 5,
                  "MaxAttempts": 5,
                  "BackoffRate": 3
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "Next": "CatchHandleArtworkErrors",
                  "ResultPath": "$.errors"
                }
              ],
              "End": true
            },
            "CatchHandleArtworkErrors": {
              "Type": "Succeed"
            },
            "MissingArtwork": {
              "Type": "Succeed"
            }
          }
        },
        {
          "StartAt": "ProcessPricing",
          "States": {
            "ProcessPricing": {
              "Type": "Task",
              "Resource": "${lambda_set_pricing_arn}",
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": 5,
                  "MaxAttempts": 5,
                  "BackoffRate": 3
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "ResultPath": "$.errors",
                  "Next": "CatchPricingErrors"
                }
              ],
              "End": true
            },
            "CatchPricingErrors": {
              "Type": "Succeed"
            }
          }
        },
        {
          "StartAt": "SetMainReleaseDate",
          "States": {
            "SetMainReleaseDate": {
              "Type": "Task",
              "Resource": "${lambda_set_main_release_date_arn}",
              "HeartbeatSeconds": 60,
              "TimeoutSeconds": 3600,
              "Retry": [
                {
                  "ErrorEquals": [
                    "States.Timeout",
                    "Lambda.ServiceException",
                    "Lambda.AWSLambdaException",
                    "Lambda.SdkClientException",
                    "Lambda.TooManyRequestsException"
                  ],
                  "IntervalSeconds": 5,
                  "MaxAttempts": 5,
                  "BackoffRate": 3
                }
              ],
              "Catch": [
                {
                  "ErrorEquals": [
                    "States.ALL"
                  ],
                  "Next": "CatchReleaseDateErrors",
                  "ResultPath": "$.errors"
                }
              ],
              "End": true
            },
            "CatchReleaseDateErrors": {
              "Type": "Succeed"
            }
          }
        }
      ],
      "Next": "DetectErrors"
    },
    "DetectErrors": {
      "Type": "Task",
      "Resource": "${lambda_detect_errors_arn}",
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "EmailErrors",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "SubmitProduct"
    },
    "SubmitProduct": {
      "Type": "Task",
      "Resource": "${lambda_submit_product_arn}",
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "EmailErrors",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "EmailSuccess"
    },
    "EmailSuccess": {
      "Type": "Task",
      "Resource": "${lambda_report_errors_arn}",
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "EmailErrors",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "SendSuccessXML"
    },
    "EmailErrors": {
      "Type": "Task",
      "Resource": "${lambda_report_errors_arn}",
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Catch": [
        {
          "ErrorEquals": [
            "States.ALL"
          ],
          "Next": "SendFailureXML",
          "ResultPath": "$.errors"
        }
      ],
      "Next": "SendFailureXML"
    },
    "SendSuccessXML": {
      "Type": "Task",
      "Resource": "${lambda_send_sme_ack_arn}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "End": true
    },
    "SendFailureXML": {
      "Type": "Task",
      "Resource": "${lambda_send_sme_ack_arn}",
      "HeartbeatSeconds": 60,
      "TimeoutSeconds": 3600,
      "Retry": [
        {
          "ErrorEquals": [
            "States.Timeout",
            "Lambda.ServiceException",
            "Lambda.AWSLambdaException",
            "Lambda.SdkClientException",
            "Lambda.TooManyRequestsException"
          ],
          "IntervalSeconds": 5,
          "MaxAttempts": 5,
          "BackoffRate": 3
        }
      ],
      "Next": "FailState"
    },
    "FailState": {
      "Type": "Fail"
    }
  }
}
