openapi: 3.0.0
servers:
    - description: Controller URL
      url: https://localhost:8080/controller/alerting/rest/v1/applications/{application-id}/
info:
    description: |
        AppDynamics Schedule APIs
        [Details at AppDynamics documentation](https://docs.appdynamics.com/display/PRO45/AppDynamics+APIs)
    version: "1.0.0"
    title: AppDynamics Schedule APIs
    termsOfService: 'https://www.appdynamics.com/terms/'
    license:
        name: Apache 2.0
        url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
tags:
    - name: schedule
      description: Manage Health Rule Schedules
      externalDocs:
          description: Find out more
          url: 'https://docs.appdynamics.com'

externalDocs:
    description: Find out more about Swagger
    url: 'http://swagger.io'

paths:
    /schedules :
        parameters:
            - $ref: '#/components/parameters/applicationId'

        get:
            tags:
                - schedule
            summary: Returns a list of all the schedule IDs, names and description for the given application ID.
            description: |
                Returns a list of all the schedules associated with the specified application. <br>
                The schedule ID, name and description are retuned. <br>
                For complete details of a Health Rule, see `GET /schedules/{schedule-id}`
            operationId: getSchedules
            responses:
                '200':
                    description: Fetched successfully
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/ScheduleSummary'
                '400':
                    $ref: '#/components/responses/BadRequest'
                '401':
                    $ref: '#/components/responses/Unauthorized'
                '403':
                    $ref: '#/components/responses/Forbidden'
                '404':
                    $ref: '#/components/responses/NotFound'

        post:
            tags:
                - schedule
            summary: Create a new schedule
            description: Creates a new schedule for the defined payload and returns the ID.
            operationId: createSchedule
            responses:
                '201':
                    description: Created successfully
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Schedule'

                    links:
                        CreatedScheduleId:
                            operationId: getSchedule
                            parameters:
                                scheduleId: '$response.body#/id'
                            description:
                                The `id` value returned in the response can be used as `schedule-id` parameter in `GET /schedules/{schedule-id}`
                '400':
                    $ref: '#/components/responses/BadRequest'
                '401':
                    $ref: '#/components/responses/Unauthorized'
                '403':
                    $ref: '#/components/responses/Forbidden'
                '409':
                    $ref: '#/components/responses/Conflict'
            requestBody:
                $ref: '#/components/requestBodies/Schedule'

    '/schedules/{schedule-id}':
        parameters:
            - $ref: '#/components/parameters/applicationId'
            - $ref: '#/components/parameters/scheduleId'

        get:
            tags:
                - schedule
            summary: Get a JSON representation of the schedule for a specified schedule ID.
            description: Get a JSON representation of the schedule for a specified schedule ID.
            operationId: getSchedule
            responses:
                '200':
                    description: Fetched Successfully
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Schedule'
                '400':
                    $ref: '#/components/responses/BadRequest'
                '401':
                    $ref: '#/components/responses/Unauthorized'
                '403':
                    $ref: '#/components/responses/Forbidden'
                '404':
                    $ref: '#/components/responses/NotFound'

        put:
            tags:
                - schedule
            summary: Update existing schedule using JSON representation.
            description: Update existing schedule using JSON representation.
            operationId: updateSchedule
            responses:
                '200':
                    description: Updated Successfully
                    content:
                        application/json:
                            schema:
                                $ref: '#/components/schemas/Schedule'
                '400':
                    $ref: '#/components/responses/BadRequest'
                '401':
                    $ref: '#/components/responses/Unauthorized'
                '403':
                    $ref: '#/components/responses/Forbidden'
                '404':
                    $ref: '#/components/responses/NotFound'
            requestBody:
                $ref: '#/components/requestBodies/Schedule'

        delete:
            tags:
                - schedule
            summary: Delete existing schedule using schedule ID.
            description: Delete existing schedule using schedule ID.
            operationId: deleteSchedule
            responses:
                '204':
                    description: Deleted Successfully
                '400':
                    $ref: '#/components/responses/BadRequest'
                '401':
                    $ref: '#/components/responses/Unauthorized'
                '403':
                    $ref: '#/components/responses/Forbidden'
                '404':
                    $ref: '#/components/responses/NotFound'

components:
    schemas:
        Schedule:
            description: "Schedule definition"
            type: object
            properties:
                id:
                    description: Auto-generated by the system and returned in response.
                    type: integer
                    format: int64
                    readOnly: true
                name:
                    description: Name of the schedule.
                    type: string
                    minLength: 1
                    example: "Every day at noon"
                description:
                    description: Description of the schedule.
                    type: string
                    example: "Runs every day at 12 in afternoon"
                timezone:
                    $ref: '#/components/schemas/Timezone'
                scheduleConfiguration:
                    $ref: '#/components/schemas/ScheduleConfiguration'
            required:
                - name
                - timezone
                - scheduleConfiguration
            example:
                name: "One Time Schedule"
                description: "One Time Schedule"
                timezone: "America/Los_Angeles"
                scheduleConfiguration:
                    scheduleFrequency: "ONE_TIME"
                    startDate: "01/01/2019"
                    startTime: "06:00"
                    endDate: "01/01/2019"
                    endTime: "18:00"

        ScheduleConfiguration:
            type: object
            properties:
                scheduleFrequency:
                    $ref: '#/components/schemas/ScheduleFrequency'
            required:
                - scheduleFrequency
            discriminator:
                propertyName: scheduleFrequency
                mapping:
                    ONE_TIME: '#/components/schemas/OneTimeSchedule'
                    DAILY: '#/components/schemas/DailySchedule'
                    WEEKLY: '#/components/schemas/WeeklySchedule'
                    MONTHLY_SPECIFIC_DATE: '#/components/schemas/MonthlySpecificDateSchedule'
                    MONTHLY_SPECIFIC_DAY: '#/components/schemas/MonthlySpecificDaySchedule'
                    CUSTOM: '#/components/schemas/CustomSchedule'

        OneTimeSchedule:
            description: "scheduleFrequency: ONE_TIME"
            allOf:
                - $ref: '#/components/schemas/ScheduleConfiguration'
                - type: object
                  properties:
                      startDate:
                          $ref: '#/components/schemas/Date'
                      startTime:
                          $ref: '#/components/schemas/Time'
                      endDate:
                          $ref: '#/components/schemas/Date'
                      endTime:
                          $ref: '#/components/schemas/Time'
                  required:
                      - startDate
                      - startTime
                      - endDate
                      - endTime

        DailySchedule:
            description: "scheduleFrequency: DAILY"
            allOf:
                - $ref: '#/components/schemas/ScheduleConfiguration'
                - type: object
                  properties:
                      startTime:
                          $ref: '#/components/schemas/Time'
                      endTime:
                          $ref: '#/components/schemas/Time'
                  required:
                      - startTime
                      - endTime

        WeeklySchedule:
            description: "scheduleFrequency: WEEKLY"
            allOf:
                - $ref: '#/components/schemas/ScheduleConfiguration'
                - type: object
                  properties:
                      days:
                          type: array
                          items:
                              $ref: '#/components/schemas/DayOfWeek'
                          minItems: 1
                          maxItems: 7
                      startTime:
                          $ref: '#/components/schemas/Time'
                      endTime:
                          $ref: '#/components/schemas/Time'
                  required:
                      - days
                      - startTime
                      - endTime

        MonthlySpecificDateSchedule:
            description: "scheduleFrequency: MONTHLY_SPECIFIC_DATE"
            allOf:
                - $ref: '#/components/schemas/ScheduleConfiguration'
                - type: object
                  properties:
                      startDate:
                          description: The start date of the month.
                          type: string
                          pattern: '^(0[1-9]|[12][0-9]|3[01])$|^Last Day$'
                          example: "01"
                      startTime:
                          $ref: '#/components/schemas/Time'
                      endDate:
                          description: The end date of the month.
                          type: string
                          pattern: '^(0[1-9]|[12][0-9]|3[01])$|^Last Day$'
                          example: "Last Day"
                      endTime:
                          $ref: '#/components/schemas/Time'
                  required:
                      - startTime
                      - endTime
                      - startDate
                      - endDate

        MonthlySpecificDaySchedule:
            description: "scheduleFrequency: MONTHLY_SPECIFIC_DAY"
            allOf:
                - $ref: '#/components/schemas/ScheduleConfiguration'
                - type: object
                  properties:
                      startTime:
                          $ref: '#/components/schemas/Time'
                      endTime:
                          $ref: '#/components/schemas/Time'
                      day:
                          $ref: '#/components/schemas/DayOfWeek'
                      occurrence:
                          $ref: '#/components/schemas/Occurrence'
                  required:
                      - startTime
                      - endTime
                      - day
                      - occurrence

        CustomSchedule:
            description: "scheduleFrequency: CUSTOM"
            allOf:
                - $ref: '#/components/schemas/ScheduleConfiguration'
                - type: object
                  properties:
                      startCron:
                          description: The beginning of the cron expression.
                          type: string
                          example: "0 0 6 ? * 2-6"
                      endCron:
                          description: The end of the cron expression
                          type: string
                          example: "0 0 15 ? * 2-6"
                  required:
                      - startCron
                      - endCron

        ScheduleFrequency:
            type: string
            enum:
                - ONE_TIME
                - DAILY
                - WEEKLY
                - MONTHLY_SPECIFIC_DATE
                - MONTHLY_SPECIFIC_DAY
                - CUSTOM
            example: CUSTOM

        Occurrence:
            type: string
            enum:
                - FIRST
                - SECOND
                - THIRD
                - FOURTH
                - LAST
            example: FIRST

        DayOfWeek:
            type: string
            enum:
                - SUNDAY
                - MONDAY
                - TUESDAY
                - WEDNESDAY
                - THURSDAY
                - FRIDAY
                - SATURDAY
            example: SUNDAY

        Time:
            description: The time in 24 hour format.
            type: string
            pattern: '^([01]\d|2[0-3]):([0-5]\d)$'
            example: "14:00"

        Date:
            description: The date in the following format :- DD/MM/YYYY
            type: string
            pattern: '^(?:(?:31(\/|-|\.)(?:0?[13578]|1[02]))\1|(?:(?:29|30)(\/|-|\.)(?:0?[13-9]|1[0-2])\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})$|^(?:29(\/|-|\.)0?2\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))$|^(?:0?[1-9]|1\d|2[0-8])(\/|-|\.)(?:(?:0?[1-9])|(?:1[0-2]))\4(?:(?:1[6-9]|[2-9]\d)?\d{2})$'
            example: "01/01/2019"

        Timezone:
            description: Timezone Id
            type: string
            example: "America/Los_Angeles"

        ScheduleSummary:
            type: array
            items:
                $ref: '#/components/schemas/ScheduleSummaryElement'

        ScheduleSummaryElement:
            type: object
            properties:
                id:
                    description: The schedule ID.
                    type: integer
                    format: int64
                    example: 10
                name:
                    description: Name of the schedule.
                    type: string
                    minLength: 1
                    example: "Every day at noon"
                description:
                    description: Description of the schedule.
                    type: string
                    minLength: 1
                    example: "Runs every day at 12 noon"
                timezone:
                    $ref: '#/components/schemas/Timezone'
            required:
                - id
                - name
                - description
                - timezone
        ErrorResponse:
            type: object
            properties:
                statusCode:
                    type: integer
                    example: 404
                message:
                    type: string
                    example: "Schedule with given Id not found."

    parameters:
        applicationId:
            in: path
            name: application-id
            required: true
            schema:
                type: integer
                format: int64
                example: 10
        scheduleId:
            in: path
            name: schedule-id
            description: The ID of the schedule to be updated.
            required: true
            schema:
                type: integer
                format: int64
                example: 1

    requestBodies:
        Schedule:
            content:
                application/json:
                    schema:
                        $ref: '#/components/schemas/Schedule'

    responses:
        NotFound:
            description: The specified resource was not found
            content:
                application/json:
                    schema:
                        $ref: '#/components/schemas/ErrorResponse'
        Unauthorized:
            description: Unauthorized
            content:
                application/json:
                    schema:
                        $ref: '#/components/schemas/ErrorResponse'
        BadRequest:
            description: Bad Request
            content:
                application/json:
                    schema:
                        $ref: '#/components/schemas/ErrorResponse'
        Forbidden:
            description: Forbidden
            content:
                application/json:
                    schema:
                        $ref: '#/components/schemas/ErrorResponse'
        Conflict:
            description: Already exists
            content:
                application/json:
                    schema:
                        $ref: '#/components/schemas/ErrorResponse'
