2019-06-09 23:09:38 +01:00
|
|
|
package router
|
|
|
|
|
2019-06-18 18:33:05 +01:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
2019-06-12 22:30:42 +01:00
|
|
|
|
2019-06-13 15:12:07 +01:00
|
|
|
var (
|
|
|
|
// DefaultLocalMetric is default route cost for local network
|
|
|
|
DefaultLocalMetric = 1
|
|
|
|
// DefaultNetworkMetric is default route cost for micro network
|
|
|
|
DefaultNetworkMetric = 10
|
|
|
|
)
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
// RoutePolicy defines routing table addition policy
|
|
|
|
type RoutePolicy int
|
2019-06-09 23:09:38 +01:00
|
|
|
|
|
|
|
const (
|
2019-06-13 23:28:47 +02:00
|
|
|
// OverrideIfExists overrides route if it already exists
|
2019-06-19 21:22:14 +01:00
|
|
|
OverrideIfExists RoutePolicy = iota
|
2019-06-13 23:28:47 +02:00
|
|
|
// IgnoreIfExists does not modify existing route
|
2019-06-12 22:30:42 +01:00
|
|
|
IgnoreIfExists
|
2019-06-09 23:09:38 +01:00
|
|
|
)
|
|
|
|
|
2019-06-18 18:33:05 +01:00
|
|
|
// String returns human reprensentation of policy
|
2019-06-19 21:22:14 +01:00
|
|
|
func (p RoutePolicy) String() string {
|
2019-06-18 18:33:05 +01:00
|
|
|
switch p {
|
|
|
|
case OverrideIfExists:
|
|
|
|
return "OVERRIDE"
|
|
|
|
case IgnoreIfExists:
|
|
|
|
return "IGNORE"
|
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
// Route is network route
|
|
|
|
type Route struct {
|
|
|
|
// Destination is destination address
|
|
|
|
Destination string
|
|
|
|
// Router is the network router
|
|
|
|
Router Router
|
|
|
|
// Network is micro network address
|
2019-06-10 19:50:54 +01:00
|
|
|
Network string
|
2019-06-19 21:22:14 +01:00
|
|
|
// Metric is the route cost metric
|
2019-06-09 23:09:38 +01:00
|
|
|
Metric int
|
2019-06-19 21:22:14 +01:00
|
|
|
// Policy defines route policy
|
|
|
|
Policy RoutePolicy
|
2019-06-09 23:09:38 +01:00
|
|
|
}
|
2019-06-18 18:33:05 +01:00
|
|
|
|
|
|
|
// String allows to print the route
|
2019-06-19 21:22:14 +01:00
|
|
|
func (r *Route) String() string {
|
2019-06-18 18:33:05 +01:00
|
|
|
// this will help us build routing table string
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
// create nice table printing structure
|
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-06-19 21:22:14 +01:00
|
|
|
table.SetHeader([]string{"Destination", "Router", "Network", "Metric"})
|
2019-06-18 18:33:05 +01:00
|
|
|
|
|
|
|
strRoute := []string{
|
2019-06-19 21:22:14 +01:00
|
|
|
r.Destination,
|
|
|
|
r.Router.Address(),
|
|
|
|
r.Network,
|
|
|
|
fmt.Sprintf("%d", r.Metric),
|
2019-06-18 18:33:05 +01:00
|
|
|
}
|
|
|
|
table.Append(strRoute)
|
|
|
|
|
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|