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
    ERR_TIMEOUT             =17; // Script timed out
    ERR_ACTIVATOR_LOST      =18; // Connection to activator lost during transaction
    ERR_LOGIN_FAILED        =19; // Failed to log in
    ERR_CANCELLED           =20; // Script cancelled
    ERR_NOT_SUPPORTED       =21; // Not supported feature
    ERR_HTTP_ERROR          =22; // HTTP Error
    ERR_INVALID_SHARD       =23; // Shard mismatch
    ERR_SHARD_IS_DOWN       =24; // Shard is disabled
    ERR_OBJ_OVERLOAD        =25; // Too many scripts on object
}

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

message AccessProfile {
    message KWArg {
        required string key = 1;
        required string value = 2;
    }
    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;
    repeated KWArg        attrs          =11;
}
//
// 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 {
}

//
// Capabilities
//
message SetCapsRequest {
    required int32 max_scripts = 1;
}

message SetCapsResponse {
}

//
// 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;
    optional uint32        timeout = 4;
}

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 RefreshEventFilterRequest{
}

message RefreshEventFilterResponse{
}

message EventFilterRequest {
}

message EventFilterResponse {
    message EventIgnoreRuleItem {
        required string left_re  = 1;
        required string right_re = 2;
    }
    required int32  expire  = 1;
    repeated string sources = 2;
    repeated EventIgnoreRuleItem ignore_rules = 3;
}

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;
}

//
// Performance management data
//
message PMDataRequest {
    enum PMResultCode {
        PR_OK   = 0;
        PR_WARN = 1;
        PR_FAIL = 2;
    }
    message PMResult {
        required string       probe_name = 1;
        required string       probe_type = 2;
        required int32        timestamp  = 3;
        required string       service    = 4;
        required PMResultCode result     = 5;
        required string       message    = 6;
    }
    message PMData {
        required string name   = 1;
        required int32  timestamp = 2;
        required bool   is_null   = 3;
        required double value     = 4;
    }
    repeated PMResult result = 1;
    repeated PMData   data   = 2;
}

message PMDataResponse {}

//
// 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 set_caps         (SetCapsRequest)         returns (SetCapsResponse);
    rpc script           (ScriptRequest)          returns (ScriptResponse);
    rpc refresh_event_filter (RefreshEventFilterRequest) returns (RefreshEventFilterResponse);
    rpc event_filter     (EventFilterRequest)     returns (EventFilterResponse);
    rpc event            (EventRequest)           returns (EventResponse);
    rpc ping_check       (PingCheckRequest)       returns (PingCheckResponse);
    rpc pm_data          (PMDataRequest)          returns (PMDataResponse);
}
