// Protocol Buffers for Saturnin Node Service
// Copyright 2019 Firebird Project.  All rights reserved.
// https://www.firebirdsql.org/
// https://saturnin.rtfd.io
//
// License: The MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom
// the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax = "proto3";

package saturnin.protobuf;

import "google/protobuf/any.proto";
import "google/protobuf/struct.proto";
import "firebird/butler/fbsd.proto";
import "saturnin/protobuf/common.proto";

// Enums

// Dependency

message Dependency {
  firebird.butler.DependencyTypeEnum type = 1 ; // [required]
  bytes                              uid  = 2 ; // [required] Interface uid
}

// Interface

message RequestCode {
  uint32 code = 1 ; // [required]
  string name = 2 ; // [required]
}

message InterfaceDescriptor {
  bytes                uid      = 1 ; // [required] Interface ID (uuid5 - SHA1, namespace OID)
  string               name     = 2 ; // [required] Interface name
  uint32               revision = 3 ; // [required] Revision number
  uint32               number   = 4 ; // [required] Identification Number assigned by Service
  repeated RequestCode requests = 5 ; // [required] Request codes
}

// Installed service specification

message InstalledService {
  firebird.butler.AgentIdentification agent        = 1 ; // [required]
  repeated InterfaceDescriptor        api          = 2 ; // [required]
  repeated Dependency                 dependencies = 3 ; // List of dependencies
  ServiceTypeEnum                     service_type = 4 ; // Service type
  string                              description  = 5 ; // Textual service description
  ExecutionModeEnum                   mode         = 6 ; // Default execution mode
  repeated ServiceFacilitiesEnum      facilities   = 7 ; // Declared service facilities
  repeated google.protobuf.Any        supplement   = 8 ; // Additional information about service
}

// INSTALLED_SERVICES Message Data

message ReplyInstalledServices {
  repeated InstalledService services = 1 ;
}

// RUNNING_SERVICES Message Data

message RunningService {
  firebird.butler.PeerIdentification  peer         = 1 ; // Peer descriptor
  firebird.butler.AgentIdentification agent        = 2 ; // Agent descriptor
  ServiceTypeEnum                     service_type = 3 ; // Service type
  string                              description  = 4 ; // Textual service description
  ExecutionModeEnum                   mode         = 5 ; // Service execution mode
  repeated ServiceFacilitiesEnum      facilities   = 6 ; // Service runtime facilities
  google.protobuf.Struct              config       = 7 ; // Service runtime configuration
  repeated string                     endpoints    = 8 ; // ZMQ endpoints used by service, if any
  repeated google.protobuf.Any        supplement   = 9 ; // Additional information about service
}

message ReplyRunningServices {
  repeated RunningService services = 1 ;
}

// INTERFACE_PROVIDERS Message Data

message RequestInterfaceProviders {
  bytes interface_uid = 1 ; // [required] Interface ID (uuid5 - SHA1, namespace OID)
}

message ReplyInterfaceProviders {
  repeated bytes agent_uids = 1 ; // Agent ID (uuid5 - SHA1, namespace OID)
}

// START_SERVICE Message Data

message RequestStartService {
  bytes                  agent_uid     = 1 ; // [required] Agent ID (uuid5 - SHA1, namespace OID)
  google.protobuf.Struct config        = 2 ; // [required] Service runtime configuration
  string                 name          = 3 ; // Name for the service instance
  uint32                 timeout       = 4 ; // Timeout in milliseconds, zero means default timeout
  bool                   singleton     = 5 ; // False: start new instance of teh service
                                             // True: do not start new instance if one is already running
}

message ReplyStartService {
  RunningService  service = 1 ; // Information about running service
}

// STOP_SERVICE Message Data

message RequestStopService {
  bytes  peer_uid = 1 ; // [required] Peer ID assigned to service (uuid1)
  uint32 timeout  = 2 ; // Timeout in milliseconds, zero means default timeout
  bool   forced   = 3 ; // True to kill the service if it does not stop in time
}

message ReplyStopService {
  firebird.butler.StateEnum result = 1 ; // STOPPED (SUSPENDED) or TERMINATED (ABORTED)
}

