syntax = "proto3";
option go_package = "./pb";

package pb;

import "models/model_node.proto";
import "models/model_node_login.proto";
import "models/model_node_install_status.proto";
import "models/model_dns_route.proto";
import "models/model_size_capacity.proto";
import "models/rpc_messages.proto";

service NodeService {
	// 创建节点
	rpc createNode (CreateNodeRequest) returns (CreateNodeResponse);

	// 注册集群节点
	rpc registerClusterNode (RegisterClusterNodeRequest) returns (RegisterClusterNodeResponse);

	// 所有可用的节点数量
	rpc countAllEnabledNodes (CountAllEnabledNodesRequest) returns (RPCCountResponse);

	// 计算匹配的节点数量
	rpc countAllEnabledNodesMatch (CountAllEnabledNodesMatchRequest) returns (RPCCountResponse);

	// 列出单页节点
	rpc listEnabledNodesMatch (ListEnabledNodesMatchRequest) returns (ListEnabledNodesMatchResponse);

	// 根据集群查找所有节点
	rpc findAllEnabledNodesWithNodeClusterId (FindAllEnabledNodesWithNodeClusterIdRequest) returns (FindAllEnabledNodesWithNodeClusterIdResponse);

	// 删除节点
	rpc deleteNode (DeleteNodeRequest) returns (RPCSuccess);

	// 从集群中删除节点
	rpc deleteNodeFromNodeCluster (DeleteNodeFromNodeClusterRequest) returns (RPCSuccess);

	// 修改节点
	rpc updateNode (UpdateNodeRequest) returns (RPCSuccess);

	// 获取单个节点信息
	rpc findEnabledNode (FindEnabledNodeRequest) returns (FindEnabledNodeResponse);

	// 获取单个节点基本信息
	rpc findEnabledBasicNode (FindEnabledBasicNodeRequest) returns (FindEnabledBasicNodeResponse);

	// 获取当前节点配置
	rpc findCurrentNodeConfig (FindCurrentNodeConfigRequest) returns (FindCurrentNodeConfigResponse);

	// 节点stream
	rpc nodeStream (stream NodeStreamMessage) returns (stream NodeStreamMessage);

	// 向节点发送命令
	rpc sendCommandToNode (NodeStreamMessage) returns (NodeStreamMessage);

	// 更新节点状态
	rpc updateNodeStatus (UpdateNodeStatusRequest) returns (RPCSuccess);

	// 修改节点安装状态
	rpc updateNodeIsInstalled (UpdateNodeIsInstalledRequest) returns (RPCSuccess);

	// 安装节点
	rpc installNode (InstallNodeRequest) returns (InstallNodeResponse);

	// 升级节点
	rpc upgradeNode (UpgradeNodeRequest) returns (UpgradeNodeResponse);

	// 启动节点
	rpc startNode (StartNodeRequest) returns (StartNodeResponse);

	// 停止节点
	rpc stopNode (StopNodeRequest) returns (StopNodeResponse);

	// 更改节点连接的API节点信息
	rpc updateNodeConnectedAPINodes (UpdateNodeConnectedAPINodesRequest) returns (RPCSuccess);

	// 计算使用某个认证的节点数量
	rpc countAllEnabledNodesWithNodeGrantId (CountAllEnabledNodesWithNodeGrantIdRequest) returns (RPCCountResponse);

	// 查找使用某个认证的所有节点
	rpc findAllEnabledNodesWithNodeGrantId (FindAllEnabledNodesWithNodeGrantIdRequest) returns (FindAllEnabledNodesWithNodeGrantIdResponse);

	// 计算没有安装的节点数量
	rpc countAllNotInstalledNodesWithNodeClusterId (CountAllNotInstalledNodesWithNodeClusterIdRequest) returns (RPCCountResponse);

	// 列出所有未安装的节点
	rpc findAllNotInstalledNodesWithNodeClusterId (FindAllNotInstalledNodesWithNodeClusterIdRequest) returns (FindAllNotInstalledNodesWithNodeClusterIdResponse);

	// 计算需要升级的节点数量
	rpc countAllUpgradeNodesWithNodeClusterId (CountAllUpgradeNodesWithNodeClusterIdRequest) returns (RPCCountResponse);

	// 列出所有需要升级的节点
	rpc findAllUpgradeNodesWithNodeClusterId (FindAllUpgradeNodesWithNodeClusterIdRequest) returns (FindAllUpgradeNodesWithNodeClusterIdResponse);

	// 读取节点安装状态
	rpc findNodeInstallStatus (FindNodeInstallStatusRequest) returns (FindNodeInstallStatusResponse);

	// 修改节点登录信息
	rpc updateNodeLogin (UpdateNodeLoginRequest) returns (RPCSuccess);

	// 计算某个节点分组内的节点数量
	rpc countAllEnabledNodesWithNodeGroupId (CountAllEnabledNodesWithNodeGroupIdRequest) returns (RPCCountResponse);

	// 取得某个集群下的所有节点DNS信息
	rpc findAllEnabledNodesDNSWithNodeClusterId (FindAllEnabledNodesDNSWithNodeClusterIdRequest) returns (FindAllEnabledNodesDNSWithNodeClusterIdResponse);

	// 查找单个节点的域名解析信息
	rpc findEnabledNodeDNS (FindEnabledNodeDNSRequest) returns (FindEnabledNodeDNSResponse);

	// 修改节点的DNS解析信息
	rpc updateNodeDNS (UpdateNodeDNSRequest) returns (RPCSuccess);

	// 计算某个区域下的节点数量
	rpc countAllEnabledNodesWithNodeRegionId (CountAllEnabledNodesWithNodeRegionIdRequest) returns (RPCCountResponse);

	// 根据一组ID获取节点信息
	rpc findEnabledNodesWithIds (FindEnabledNodesWithIdsRequest) returns (FindEnabledNodesWithIdsResponse);

	// 检查新版本
	rpc checkNodeLatestVersion (CheckNodeLatestVersionRequest) returns (CheckNodeLatestVersionResponse);

	// 设置节点上线状态
	rpc updateNodeUp (UpdateNodeUpRequest) returns (RPCSuccess);

	// 下载最新边缘节点安装文件
	rpc downloadNodeInstallationFile (DownloadNodeInstallationFileRequest) returns (DownloadNodeInstallationFileResponse);

	// 修改节点系统信息
	rpc updateNodeSystem(UpdateNodeSystemRequest) returns (RPCSuccess);

	// 修改节点缓存设置
	rpc updateNodeCache(UpdateNodeCacheRequest) returns (RPCSuccess);
}

