$schema: 'http://support.riverbed.com/apis/service_def/2.2'
id: 'http://support.riverbed.com/apis/cmc.pathrules/1.1'
provider: 'riverbed'
name: "cmc.pathrules"
version: "1.1"
title: "REST API for Path Selection Rules"

###############################################################################
#
# Types
#
###############################################################################

types:
    identifier:
        type: integer
        readOnly: true

    generic_name:
        type: string
        description: "Case insensitive name"
        minLength: 2
        maxLength: 255

    dscp:
        description: "DSCP integer constrained type"
        type: integer
        minimum: -1
        maximum: 255

    position:
        description: "Position of the path rule"
        type: integer
        minimum: 1

    uplinks:
        description: Uplink template ID and name
        type: array
        items:
            type: object
            additionalProperties: false
            properties:
                uplink_type_id:
                    type: integer
                name:
                    type: string
                dscp: {$ref: '#/types/dscp'}
                network_security_level:
                    description: "Network security level"
                    type: integer
                    minimum: 0
                    maximum: 1

###############################################################################
#
# Define the REST API
#
###############################################################################

resources:
#-------------------------------------------------------------------------------
# /global_config
#-------------------------------------------------------------------------------
    global_config:
        description: "PS global configuration."
        type: object
        additionalProperties: false
        properties:
            path_selection:
                description: "This is the field for enabling/disabling PS."
                type: boolean

        required: [ path_selection ]

        links:
            self:
                description: "Base URI for "
                path: "$/global_config"

            set:
                description: "Update the global Path Selection flag."
                method: PUT
                request:
                    $ref: '#/resources/global_config'
                response:
                    $ref: '#/resources/global_config'

            get:
                description: "Get the value of global Path Selection flag."
                method: GET
                response:
                    $ref: '#/resources/global_config'

#-------------------------------------------------------------------------------
# /path_rules resource
#-------------------------------------------------------------------------------

    path_rules:
        description: "Path Selection Rules collection"
        type: array
        items:
            type: object
            additionalProperties: false
            properties:
                id:
                    $ref: '#/types/identifier'
                position:
                    $ref: '#/types/position'
                app_id:
                    $ref: '#/types/identifier'
                apptag_id:
                    $ref: '#/types/identifier'
                is_default:
                    type: boolean
                    readOnly: true
                position:
                    $ref: '#/types/position'
                uplinks:
                    type: array
                    items:
                        type: object
                        additionalProperties: false
                        properties:
                            uplink_type_id: { type: integer }
                            uplink_dscp: {$ref: '#/types/dscp' }
                            uplink_secured: { type: boolean }
                is_backhaul:
                    type: boolean
                    readOnly: true

            anyOf:
                - type: object
                  required: [ id ]
                - type: object
                  required: [ position, id ]
                - type: object
                  required: [ apptag_id, position, id ]
                - type: object
                  required: [ app_id, position, id ]

            relations:
                full:
                   resource: '#/resources/path_rule'
                   vars:
                       id: "0/id"

        links:
            self:
                description: "Base URI for a list of Path Selection Rules"
                path: "$/path_rules"
                params:
                    app_id:
                        type: integer
                    apptag_id:
                        type: integer
                    uplink_type_id:
                        type: integer

            create:
                description: "Create a new Path Selection Rule instance"
                method: POST
                request:
                    $ref: '#/resources/path_rule'
                response:
                    $ref: '#/resources/path_rule'

            clear:
                description: "Delete all the Path Selection Rule instances"
                method: POST
                path: "$/path_rules/clear"
                response:
                    type: "null"

            get:
                description: "Get a list of references to Path Rule instances"
                method: GET
                response:
                    $ref: '#/resources/path_rules'

            set:
                description: "Update the positions of the Path Selection Rules"
                method: PUT
                request:
                    $ref: '#/resources/path_rules'
                response:
                    $ref: '#/resources/path_rules'

#-------------------------------------------------------------------------------
# /path_rules/{id} resource
#-------------------------------------------------------------------------------

    path_rule:
        description: "Path-Selection Rule object"
        type: object
        additionalProperties: false
        required: [default_action]
        properties:
            id:
                $ref: '#/types/identifier'
            app_id:
                description: "Application ID. Only one of Application/ApplicationTag/group ID can be specified, not all."
                anyOf:
                    - type: integer
                    - type: "null"
            apptag_id:
                description: "Application tag ID. Only one of Application/ApplicationTag/group ID can be specified, not all."
                anyOf:
                    - type: integer
                    - type: "null"
            position:
                $ref: '#/types/position'
            uplinks:
                description: "Uplinks specified for the rule"
                type: array
                items:
                    type: object
                    additionalProperties: false
                    properties:
                        uplink_type_id: { type: integer }
                        uplink_dscp: {$ref: '#/types/dscp' }
                        uplink_secured: { type: boolean }
                    required: [uplink_type_id, uplink_dscp]
            default_action:
                description: "Default action to be taken in case all the links are down."
                type: integer
            is_default:
                description: "Is the path-selection rule a default rule?"
                type: boolean
                readOnly: true
            is_backhaul:
                description: "Is the path-selection rule a backhaul site only rule?"
                type: boolean
                readOnly: true

        relations:
            instances:
                resource: '#/resources/path_rules'

            default_action:
                resource: '#/resources/default_action'
                vars: { id: "0/default_action" }

        links:
            self:
                description: "Base URI for an instance of a Path Selection Rule"
                path: "$/path_rules/items/{id}"

            get:
                description: "Retrieve a data representation of a Path Selection Rule."
                method: GET
                response:
                    $ref: '#/resources/path_rule'

            set:
                description: "Replace the data representation for a Path Selection Rule."
                method: PUT
                request:
                    $ref: '#/resources/path_rule'
                response:
                    $ref: '#/resources/path_rule'

            delete:
                description: "Delete an Path Selection Rule instance"
                method: DELETE


#-------------------------------------------------------------------------------
# /default_actions and /default_action resource
#-------------------------------------------------------------------------------

    default_actions:
       description: "Default action resource for the Path Selection rule"
       type: array
       items:
           $merge:
               source: { $ref: '#/resources/default_action' }
               with:
                   relations:
                       full:
                           resource: '#/resources/default_action'
                           vars: {id: "0/id"}

       links:
          self:
              path: "$/default_actions"
          get:
             method: GET
             response:
                 $ref: '#/resources/default_actions'

    default_action:
       type: object
       properties:
          id:
              $ref: '#/types/identifier'
          name:
              type: string
       additionalProperties: false
       required: [ name ]
       links:
          self:
              path: "$/default_actions/items/{id}"
          get:
            method: GET
            response:
                $ref: '#/resources/default_action'
       relations:
            instances:
                resource: '#/resources/default_actions'

#####################################################################
#
# Path rule status resource
#
#####################################################################

    # Status of pathrules at the site.
    pathrule_status:
        description: Path rule status of a site.
        readOnly: true
        type: object
        required: [status]
        properties:
            status:
                type: string
            rules:
                type: array
                items:
                    type: object
                    additionalProperties: false
                    properties:
                        id: { $ref: '#/types/identifier' }
                        status:
                            type: string

        links:
            self:
                path: "$/status"
                params:
                    site_id: {type: integer}

            get:
                method: GET
                response: { $ref: '#/resources/pathrule_status' }
