2021-01-14 16:06:49 +00:00
|
|
|
package control
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2021-01-14 16:32:23 +00:00
|
|
|
netmapAPI "github.com/nspcc-dev/neofs-api-go/pkg/netmap"
|
2021-01-14 16:06:49 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/control"
|
2021-01-14 16:32:23 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2021-01-14 16:06:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// NetmapSnapshot reads network map snapshot from Netmap storage.
|
|
|
|
func (s *Server) NetmapSnapshot(ctx context.Context, req *control.NetmapSnapshotRequest) (*control.NetmapSnapshotResponse, error) {
|
2021-01-14 16:32:23 +00:00
|
|
|
// verify request
|
|
|
|
if err := s.isValidRequest(req); err != nil {
|
|
|
|
return nil, status.Error(codes.PermissionDenied, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
// get current epoch
|
|
|
|
epoch, err := s.netMapSrc.Epoch()
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
apiNetMap, err := s.netMapSrc.GetNetMapByEpoch(epoch)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Error(codes.NotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
nm := new(control.Netmap)
|
|
|
|
nm.SetEpoch(epoch)
|
|
|
|
nm.SetNodes(nodesFromAPI(apiNetMap.Nodes))
|
|
|
|
|
|
|
|
// create and fill response
|
|
|
|
resp := new(control.NetmapSnapshotResponse)
|
|
|
|
|
|
|
|
body := new(control.NetmapSnapshotResponse_Body)
|
|
|
|
resp.SetBody(body)
|
|
|
|
|
|
|
|
body.SetNetmap(nm)
|
|
|
|
|
|
|
|
// sign the response
|
|
|
|
if err := SignMessage(s.key, resp); err != nil {
|
|
|
|
return nil, status.Error(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func nodesFromAPI(apiNodes netmapAPI.Nodes) []*control.NodeInfo {
|
|
|
|
nodes := make([]*control.NodeInfo, 0, len(apiNodes))
|
|
|
|
|
|
|
|
for _, apiNode := range apiNodes {
|
|
|
|
node := new(control.NodeInfo)
|
|
|
|
node.SetPublicKey(apiNode.PublicKey())
|
|
|
|
node.SetAddress(apiNode.Address())
|
|
|
|
node.SetAttributes(attributesFromAPI(apiNode.Attributes()))
|
|
|
|
node.SetState(stateFromAPI(apiNode.State()))
|
|
|
|
|
|
|
|
nodes = append(nodes, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
|
|
|
|
2021-01-15 09:42:31 +00:00
|
|
|
func stateFromAPI(s netmapAPI.NodeState) control.NetmapStatus {
|
2021-01-14 16:32:23 +00:00
|
|
|
switch s {
|
|
|
|
default:
|
2021-01-15 09:42:31 +00:00
|
|
|
return control.NetmapStatus_STATUS_UNDEFINED
|
2021-01-14 16:32:23 +00:00
|
|
|
case netmapAPI.NodeStateOffline:
|
2021-01-15 09:42:31 +00:00
|
|
|
return control.NetmapStatus_OFFLINE
|
2021-01-14 16:32:23 +00:00
|
|
|
case netmapAPI.NodeStateOnline:
|
2021-01-15 09:42:31 +00:00
|
|
|
return control.NetmapStatus_ONLINE
|
2021-01-14 16:32:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func attributesFromAPI(apiAttrs []*netmapAPI.NodeAttribute) []*control.NodeInfo_Attribute {
|
|
|
|
attrs := make([]*control.NodeInfo_Attribute, 0, len(apiAttrs))
|
|
|
|
|
|
|
|
for _, apiAttr := range apiAttrs {
|
|
|
|
a := new(control.NodeInfo_Attribute)
|
|
|
|
a.SetKey(apiAttr.Key())
|
|
|
|
a.SetValue(apiAttr.Value())
|
|
|
|
|
2021-01-15 13:06:02 +00:00
|
|
|
apiParents := apiAttr.ParentKeys()
|
|
|
|
parents := make([]string, 0, len(apiParents))
|
|
|
|
|
|
|
|
for i := range apiParents {
|
|
|
|
parents = append(parents, apiParents[i])
|
|
|
|
}
|
|
|
|
|
|
|
|
a.SetParents(parents)
|
|
|
|
|
2021-01-14 16:32:23 +00:00
|
|
|
attrs = append(attrs, a)
|
|
|
|
}
|
|
|
|
|
|
|
|
return attrs
|
2021-01-14 16:06:49 +00:00
|
|
|
}
|