diff options
author | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-07-29 00:43:52 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2016-07-29 00:43:52 +0900 |
commit | af98780240d247f4df352c364002d1d77e71f31e (patch) | |
tree | 96328d3ea91123062588447ac2b0557495e692fa | |
parent | 2a552a49b4991fcf765dbbbbd56619256dab96ac (diff) |
move grpc_server.go from server/ to api/
Now all gRPC code was moved to api/.
server/ isn't dependent on gRPC anymore.
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r-- | api/grpc_server.go (renamed from server/grpc_server.go) | 548 | ||||
-rw-r--r-- | docs/sources/lib.md | 3 | ||||
-rw-r--r-- | gobgpd/main.go | 5 |
3 files changed, 279 insertions, 277 deletions
diff --git a/server/grpc_server.go b/api/grpc_server.go index 03dc9c7b..69864831 100644 --- a/server/grpc_server.go +++ b/api/grpc_server.go @@ -13,14 +13,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -package server +package gobgpapi import ( "fmt" log "github.com/Sirupsen/logrus" - api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" + "github.com/osrg/gobgp/server" "github.com/osrg/gobgp/table" "golang.org/x/net/context" "google.golang.org/grpc" @@ -35,12 +35,12 @@ import ( ) type Server struct { - bgpServer *BgpServer + bgpServer *server.BgpServer grpcServer *grpc.Server hosts string } -func NewGrpcServer(b *BgpServer, hosts string) *Server { +func NewGrpcServer(b *server.BgpServer, hosts string) *Server { grpc.EnableTracing = false grpcServer := grpc.NewServer() server := &Server{ @@ -48,7 +48,7 @@ func NewGrpcServer(b *BgpServer, hosts string) *Server { grpcServer: grpcServer, hosts: hosts, } - api.RegisterGobgpApiServer(grpcServer, server) + RegisterGobgpApiServer(grpcServer, server) return server } @@ -84,13 +84,13 @@ func (s *Server) Serve() error { return nil } -func (s *Server) GetNeighbor(ctx context.Context, arg *api.GetNeighborRequest) (*api.GetNeighborResponse, error) { - toApi := func(pconf *config.Neighbor) *api.Peer { - prefixLimits := make([]*api.PrefixLimit, 0, len(pconf.AfiSafis)) +func (s *Server) GetNeighbor(ctx context.Context, arg *GetNeighborRequest) (*GetNeighborResponse, error) { + toApi := func(pconf *config.Neighbor) *Peer { + prefixLimits := make([]*PrefixLimit, 0, len(pconf.AfiSafis)) for _, family := range pconf.AfiSafis { if c := family.PrefixLimit.Config; c.MaxPrefixes > 0 { k, _ := bgp.GetRouteFamily(string(family.Config.AfiSafiName)) - prefixLimits = append(prefixLimits, &api.PrefixLimit{ + prefixLimits = append(prefixLimits, &PrefixLimit{ Family: uint32(k), MaxPrefixes: c.MaxPrefixes, ShutdownThresholdPct: uint32(c.ShutdownThresholdPct), @@ -100,8 +100,8 @@ func (s *Server) GetNeighbor(ctx context.Context, arg *api.GetNeighborRequest) ( timer := pconf.Timers s := pconf.State - return &api.Peer{ - Conf: &api.PeerConf{ + return &Peer{ + Conf: &PeerConf{ NeighborAddress: pconf.Config.NeighborAddress, Id: s.Description, PeerAs: pconf.Config.PeerAs, @@ -117,11 +117,11 @@ func (s *Server) GetNeighbor(ctx context.Context, arg *api.GetNeighborRequest) ( LocalCap: s.Capabilities.LocalList, PrefixLimits: prefixLimits, }, - Info: &api.PeerState{ + Info: &PeerState{ BgpState: bgp.FSMState(s.SessionState.ToInt()).String(), AdminState: s.AdminState, - Messages: &api.Messages{ - Received: &api.Message{ + Messages: &Messages{ + Received: &Message{ NOTIFICATION: s.Messages.Received.Notification, UPDATE: s.Messages.Received.Update, OPEN: s.Messages.Received.Open, @@ -130,7 +130,7 @@ func (s *Server) GetNeighbor(ctx context.Context, arg *api.GetNeighborRequest) ( DISCARDED: s.Messages.Received.Discarded, TOTAL: s.Messages.Received.Total, }, - Sent: &api.Message{ + Sent: &Message{ NOTIFICATION: s.Messages.Sent.Notification, UPDATE: s.Messages.Sent.Update, OPEN: s.Messages.Sent.Open, @@ -144,37 +144,37 @@ func (s *Server) GetNeighbor(ctx context.Context, arg *api.GetNeighborRequest) ( Accepted: s.AdjTable.Accepted, Advertised: s.AdjTable.Advertised, }, - Timers: &api.Timers{ - Config: &api.TimersConfig{ + Timers: &Timers{ + Config: &TimersConfig{ ConnectRetry: uint64(timer.Config.ConnectRetry), HoldTime: uint64(timer.Config.HoldTime), KeepaliveInterval: uint64(timer.Config.KeepaliveInterval), }, - State: &api.TimersState{ + State: &TimersState{ KeepaliveInterval: uint64(timer.State.KeepaliveInterval), NegotiatedHoldTime: uint64(timer.State.NegotiatedHoldTime), Uptime: uint64(timer.State.Uptime), Downtime: uint64(timer.State.Downtime), }, }, - RouteReflector: &api.RouteReflector{ + RouteReflector: &RouteReflector{ RouteReflectorClient: pconf.RouteReflector.Config.RouteReflectorClient, RouteReflectorClusterId: string(pconf.RouteReflector.Config.RouteReflectorClusterId), }, - RouteServer: &api.RouteServer{ + RouteServer: &RouteServer{ RouteServerClient: pconf.RouteServer.Config.RouteServerClient, }, } } - p := []*api.Peer{} + p := []*Peer{} for _, e := range s.bgpServer.GetNeighbor() { p = append(p, toApi(e)) } - return &api.GetNeighborResponse{Peers: p}, nil + return &GetNeighborResponse{Peers: p}, nil } -func toPathApi(id string, path *table.Path) *api.Path { +func toPathApi(id string, path *table.Path) *Path { nlri := path.GetNlri() n, _ := nlri.Serialize() family := uint32(bgp.AfiSafiToRouteFamily(nlri.AFI(), nlri.SAFI())) @@ -186,7 +186,7 @@ func toPathApi(id string, path *table.Path) *api.Path { } return ret }(path.GetPathAttrs()) - return &api.Path{ + return &Path{ Nlri: n, Pattrs: pattrs, Age: path.GetTimestamp().Unix(), @@ -202,19 +202,19 @@ func toPathApi(id string, path *table.Path) *api.Path { } } -func (s *Server) GetRib(ctx context.Context, arg *api.GetRibRequest) (*api.GetRibResponse, error) { - f := func() []*LookupPrefix { - l := make([]*LookupPrefix, 0, len(arg.Table.Destinations)) +func (s *Server) GetRib(ctx context.Context, arg *GetRibRequest) (*GetRibResponse, error) { + f := func() []*server.LookupPrefix { + l := make([]*server.LookupPrefix, 0, len(arg.Table.Destinations)) for _, p := range arg.Table.Destinations { - l = append(l, &LookupPrefix{ + l = append(l, &server.LookupPrefix{ Prefix: p.Prefix, - LookupOption: func() LookupOption { + LookupOption: func() server.LookupOption { if p.LongerPrefixes { - return LOOKUP_LONGER + return server.LOOKUP_LONGER } else if p.ShorterPrefixes { - return LOOKUP_SHORTER + return server.LOOKUP_SHORTER } - return LOOKUP_EXACT + return server.LOOKUP_EXACT }(), }) } @@ -228,30 +228,30 @@ func (s *Server) GetRib(ctx context.Context, arg *api.GetRibRequest) (*api.GetRi family := bgp.RouteFamily(arg.Table.Family) switch arg.Table.Type { - case api.Resource_LOCAL, api.Resource_GLOBAL: + case Resource_LOCAL, Resource_GLOBAL: id, r, err = s.bgpServer.GetRib(arg.Table.Name, family, f()) - case api.Resource_ADJ_IN: + case Resource_ADJ_IN: in = true fallthrough - case api.Resource_ADJ_OUT: + case Resource_ADJ_OUT: id, r, err = s.bgpServer.GetAdjRib(arg.Table.Name, family, in, f()) - case api.Resource_VRF: - id, r, err = s.bgpServer.GetVrfRib(arg.Table.Name, family, []*LookupPrefix{}) + case Resource_VRF: + id, r, err = s.bgpServer.GetVrfRib(arg.Table.Name, family, []*server.LookupPrefix{}) default: return nil, fmt.Errorf("unsupported resource type: %v", arg.Table.Type) } - dsts := make([]*api.Destination, 0, len(r)) + dsts := make([]*Destination, 0, len(r)) if err == nil { for k, v := range r { - dsts = append(dsts, &api.Destination{ + dsts = append(dsts, &Destination{ Prefix: k, - Paths: func(paths []*table.Path) []*api.Path { - l := make([]*api.Path, 0, len(v)) + Paths: func(paths []*table.Path) []*Path { + l := make([]*Path, 0, len(v)) for i, p := range paths { pp := toPathApi(id, p) switch arg.Table.Type { - case api.Resource_LOCAL, api.Resource_GLOBAL: + case Resource_LOCAL, Resource_GLOBAL: if i == 0 { pp.Best = true } @@ -263,23 +263,23 @@ func (s *Server) GetRib(ctx context.Context, arg *api.GetRibRequest) (*api.GetRi }) } } - return &api.GetRibResponse{Table: &api.Table{ + return &GetRibResponse{Table: &Table{ Type: arg.Table.Type, Family: arg.Table.Family, Destinations: dsts}, }, err } -func (s *Server) MonitorRib(arg *api.Table, stream api.GobgpApi_MonitorRibServer) error { - w, err := func() (*Watcher, error) { +func (s *Server) MonitorRib(arg *Table, stream GobgpApi_MonitorRibServer) error { + w, err := func() (*server.Watcher, error) { switch arg.Type { - case api.Resource_GLOBAL: - return s.bgpServer.Watch(WatchBestPath()), nil - case api.Resource_ADJ_IN: + case Resource_GLOBAL: + return s.bgpServer.Watch(server.WatchBestPath()), nil + case Resource_ADJ_IN: if arg.PostPolicy { - return s.bgpServer.Watch(WatchPostUpdate(false)), nil + return s.bgpServer.Watch(server.WatchPostUpdate(false)), nil } - return s.bgpServer.Watch(WatchUpdate(false)), nil + return s.bgpServer.Watch(server.WatchUpdate(false)), nil default: return nil, fmt.Errorf("unsupported resource type: %v", arg.Type) } @@ -292,7 +292,7 @@ func (s *Server) MonitorRib(arg *api.Table, stream api.GobgpApi_MonitorRibServer defer func() { w.Stop() }() sendPath := func(pathList []*table.Path) error { - dsts := make(map[string]*api.Destination) + dsts := make(map[string]*Destination) for _, path := range pathList { if path == nil { continue @@ -300,9 +300,9 @@ func (s *Server) MonitorRib(arg *api.Table, stream api.GobgpApi_MonitorRibServer if dst, y := dsts[path.GetNlri().String()]; y { dst.Paths = append(dst.Paths, toPathApi(table.GLOBAL_RIB_NAME, path)) } else { - dsts[path.GetNlri().String()] = &api.Destination{ + dsts[path.GetNlri().String()] = &Destination{ Prefix: path.GetNlri().String(), - Paths: []*api.Path{toPathApi(table.GLOBAL_RIB_NAME, path)}, + Paths: []*Path{toPathApi(table.GLOBAL_RIB_NAME, path)}, } } } @@ -317,7 +317,7 @@ func (s *Server) MonitorRib(arg *api.Table, stream api.GobgpApi_MonitorRibServer select { case ev := <-w.Event(): switch msg := ev.(type) { - case *WatchEventBestPath: + case *server.WatchEventBestPath: if err := sendPath(func() []*table.Path { if len(msg.MultiPathList) > 0 { l := make([]*table.Path, 0) @@ -331,7 +331,7 @@ func (s *Server) MonitorRib(arg *api.Table, stream api.GobgpApi_MonitorRibServer }()); err != nil { return err } - case *WatchEventUpdate: + case *server.WatchEventUpdate: if err := sendPath(msg.PathList); err != nil { return err } @@ -341,34 +341,34 @@ func (s *Server) MonitorRib(arg *api.Table, stream api.GobgpApi_MonitorRibServer }() } -func (s *Server) MonitorPeerState(arg *api.Arguments, stream api.GobgpApi_MonitorPeerStateServer) error { +func (s *Server) MonitorPeerState(arg *Arguments, stream GobgpApi_MonitorPeerStateServer) error { return func() error { - w := s.bgpServer.Watch(WatchPeerState(false)) + w := s.bgpServer.Watch(server.WatchPeerState(false)) defer func() { w.Stop() }() for { select { case ev := <-w.Event(): switch msg := ev.(type) { - case *WatchEventPeerState: + case *server.WatchEventPeerState: if len(arg.Name) > 0 && arg.Name != msg.PeerAddress.String() { continue } - if err := stream.Send(&api.Peer{ - Conf: &api.PeerConf{ + if err := stream.Send(&Peer{ + Conf: &PeerConf{ PeerAs: msg.PeerAS, LocalAs: msg.LocalAS, NeighborAddress: msg.PeerAddress.String(), Id: msg.PeerID.String(), }, - Info: &api.PeerState{ + Info: &PeerState{ PeerAs: msg.PeerAS, LocalAs: msg.LocalAS, NeighborAddress: msg.PeerAddress.String(), BgpState: msg.State.String(), AdminState: msg.AdminState.String(), }, - Transport: &api.Transport{ + Transport: &Transport{ LocalAddress: msg.LocalAddress.String(), LocalPort: uint32(msg.LocalPort), RemotePort: uint32(msg.PeerPort), @@ -382,11 +382,11 @@ func (s *Server) MonitorPeerState(arg *api.Arguments, stream api.GobgpApi_Monito }() } -func (s *Server) ResetNeighbor(ctx context.Context, arg *api.ResetNeighborRequest) (*api.ResetNeighborResponse, error) { - return &api.ResetNeighborResponse{}, s.bgpServer.ResetNeighbor(arg.Address) +func (s *Server) ResetNeighbor(ctx context.Context, arg *ResetNeighborRequest) (*ResetNeighborResponse, error) { + return &ResetNeighborResponse{}, s.bgpServer.ResetNeighbor(arg.Address) } -func (s *Server) SoftResetNeighbor(ctx context.Context, arg *api.SoftResetNeighborRequest) (*api.SoftResetNeighborResponse, error) { +func (s *Server) SoftResetNeighbor(ctx context.Context, arg *SoftResetNeighborRequest) (*SoftResetNeighborResponse, error) { var err error addr := arg.Address if addr == "all" { @@ -394,29 +394,29 @@ func (s *Server) SoftResetNeighbor(ctx context.Context, arg *api.SoftResetNeighb } family := bgp.RouteFamily(0) switch arg.Direction { - case api.SoftResetNeighborRequest_IN: + case SoftResetNeighborRequest_IN: err = s.bgpServer.SoftResetIn(addr, family) - case api.SoftResetNeighborRequest_OUT: + case SoftResetNeighborRequest_OUT: err = s.bgpServer.SoftResetOut(addr, family) default: err = s.bgpServer.SoftReset(addr, family) } - return &api.SoftResetNeighborResponse{}, err + return &SoftResetNeighborResponse{}, err } -func (s *Server) ShutdownNeighbor(ctx context.Context, arg *api.ShutdownNeighborRequest) (*api.ShutdownNeighborResponse, error) { - return &api.ShutdownNeighborResponse{}, s.bgpServer.ShutdownNeighbor(arg.Address) +func (s *Server) ShutdownNeighbor(ctx context.Context, arg *ShutdownNeighborRequest) (*ShutdownNeighborResponse, error) { + return &ShutdownNeighborResponse{}, s.bgpServer.ShutdownNeighbor(arg.Address) } -func (s *Server) EnableNeighbor(ctx context.Context, arg *api.EnableNeighborRequest) (*api.EnableNeighborResponse, error) { - return &api.EnableNeighborResponse{}, s.bgpServer.EnableNeighbor(arg.Address) +func (s *Server) EnableNeighbor(ctx context.Context, arg *EnableNeighborRequest) (*EnableNeighborResponse, error) { + return &EnableNeighborResponse{}, s.bgpServer.EnableNeighbor(arg.Address) } -func (s *Server) DisableNeighbor(ctx context.Context, arg *api.DisableNeighborRequest) (*api.DisableNeighborResponse, error) { - return &api.DisableNeighborResponse{}, s.bgpServer.DisableNeighbor(arg.Address) +func (s *Server) DisableNeighbor(ctx context.Context, arg *DisableNeighborRequest) (*DisableNeighborResponse, error) { + return &DisableNeighborResponse{}, s.bgpServer.DisableNeighbor(arg.Address) } -func (s *Server) api2PathList(resource api.Resource, ApiPathList []*api.Path) ([]*table.Path, error) { +func (s *Server) api2PathList(resource Resource, ApiPathList []*Path) ([]*table.Path, error) { var nlri bgp.AddrPrefixInterface var nexthop string var pi *table.PeerInfo @@ -485,7 +485,7 @@ func (s *Server) api2PathList(resource api.Resource, ApiPathList []*api.Path) ([ rf := bgp.AfiSafiToRouteFamily(nlri.AFI(), nlri.SAFI()) - if resource != api.Resource_VRF && rf == bgp.RF_IPv4_UC { + if resource != Resource_VRF && rf == bgp.RF_IPv4_UC { pattr = append(pattr, bgp.NewPathAttributeNextHop(nexthop)) } else { pattr = append(pattr, bgp.NewPathAttributeMpReachNLRI(nexthop, []bgp.AddrPrefixInterface{nlri})) @@ -501,42 +501,42 @@ func (s *Server) api2PathList(resource api.Resource, ApiPathList []*api.Path) ([ return pathList, nil } -func (s *Server) AddPath(ctx context.Context, arg *api.AddPathRequest) (*api.AddPathResponse, error) { - pathList, err := s.api2PathList(arg.Resource, []*api.Path{arg.Path}) +func (s *Server) AddPath(ctx context.Context, arg *AddPathRequest) (*AddPathResponse, error) { + pathList, err := s.api2PathList(arg.Resource, []*Path{arg.Path}) var uuid []byte if err == nil { uuid, err = s.bgpServer.AddPath(arg.VrfId, pathList) } - return &api.AddPathResponse{Uuid: uuid}, err + return &AddPathResponse{Uuid: uuid}, err } -func (s *Server) DeletePath(ctx context.Context, arg *api.DeletePathRequest) (*api.DeletePathResponse, error) { +func (s *Server) DeletePath(ctx context.Context, arg *DeletePathRequest) (*DeletePathResponse, error) { pathList, err := func() ([]*table.Path, error) { if arg.Path != nil { arg.Path.IsWithdraw = true - return s.api2PathList(arg.Resource, []*api.Path{arg.Path}) + return s.api2PathList(arg.Resource, []*Path{arg.Path}) } return []*table.Path{}, nil }() if err != nil { return nil, err } - return &api.DeletePathResponse{}, s.bgpServer.DeletePath(arg.Uuid, bgp.RouteFamily(arg.Family), arg.VrfId, pathList) + return &DeletePathResponse{}, s.bgpServer.DeletePath(arg.Uuid, bgp.RouteFamily(arg.Family), arg.VrfId, pathList) } -func (s *Server) EnableMrt(ctx context.Context, arg *api.EnableMrtRequest) (*api.EnableMrtResponse, error) { - return &api.EnableMrtResponse{}, s.bgpServer.EnableMrt(&config.Mrt{ +func (s *Server) EnableMrt(ctx context.Context, arg *EnableMrtRequest) (*EnableMrtResponse, error) { + return &EnableMrtResponse{}, s.bgpServer.EnableMrt(&config.Mrt{ Interval: arg.Interval, DumpType: config.IntToMrtTypeMap[int(arg.DumpType)], FileName: arg.Filename, }) } -func (s *Server) DisableMrt(ctx context.Context, arg *api.DisableMrtRequest) (*api.DisableMrtResponse, error) { - return &api.DisableMrtResponse{}, s.bgpServer.DisableMrt() +func (s *Server) DisableMrt(ctx context.Context, arg *DisableMrtRequest) (*DisableMrtResponse, error) { + return &DisableMrtResponse{}, s.bgpServer.DisableMrt() } -func (s *Server) InjectMrt(stream api.GobgpApi_InjectMrtServer) error { +func (s *Server) InjectMrt(stream GobgpApi_InjectMrtServer) error { for { arg, err := stream.Recv() @@ -546,7 +546,7 @@ func (s *Server) InjectMrt(stream api.GobgpApi_InjectMrtServer) error { return err } - if arg.Resource != api.Resource_GLOBAL && arg.Resource != api.Resource_VRF { + if arg.Resource != Resource_GLOBAL && arg.Resource != Resource_VRF { return fmt.Errorf("unsupported resource: %s", arg.Resource) } @@ -558,82 +558,82 @@ func (s *Server) InjectMrt(stream api.GobgpApi_InjectMrtServer) error { } } } - return stream.SendAndClose(&api.InjectMrtResponse{}) + return stream.SendAndClose(&InjectMrtResponse{}) } -func (s *Server) AddBmp(ctx context.Context, arg *api.AddBmpRequest) (*api.AddBmpResponse, error) { - return &api.AddBmpResponse{}, s.bgpServer.AddBmp(&config.BmpServerConfig{ +func (s *Server) AddBmp(ctx context.Context, arg *AddBmpRequest) (*AddBmpResponse, error) { + return &AddBmpResponse{}, s.bgpServer.AddBmp(&config.BmpServerConfig{ Address: arg.Address, Port: arg.Port, RouteMonitoringPolicy: config.BmpRouteMonitoringPolicyType(arg.Type), }) } -func (s *Server) DeleteBmp(ctx context.Context, arg *api.DeleteBmpRequest) (*api.DeleteBmpResponse, error) { - return &api.DeleteBmpResponse{}, s.bgpServer.DeleteBmp(&config.BmpServerConfig{ +func (s *Server) DeleteBmp(ctx context.Context, arg *DeleteBmpRequest) (*DeleteBmpResponse, error) { + return &DeleteBmpResponse{}, s.bgpServer.DeleteBmp(&config.BmpServerConfig{ Address: arg.Address, Port: arg.Port, }) } -func (s *Server) ValidateRib(ctx context.Context, arg *api.ValidateRibRequest) (*api.ValidateRibResponse, error) { - return &api.ValidateRibResponse{}, s.bgpServer.ValidateRib(arg.Prefix) +func (s *Server) ValidateRib(ctx context.Context, arg *ValidateRibRequest) (*ValidateRibResponse, error) { + return &ValidateRibResponse{}, s.bgpServer.ValidateRib(arg.Prefix) } -func (s *Server) AddRpki(ctx context.Context, arg *api.AddRpkiRequest) (*api.AddRpkiResponse, error) { - return &api.AddRpkiResponse{}, s.bgpServer.AddRpki(&config.RpkiServerConfig{ +func (s *Server) AddRpki(ctx context.Context, arg *AddRpkiRequest) (*AddRpkiResponse, error) { + return &AddRpkiResponse{}, s.bgpServer.AddRpki(&config.RpkiServerConfig{ Address: arg.Address, Port: arg.Port, RecordLifetime: arg.Lifetime, }) } -func (s *Server) DeleteRpki(ctx context.Context, arg *api.DeleteRpkiRequest) (*api.DeleteRpkiResponse, error) { - return &api.DeleteRpkiResponse{}, s.bgpServer.DeleteRpki(&config.RpkiServerConfig{ +func (s *Server) DeleteRpki(ctx context.Context, arg *DeleteRpkiRequest) (*DeleteRpkiResponse, error) { + return &DeleteRpkiResponse{}, s.bgpServer.DeleteRpki(&config.RpkiServerConfig{ Address: arg.Address, Port: arg.Port, }) } -func (s *Server) EnableRpki(ctx context.Context, arg *api.EnableRpkiRequest) (*api.EnableRpkiResponse, error) { - return &api.EnableRpkiResponse{}, s.bgpServer.EnableRpki(&config.RpkiServerConfig{ +func (s *Server) EnableRpki(ctx context.Context, arg *EnableRpkiRequest) (*EnableRpkiResponse, error) { + return &EnableRpkiResponse{}, s.bgpServer.EnableRpki(&config.RpkiServerConfig{ Address: arg.Address, }) } -func (s *Server) DisableRpki(ctx context.Context, arg *api.DisableRpkiRequest) (*api.DisableRpkiResponse, error) { - return &api.DisableRpkiResponse{}, s.bgpServer.DisableRpki(&config.RpkiServerConfig{ +func (s *Server) DisableRpki(ctx context.Context, arg *DisableRpkiRequest) (*DisableRpkiResponse, error) { + return &DisableRpkiResponse{}, s.bgpServer.DisableRpki(&config.RpkiServerConfig{ Address: arg.Address, }) } -func (s *Server) ResetRpki(ctx context.Context, arg *api.ResetRpkiRequest) (*api.ResetRpkiResponse, error) { - return &api.ResetRpkiResponse{}, s.bgpServer.ResetRpki(&config.RpkiServerConfig{ +func (s *Server) ResetRpki(ctx context.Context, arg *ResetRpkiRequest) (*ResetRpkiResponse, error) { + return &ResetRpkiResponse{}, s.bgpServer.ResetRpki(&config.RpkiServerConfig{ Address: arg.Address, }) } -func (s *Server) SoftResetRpki(ctx context.Context, arg *api.SoftResetRpkiRequest) (*api.SoftResetRpkiResponse, error) { - return &api.SoftResetRpkiResponse{}, s.bgpServer.SoftResetRpki(&config.RpkiServerConfig{ +func (s *Server) SoftResetRpki(ctx context.Context, arg *SoftResetRpkiRequest) (*SoftResetRpkiResponse, error) { + return &SoftResetRpkiResponse{}, s.bgpServer.SoftResetRpki(&config.RpkiServerConfig{ Address: arg.Address, }) } -func (s *Server) GetRpki(ctx context.Context, arg *api.GetRpkiRequest) (*api.GetRpkiResponse, error) { +func (s *Server) GetRpki(ctx context.Context, arg *GetRpkiRequest) (*GetRpkiResponse, error) { servers, err := s.bgpServer.GetRpki() if err != nil { return nil, err } - l := make([]*api.Rpki, 0, len(servers)) + l := make([]*Rpki, 0, len(servers)) for _, s := range servers { received := &s.State.RpkiMessages.RpkiReceived sent := &s.State.RpkiMessages.RpkiSent - rpki := &api.Rpki{ - Conf: &api.RPKIConf{ + rpki := &Rpki{ + Conf: &RPKIConf{ Address: s.Config.Address, RemotePort: strconv.Itoa(int(s.Config.Port)), }, - State: &api.RPKIState{ + State: &RPKIState{ Uptime: s.State.Uptime, Downtime: s.State.Downtime, Up: s.State.Up, @@ -655,33 +655,33 @@ func (s *Server) GetRpki(ctx context.Context, arg *api.GetRpkiRequest) (*api.Get } l = append(l, rpki) } - return &api.GetRpkiResponse{Servers: l}, nil + return &GetRpkiResponse{Servers: l}, nil } -func (s *Server) GetRoa(ctx context.Context, arg *api.GetRoaRequest) (*api.GetRoaResponse, error) { +func (s *Server) GetRoa(ctx context.Context, arg *GetRoaRequest) (*GetRoaResponse, error) { roas, err := s.bgpServer.GetRoa(bgp.RouteFamily(arg.Family)) if err != nil { return nil, err } - l := make([]*api.Roa, 0, len(roas)) + l := make([]*Roa, 0, len(roas)) for _, r := range roas { host, port, _ := net.SplitHostPort(r.Src) - l = append(l, &api.Roa{ + l = append(l, &Roa{ As: r.AS, Maxlen: uint32(r.MaxLen), Prefixlen: uint32(r.Prefix.Length), Prefix: r.Prefix.Prefix.String(), - Conf: &api.RPKIConf{ + Conf: &RPKIConf{ Address: host, RemotePort: port, }, }) } - return &api.GetRoaResponse{Roas: l}, nil + return &GetRoaResponse{Roas: l}, nil } -func (s *Server) GetVrf(ctx context.Context, arg *api.GetVrfRequest) (*api.GetVrfResponse, error) { - toApi := func(v *table.Vrf) *api.Vrf { +func (s *Server) GetVrf(ctx context.Context, arg *GetVrfRequest) (*GetVrfResponse, error) { + toApi := func(v *table.Vrf) *Vrf { f := func(rts []bgp.ExtendedCommunityInterface) [][]byte { ret := make([][]byte, 0, len(rts)) for _, rt := range rts { @@ -691,7 +691,7 @@ func (s *Server) GetVrf(ctx context.Context, arg *api.GetVrfRequest) (*api.GetVr return ret } rd, _ := v.Rd.Serialize() - return &api.Vrf{ + return &Vrf{ Name: v.Name, Rd: rd, ImportRt: f(v.ImportRt), @@ -699,14 +699,14 @@ func (s *Server) GetVrf(ctx context.Context, arg *api.GetVrfRequest) (*api.GetVr } } vrfs := s.bgpServer.GetVrf() - l := make([]*api.Vrf, 0, len(vrfs)) + l := make([]*Vrf, 0, len(vrfs)) for _, v := range vrfs { l = append(l, toApi(v)) } - return &api.GetVrfResponse{Vrfs: l}, nil + return &GetVrfResponse{Vrfs: l}, nil } -func (s *Server) AddVrf(ctx context.Context, arg *api.AddVrfRequest) (r *api.AddVrfResponse, err error) { +func (s *Server) AddVrf(ctx context.Context, arg *AddVrfRequest) (r *AddVrfResponse, err error) { rd := bgp.GetRouteDistinguisher(arg.Vrf.Rd) f := func(bufs [][]byte) ([]bgp.ExtendedCommunityInterface, error) { ret := make([]bgp.ExtendedCommunityInterface, 0, len(bufs)) @@ -721,21 +721,21 @@ func (s *Server) AddVrf(ctx context.Context, arg *api.AddVrfRequest) (r *api.Add } im, err := f(arg.Vrf.ImportRt) if err != nil { - return &api.AddVrfResponse{}, err + return &AddVrfResponse{}, err } ex, err := f(arg.Vrf.ExportRt) if err != nil { - return &api.AddVrfResponse{}, err + return &AddVrfResponse{}, err } - return &api.AddVrfResponse{}, s.bgpServer.AddVrf(arg.Vrf.Name, rd, im, ex) + return &AddVrfResponse{}, s.bgpServer.AddVrf(arg.Vrf.Name, rd, im, ex) } -func (s *Server) DeleteVrf(ctx context.Context, arg *api.DeleteVrfRequest) (*api.DeleteVrfResponse, error) { - return &api.DeleteVrfResponse{}, s.bgpServer.DeleteVrf(arg.Vrf.Name) +func (s *Server) DeleteVrf(ctx context.Context, arg *DeleteVrfRequest) (*DeleteVrfResponse, error) { + return &DeleteVrfResponse{}, s.bgpServer.DeleteVrf(arg.Vrf.Name) } -func (s *Server) AddNeighbor(ctx context.Context, arg *api.AddNeighborRequest) (*api.AddNeighborResponse, error) { - c, err := func(a *api.Peer) (config.Neighbor, error) { +func (s *Server) AddNeighbor(ctx context.Context, arg *AddNeighborRequest) (*AddNeighborResponse, error) { + c, err := func(a *Peer) (config.Neighbor, error) { pconf := config.Neighbor{} if a.Conf != nil { pconf.Config.NeighborAddress = a.Conf.NeighborAddress @@ -845,16 +845,16 @@ func (s *Server) AddNeighbor(ctx context.Context, arg *api.AddNeighborRequest) ( if err != nil { return nil, err } - return &api.AddNeighborResponse{}, s.bgpServer.AddNeighbor(&c) + return &AddNeighborResponse{}, s.bgpServer.AddNeighbor(&c) } -func (s *Server) DeleteNeighbor(ctx context.Context, arg *api.DeleteNeighborRequest) (*api.DeleteNeighborResponse, error) { - return &api.DeleteNeighborResponse{}, s.bgpServer.DeleteNeighbor(&config.Neighbor{Config: config.NeighborConfig{ +func (s *Server) DeleteNeighbor(ctx context.Context, arg *DeleteNeighborRequest) (*DeleteNeighborResponse, error) { + return &DeleteNeighborResponse{}, s.bgpServer.DeleteNeighbor(&config.Neighbor{Config: config.NeighborConfig{ NeighborAddress: arg.Peer.Conf.NeighborAddress, }}) } -func NewPrefixFromApiStruct(a *api.Prefix) (*table.Prefix, error) { +func NewPrefixFromApiStruct(a *Prefix) (*table.Prefix, error) { addr, prefix, err := net.ParseCIDR(a.IpPrefix) if err != nil { return nil, err @@ -871,7 +871,7 @@ func NewPrefixFromApiStruct(a *api.Prefix) (*table.Prefix, error) { }, nil } -func NewDefinedSetFromApiStruct(a *api.DefinedSet) (table.DefinedSet, error) { +func NewDefinedSetFromApiStruct(a *DefinedSet) (table.DefinedSet, error) { if a.Name == "" { return nil, fmt.Errorf("empty neighbor set name") } @@ -916,25 +916,25 @@ func NewDefinedSetFromApiStruct(a *api.DefinedSet) (table.DefinedSet, error) { } } -func (s *Server) GetDefinedSet(ctx context.Context, arg *api.GetDefinedSetRequest) (*api.GetDefinedSetResponse, error) { +func (s *Server) GetDefinedSet(ctx context.Context, arg *GetDefinedSetRequest) (*GetDefinedSetResponse, error) { cd, err := s.bgpServer.GetDefinedSet(table.DefinedType(arg.Type)) if err != nil { return nil, err } - sets := make([]*api.DefinedSet, 0) + sets := make([]*DefinedSet, 0) for _, cs := range cd.PrefixSets { - ad := &api.DefinedSet{ - Type: api.DefinedType_PREFIX, + ad := &DefinedSet{ + Type: DefinedType_PREFIX, Name: cs.PrefixSetName, - Prefixes: func() []*api.Prefix { - l := make([]*api.Prefix, 0, len(cs.PrefixList)) + Prefixes: func() []*Prefix { + l := make([]*Prefix, 0, len(cs.PrefixList)) for _, p := range cs.PrefixList { exp := regexp.MustCompile("(\\d+)\\.\\.(\\d+)") elems := exp.FindStringSubmatch(p.MasklengthRange) min, _ := strconv.Atoi(elems[1]) max, _ := strconv.Atoi(elems[2]) - l = append(l, &api.Prefix{IpPrefix: p.IpPrefix, MaskLengthMin: uint32(min), MaskLengthMax: uint32(max)}) + l = append(l, &Prefix{IpPrefix: p.IpPrefix, MaskLengthMin: uint32(min), MaskLengthMax: uint32(max)}) } return l }(), @@ -943,137 +943,137 @@ func (s *Server) GetDefinedSet(ctx context.Context, arg *api.GetDefinedSetReques } for _, cs := range cd.NeighborSets { - ad := &api.DefinedSet{ - Type: api.DefinedType_NEIGHBOR, + ad := &DefinedSet{ + Type: DefinedType_NEIGHBOR, Name: cs.NeighborSetName, List: cs.NeighborInfoList, } sets = append(sets, ad) } for _, cs := range cd.BgpDefinedSets.CommunitySets { - ad := &api.DefinedSet{ - Type: api.DefinedType_COMMUNITY, + ad := &DefinedSet{ + Type: DefinedType_COMMUNITY, Name: cs.CommunitySetName, List: cs.CommunityList, } sets = append(sets, ad) } for _, cs := range cd.BgpDefinedSets.ExtCommunitySets { - ad := &api.DefinedSet{ - Type: api.DefinedType_EXT_COMMUNITY, + ad := &DefinedSet{ + Type: DefinedType_EXT_COMMUNITY, Name: cs.ExtCommunitySetName, List: cs.ExtCommunityList, } sets = append(sets, ad) } for _, cs := range cd.BgpDefinedSets.AsPathSets { - ad := &api.DefinedSet{ - Type: api.DefinedType_AS_PATH, + ad := &DefinedSet{ + Type: DefinedType_AS_PATH, Name: cs.AsPathSetName, List: cs.AsPathList, } sets = append(sets, ad) } - return &api.GetDefinedSetResponse{Sets: sets}, nil + return &GetDefinedSetResponse{Sets: sets}, nil } -func (s *Server) AddDefinedSet(ctx context.Context, arg *api.AddDefinedSetRequest) (*api.AddDefinedSetResponse, error) { +func (s *Server) AddDefinedSet(ctx context.Context, arg *AddDefinedSetRequest) (*AddDefinedSetResponse, error) { set, err := NewDefinedSetFromApiStruct(arg.Set) if err != nil { return nil, err } - return &api.AddDefinedSetResponse{}, s.bgpServer.AddDefinedSet(set) + return &AddDefinedSetResponse{}, s.bgpServer.AddDefinedSet(set) } -func (s *Server) DeleteDefinedSet(ctx context.Context, arg *api.DeleteDefinedSetRequest) (*api.DeleteDefinedSetResponse, error) { +func (s *Server) DeleteDefinedSet(ctx context.Context, arg *DeleteDefinedSetRequest) (*DeleteDefinedSetResponse, error) { set, err := NewDefinedSetFromApiStruct(arg.Set) if err != nil { return nil, err } - return &api.DeleteDefinedSetResponse{}, s.bgpServer.DeleteDefinedSet(set, arg.All) + return &DeleteDefinedSetResponse{}, s.bgpServer.DeleteDefinedSet(set, arg.All) } -func (s *Server) ReplaceDefinedSet(ctx context.Context, arg *api.ReplaceDefinedSetRequest) (*api.ReplaceDefinedSetResponse, error) { +func (s *Server) ReplaceDefinedSet(ctx context.Context, arg *ReplaceDefinedSetRequest) (*ReplaceDefinedSetResponse, error) { set, err := NewDefinedSetFromApiStruct(arg.Set) if err != nil { return nil, err } - return &api.ReplaceDefinedSetResponse{}, s.bgpServer.ReplaceDefinedSet(set) + return &ReplaceDefinedSetResponse{}, s.bgpServer.ReplaceDefinedSet(set) } -func toStatementApi(s *config.Statement) *api.Statement { - cs := &api.Conditions{} +func toStatementApi(s *config.Statement) *Statement { + cs := &Conditions{} if s.Conditions.MatchPrefixSet.PrefixSet != "" { - cs.PrefixSet = &api.MatchSet{ - Type: api.MatchType(s.Conditions.MatchPrefixSet.MatchSetOptions.ToInt()), + cs.PrefixSet = &MatchSet{ + Type: MatchType(s.Conditions.MatchPrefixSet.MatchSetOptions.ToInt()), Name: s.Conditions.MatchPrefixSet.PrefixSet, } } if s.Conditions.MatchNeighborSet.NeighborSet != "" { - cs.NeighborSet = &api.MatchSet{ - Type: api.MatchType(s.Conditions.MatchNeighborSet.MatchSetOptions.ToInt()), + cs.NeighborSet = &MatchSet{ + Type: MatchType(s.Conditions.MatchNeighborSet.MatchSetOptions.ToInt()), Name: s.Conditions.MatchNeighborSet.NeighborSet, } } if s.Conditions.BgpConditions.AsPathLength.Operator != "" { - cs.AsPathLength = &api.AsPathLength{ + cs.AsPathLength = &AsPathLength{ Length: s.Conditions.BgpConditions.AsPathLength.Value, - Type: api.AsPathLengthType(s.Conditions.BgpConditions.AsPathLength.Operator.ToInt()), + Type: AsPathLengthType(s.Conditions.BgpConditions.AsPathLength.Operator.ToInt()), } } if s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet != "" { - cs.AsPathSet = &api.MatchSet{ - Type: api.MatchType(s.Conditions.BgpConditions.MatchAsPathSet.MatchSetOptions.ToInt()), + cs.AsPathSet = &MatchSet{ + Type: MatchType(s.Conditions.BgpConditions.MatchAsPathSet.MatchSetOptions.ToInt()), Name: s.Conditions.BgpConditions.MatchAsPathSet.AsPathSet, } } if s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet != "" { - cs.CommunitySet = &api.MatchSet{ - Type: api.MatchType(s.Conditions.BgpConditions.MatchCommunitySet.MatchSetOptions.ToInt()), + cs.CommunitySet = &MatchSet{ + Type: MatchType(s.Conditions.BgpConditions.MatchCommunitySet.MatchSetOptions.ToInt()), Name: s.Conditions.BgpConditions.MatchCommunitySet.CommunitySet, } } if s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet != "" { - cs.CommunitySet = &api.MatchSet{ - Type: api.MatchType(s.Conditions.BgpConditions.MatchExtCommunitySet.MatchSetOptions.ToInt()), + cs.CommunitySet = &MatchSet{ + Type: MatchType(s.Conditions.BgpConditions.MatchExtCommunitySet.MatchSetOptions.ToInt()), Name: s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet, } } cs.RpkiResult = int32(s.Conditions.BgpConditions.RpkiValidationResult.ToInt()) - as := &api.Actions{ - RouteAction: func() api.RouteAction { + as := &Actions{ + RouteAction: func() RouteAction { if s.Actions.RouteDisposition.AcceptRoute { - return api.RouteAction_ACCEPT + return RouteAction_ACCEPT } - return api.RouteAction_REJECT + return RouteAction_REJECT }(), - Community: func() *api.CommunityAction { + Community: func() *CommunityAction { if len(s.Actions.BgpActions.SetCommunity.SetCommunityMethod.CommunitiesList) == 0 { return nil } - return &api.CommunityAction{ - Type: api.CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetCommunity.Options)]), + return &CommunityAction{ + Type: CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetCommunity.Options)]), Communities: s.Actions.BgpActions.SetCommunity.SetCommunityMethod.CommunitiesList} }(), - Med: func() *api.MedAction { + Med: func() *MedAction { if len(string(s.Actions.BgpActions.SetMed)) == 0 { return nil } exp := regexp.MustCompile("^(\\+|\\-)?(\\d+)$") elems := exp.FindStringSubmatch(string(s.Actions.BgpActions.SetMed)) - action := api.MedActionType_MED_REPLACE + action := MedActionType_MED_REPLACE switch elems[1] { case "+", "-": - action = api.MedActionType_MED_MOD + action = MedActionType_MED_MOD } value, _ := strconv.Atoi(string(s.Actions.BgpActions.SetMed)) - return &api.MedAction{ + return &MedAction{ Value: int64(value), Type: action, } }(), - AsPrepend: func() *api.AsPrependAction { + AsPrepend: func() *AsPrependAction { if len(s.Actions.BgpActions.SetAsPathPrepend.As) == 0 { return nil } @@ -1084,57 +1084,57 @@ func toStatementApi(s *config.Statement) *api.Statement { } else { useleft = true } - return &api.AsPrependAction{ + return &AsPrependAction{ Asn: uint32(asn), Repeat: uint32(s.Actions.BgpActions.SetAsPathPrepend.RepeatN), UseLeftMost: useleft, } }(), - ExtCommunity: func() *api.CommunityAction { + ExtCommunity: func() *CommunityAction { if len(s.Actions.BgpActions.SetExtCommunity.SetExtCommunityMethod.CommunitiesList) == 0 { return nil } - return &api.CommunityAction{ - Type: api.CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetExtCommunity.Options)]), + return &CommunityAction{ + Type: CommunityActionType(config.BgpSetCommunityOptionTypeToIntMap[config.BgpSetCommunityOptionType(s.Actions.BgpActions.SetExtCommunity.Options)]), Communities: s.Actions.BgpActions.SetExtCommunity.SetExtCommunityMethod.CommunitiesList, } }(), - Nexthop: func() *api.NexthopAction { + Nexthop: func() *NexthopAction { if len(string(s.Actions.BgpActions.SetNextHop)) == 0 { return nil } if string(s.Actions.BgpActions.SetNextHop) == "self" { - return &api.NexthopAction{ + return &NexthopAction{ Self: true, } } - return &api.NexthopAction{ + return &NexthopAction{ Address: string(s.Actions.BgpActions.SetNextHop), } }(), - LocalPref: func() *api.LocalPrefAction { + LocalPref: func() *LocalPrefAction { if s.Actions.BgpActions.SetLocalPref == 0 { return nil } - return &api.LocalPrefAction{Value: s.Actions.BgpActions.SetLocalPref} + return &LocalPrefAction{Value: s.Actions.BgpActions.SetLocalPref} }(), } - return &api.Statement{ + return &Statement{ Name: s.Name, Conditions: cs, Actions: as, } } -func toConfigMatchSetOption(a api.MatchType) (config.MatchSetOptionsType, error) { +func toConfigMatchSetOption(a MatchType) (config.MatchSetOptionsType, error) { var typ config.MatchSetOptionsType switch a { - case api.MatchType_ANY: + case MatchType_ANY: typ = config.MATCH_SET_OPTIONS_TYPE_ANY - case api.MatchType_ALL: + case MatchType_ALL: typ = config.MATCH_SET_OPTIONS_TYPE_ALL - case api.MatchType_INVERT: + case MatchType_INVERT: typ = config.MATCH_SET_OPTIONS_TYPE_INVERT default: return typ, fmt.Errorf("invalid match type") @@ -1142,12 +1142,12 @@ func toConfigMatchSetOption(a api.MatchType) (config.MatchSetOptionsType, error) return typ, nil } -func toConfigMatchSetOptionRestricted(a api.MatchType) (config.MatchSetOptionsRestrictedType, error) { +func toConfigMatchSetOptionRestricted(a MatchType) (config.MatchSetOptionsRestrictedType, error) { var typ config.MatchSetOptionsRestrictedType switch a { - case api.MatchType_ANY: + case MatchType_ANY: typ = config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_ANY - case api.MatchType_INVERT: + case MatchType_INVERT: typ = config.MATCH_SET_OPTIONS_RESTRICTED_TYPE_INVERT default: return typ, fmt.Errorf("invalid match type") @@ -1155,7 +1155,7 @@ func toConfigMatchSetOptionRestricted(a api.MatchType) (config.MatchSetOptionsRe return typ, nil } -func NewPrefixConditionFromApiStruct(a *api.MatchSet) (*table.PrefixCondition, error) { +func NewPrefixConditionFromApiStruct(a *MatchSet) (*table.PrefixCondition, error) { if a == nil { return nil, nil } @@ -1170,7 +1170,7 @@ func NewPrefixConditionFromApiStruct(a *api.MatchSet) (*table.PrefixCondition, e return table.NewPrefixCondition(c) } -func NewNeighborConditionFromApiStruct(a *api.MatchSet) (*table.NeighborCondition, error) { +func NewNeighborConditionFromApiStruct(a *MatchSet) (*table.NeighborCondition, error) { if a == nil { return nil, nil } @@ -1185,7 +1185,7 @@ func NewNeighborConditionFromApiStruct(a *api.MatchSet) (*table.NeighborConditio return table.NewNeighborCondition(c) } -func NewAsPathLengthConditionFromApiStruct(a *api.AsPathLength) (*table.AsPathLengthCondition, error) { +func NewAsPathLengthConditionFromApiStruct(a *AsPathLength) (*table.AsPathLengthCondition, error) { if a == nil { return nil, nil } @@ -1195,7 +1195,7 @@ func NewAsPathLengthConditionFromApiStruct(a *api.AsPathLength) (*table.AsPathLe }) } -func NewAsPathConditionFromApiStruct(a *api.MatchSet) (*table.AsPathCondition, error) { +func NewAsPathConditionFromApiStruct(a *MatchSet) (*table.AsPathCondition, error) { if a == nil { return nil, nil } @@ -1217,7 +1217,7 @@ func NewRpkiValidationConditionFromApiStruct(a int32) (*table.RpkiValidationCond return table.NewRpkiValidationCondition(config.IntToRpkiValidationResultTypeMap[int(a)]) } -func NewCommunityConditionFromApiStruct(a *api.MatchSet) (*table.CommunityCondition, error) { +func NewCommunityConditionFromApiStruct(a *MatchSet) (*table.CommunityCondition, error) { if a == nil { return nil, nil } @@ -1232,7 +1232,7 @@ func NewCommunityConditionFromApiStruct(a *api.MatchSet) (*table.CommunityCondit return table.NewCommunityCondition(c) } -func NewExtCommunityConditionFromApiStruct(a *api.MatchSet) (*table.ExtCommunityCondition, error) { +func NewExtCommunityConditionFromApiStruct(a *MatchSet) (*table.ExtCommunityCondition, error) { if a == nil { return nil, nil } @@ -1247,12 +1247,12 @@ func NewExtCommunityConditionFromApiStruct(a *api.MatchSet) (*table.ExtCommunity return table.NewExtCommunityCondition(c) } -func NewRoutingActionFromApiStruct(a api.RouteAction) (*table.RoutingAction, error) { - if a == api.RouteAction_NONE { +func NewRoutingActionFromApiStruct(a RouteAction) (*table.RoutingAction, error) { + if a == RouteAction_NONE { return nil, nil } accept := false - if a == api.RouteAction_ACCEPT { + if a == RouteAction_ACCEPT { accept = true } return &table.RoutingAction{ @@ -1260,7 +1260,7 @@ func NewRoutingActionFromApiStruct(a api.RouteAction) (*table.RoutingAction, err }, nil } -func NewCommunityActionFromApiStruct(a *api.CommunityAction) (*table.CommunityAction, error) { +func NewCommunityActionFromApiStruct(a *CommunityAction) (*table.CommunityAction, error) { if a == nil { return nil, nil } @@ -1272,7 +1272,7 @@ func NewCommunityActionFromApiStruct(a *api.CommunityAction) (*table.CommunityAc }) } -func NewExtCommunityActionFromApiStruct(a *api.CommunityAction) (*table.ExtCommunityAction, error) { +func NewExtCommunityActionFromApiStruct(a *CommunityAction) (*table.ExtCommunityAction, error) { if a == nil { return nil, nil } @@ -1284,21 +1284,21 @@ func NewExtCommunityActionFromApiStruct(a *api.CommunityAction) (*table.ExtCommu }) } -func NewMedActionFromApiStruct(a *api.MedAction) (*table.MedAction, error) { +func NewMedActionFromApiStruct(a *MedAction) (*table.MedAction, error) { if a == nil { return nil, nil } return table.NewMedActionFromApiStruct(table.MedActionType(a.Type), int(a.Value)), nil } -func NewLocalPrefActionFromApiStruct(a *api.LocalPrefAction) (*table.LocalPrefAction, error) { +func NewLocalPrefActionFromApiStruct(a *LocalPrefAction) (*table.LocalPrefAction, error) { if a == nil || a.Value == 0 { return nil, nil } return table.NewLocalPrefAction(a.Value) } -func NewAsPathPrependActionFromApiStruct(a *api.AsPrependAction) (*table.AsPathPrependAction, error) { +func NewAsPathPrependActionFromApiStruct(a *AsPrependAction) (*table.AsPathPrependAction, error) { if a == nil { return nil, nil } @@ -1313,7 +1313,7 @@ func NewAsPathPrependActionFromApiStruct(a *api.AsPrependAction) (*table.AsPathP }) } -func NewNexthopActionFromApiStruct(a *api.NexthopAction) (*table.NexthopAction, error) { +func NewNexthopActionFromApiStruct(a *NexthopAction) (*table.NexthopAction, error) { if a == nil { return nil, nil } @@ -1327,7 +1327,7 @@ func NewNexthopActionFromApiStruct(a *api.NexthopAction) (*table.NexthopAction, )) } -func NewStatementFromApiStruct(a *api.Statement) (*table.Statement, error) { +func NewStatementFromApiStruct(a *Statement) (*table.Statement, error) { if a.Name == "" { return nil, fmt.Errorf("empty statement name") } @@ -1414,43 +1414,43 @@ func NewStatementFromApiStruct(a *api.Statement) (*table.Statement, error) { }, nil } -func (s *Server) GetStatement(ctx context.Context, arg *api.GetStatementRequest) (*api.GetStatementResponse, error) { - l := make([]*api.Statement, 0) +func (s *Server) GetStatement(ctx context.Context, arg *GetStatementRequest) (*GetStatementResponse, error) { + l := make([]*Statement, 0) for _, s := range s.bgpServer.GetStatement() { l = append(l, toStatementApi(s)) } - return &api.GetStatementResponse{Statements: l}, nil + return &GetStatementResponse{Statements: l}, nil } -func (s *Server) AddStatement(ctx context.Context, arg *api.AddStatementRequest) (*api.AddStatementResponse, error) { +func (s *Server) AddStatement(ctx context.Context, arg *AddStatementRequest) (*AddStatementResponse, error) { st, err := NewStatementFromApiStruct(arg.Statement) if err == nil { err = s.bgpServer.AddStatement(st) } - return &api.AddStatementResponse{}, err + return &AddStatementResponse{}, err } -func (s *Server) DeleteStatement(ctx context.Context, arg *api.DeleteStatementRequest) (*api.DeleteStatementResponse, error) { +func (s *Server) DeleteStatement(ctx context.Context, arg *DeleteStatementRequest) (*DeleteStatementResponse, error) { st, err := NewStatementFromApiStruct(arg.Statement) if err == nil { err = s.bgpServer.DeleteStatement(st, arg.All) } - return &api.DeleteStatementResponse{}, err + return &DeleteStatementResponse{}, err } -func (s *Server) ReplaceStatement(ctx context.Context, arg *api.ReplaceStatementRequest) (*api.ReplaceStatementResponse, error) { +func (s *Server) ReplaceStatement(ctx context.Context, arg *ReplaceStatementRequest) (*ReplaceStatementResponse, error) { st, err := NewStatementFromApiStruct(arg.Statement) if err == nil { err = s.bgpServer.ReplaceStatement(st) } - return &api.ReplaceStatementResponse{}, err + return &ReplaceStatementResponse{}, err } -func toPolicyApi(p *config.PolicyDefinition) *api.Policy { - return &api.Policy{ +func toPolicyApi(p *config.PolicyDefinition) *Policy { + return &Policy{ Name: p.Name, - Statements: func() []*api.Statement { - l := make([]*api.Statement, 0) + Statements: func() []*Statement { + l := make([]*Statement, 0) for _, s := range p.Statements { l = append(l, toStatementApi(&s)) } @@ -1459,7 +1459,7 @@ func toPolicyApi(p *config.PolicyDefinition) *api.Policy { } } -func NewPolicyFromApiStruct(a *api.Policy) (*table.Policy, error) { +func NewPolicyFromApiStruct(a *Policy) (*table.Policy, error) { if a.Name == "" { return nil, fmt.Errorf("empty policy name") } @@ -1480,56 +1480,56 @@ func NewPolicyFromApiStruct(a *api.Policy) (*table.Policy, error) { }, nil } -func (s *Server) GetPolicy(ctx context.Context, arg *api.GetPolicyRequest) (*api.GetPolicyResponse, error) { - l := make([]*api.Policy, 0) +func (s *Server) GetPolicy(ctx context.Context, arg *GetPolicyRequest) (*GetPolicyResponse, error) { + l := make([]*Policy, 0) for _, p := range s.bgpServer.GetPolicy() { l = append(l, toPolicyApi(p)) } - return &api.GetPolicyResponse{Policies: l}, nil + return &GetPolicyResponse{Policies: l}, nil } -func (s *Server) AddPolicy(ctx context.Context, arg *api.AddPolicyRequest) (*api.AddPolicyResponse, error) { +func (s *Server) AddPolicy(ctx context.Context, arg *AddPolicyRequest) (*AddPolicyResponse, error) { x, err := NewPolicyFromApiStruct(arg.Policy) if err != nil { return nil, err } - return &api.AddPolicyResponse{}, s.bgpServer.AddPolicy(x, arg.ReferExistingStatements) + return &AddPolicyResponse{}, s.bgpServer.AddPolicy(x, arg.ReferExistingStatements) } -func (s *Server) DeletePolicy(ctx context.Context, arg *api.DeletePolicyRequest) (*api.DeletePolicyResponse, error) { +func (s *Server) DeletePolicy(ctx context.Context, arg *DeletePolicyRequest) (*DeletePolicyResponse, error) { x, err := NewPolicyFromApiStruct(arg.Policy) if err != nil { return nil, err } - return &api.DeletePolicyResponse{}, s.bgpServer.DeletePolicy(x, arg.All, arg.PreserveStatements) + return &DeletePolicyResponse{}, s.bgpServer.DeletePolicy(x, arg.All, arg.PreserveStatements) } -func (s *Server) ReplacePolicy(ctx context.Context, arg *api.ReplacePolicyRequest) (*api.ReplacePolicyResponse, error) { +func (s *Server) ReplacePolicy(ctx context.Context, arg *ReplacePolicyRequest) (*ReplacePolicyResponse, error) { x, err := NewPolicyFromApiStruct(arg.Policy) if err != nil { return nil, err } - return &api.ReplacePolicyResponse{}, s.bgpServer.ReplacePolicy(x, arg.ReferExistingStatements, arg.PreserveStatements) + return &ReplacePolicyResponse{}, s.bgpServer.ReplacePolicy(x, arg.ReferExistingStatements, arg.PreserveStatements) } -func toPolicyAssignmentName(a *api.PolicyAssignment) (string, table.PolicyDirection, error) { +func toPolicyAssignmentName(a *PolicyAssignment) (string, table.PolicyDirection, error) { switch a.Resource { - case api.Resource_GLOBAL: + case Resource_GLOBAL: switch a.Type { - case api.PolicyType_IMPORT: + case PolicyType_IMPORT: return "", table.POLICY_DIRECTION_IMPORT, nil - case api.PolicyType_EXPORT: + case PolicyType_EXPORT: return "", table.POLICY_DIRECTION_EXPORT, nil default: return "", table.POLICY_DIRECTION_NONE, fmt.Errorf("invalid policy type") } - case api.Resource_LOCAL: + case Resource_LOCAL: switch a.Type { - case api.PolicyType_IN: + case PolicyType_IN: return a.Name, table.POLICY_DIRECTION_IN, nil - case api.PolicyType_IMPORT: + case PolicyType_IMPORT: return a.Name, table.POLICY_DIRECTION_IMPORT, nil - case api.PolicyType_EXPORT: + case PolicyType_EXPORT: return a.Name, table.POLICY_DIRECTION_EXPORT, nil default: return "", table.POLICY_DIRECTION_NONE, fmt.Errorf("invalid policy type") @@ -1540,7 +1540,7 @@ func toPolicyAssignmentName(a *api.PolicyAssignment) (string, table.PolicyDirect } -func (s *Server) GetPolicyAssignment(ctx context.Context, arg *api.GetPolicyAssignmentRequest) (*api.GetPolicyAssignmentResponse, error) { +func (s *Server) GetPolicyAssignment(ctx context.Context, arg *GetPolicyAssignmentRequest) (*GetPolicyAssignmentResponse, error) { name, dir, err := toPolicyAssignmentName(arg.Assignment) if err != nil { return nil, err @@ -1549,20 +1549,20 @@ func (s *Server) GetPolicyAssignment(ctx context.Context, arg *api.GetPolicyAssi if err != nil { return nil, err } - return &api.GetPolicyAssignmentResponse{ - Assignment: &api.PolicyAssignment{ - Default: func() api.RouteAction { + return &GetPolicyAssignmentResponse{ + Assignment: &PolicyAssignment{ + Default: func() RouteAction { switch d { case table.ROUTE_TYPE_ACCEPT: - return api.RouteAction_ACCEPT + return RouteAction_ACCEPT case table.ROUTE_TYPE_REJECT: - return api.RouteAction_REJECT + return RouteAction_REJECT } - return api.RouteAction_NONE + return RouteAction_NONE }(), - Policies: func() []*api.Policy { - l := make([]*api.Policy, 0) + Policies: func() []*Policy { + l := make([]*Policy, 0) for _, p := range a { l = append(l, toPolicyApi(p)) } @@ -1572,18 +1572,18 @@ func (s *Server) GetPolicyAssignment(ctx context.Context, arg *api.GetPolicyAssi }, err } -func defaultRouteType(d api.RouteAction) table.RouteType { +func defaultRouteType(d RouteAction) table.RouteType { switch d { - case api.RouteAction_ACCEPT: + case RouteAction_ACCEPT: return table.ROUTE_TYPE_ACCEPT - case api.RouteAction_REJECT: + case RouteAction_REJECT: return table.ROUTE_TYPE_REJECT default: return table.ROUTE_TYPE_NONE } } -func toPolicyDefinition(policies []*api.Policy) []*config.PolicyDefinition { +func toPolicyDefinition(policies []*Policy) []*config.PolicyDefinition { l := make([]*config.PolicyDefinition, 0, len(policies)) for _, p := range policies { l = append(l, &config.PolicyDefinition{Name: p.Name}) @@ -1591,34 +1591,34 @@ func toPolicyDefinition(policies []*api.Policy) []*config.PolicyDefinition { return l } -func (s *Server) AddPolicyAssignment(ctx context.Context, arg *api.AddPolicyAssignmentRequest) (*api.AddPolicyAssignmentResponse, error) { +func (s *Server) AddPolicyAssignment(ctx context.Context, arg *AddPolicyAssignmentRequest) (*AddPolicyAssignmentResponse, error) { name, dir, err := toPolicyAssignmentName(arg.Assignment) if err != nil { return nil, err } - return &api.AddPolicyAssignmentResponse{}, s.bgpServer.AddPolicyAssignment(name, dir, toPolicyDefinition(arg.Assignment.Policies), defaultRouteType(arg.Assignment.Default)) + return &AddPolicyAssignmentResponse{}, s.bgpServer.AddPolicyAssignment(name, dir, toPolicyDefinition(arg.Assignment.Policies), defaultRouteType(arg.Assignment.Default)) } -func (s *Server) DeletePolicyAssignment(ctx context.Context, arg *api.DeletePolicyAssignmentRequest) (*api.DeletePolicyAssignmentResponse, error) { +func (s *Server) DeletePolicyAssignment(ctx context.Context, arg *DeletePolicyAssignmentRequest) (*DeletePolicyAssignmentResponse, error) { name, dir, err := toPolicyAssignmentName(arg.Assignment) if err != nil { return nil, err } - return &api.DeletePolicyAssignmentResponse{}, s.bgpServer.DeletePolicyAssignment(name, dir, toPolicyDefinition(arg.Assignment.Policies), arg.All) + return &DeletePolicyAssignmentResponse{}, s.bgpServer.DeletePolicyAssignment(name, dir, toPolicyDefinition(arg.Assignment.Policies), arg.All) } -func (s *Server) ReplacePolicyAssignment(ctx context.Context, arg *api.ReplacePolicyAssignmentRequest) (*api.ReplacePolicyAssignmentResponse, error) { +func (s *Server) ReplacePolicyAssignment(ctx context.Context, arg *ReplacePolicyAssignmentRequest) (*ReplacePolicyAssignmentResponse, error) { name, dir, err := toPolicyAssignmentName(arg.Assignment) if err != nil { return nil, err } - return &api.ReplacePolicyAssignmentResponse{}, s.bgpServer.ReplacePolicyAssignment(name, dir, toPolicyDefinition(arg.Assignment.Policies), defaultRouteType(arg.Assignment.Default)) + return &ReplacePolicyAssignmentResponse{}, s.bgpServer.ReplacePolicyAssignment(name, dir, toPolicyDefinition(arg.Assignment.Policies), defaultRouteType(arg.Assignment.Default)) } -func (s *Server) GetServer(ctx context.Context, arg *api.GetServerRequest) (*api.GetServerResponse, error) { +func (s *Server) GetServer(ctx context.Context, arg *GetServerRequest) (*GetServerResponse, error) { g := s.bgpServer.GetServer() - return &api.GetServerResponse{ - Global: &api.Global{ + return &GetServerResponse{ + Global: &Global{ As: g.Config.As, RouterId: g.Config.RouterId, ListenPort: g.Config.Port, @@ -1629,7 +1629,7 @@ func (s *Server) GetServer(ctx context.Context, arg *api.GetServerRequest) (*api }, nil } -func (s *Server) StartServer(ctx context.Context, arg *api.StartServerRequest) (*api.StartServerResponse, error) { +func (s *Server) StartServer(ctx context.Context, arg *StartServerRequest) (*StartServerResponse, error) { g := arg.Global if net.ParseIP(g.RouterId) == nil { return nil, fmt.Errorf("invalid router-id format: %s", g.RouterId) @@ -1665,9 +1665,9 @@ func (s *Server) StartServer(ctx context.Context, arg *api.StartServerRequest) ( if err := config.SetDefaultConfigValues(nil, b); err != nil { return nil, err } - return &api.StartServerResponse{}, s.bgpServer.Start(&b.Global) + return &StartServerResponse{}, s.bgpServer.Start(&b.Global) } -func (s *Server) StopServer(ctx context.Context, arg *api.StopServerRequest) (*api.StopServerResponse, error) { - return &api.StopServerResponse{}, s.bgpServer.Stop() +func (s *Server) StopServer(ctx context.Context, arg *StopServerRequest) (*StopServerResponse, error) { + return &StopServerResponse{}, s.bgpServer.Stop() } diff --git a/docs/sources/lib.md b/docs/sources/lib.md index 34b0225a..fd4182ca 100644 --- a/docs/sources/lib.md +++ b/docs/sources/lib.md @@ -13,6 +13,7 @@ package main import ( "fmt" log "github.com/Sirupsen/logrus" + api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" "github.com/osrg/gobgp/packet/bgp" gobgp "github.com/osrg/gobgp/server" @@ -27,7 +28,7 @@ func main() { // start grpc api server. this is not mandatory // but you will be able to use `gobgp` cmd with this. - g := gobgp.NewGrpcServer(s, ":50051", s.GrpcReqCh) + g := api.NewGrpcServer(s, ":50051") go g.Serve() // global configuration diff --git a/gobgpd/main.go b/gobgpd/main.go index 48479e60..aca52a7e 100644 --- a/gobgpd/main.go +++ b/gobgpd/main.go @@ -1,4 +1,4 @@ -// Copyright (C) 2014 Nippon Telegraph and Telephone Corporation. +// Copyright (C) 2014-2016 Nippon Telegraph and Telephone Corporation. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import ( "github.com/Sirupsen/logrus/hooks/syslog" "github.com/jessevdk/go-flags" p "github.com/kr/pretty" + api "github.com/osrg/gobgp/api" "github.com/osrg/gobgp/config" ops "github.com/osrg/gobgp/openswitch" "github.com/osrg/gobgp/packet/bgp" @@ -179,7 +180,7 @@ func main() { go bgpServer.Serve() // start grpc Server - grpcServer := server.NewGrpcServer(bgpServer, opts.GrpcHosts) + grpcServer := api.NewGrpcServer(bgpServer, opts.GrpcHosts) go func() { if err := grpcServer.Serve(); err != nil { log.Fatalf("failed to listen grpc port: %s", err) |