###############################################################
# Copyright (c) 2021 Robert Bosch Manufacturing Solutions GmbH
# Copyright (c) 2021 Contributors to the Eclipse Foundation
#
# See the NOTICE file(s) distributed with this work for additional
# information regarding copyright ownership.
#
# This program and the accompanying materials are made available under the
# terms of the Apache License, Version 2.0 which is available at
# https://www.apache.org/licenses/LICENSE-2.0.
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# SPDX-License-Identifier: Apache-2.0
###############################################################

openapi: 3.0.3
info:
  title: Discovery Finder
  description: Discovery Finder to find endpoint of the BPN/EDC Discovery.
  contact:
    name: SLDT Team
  version: 0.0.1

security:
  - CatenaXOpenId:
      - profile
servers:
  - url: "{protocol}://{host_name}:{port}/api/{version_prefix}"
    variables:
      protocol:
        description: Allows access through http and https (recommended)
        default: http
        enum:
          - http
          - https
      host_name:
        description: Hostname of server hosting the api
        default: localhost
      port:
        description: "80 is default for http, 443 for https"
        default: "443"
        enum:
          - "80"
          - "443"
          - "4243"
      version_prefix:
        default: v1.0
        enum:
          - v1.0
paths:
  /administration/connectors/discovery:
    post:
      tags:
        - Finder
      summary: Define the endpoint to BPN discovery services and the EDC discovery service.
      operationId: PostDiscoveryEndpoint
      requestBody:
        description: Create new endpoint for bpn discovery service / edc discovery service. The type is uniqued. This service is allowed to administrators only
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/DiscoveryEndpoint'
            examples:
              complete:
                $ref: '#/components/examples/discovery-endpoint'
        required: true
      responses:
        "201":
          description: Discovery Endpoint created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DiscoveryEndpoint'
              examples:
                complete:
                  $ref: '#/components/examples/discovery-endpoint-result'
  /administration/connectors/discovery/search:
    post:
      tags:
        - Finder
      summary: Find the endpoints to BPN discovery services and the EDC discovery service.
      operationId: GetDiscoveryEndpoints
      requestBody:
        description: The request body is expecting a list of types ( e.g. oen, bpid, etc.) to return available discovery endpoints.
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SearchRequest'
            examples:
              complete:
                $ref: '#/components/examples/discovey-endpoint-search'
        required: true
      responses:
        "200":
          description: Returns discovery endpoints based on search filter.
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DiscoveryEndpointCollection'
              examples:
                complete:
                  $ref: '#/components/examples/discovery-endpoint-search-result'
  /administration/connectors/discovery/{resourceId}:
    delete:
      tags:
        - Finder
      summary: Delete the endpoint to BPN discovery services and the EDC discovery service.
      operationId: DeleteDiscoveryEndpointByResourceId
      parameters:
        - name: resourceId
          in: path
          description: The IdentifierKeyValue unique id (BASE64-URL-encoded)
          required: true
          style: simple
          explode: false
          schema:
            type: string
      responses:
        "204":
          description: Discovery Endpoint deleted successfully
components:
  schemas:
    DiscoveryEndpoint:
      title: DiscoveryEndpoint
      type: object
      required:
        - type
        - description
        - endpointAddress
        - timeToLive
      properties:
        type:
          type: string
          minLength: 1
          maxLength: 200
        description:
          type: string
          minLength: 1
          maxLength: 500
        endpointAddress:
          type: string
          minLength: 1
          maxLength: 300
        documentation:
          type: string
          minLength: 1
          maxLength: 500
        resourceId:
          type: string
        timeToLive:
          type: integer
          minimum: 1
          maximum: 31536000
    SearchRequest:
      title: SearchRequest
      required:
        - types
      properties:
        types:
          title: types
          type: array
          maxItems: 10000
          items:
            type: string
    DiscoveryEndpointCollection:
      title: DiscoveryEndpointCollection
      properties:
        endpoints:
          title: endpoints
          type: array
          maxItems: 10000
          items:
            $ref: '#/components/schemas/DiscoveryEndpoint'
            type: object
    ErrorResponse:
      type: object
      required:
        - error
      properties:
        error:
          $ref: '#/components/schemas/Error'
    Error:
      type: object
      required:
        - details
      properties:
        message:
          type: string
          example: size must be between {min} and {max}
          description: The detailed error message for the exception which occurred.
          minLength: 1
        path:
          type: string
          description: The requested path.
          minLength: 1
        details:
          type: object
          additionalProperties:
            type: object
          description: An object with key/value pairs containing additional information about the error
  securitySchemes:
    CatenaXOpenId:
      type: openIdConnect
      openIdConnectUrl: ../.well-known/openid-configuration
  examples:
    discovery-endpoint:
      value:
        {
          "type": "oen",
          "description": "Service to discover BPN to a particular OEN",
          "endpointAddress": "http://...",
          "documentation": "http://.../swagger/index.html",
          "timeToLive": 31536000
        }
    discovery-endpoint-result:
      value:
        {
          "type": "oen",
          "description": "Service to discover BPN to a particular OEN",
          "endpointAddress": "http://...",
          "documentation": "http://.../swagger/index.html",
          "resourceId": "ec6f407b-4296-418c-9e4e-fb739fe72a67",
          "timeToLive": 31536000
        }
    discovey-endpoint-search:
      value:
        {
          "types": [ "oen", "bpid", "bpn" ]
        }
    discovery-endpoint-search-result:
      value:
        {
          "endpoints": [
            {
              "type": "oen",
              "description": "Service to discover BPN to a particular OEN",
              "endpointAddress": "http://...",
              "documentation": "http://.../swagger/index.html",
              "resourceId": "ec6f407b-4296-418c-9e4e-fb739fe72a67",
              "timeToLive": 31536000
            },
            {
              "type": "bpid",
              "description": "Service to discover BPN to a particular Battery Pass ID",
              "endpointAddress": "http://...",
              "documentation": "http://.../swagger/index.html",
              "resourceId": "08702529-3714-4c4f-b022-346b9b4fbbe2",
              "timeToLive": 31536000
            },
            {
              "type": "bpn",
              "description": "Service to discover EDC to a particular BPN",
              "endpointAddress": "http://...",
              "documentation": "http://.../swagger/index.html",
              "resourceId": "316417cd-0fb5-4daf-8dfa-8f68125923f1",
              "timeToLive": 31536000
            }
          ]
        }