2021-05-20 19:35:03 +00:00
|
|
|
package policy
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-10-27 10:00:36 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2021-05-20 19:35:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
filter struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Key string `json:"key,omitempty"`
|
|
|
|
Op string `json:"op,omitempty"`
|
|
|
|
Value string `json:"value,omitempty"`
|
|
|
|
Filters []filter `json:"filters,omitempty"`
|
|
|
|
}
|
|
|
|
replica struct {
|
|
|
|
Count uint32 `json:"count"`
|
|
|
|
Selector string `json:"selector,omitempty"`
|
|
|
|
}
|
|
|
|
selector struct {
|
|
|
|
Count uint32 `json:"count"`
|
|
|
|
Attribute string `json:"attribute"`
|
|
|
|
Filter string `json:"filter,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
Clause string `json:"clause,omitempty"`
|
|
|
|
}
|
|
|
|
placement struct {
|
|
|
|
Replicas []replica `json:"replicas"`
|
|
|
|
CBF uint32 `json:"container_backup_factor,omitempty"`
|
|
|
|
Selectors []selector `json:"selectors,omitempty"`
|
|
|
|
Filters []filter `json:"filters,omitempty"`
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
// ToJSON converts placement policy to JSON.
|
|
|
|
func ToJSON(np *netmap.PlacementPolicy) ([]byte, error) {
|
|
|
|
p := new(placement)
|
2021-09-13 08:04:51 +00:00
|
|
|
p.CBF = np.ContainerBackupFactor()
|
|
|
|
p.Filters = make([]filter, len(np.Filters()))
|
|
|
|
for i, f := range np.Filters() {
|
2022-03-11 09:46:21 +00:00
|
|
|
p.Filters[i].fromNetmap(&f)
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
2021-09-13 08:04:51 +00:00
|
|
|
p.Selectors = make([]selector, len(np.Selectors()))
|
|
|
|
for i, s := range np.Selectors() {
|
2022-03-11 09:46:21 +00:00
|
|
|
p.Selectors[i].fromNetmap(&s)
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
2021-09-13 08:04:51 +00:00
|
|
|
p.Replicas = make([]replica, len(np.Replicas()))
|
|
|
|
for i, r := range np.Replicas() {
|
2022-03-11 09:46:21 +00:00
|
|
|
p.Replicas[i].fromNetmap(&r)
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
|
|
|
return json.Marshal(p)
|
|
|
|
}
|
|
|
|
|
2021-05-20 21:01:28 +00:00
|
|
|
// FromJSON creates placement policy from JSON.
|
2021-05-20 19:35:03 +00:00
|
|
|
func FromJSON(data []byte) (*netmap.PlacementPolicy, error) {
|
|
|
|
p := new(placement)
|
|
|
|
if err := json.Unmarshal(data, p); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:46:21 +00:00
|
|
|
rs := make([]netmap.Replica, len(p.Replicas))
|
2021-05-20 19:35:03 +00:00
|
|
|
for i := range p.Replicas {
|
2022-03-11 09:46:21 +00:00
|
|
|
rs[i] = *p.Replicas[i].toNetmap()
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 09:46:21 +00:00
|
|
|
var fs []netmap.Filter
|
2021-05-20 19:35:03 +00:00
|
|
|
if len(p.Filters) != 0 {
|
2022-03-11 09:46:21 +00:00
|
|
|
fs = make([]netmap.Filter, len(p.Filters))
|
2021-05-20 19:35:03 +00:00
|
|
|
for i := range p.Filters {
|
|
|
|
f, err := p.Filters[i].toNetmap()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-11 09:46:21 +00:00
|
|
|
fs[i] = *f
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:46:21 +00:00
|
|
|
var ss []netmap.Selector
|
2021-05-20 19:35:03 +00:00
|
|
|
if len(p.Selectors) != 0 {
|
2022-03-11 09:46:21 +00:00
|
|
|
ss = make([]netmap.Selector, len(p.Selectors))
|
2021-05-20 19:35:03 +00:00
|
|
|
for i := range p.Selectors {
|
|
|
|
s, err := p.Selectors[i].toNetmap()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-11 09:46:21 +00:00
|
|
|
ss[i] = *s
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pp := new(netmap.PlacementPolicy)
|
2021-09-13 08:04:51 +00:00
|
|
|
pp.SetReplicas(rs...)
|
2021-05-20 19:35:03 +00:00
|
|
|
pp.SetContainerBackupFactor(p.CBF)
|
2021-09-13 08:04:51 +00:00
|
|
|
pp.SetFilters(fs...)
|
|
|
|
pp.SetSelectors(ss...)
|
2021-05-20 19:35:03 +00:00
|
|
|
return pp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *replica) toNetmap() *netmap.Replica {
|
|
|
|
nr := new(netmap.Replica)
|
|
|
|
nr.SetCount(r.Count)
|
|
|
|
nr.SetSelector(r.Selector)
|
|
|
|
return nr
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *replica) fromNetmap(nr *netmap.Replica) {
|
2021-09-13 08:04:51 +00:00
|
|
|
r.Count = nr.Count()
|
|
|
|
r.Selector = nr.Selector()
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filter) toNetmap() (*netmap.Filter, error) {
|
|
|
|
var op netmap.Operation
|
|
|
|
switch strings.ToUpper(f.Op) {
|
|
|
|
case "EQ":
|
2021-09-13 08:04:51 +00:00
|
|
|
op = netmap.OpEQ
|
2021-05-20 19:35:03 +00:00
|
|
|
case "NE":
|
2021-09-13 08:04:51 +00:00
|
|
|
op = netmap.OpNE
|
2021-05-20 19:35:03 +00:00
|
|
|
case "GT":
|
2021-09-13 08:04:51 +00:00
|
|
|
op = netmap.OpGT
|
2021-05-20 19:35:03 +00:00
|
|
|
case "GE":
|
2021-09-13 08:04:51 +00:00
|
|
|
op = netmap.OpGE
|
2021-05-20 19:35:03 +00:00
|
|
|
case "LT":
|
2021-09-13 08:04:51 +00:00
|
|
|
op = netmap.OpLT
|
2021-05-20 19:35:03 +00:00
|
|
|
case "LE":
|
2021-09-13 08:04:51 +00:00
|
|
|
op = netmap.OpLE
|
2021-05-20 19:35:03 +00:00
|
|
|
case "AND":
|
2021-09-13 08:04:51 +00:00
|
|
|
op = netmap.OpAND
|
2021-05-20 19:35:03 +00:00
|
|
|
case "OR":
|
2021-09-13 08:04:51 +00:00
|
|
|
op = netmap.OpOR
|
2021-05-20 19:35:03 +00:00
|
|
|
case "":
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("%w: '%s'", ErrUnknownOp, f.Op)
|
|
|
|
}
|
|
|
|
|
2022-03-11 09:46:21 +00:00
|
|
|
var fs []netmap.Filter
|
2021-05-20 19:35:03 +00:00
|
|
|
if len(f.Filters) != 0 {
|
2022-03-11 09:46:21 +00:00
|
|
|
fs = make([]netmap.Filter, len(f.Filters))
|
2021-05-20 19:35:03 +00:00
|
|
|
for i := range f.Filters {
|
|
|
|
var err error
|
2022-03-11 09:46:21 +00:00
|
|
|
fsp, err := f.Filters[i].toNetmap()
|
2021-05-20 19:35:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-03-11 09:46:21 +00:00
|
|
|
fs[i] = *fsp
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nf := new(netmap.Filter)
|
2021-09-13 08:04:51 +00:00
|
|
|
nf.SetInnerFilters(fs...)
|
|
|
|
nf.SetOperation(op)
|
2021-05-20 19:35:03 +00:00
|
|
|
nf.SetName(f.Name)
|
|
|
|
nf.SetValue(f.Value)
|
|
|
|
nf.SetKey(f.Key)
|
|
|
|
return nf, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *filter) fromNetmap(nf *netmap.Filter) {
|
2021-09-13 08:04:51 +00:00
|
|
|
f.Name = nf.Name()
|
|
|
|
f.Key = nf.Key()
|
|
|
|
f.Value = nf.Value()
|
|
|
|
switch nf.Operation() {
|
|
|
|
case netmap.OpEQ:
|
2021-05-20 19:35:03 +00:00
|
|
|
f.Op = "EQ"
|
2021-09-13 08:04:51 +00:00
|
|
|
case netmap.OpNE:
|
2021-05-20 19:35:03 +00:00
|
|
|
f.Op = "NE"
|
2021-09-13 08:04:51 +00:00
|
|
|
case netmap.OpGT:
|
2021-05-20 19:35:03 +00:00
|
|
|
f.Op = "GT"
|
2021-09-13 08:04:51 +00:00
|
|
|
case netmap.OpGE:
|
2021-05-20 19:35:03 +00:00
|
|
|
f.Op = "GE"
|
2021-09-13 08:04:51 +00:00
|
|
|
case netmap.OpLT:
|
2021-05-20 19:35:03 +00:00
|
|
|
f.Op = "LT"
|
2021-09-13 08:04:51 +00:00
|
|
|
case netmap.OpLE:
|
2021-05-20 19:35:03 +00:00
|
|
|
f.Op = "LE"
|
2021-09-13 08:04:51 +00:00
|
|
|
case netmap.OpAND:
|
2021-05-20 19:35:03 +00:00
|
|
|
f.Op = "AND"
|
2021-09-13 08:04:51 +00:00
|
|
|
case netmap.OpOR:
|
2021-05-20 19:35:03 +00:00
|
|
|
f.Op = "OR"
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
2021-09-13 08:04:51 +00:00
|
|
|
if nf.InnerFilters() != nil {
|
|
|
|
f.Filters = make([]filter, len(nf.InnerFilters()))
|
|
|
|
for i, sf := range nf.InnerFilters() {
|
2022-03-11 09:46:21 +00:00
|
|
|
f.Filters[i].fromNetmap(&sf)
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *selector) toNetmap() (*netmap.Selector, error) {
|
|
|
|
var c netmap.Clause
|
|
|
|
switch strings.ToUpper(s.Clause) {
|
|
|
|
case "SAME":
|
2021-09-13 08:04:51 +00:00
|
|
|
c = netmap.ClauseSame
|
2021-05-20 19:35:03 +00:00
|
|
|
case "DISTINCT":
|
2021-09-13 08:04:51 +00:00
|
|
|
c = netmap.ClauseDistinct
|
2021-05-20 19:35:03 +00:00
|
|
|
case "":
|
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("%w: '%s'", ErrUnknownClause, s.Clause)
|
|
|
|
}
|
|
|
|
ns := new(netmap.Selector)
|
|
|
|
ns.SetName(s.Name)
|
|
|
|
ns.SetAttribute(s.Attribute)
|
|
|
|
ns.SetCount(s.Count)
|
|
|
|
ns.SetClause(c)
|
|
|
|
ns.SetFilter(s.Filter)
|
|
|
|
return ns, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *selector) fromNetmap(ns *netmap.Selector) {
|
2021-09-13 08:04:51 +00:00
|
|
|
s.Name = ns.Name()
|
|
|
|
s.Filter = ns.Filter()
|
|
|
|
s.Count = ns.Count()
|
|
|
|
s.Attribute = ns.Attribute()
|
|
|
|
switch ns.Clause() {
|
|
|
|
case netmap.ClauseSame:
|
2021-05-20 19:35:03 +00:00
|
|
|
s.Clause = "same"
|
2021-09-13 08:04:51 +00:00
|
|
|
case netmap.ClauseDistinct:
|
2021-05-20 19:35:03 +00:00
|
|
|
s.Clause = "distinct"
|
|
|
|
default:
|
|
|
|
// do nothing
|
|
|
|
}
|
2021-09-13 08:04:51 +00:00
|
|
|
s.Name = ns.Name()
|
2021-05-20 19:35:03 +00:00
|
|
|
}
|