package sae;

//
// RPC Transport
//
message Message {
    required uint32   id       = 1; // Transaction id
    optional Request  request  = 2;
    optional Response response = 3;
    optional Error    error    = 4;
}

message Request {
    required string method             = 1;
    required bytes  serialized_request = 2; // encoded *Request
}

message Response {
    optional bytes  serialized_response = 1; // encoded *Response
}

message Error {
    required ErrorCode code = 1;
    optional string    text = 2;
}
//
//
//
enum ErrorCode {
    ERR_OK                  = 0; //
    ERR_INTERNAL            = 1; // Unhandled exception on server side during request processing
    ERR_INVALID_METHOD      = 2; // Invalid method name
    ERR_INVALID_TRANSACTION = 3; // Transaction does not exists
    ERR_TRANSACTION_EXISTS  = 4;
    ERR_UNKNOWN_ACTIVATOR   = 5;
    ERR_INVALID_PROFILE     = 6; // Invalid profile
    ERR_INVALID_SCHEME      = 7; // Invalid access scheme
    ERR_UNKNOWN_EVENT_SOURCE = 8;
    ERR_AUTH_FAILED         = 9; // Authentication failed
    ERR_AUTH_REQUIRED       =10; // Authentication required
    ERR_INVALID_UPGRADE     =11; // Invalid file requested for upgrade
    ERR_OVERLOAD            =12; // System limit exceeded. Retry operation after delay
    ERR_INVALID_SCRIPT      =13; // Invalid script
    ERR_SCRIPT_EXCEPTION    =14; // Script terminated with exception
    ERR_ACTIVATOR_NOT_AVAILABLE =15; // Activator not available now
    ERR_DOWN                =16; // Host is down
}

//
//
//
enum AccessScheme {
    TELNET = 0;
    SSH    = 1;
    HTTP   = 2;
}

message AccessProfile {
    required string       profile        = 1;
    required AccessScheme scheme         = 2;
    required string       address        = 3;
    optional uint32       port           = 4;
    optional string       user           = 5;
    optional string       password       = 6;
    optional string       super_password = 7;
    optional string       path           = 8;
    optional string       snmp_ro        = 9;
    optional string       snmp_rw        =10;
}
//
// Ping server
// SAE->Activator
// Activator->SAE
message PingRequest {
}

message PingResponse {
}
//
// Register activator at the SAE
// Activator->SAE
message RegisterRequest {
    required string name = 1;
}

message RegisterResponse {
    required string nonce = 1;
}
//
//
//
message AuthRequest {
    required string name   = 1;
    required string digest = 2;
}

message AuthResponse {
}

//
// Execute script
//
message ScriptRequest {
    message KWArg {
        required string key = 1;
        required string value = 2;
    }
    required AccessProfile access_profile = 1;
    required string        script = 2;
    repeated KWArg         kwargs = 3;
}

message ScriptResponse {
    required string result = 1;
}

//
// Software Upgrade
//
message FileChecksum {
    required string name   = 1; // Relative file name
    required string hash   = 2; // SHA1 hexdigest
}

message ManifestRequest {
}

message ManifestResponse {
    repeated FileChecksum files = 1;
}

message SoftwareUpgradeRequest {
    repeated string names = 1; // Relative file names
}

message FileCode {
    required string name = 1;
    required string code = 2;
}

message SoftwareUpgradeResponse {
    repeated FileCode codes = 1;
}
//
// Event processing
//
message EventFilterRequest {
}

message EventFilterResponse {
    required int32  expire  = 1;
    repeated string sources = 2;
}

message EventRequest {
    message EventBodyItem {
        required string key   = 1;
        required string value = 2;
    }
    required int32         timestamp = 1;
    required string        ip        = 2;
    repeated EventBodyItem body      = 3;
}

message EventResponse {}

//
// Ping hosts
//
message PingCheckRequest {
    repeated string addresses = 1;
}

message PingCheckResponse {
    repeated string reachable   = 1;
    repeated string unreachable = 2;
}

//
// RPC Services
//
service SAEService {
    rpc ping             (PingRequest)            returns (PingResponse);
    rpc register         (RegisterRequest)        returns (RegisterResponse);
    rpc auth             (AuthRequest)            returns (AuthResponse);
    rpc manifest         (ManifestRequest)        returns (ManifestResponse);
    rpc software_upgrade (SoftwareUpgradeRequest) returns (SoftwareUpgradeResponse);
    rpc script           (ScriptRequest)          returns (ScriptResponse);
    rpc event_filter     (EventFilterRequest)     returns (EventFilterResponse);
    rpc event            (EventRequest)           returns (EventResponse);
    rpc ping_check       (PingCheckRequest)       returns (PingCheckResponse);
}