// 创建节点
message CreateNodeRequest {
	string name = 1;
	int64 nodeClusterId = 2;
	NodeLogin nodeLogin = 3;
	int64 nodeGroupId = 4;
	int64 dnsDomainId = 5 [deprecated = true];
	repeated string dnsRoutes = 6;
	int64 nodeRegionId = 7;
}

message CreateNodeResponse {
	int64 nodeId = 1;
}

// 注册集群节点
message RegisterClusterNodeRequest {
	string name = 1;
}

message RegisterClusterNodeResponse {
	string uniqueId = 1;
	string secret = 2;
	repeated string endpoints = 3;
}

// 所有可用的节点数量
message CountAllEnabledNodesRequest {

}

// 列出单页节点
message ListEnabledNodesMatchRequest {
	int64 offset = 1;
	int64 size = 2;
	int64 nodeClusterId = 3;
	int32 installState = 4;
	int32 activeState = 5;
	string keyword = 6;
	int64 nodeGroupId = 7;
	int64 nodeRegionId = 8;

	bool cpuAsc = 20;
	bool cpuDesc = 21;
	bool memoryAsc = 22;
	bool memoryDesc = 23;
	bool trafficInAsc = 24;
	bool trafficInDesc = 25;
	bool trafficOutAsc = 26;
	bool trafficOutDesc = 27;
}

