2019-07-08 18:51:55 +03:00
|
|
|
package table
|
2019-06-10 01:09:38 +03:00
|
|
|
|
2019-06-18 20:33:05 +03:00
|
|
|
import (
|
|
|
|
"fmt"
|
2019-07-05 21:15:32 +03:00
|
|
|
"hash/fnv"
|
2019-06-18 20:33:05 +03:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/olekukonko/tablewriter"
|
|
|
|
)
|
2019-06-13 00:30:42 +03:00
|
|
|
|
2019-06-13 17:12:07 +03:00
|
|
|
var (
|
2019-07-08 18:16:50 +03:00
|
|
|
// DefaultLocalMetric is default route cost metric for the local network
|
2019-06-13 17:12:07 +03:00
|
|
|
DefaultLocalMetric = 1
|
2019-07-08 18:16:50 +03:00
|
|
|
// DefaultNetworkMetric is default route cost metric for the micro network
|
2019-06-13 17:12:07 +03:00
|
|
|
DefaultNetworkMetric = 10
|
|
|
|
)
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// RoutePolicy defines routing table policy
|
2019-06-19 23:22:14 +03:00
|
|
|
type RoutePolicy int
|
2019-06-10 01:09:38 +03:00
|
|
|
|
|
|
|
const (
|
2019-07-08 18:16:50 +03:00
|
|
|
// Insert inserts a new route if it does not already exist
|
|
|
|
Insert RoutePolicy = iota
|
|
|
|
// Override overrides the route if it already exists
|
|
|
|
Override
|
|
|
|
// Skip skips modifying the route if it already exists
|
|
|
|
Skip
|
2019-06-10 01:09:38 +03:00
|
|
|
)
|
|
|
|
|
2019-06-18 20:33:05 +03:00
|
|
|
// String returns human reprensentation of policy
|
2019-06-19 23:22:14 +03:00
|
|
|
func (p RoutePolicy) String() string {
|
2019-06-18 20:33:05 +03:00
|
|
|
switch p {
|
2019-07-08 18:16:50 +03:00
|
|
|
case Insert:
|
|
|
|
return "INSERT"
|
|
|
|
case Override:
|
|
|
|
return "OVERRIDE"
|
|
|
|
case Skip:
|
|
|
|
return "SKIP"
|
2019-06-18 20:33:05 +03:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 23:22:14 +03:00
|
|
|
// Route is network route
|
|
|
|
type Route struct {
|
|
|
|
// Destination is destination address
|
|
|
|
Destination string
|
2019-06-26 18:03:19 +03:00
|
|
|
// Gateway is route gateway
|
|
|
|
Gateway string
|
2019-07-08 18:16:50 +03:00
|
|
|
// Router is the router address
|
2019-06-26 18:03:19 +03:00
|
|
|
Router string
|
2019-07-08 18:16:50 +03:00
|
|
|
// Network is network address
|
2019-06-10 21:50:54 +03:00
|
|
|
Network string
|
2019-06-19 23:22:14 +03:00
|
|
|
// Metric is the route cost metric
|
2019-06-10 01:09:38 +03:00
|
|
|
Metric int
|
2019-06-19 23:22:14 +03:00
|
|
|
// Policy defines route policy
|
|
|
|
Policy RoutePolicy
|
2019-06-10 01:09:38 +03:00
|
|
|
}
|
2019-06-18 20:33:05 +03:00
|
|
|
|
2019-07-05 21:15:32 +03:00
|
|
|
// Hash returns route hash sum.
|
|
|
|
func (r *Route) Hash() uint64 {
|
|
|
|
h := fnv.New64()
|
|
|
|
h.Reset()
|
|
|
|
h.Write([]byte(r.Destination + r.Gateway + r.Network))
|
|
|
|
|
|
|
|
return h.Sum64()
|
|
|
|
}
|
|
|
|
|
2019-07-08 18:16:50 +03:00
|
|
|
// String returns human readable route
|
2019-07-05 21:15:32 +03:00
|
|
|
func (r Route) String() string {
|
2019-06-18 20:33:05 +03:00
|
|
|
// this will help us build routing table string
|
|
|
|
sb := &strings.Builder{}
|
|
|
|
|
|
|
|
// create nice table printing structure
|
|
|
|
table := tablewriter.NewWriter(sb)
|
2019-06-26 18:03:19 +03:00
|
|
|
table.SetHeader([]string{"Destination", "Gateway", "Router", "Network", "Metric"})
|
2019-06-18 20:33:05 +03:00
|
|
|
|
|
|
|
strRoute := []string{
|
2019-06-19 23:22:14 +03:00
|
|
|
r.Destination,
|
2019-06-26 18:03:19 +03:00
|
|
|
r.Gateway,
|
|
|
|
r.Router,
|
2019-06-19 23:22:14 +03:00
|
|
|
r.Network,
|
|
|
|
fmt.Sprintf("%d", r.Metric),
|
2019-06-18 20:33:05 +03:00
|
|
|
}
|
|
|
|
table.Append(strRoute)
|
|
|
|
|
|
|
|
// render table into sb
|
|
|
|
table.Render()
|
|
|
|
|
|
|
|
return sb.String()
|
|
|
|
}
|