###############################################################
# Copyright (c) 2021-2022 Robert Bosch Manufacturing Solutions GmbH
# Copyright (c) 2021-2022 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: BPN Discovery Service
  description: BPN Discovery Service to find BPN based on a local identifier.
  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: https
        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/bpnDiscovery:
    post:
      tags:
        - Discovery
      summary: Create new numbers (e.g. OEN, batteryID, etc.) corresponding to BPN endpoints as single entry.
      operationId: PostBpnDiscovery
      requestBody:
        description: the request body is expecting a key (type, e.g. OEN, batteryID, etc.) - value (explicit number) pair, where this service will respond with success/error message on persistance of this key-value-pair. The BPN is hand-over by the authentication/authorization (→ token). Only the owner of a BPN can link any number (e.g. OEN, batteryID, etc.) to his BPN.
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/IdentifierTypeKeyPair'
            examples:
              complete:
                $ref: '#/components/examples/identifier-type-key-pair'
        required: true
      responses:
        "201":
          description: key value for BPN created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/Bpn'
              examples:
                complete:
                  $ref: '#/components/examples/bpn-result'
  /administration/connectors/bpnDiscovery/batch:
    post:
      tags:
        - Discovery
      summary: Create new numbers (e.g. OEN, batteryID, etc.) corresponding to BPN endpoints as batch.
      operationId: PostBatchBpnDiscovery
      requestBody:
        description: The request body is expecting a list of key (type, e.g. OEN, batteryID, etc.) - value (explicit number) pairs, where this service will respond with success/error message on persistance of this key-value-pairs. The BPN is hand-over by the authentication/authorization (→ token). Only the owner of a BPN can link any number (e.g. OEN, batteryID, etc.) to his BPN.
        content:
          application/json:
            schema:
              type: array
              maxItems: 10000
              items:
                $ref: '#/components/schemas/IdentifierTypeKeyPair'
            examples:
              complete:
                $ref: '#/components/examples/identifier-type-key-pair-batch'
        required: true
      responses:
        "201":
          description: key value for BPN in batch created successfully
          content:
            application/json:
              schema:
                type: array
                maxItems: 10000
                items:
                  $ref: '#/components/schemas/TypeKeyPairBatchResult'
              examples:
                complete:
                  $ref: '#/components/examples/identifier-type-key-pair-batch-result'
  /administration/connectors/bpnDiscovery/search:
    post:
      tags:
        - Discovery
      summary: Find BPN endpoints corresponding to numbers (e.g. OEN, batteryID, etc.)
      operationId: GetBpnDiscoveries
      requestBody:
        description: the request body is expecting a list of key (type, e.g. OEN, batteryID, etc.) - value (explicit number) pairs, where this service will respond with the corresponding BPN. Please add minimum one key-value pair.
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/SearchRequest'
            examples:
              complete:
                $ref: '#/components/examples/search-filter'
        required: true
      responses:
        "200":
          description: key value for BPN created successfully
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BpnCollection'
              examples:
                complete:
                  $ref: '#/components/examples/search-filter-result'

  /administration/connectors/bpnDiscovery/{resourceId}:
    delete:
      tags:
        - Discovery
      summary: Delete numbers (e.g. OEN, batteryID, etc.) corresponding to BPN endpoints as single entry.
      operationId: DeleteBpnDiscoveryById
      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: BPN Identifier Key-Value deleted successfully
components:
  schemas:
    IdentifierTypeKeyPair:
      title: IdentifierTypeKeyPair
      type: object
      required:
        - type
        - key
      properties:
        type:
          type: string
          minLength: 1
          maxLength: 200
        key:
          type: string
          minLength: 1
          maxLength: 200
    SearchRequest:
      title: SearchRequest
      properties:
        searchFilter:
          title: searchFilter
          type: array
          maxItems: 10000
          items:
            $ref: '#/components/schemas/SearchIdentifierTypeKeyPair'
    SearchIdentifierTypeKeyPair:
      title: SearchIdentifierTypeKeyPair
      type: object
      properties:
        type:
          type: string
          minLength: 1
          maxLength: 200
        keys:
          type: array
          maxItems: 10000
          items:
            type: string
    BpnCollection:
      title: BpnCollection
      properties:
        bpns:
          title: bpns
          type: array
          maxItems: 10000
          items:
            $ref: '#/components/schemas/Bpn'
    Bpn:
      title: Bpn
      type: object
      required:
        - type
        - key
        - value
        - resourceId
      properties:
        type:
          type: string
        key:
          type: string
        value:
          type: string
        resourceId:
          type: string
    TypeKeyPairResult:
      type: object
      required:
        - message
        - key
      properties:
        message:
          type: string
          description: The detailed message for the creation.
        key:
          type: string
          description: The created key.
    TypeKeyPairBatchResult:
      type: object
      required:
        - message
        - key
        - status
      properties:
        message:
          type: string
          description: The detailed message for the creation.
        details:
          type: object
          additionalProperties:
            type: object
          description: An object with key/value pairs containing additional information exception.
        key:
          type: string
          description: The created key.
        status:
          type: integer
          description: The status code
    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:
    identifier-type-key-pair:
      value:
        {
          "type": "oen",
          "key": "oen-123"
        }
    bpn-result:
      value:
        {
          "type": "oen",
          "key": "oen-1243",
          "value": "bpn-123",
          "resourceId": "1ca6f9b5-8e1d-422a-8541-9bb2cf5fe485"
        }
    identifier-type-key-pair-batch:
      value:
        [
          {
            "type": "oenId",
            "key": "oenId-123"
          },
          {
            "type": "oen",
            "key": "oen-4444"
          },
          {
            "type": "bpid",
            "key": "bpid-123"
          }
        ]
    identifier-type-key-pair-batch-result:
      value:
        [
          {
            "message": "Validation failed.",
            "details": {
              "type": "type oenId is not allowed."
            },
            "key": "oenId-123",
            "status": 400
          },
          {
            "message": "BpnDiscovery successfully created",
            "details": null,
            "key": "oen-4444",
            "status": 200
          },
          {
            "message": "BpnDiscovery successfully created",
            "details": null,
            "key": "bpid-123",
            "status": 200
          }
        ]
    search-filter:
      value:
        {
          "searchFilter": [
            {
              "type": "oen",
              "keys": [ "oen-1243","oen-11" ]
            },
            {
              "type": "bpid",
              "keys": [ "bpid-1243","bpid-11" ]
            }
          ]
        }
    search-filter-result:
      value:
        {
          "bpns": [
            {
              "type": "oen",
              "key": "oen-1243",
              "value": "bpn-123",
              "resourceId": "1ca6f9b5-8e1d-422a-8541-9bb2cf5fe485"
            },
            {
              "type": "oen",
              "key": "oen-11",
              "value": "bpn-123",
              "resourceId": "972262d7-7e05-4578-936f-de236d7feb94"
            },
            {
              "type": "bpid",
              "key": "bpid-1243",
              "value": "bpn-444",
              "resourceId": "1b754aeb-c753-4adf-ae6d-52842f5a38b7"
            },
            {
              "type": "bpid",
              "key": "oen-11",
              "value": "bpn-444",
              "resourceId": "e43f9cf8-f0d4-4c51-b7fe-b68ea4aeea7c"
            }
          ]
        }