message ListEnabledNodesMatchResponse {
	repeated Node nodes = 1;
}

// 根据集群查找所有节点
message FindAllEnabledNodesWithNodeClusterIdRequest {
	int64 nodeClusterId = 1;
}

message FindAllEnabledNodesWithNodeClusterIdResponse {
	repeated Node nodes = 1;
}

// 删除节点
message DeleteNodeRequest {
	int64 nodeId = 1;
}

// 从集群中删除节点
message DeleteNodeFromNodeClusterRequest {
	int64 nodeId = 1;
	int64 nodeClusterId = 2;
}

// 修改节点
message UpdateNodeRequest {
	int64 nodeId = 1;
	string name = 2;
	int64 nodeClusterId = 3;
	repeated int64 secondaryNodeClusterIds = 13;
	bool isOn = 6;
	int64 nodeGroupId = 7;
	int64 nodeRegionId = 10;
}

// 获取单个节点信息
message FindEnabledNodeRequest {
	int64 nodeId = 1;
}

message FindEnabledNodeResponse {
	Node node = 1;
}

// 获取单个节点基本信息
message FindEnabledBasicNodeRequest {
	int64 nodeId = 1;
}

message FindEnabledBasicNodeResponse {
	BasicNode node = 1;
}

// 组合单个节点配置
message FindCurrentNodeConfigRequest {
	// 由于登录信息中已经包含了节点信息，所以这里不需要nodeId
	int64 version = 1;
}

message FindCurrentNodeConfigResponse {
	bytes nodeJSON = 1;
	bool isChanged = 2;
}

// 节点stream
message NodeStreamMessage {
	int64 nodeId = 1;
	int64 requestId = 2;
	int32 timeoutSeconds = 3;
	string code = 4;
	bytes dataJSON = 5;
	bool isOk = 6;
	string message = 7;
}

// 更新节点状态
message UpdateNodeStatusRequest {
	int64 nodeId = 1;
	bytes statusJSON = 2;
}

// 计算匹配的节点数量
message CountAllEnabledNodesMatchRequest {
	int64 nodeClusterId = 1;
	int32 installState = 2;
	int32 activeState = 3;
	string keyword = 4;
	int64 nodeGroupId = 5;
	int64 nodeRegionId = 6;
}

// 修改节点安装状态
message UpdateNodeIsInstalledRequest {
	int64 nodeId = 1;
	bool isInstalled = 2;
}

// 安装节点
message InstallNodeRequest {
	int64 nodeId = 1;
}

message InstallNodeResponse {

}

// 升级节点
message UpgradeNodeRequest {
	int64 nodeId = 1;
}

message UpgradeNodeResponse {
}

// 启动节点
message StartNodeRequest {
	int64 nodeId = 1;
}

message StartNodeResponse {
	bool isOk = 1;
	string error = 2;
}

// 停止节点
message StopNodeRequest {
	int64 nodeId = 1;
}

message StopNodeResponse {
	bool isOk = 1;
	string error = 2;
}

// 更改节点连接的API节点信息
message UpdateNodeConnectedAPINodesRequest {
	repeated int64 apiNodeIds = 1;
}

// 计算使用某个认证的节点数量
message CountAllEnabledNodesWithNodeGrantIdRequest {
	int64 nodeGrantId = 1;
}

// 查找使用某个认证的所有节点
message FindAllEnabledNodesWithNodeGrantIdRequest {
	int64 nodeGrantId = 1;
}

message FindAllEnabledNodesWithNodeGrantIdResponse {
	repeated Node nodes = 1;
}

// 列出所有未安装的节点
message FindAllNotInstalledNodesWithNodeClusterIdRequest {
	int64 nodeClusterId = 1;
}

