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-27 22:52:51 +01:00
|
|
|
// AddIfNotExist adds the route if it does not exist
|
2019-07-01 22:57:27 +01:00
|
|
|
AddIfNotExists RoutePolicy = iota
|
|
|
|
// OverrideIfExists overrides route if it already exists
|
|
|
|
OverrideIfExists
|
2019-06-26 16:03:19 +01:00
|
|
|
// IgnoreIfExists instructs to 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 {
|
2019-06-27 22:52:51 +01:00
|
|
|
case AddIfNotExists:
|
|
|
|
return "ADD_IF_NOT_EXISTS"
|
2019-07-01 23:19:35 +01:00
|
|
|
case OverrideIfExists:
|
|
|
|
return "OVERRIDE_IF_EXISTS"
|
2019-06-18 18:33:05 +01:00
|
|
|
case IgnoreIfExists:
|
2019-06-27 22:52:51 +01:00
|
|
|
return "IGNORE_IF_EXISTS"
|
2019-06-18 18:33:05 +01:00
|
|
|
default:
|
|
|
|
return "UNKNOWN"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-19 21:22:14 +01:00
|
|
|
// Route is network route
|
|
|
|
type Route struct {
|
|
|
|
// Destination is destination address
|
|
|
|
Destination string
|
2019-06-26 16:03:19 +01:00
|
|
|
// Gateway is route gateway
|
|
|
|
Gateway string
|
|
|
|
// Router is the network router address
|
|
|
|
Router string
|
2019-06-19 21:22:14 +01:00
|
|
|
// 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-26 16:03:19 +01:00
|
|
|
table.SetHeader([]string{"Destination", "Gateway", "Router", "Network", "Metric"})
|
2019-06-18 18:33:05 +01:00
|
|
|
|
|
|
|
strRoute := []string{
|
2019-06-19 21:22:14 +01:00
|
|
|
r.Destination,
|
2019-06-26 16:03:19 +01:00
|
|
|
r.Gateway,
|
|
|
|
r.Router,
|
2019-06-19 21:22:14 +01:00
|
|
|
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()
|
|
|
|
}
|