Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

topology: Reduce exposed types, remove unused code #3277

Merged
merged 2 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 27 additions & 46 deletions go/lib/topology/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ func (m IDAddrMap) GetById(id string) *TopoAddr {
return nil
}

type ServiceNames []string
type serviceNames []string

// GetRandom returns a random entry, or an error if the slice is empty.
func (s ServiceNames) GetRandom() (string, error) {
func (s serviceNames) GetRandom() (string, error) {
numServers := len(s)
if numServers == 0 {
return "", serrors.New("No names present")
Expand All @@ -53,17 +53,6 @@ func (s ServiceNames) GetRandom() (string, error) {
// IfInfoMap maps interface ids to the interface information.
type IfInfoMap map[common.IFIDType]IFInfo

// OfLinkType returns all interface info in the map that have the given link type.
func (iim IfInfoMap) OfLinkType(lt proto.LinkType) IfInfoMap {
infos := make(IfInfoMap)
for id, info := range iim {
if info.LinkType == lt {
infos[id] = info
}
}
return infos
}

// Topo is the main struct encompassing topology information for use in Go code.
// The first section contains metadata about the topology. All of these fields
// should be self-explanatory. The unit of TTL is seconds, with the zero value
Expand Down Expand Up @@ -98,24 +87,24 @@ type Topo struct {
IFInfoMap IfInfoMap

BS IDAddrMap
BSNames ServiceNames
BSNames serviceNames
CS IDAddrMap
CSNames ServiceNames
CSNames serviceNames
PS IDAddrMap
PSNames ServiceNames
PSNames serviceNames
SB IDAddrMap
SBNames ServiceNames
SBNames serviceNames
RS IDAddrMap
RSNames ServiceNames
RSNames serviceNames
DS IDAddrMap
DSNames ServiceNames
DSNames serviceNames
SIG IDAddrMap
SIGNames ServiceNames
SIGNames serviceNames

ZK map[int]*addr.AppAddr
}

// Create new empty Topo object, including all possible service maps etc.
// NewTopo creates new empty Topo object, including all possible service maps etc.
func NewTopo() *Topo {
return &Topo{
BR: make(map[string]BRInfo),
Expand All @@ -131,7 +120,7 @@ func NewTopo() *Topo {
}
}

// Convert a JSON-filled RawTopo to a Topo usabled by Go code.
// TopoFromRaw converts a JSON-filled RawTopo to a Topo usable by Go code.
func TopoFromRaw(raw *RawTopo) (*Topo, error) {
t := NewTopo()

Expand Down Expand Up @@ -295,7 +284,7 @@ func (t *Topo) Expiry() time.Time {
}

func (t *Topo) GetTopoAddr(id string, svc proto.ServiceType) (*TopoAddr, error) {
svcInfo, err := t.GetSvcInfo(svc)
svcInfo, err := t.getSvcInfo(svc)
if err != nil {
return nil, err
}
Expand All @@ -307,7 +296,7 @@ func (t *Topo) GetTopoAddr(id string, svc proto.ServiceType) (*TopoAddr, error)
}

func (t *Topo) GetAnyTopoAddr(svc proto.ServiceType) (*TopoAddr, error) {
svcInfo, err := t.GetSvcInfo(svc)
svcInfo, err := t.getSvcInfo(svc)
if err != nil {
return nil, err
}
Expand All @@ -319,54 +308,46 @@ func (t *Topo) GetAnyTopoAddr(svc proto.ServiceType) (*TopoAddr, error) {
}

func (t *Topo) GetAllTopoAddrs(svc proto.ServiceType) ([]TopoAddr, error) {
svcInfo, err := t.GetSvcInfo(svc)
svcInfo, err := t.getSvcInfo(svc)
if err != nil {
return nil, err
}
topoAddrs := svcInfo.GetAllTopoAddrs()
topoAddrs := svcInfo.getAllTopoAddrs()
if topoAddrs == nil {
return nil, serrors.New("Address not found")
}
return topoAddrs, nil
}

func (t *Topo) GetSvcInfo(svc proto.ServiceType) (*SVCInfo, error) {
func (t *Topo) getSvcInfo(svc proto.ServiceType) (*svcInfo, error) {
switch svc {
case proto.ServiceType_unset:
return nil, serrors.New("Service type unset")
case proto.ServiceType_bs:
return &SVCInfo{overlay: t.Overlay, names: t.BSNames, idTopoAddrMap: t.BS}, nil
return &svcInfo{overlay: t.Overlay, names: t.BSNames, idTopoAddrMap: t.BS}, nil
case proto.ServiceType_ps:
return &SVCInfo{overlay: t.Overlay, names: t.PSNames, idTopoAddrMap: t.PS}, nil
return &svcInfo{overlay: t.Overlay, names: t.PSNames, idTopoAddrMap: t.PS}, nil
case proto.ServiceType_cs:
return &SVCInfo{overlay: t.Overlay, names: t.CSNames, idTopoAddrMap: t.CS}, nil
return &svcInfo{overlay: t.Overlay, names: t.CSNames, idTopoAddrMap: t.CS}, nil
case proto.ServiceType_sb:
return &SVCInfo{overlay: t.Overlay, names: t.SBNames, idTopoAddrMap: t.SB}, nil
return &svcInfo{overlay: t.Overlay, names: t.SBNames, idTopoAddrMap: t.SB}, nil
case proto.ServiceType_sig:
return &SVCInfo{overlay: t.Overlay, names: t.SIGNames, idTopoAddrMap: t.SIG}, nil
return &svcInfo{overlay: t.Overlay, names: t.SIGNames, idTopoAddrMap: t.SIG}, nil
case proto.ServiceType_ds:
return &SVCInfo{overlay: t.Overlay, names: t.DSNames, idTopoAddrMap: t.DS}, nil
return &svcInfo{overlay: t.Overlay, names: t.DSNames, idTopoAddrMap: t.DS}, nil
default:
return nil, common.NewBasicError("Unsupported service type", nil, "type", svc)
}
}

// SVCInfo contains topology information for a single SCION service
type SVCInfo struct {
// svcInfo contains topology information for a single SCION service
type svcInfo struct {
overlay overlay.Type
names ServiceNames
names serviceNames
idTopoAddrMap IDAddrMap
}

func (svc *SVCInfo) GetAnyTopoAddr() *TopoAddr {
id, err := svc.names.GetRandom()
if err != nil {
return nil
}
return svc.idTopoAddrMap.GetById(id)
}

func (svc *SVCInfo) GetAllTopoAddrs() []TopoAddr {
func (svc *svcInfo) getAllTopoAddrs() []TopoAddr {
var topoAddrs []TopoAddr
for _, topoAddr := range svc.idTopoAddrMap {
topoAddrs = append(topoAddrs, topoAddr)
Expand Down Expand Up @@ -408,7 +389,7 @@ func (t *Topo) zkSvcFromRaw(zksvc map[int]*RawAddrPort) error {
return nil
}

// A list of AS-wide unique interface IDs for a router. These IDs are also used
// BRInfo is a list of AS-wide unique interface IDs for a router. These IDs are also used
// to point to the specific internal address clients should send their traffic
// to in order to use that interface, via the IFInfoMap member of the Topo
// struct.
Expand Down
45 changes: 0 additions & 45 deletions go/lib/topology/topology_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,51 +361,6 @@ func Test_IFInfoMap_COREAS(t *testing.T) {
}
}

func TestIfInfoMapOfLinkType(t *testing.T) {
Convey("Test OfLinkType function of IfInfoMap", t, func() {
ifInfo := make(IfInfoMap)
linkTypes := []proto.LinkType{
proto.LinkType_child,
proto.LinkType_core,
proto.LinkType_parent,
proto.LinkType_peer}
emptyMap := make(IfInfoMap)
Convey("Given an empty map all link types should return empty", func() {
for _, lt := range linkTypes {
SoMsg("No ifids expected", ifInfo.OfLinkType(lt), ShouldResemble, emptyMap)
}
})
Convey("Given a non empty map only ifids with the given linktype should be returned",
func() {
inputMap := IfInfoMap{
12: IFInfo{LinkType: proto.LinkType_child},
13: IFInfo{LinkType: proto.LinkType_child},
14: IFInfo{LinkType: proto.LinkType_peer},
}
expectedPeerMap := IfInfoMap{
14: inputMap[14],
}
expectedChildMap := IfInfoMap{
12: inputMap[12],
13: inputMap[13],
}
for _, lt := range linkTypes {
actual := inputMap.OfLinkType(lt)
var expected IfInfoMap
switch lt {
case proto.LinkType_child:
expected = expectedChildMap
case proto.LinkType_peer:
expected = expectedPeerMap
default:
expected = emptyMap
}
SoMsg("Expect ifIds", actual, ShouldResemble, expected)
}
})
})
}

var br_cases = []struct {
name string
intfids []common.IFIDType
Expand Down