message FindAllNotInstalledNodesWithNodeClusterIdResponse {
	repeated Node nodes = 1;
}

// 计算未安装的节点数量
message CountAllNotInstalledNodesWithNodeClusterIdRequest {
	int64 nodeClusterId = 1;
}

// 计算需要升级的节点数量
message CountAllUpgradeNodesWithNodeClusterIdRequest {
	int64 nodeClusterId = 1;
}

// 列出所有需要升级的节点
message FindAllUpgradeNodesWithNodeClusterIdRequest {
	int64 nodeClusterId = 1;
}

message FindAllUpgradeNodesWithNodeClusterIdResponse {
	repeated NodeUpgrade nodes = 1;

	message NodeUpgrade {
		Node node = 1;
		string os = 2;
		string arch = 3;
		string oldVersion = 4;
		string newVersion = 5;
	}
}

// 读取节点安装状态
message FindNodeInstallStatusRequest {
	int64 nodeId = 1;
}

message FindNodeInstallStatusResponse {
	NodeInstallStatus installStatus = 1;
}

// 修改节点登录信息
message UpdateNodeLoginRequest {
	int64 nodeId = 1;
	NodeLogin nodeLogin = 2;
}

// 计算某个节点分组内的节点数量
message CountAllEnabledNodesWithNodeGroupIdRequest {
	int64 nodeGroupId = 1;
}

// 取得某个集群下的所有节点
message FindAllEnabledNodesDNSWithNodeClusterIdRequest {
	int64 nodeClusterId = 1;
}

message FindAllEnabledNodesDNSWithNodeClusterIdResponse {
	repeated NodeDNSInfo nodes = 1;
}

message NodeDNSInfo {
	int64 id = 1;
	string name = 2;
	string ipAddr = 3;
	repeated DNSRoute routes = 4;
	int64 nodeClusterId = 5;
	int64 dnsDomainId = 6;
	string dnsDomainName = 7;
	string nodeClusterDNSName = 8;
}

// 查找单个节点的域名解析信息
message FindEnabledNodeDNSRequest {
	int64 nodeId = 1;
	int64 nodeClusterId = 2;
}

message FindEnabledNodeDNSResponse {
	NodeDNSInfo node = 1;
}

// 修改节点的DNS信息
message UpdateNodeDNSRequest {
	int64 nodeId = 1;
	string ipAddr = 2;
	int64 dnsDomainId = 3;
	repeated string routes = 4;
}

// 计算某个区域下的节点数量
message CountAllEnabledNodesWithNodeRegionIdRequest {
	int64 nodeRegionId = 1;
}

// 根据一组ID获取节点信息
message FindEnabledNodesWithIdsRequest {
	repeated int64 nodeIds = 1;
}

message FindEnabledNodesWithIdsResponse {
	repeated Node nodes = 1;
}

// 检查新版本
message CheckNodeLatestVersionRequest {
	string os = 1;
	string arch = 2;
	string currentVersion = 3;
}

message CheckNodeLatestVersionResponse {
	bool hasNewVersion = 1;
	string newVersion = 2;
}

// 设置节点上线状态
message UpdateNodeUpRequest {
	int64 nodeId = 1;
	bool isUp = 2;
}

// 下载最新边缘节点安装文件
message DownloadNodeInstallationFileRequest {
	string os = 1;
	string arch = 2;
	int64 chunkOffset = 3;
}

message DownloadNodeInstallationFileResponse {
	bytes chunkData = 1;
	string sum = 2; // 文件的md5sum
	int64 offset = 3;
	string version = 4;
	string filename = 5;
}

// 修改节点系统信息
message UpdateNodeSystemRequest {
	int64 nodeId = 1;
	int32 maxCPU = 2;
}

// 修改节点缓存设置
message UpdateNodeCacheRequest {
	int64 nodeId = 1;
	SizeCapacity maxCacheDiskCapacity = 2;
	SizeCapacity maxCacheMemoryCapacity = 3;
}
