From d4f5c27d47e220cddf1fe50a580ef8052269a417 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Thu, 5 Nov 2020 14:24:52 +0300 Subject: [PATCH] [#189] sdk/netmap: Implement NodeState enum Signed-off-by: Leonard Lyubich --- pkg/netmap/node_info.go | 48 ++++++++++++++++++++++++++++++++++++ pkg/netmap/node_info_test.go | 23 +++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/pkg/netmap/node_info.go b/pkg/netmap/node_info.go index 981a930..8db1cae 100644 --- a/pkg/netmap/node_info.go +++ b/pkg/netmap/node_info.go @@ -23,6 +23,19 @@ type ( Nodes []*Node ) +// NodeState is an enumeration of various states of the NeoFS node. +type NodeState uint32 + +const ( + _ NodeState = iota + + // NodeStateOffline is network unavailable state. + NodeStateOffline + + // NodeStateOnline is an active state in the network. + NodeStateOnline +) + // Enumeration of well-known attributes. const ( CapacityAttr = "Capacity" @@ -98,3 +111,38 @@ func GetBucketWeight(ns Nodes, a aggregator, wf weightFunc) float64 { } return a.Compute() } + +// NodeStateFromV2 converts v2 NodeState to NodeState. +func NodeStateFromV2(s netmap.NodeState) NodeState { + switch s { + default: + return 0 + case netmap.Online: + return NodeStateOnline + case netmap.Offline: + return NodeStateOffline + } +} + +// ToV2 converts NodeState to v2 NodeState. +func (s NodeState) ToV2() netmap.NodeState { + switch s { + default: + return netmap.UnspecifiedState + case NodeStateOffline: + return netmap.Offline + case NodeStateOnline: + return netmap.Online + } +} + +func (s NodeState) String() string { + switch s { + default: + return "UNSPECIFIED" + case NodeStateOffline: + return "OFFLINE" + case NodeStateOnline: + return "ONLINE" + } +} diff --git a/pkg/netmap/node_info_test.go b/pkg/netmap/node_info_test.go index 0da6ad2..93d1568 100644 --- a/pkg/netmap/node_info_test.go +++ b/pkg/netmap/node_info_test.go @@ -19,3 +19,26 @@ func TestNode_NetworkAddress(t *testing.T) { require.Equal(t, addr, n.NetworkAddress()) } + +func TestNodeStateFromV2(t *testing.T) { + for _, item := range []struct { + s NodeState + sV2 netmap.NodeState + }{ + { + s: 0, + sV2: netmap.UnspecifiedState, + }, + { + s: NodeStateOnline, + sV2: netmap.Online, + }, + { + s: NodeStateOffline, + sV2: netmap.Offline, + }, + } { + require.Equal(t, item.s, NodeStateFromV2(item.sV2)) + require.Equal(t, item.sV2, item.s.ToV2()) + } +}