// Copyright 2018-2021 Cargill Incorporated
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://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.

syntax = "proto3";

message CircuitMessage {
    // The message type for the payload
    CircuitMessageType message_type = 1;

    // either a message defined below or another message envelope
    bytes payload = 2;
}

enum CircuitMessageType {
    UNSET_CIRCUIT_MESSAGE_TYPE = 0;

    // Circuit Message
    CIRCUIT_ERROR_MESSAGE = 1;
    NETWORK_ERROR_MESSAGE = 2;
    CIRCUIT_DIRECT_MESSAGE = 3;
    SERVICE_CONNECT_REQUEST = 4;
    SERVICE_CONNECT_RESPONSE = 5;
    SERVICE_DISCONNECT_REQUEST = 7;
    SERVICE_DISCONNECT_RESPONSE = 8;

    ADMIN_DIRECT_MESSAGE = 100;
}

message CircuitError {
    enum Error {
        UNSET_ERROR = 0;
        ERROR_CIRCUIT_DOES_NOT_EXIST = 1;
        ERROR_RECIPIENT_NOT_IN_CIRCUIT_ROSTER = 2;
        ERROR_SENDER_NOT_IN_CIRCUIT_ROSTER = 3;
        ERROR_RECIPIENT_NOT_IN_DIRECTORY = 4;
        ERROR_SENDER_NOT_IN_DIRECTORY = 5;
    }

    // id that correlates response to a request
    string correlation_id = 1;

    // service id of the sender of the message
    string service_id = 2;

    // circuit id of the sender of the message
    string circuit_name = 3;

    // enum of the network specific error that was encountered
    Error error = 4;

    // explanation of the error
    string error_message = 5;
}

message NetworkError {
    enum Error {
        UNSET_ERROR = 0;
        ERROR_COULD_NOT_DELIVER = 1;
        ERROR_QUEUE_FULL = 2;
    }

    // id that correlates response to a request
    string correlation_id = 1;

    // enum of the network specific error that was encountered
    Error error = 2;

    // explanation of the error
    string error_message = 3;
}

message CircuitDirectMessage {
    // the name of the circuit the message is meant for
    string circuit = 1;

    // id of the sender of the message
    string sender = 2;

    // id of recipient of the message
    string recipient = 3;

    // the request
    bytes payload = 4;

    // id used to correlate the response with this request
    string correlation_id = 5;
}

message AdminDirectMessage {
    // the name of the circuit the message is meant for
    string circuit = 1;

    // id of the sender of the message
    string sender = 2;

    // id of recipient of the message
    string recipient = 3;

    // the request
    bytes payload = 4;

    // id used to correlate the response with this request
    string correlation_id = 5;
}

message ServiceConnectRequest {
    // the name of the circuit the message is meant for
    string circuit = 1;

    // the unique id of the service that is connecting to the circuit
    string service_id = 2;

    // id used to correlate the response with this request
    string correlation_id = 3;
}

message ServiceConnectResponse {
    // the name of the circuit the message is meant for
    string circuit = 1;

    // the unique id of the service that is connecting to the circuit
    string service_id = 2;

    enum Status {
        UNSET_STATUS = 0;
        OK = 1;
        ERROR_CIRCUIT_DOES_NOT_EXIST = 2;
        ERROR_SERVICE_NOT_IN_CIRCUIT_REGISTRY = 3;
        ERROR_SERVICE_ALREADY_REGISTERED = 4;
        ERROR_NOT_AN_ALLOWED_NODE = 5;
        ERROR_QUEUE_FULL = 6;
    }

    Status status = 3;
    // explanation of the error (optional)
    string error_message = 4;

    // id used to correlate this response with the request
    string correlation_id = 5;
}

message ServiceDisconnectRequest {
    // the name of the circuit the message is meant for
    string circuit = 1;

    // the unique id of the service that is connecting to the circuit
    string service_id = 2;

    // id used to correlate the response with this request
    string correlation_id = 3;
}

message ServiceDisconnectResponse {
    // the name of the circuit the message is meant for
    string circuit = 1;

    // the unique id of the service that is connecting to the circuit
    string service_id = 2;

    enum Status {
        UNSET_ERROR = 0;
        OK = 1;
        ERROR_CIRCUIT_DOES_NOT_EXIST = 2;
        ERROR_SERVICE_NOT_IN_CIRCUIT_REGISTRY = 3;
        ERROR_SERVICE_NOT_REGISTERED = 4;
        ERROR_QUEUE_FULL = 5;
    }

    Status status = 3;

    // explanation of the error (optional)
    string error_message = 4;

    // id used to correlate this response with the request
    string correlation_id = 5;
}
