summaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-07-09 16:33:40 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2016-07-20 04:25:25 +0900
commite88a82ed507df6fb2c9411de52427c3b466203ca (patch)
tree561a5e8e8d74a6a8687e63d7bc47b07b2889e65e /server
parent804973bff5110b1c925167d6e8da6b188b63eb0a (diff)
table: remove ToApiStruct methods from policy.go
Preparation for removing gRPC dependency. Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
Diffstat (limited to 'server')
-rw-r--r--server/grpc_server.go234
-rw-r--r--server/server.go94
2 files changed, 290 insertions, 38 deletions
diff --git a/server/grpc_server.go b/server/grpc_server.go
index e4de06f5..64a7f839 100644
--- a/server/grpc_server.go
+++ b/server/grpc_server.go
@@ -21,10 +21,13 @@ import (
api "github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/config"
"github.com/osrg/gobgp/packet/bgp"
+ "github.com/osrg/gobgp/table"
"golang.org/x/net/context"
"google.golang.org/grpc"
"io"
"net"
+ "regexp"
+ "strconv"
"strings"
"sync"
)
@@ -667,7 +670,62 @@ func (s *Server) GetDefinedSet(ctx context.Context, arg *api.GetDefinedSetReques
if err != nil {
return nil, err
}
- return d.(*api.GetDefinedSetResponse), err
+ sets := make([]*api.DefinedSet, 0)
+ cd := d.(*config.DefinedSets)
+ for _, cs := range cd.PrefixSets {
+ ad := &api.DefinedSet{
+ Type: api.DefinedType_PREFIX,
+ Name: cs.PrefixSetName,
+ Prefixes: func() []*api.Prefix {
+ l := make([]*api.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)})
+ }
+ return l
+ }(),
+ }
+ sets = append(sets, ad)
+
+ }
+ for _, cs := range cd.NeighborSets {
+ ad := &api.DefinedSet{
+ Type: api.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,
+ Name: cs.CommunitySetName,
+ List: cs.CommunityList,
+ }
+ sets = append(sets, ad)
+ }
+ for _, cs := range cd.BgpDefinedSets.ExtCommunitySets {
+ ad := &api.DefinedSet{
+ Type: api.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,
+ Name: cs.AsPathSetName,
+ List: cs.AsPathList,
+ }
+ sets = append(sets, ad)
+ }
+
+ return &api.GetDefinedSetResponse{Sets: sets}, nil
}
func (s *Server) AddDefinedSet(ctx context.Context, arg *api.AddDefinedSetRequest) (*api.AddDefinedSetResponse, error) {
@@ -694,12 +752,142 @@ func (s *Server) ReplaceDefinedSet(ctx context.Context, arg *api.ReplaceDefinedS
return d.(*api.ReplaceDefinedSetResponse), err
}
+func toStatementApi(s *config.Statement) *api.Statement {
+ cs := &api.Conditions{}
+ if s.Conditions.MatchPrefixSet.PrefixSet != "" {
+ cs.PrefixSet = &api.MatchSet{
+ Type: api.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()),
+ Name: s.Conditions.MatchNeighborSet.NeighborSet,
+ }
+ }
+ if s.Conditions.BgpConditions.AsPathLength.Operator != "" {
+ cs.AsPathLength = &api.AsPathLength{
+ Length: s.Conditions.BgpConditions.AsPathLength.Value,
+ Type: api.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()),
+ 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()),
+ 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()),
+ Name: s.Conditions.BgpConditions.MatchExtCommunitySet.ExtCommunitySet,
+ }
+ }
+ cs.RpkiResult = int32(s.Conditions.BgpConditions.RpkiValidationResult.ToInt())
+ as := &api.Actions{
+ RouteAction: func() api.RouteAction {
+ if s.Actions.RouteDisposition.AcceptRoute {
+ return api.RouteAction_ACCEPT
+ }
+ return api.RouteAction_REJECT
+ }(),
+ Community: func() *api.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)]),
+ Communities: s.Actions.BgpActions.SetCommunity.SetCommunityMethod.CommunitiesList}
+ }(),
+ Med: func() *api.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
+ switch elems[1] {
+ case "+", "-":
+ action = api.MedActionType_MED_MOD
+ }
+ value, _ := strconv.Atoi(string(s.Actions.BgpActions.SetMed))
+ return &api.MedAction{
+ Value: int64(value),
+ Type: action,
+ }
+ }(),
+ AsPrepend: func() *api.AsPrependAction {
+ if len(s.Actions.BgpActions.SetAsPathPrepend.As) == 0 {
+ return nil
+ }
+ asn := 0
+ useleft := false
+ if s.Actions.BgpActions.SetAsPathPrepend.As != "last-as" {
+ asn, _ = strconv.Atoi(s.Actions.BgpActions.SetAsPathPrepend.As)
+ } else {
+ useleft = true
+ }
+ return &api.AsPrependAction{
+ Asn: uint32(asn),
+ Repeat: uint32(s.Actions.BgpActions.SetAsPathPrepend.RepeatN),
+ UseLeftMost: useleft,
+ }
+ }(),
+ ExtCommunity: func() *api.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)]),
+ Communities: s.Actions.BgpActions.SetExtCommunity.SetExtCommunityMethod.CommunitiesList,
+ }
+ }(),
+ Nexthop: func() *api.NexthopAction {
+ if len(string(s.Actions.BgpActions.SetNextHop)) == 0 {
+ return nil
+ }
+
+ if string(s.Actions.BgpActions.SetNextHop) == "self" {
+ return &api.NexthopAction{
+ Self: true,
+ }
+ }
+ return &api.NexthopAction{
+ Address: string(s.Actions.BgpActions.SetNextHop),
+ }
+ }(),
+ LocalPref: func() *api.LocalPrefAction {
+ if s.Actions.BgpActions.SetLocalPref == 0 {
+ return nil
+ }
+ return &api.LocalPrefAction{Value: s.Actions.BgpActions.SetLocalPref}
+ }(),
+ }
+ return &api.Statement{
+ Name: s.Name,
+ Conditions: cs,
+ Actions: as,
+ }
+}
+
func (s *Server) GetStatement(ctx context.Context, arg *api.GetStatementRequest) (*api.GetStatementResponse, error) {
d, err := s.get(REQ_GET_STATEMENT, arg)
if err != nil {
return nil, err
}
- return d.(*api.GetStatementResponse), err
+
+ l := make([]*api.Statement, 0)
+ for _, s := range d.([]*config.Statement) {
+ l = append(l, toStatementApi(s))
+ }
+ return &api.GetStatementResponse{Statements: l}, err
}
func (s *Server) AddStatement(ctx context.Context, arg *api.AddStatementRequest) (*api.AddStatementResponse, error) {
@@ -726,12 +914,29 @@ func (s *Server) ReplaceStatement(ctx context.Context, arg *api.ReplaceStatement
return d.(*api.ReplaceStatementResponse), err
}
+func toPolicyApi(p *config.PolicyDefinition) *api.Policy {
+ return &api.Policy{
+ Name: p.Name,
+ Statements: func() []*api.Statement {
+ l := make([]*api.Statement, 0)
+ for _, s := range p.Statements {
+ l = append(l, toStatementApi(&s))
+ }
+ return l
+ }(),
+ }
+}
+
func (s *Server) GetPolicy(ctx context.Context, arg *api.GetPolicyRequest) (*api.GetPolicyResponse, error) {
d, err := s.get(REQ_GET_POLICY, arg)
if err != nil {
return nil, err
}
- return d.(*api.GetPolicyResponse), err
+ l := make([]*api.Policy, 0)
+ for _, p := range d.([]*config.PolicyDefinition) {
+ l = append(l, toPolicyApi(p))
+ }
+ return &api.GetPolicyResponse{Policies: l}, err
}
func (s *Server) AddPolicy(ctx context.Context, arg *api.AddPolicyRequest) (*api.AddPolicyResponse, error) {
@@ -763,7 +968,28 @@ func (s *Server) GetPolicyAssignment(ctx context.Context, arg *api.GetPolicyAssi
if err != nil {
return nil, err
}
- return d.(*api.GetPolicyAssignmentResponse), err
+ a := d.(*PolicyAssignment)
+ return &api.GetPolicyAssignmentResponse{
+ Assignment: &api.PolicyAssignment{
+ Default: func() api.RouteAction {
+ switch a.Default {
+ case table.ROUTE_TYPE_ACCEPT:
+ return api.RouteAction_ACCEPT
+ case table.ROUTE_TYPE_REJECT:
+ return api.RouteAction_REJECT
+ }
+ return api.RouteAction_NONE
+
+ }(),
+ Policies: func() []*api.Policy {
+ l := make([]*api.Policy, 0)
+ for _, p := range a.PolicyDefinitions {
+ l = append(l, toPolicyApi(p))
+ }
+ return l
+ }(),
+ },
+ }, err
}
func (s *Server) AddPolicyAssignment(ctx context.Context, arg *api.AddPolicyAssignmentRequest) (*api.AddPolicyAssignmentResponse, error) {
diff --git a/server/server.go b/server/server.go
index 8552810c..5d1e2892 100644
--- a/server/server.go
+++ b/server/server.go
@@ -2156,20 +2156,6 @@ ERROR:
return
}
-func (server *BgpServer) handleGrpcGetDefinedSet(grpcReq *GrpcRequest) (*api.GetDefinedSetResponse, error) {
- arg := grpcReq.Data.(*api.GetDefinedSetRequest)
- typ := table.DefinedType(arg.Type)
- set, ok := server.policy.DefinedSetMap[typ]
- if !ok {
- return &api.GetDefinedSetResponse{}, fmt.Errorf("invalid defined-set type: %d", typ)
- }
- sets := make([]*api.DefinedSet, 0)
- for _, s := range set {
- sets = append(sets, s.ToApiStruct())
- }
- return &api.GetDefinedSetResponse{Sets: sets}, nil
-}
-
func (server *BgpServer) handleAddNeighbor(c *config.Neighbor) error {
addr := c.Config.NeighborAddress
if _, y := server.neighborMap[addr]; y {
@@ -2310,6 +2296,39 @@ func (server *BgpServer) handleUpdateNeighbor(c *config.Neighbor) (bool, error)
return policyUpdated, err
}
+func (server *BgpServer) handleGrpcGetDefinedSet(grpcReq *GrpcRequest) (*config.DefinedSets, error) {
+ arg := grpcReq.Data.(*api.GetDefinedSetRequest)
+ typ := table.DefinedType(arg.Type)
+ set, ok := server.policy.DefinedSetMap[typ]
+ if !ok {
+ return nil, fmt.Errorf("invalid defined-set type: %d", typ)
+ }
+ sets := config.DefinedSets{
+ PrefixSets: make([]config.PrefixSet, 0),
+ NeighborSets: make([]config.NeighborSet, 0),
+ BgpDefinedSets: config.BgpDefinedSets{
+ CommunitySets: make([]config.CommunitySet, 0),
+ ExtCommunitySets: make([]config.ExtCommunitySet, 0),
+ AsPathSets: make([]config.AsPathSet, 0),
+ },
+ }
+ for _, s := range set {
+ switch s.(type) {
+ case *table.PrefixSet:
+ sets.PrefixSets = append(sets.PrefixSets, *s.(*table.PrefixSet).ToConfig())
+ case *table.NeighborSet:
+ sets.NeighborSets = append(sets.NeighborSets, *s.(*table.NeighborSet).ToConfig())
+ case *table.CommunitySet:
+ sets.BgpDefinedSets.CommunitySets = append(sets.BgpDefinedSets.CommunitySets, *s.(*table.CommunitySet).ToConfig())
+ case *table.ExtCommunitySet:
+ sets.BgpDefinedSets.ExtCommunitySets = append(sets.BgpDefinedSets.ExtCommunitySets, *s.(*table.ExtCommunitySet).ToConfig())
+ case *table.AsPathSet:
+ sets.BgpDefinedSets.AsPathSets = append(sets.BgpDefinedSets.AsPathSets, *s.(*table.AsPathSet).ToConfig())
+ }
+ }
+ return &sets, nil
+}
+
func (server *BgpServer) handleGrpcAddDefinedSet(grpcReq *GrpcRequest) (*api.AddDefinedSetResponse, error) {
arg := grpcReq.Data.(*api.AddDefinedSetRequest)
set := arg.Set
@@ -2383,12 +2402,12 @@ func (server *BgpServer) handleGrpcReplaceDefinedSet(grpcReq *GrpcRequest) (*api
return &api.ReplaceDefinedSetResponse{}, d.Replace(s)
}
-func (server *BgpServer) handleGrpcGetStatement(grpcReq *GrpcRequest) (*api.GetStatementResponse, error) {
- l := make([]*api.Statement, 0)
+func (server *BgpServer) handleGrpcGetStatement(grpcReq *GrpcRequest) ([]*config.Statement, error) {
+ l := make([]*config.Statement, 0)
for _, s := range server.policy.StatementMap {
- l = append(l, s.ToApiStruct())
+ l = append(l, s.ToConfig())
}
- return &api.GetStatementResponse{Statements: l}, nil
+ return l, nil
}
func (server *BgpServer) handleGrpcAddStatement(grpcReq *GrpcRequest) (*api.AddStatementResponse, error) {
@@ -2450,12 +2469,12 @@ func (server *BgpServer) handleGrpcReplaceStatement(grpcReq *GrpcRequest) (*api.
return &api.ReplaceStatementResponse{}, err
}
-func (server *BgpServer) handleGrpcGetPolicy(grpcReq *GrpcRequest) (*api.GetPolicyResponse, error) {
- policies := make([]*api.Policy, 0, len(server.policy.PolicyMap))
+func (server *BgpServer) handleGrpcGetPolicy(grpcReq *GrpcRequest) ([]*config.PolicyDefinition, error) {
+ policies := make([]*config.PolicyDefinition, 0, len(server.policy.PolicyMap))
for _, s := range server.policy.PolicyMap {
- policies = append(policies, s.ToApiStruct())
+ policies = append(policies, s.ToConfig())
}
- return &api.GetPolicyResponse{Policies: policies}, nil
+ return policies, nil
}
func (server *BgpServer) policyInUse(x *table.Policy) bool {
@@ -2630,21 +2649,28 @@ func (server *BgpServer) getPolicyInfo(a *api.PolicyAssignment) (string, table.P
}
-func (server *BgpServer) handleGrpcGetPolicyAssignment(grpcReq *GrpcRequest) (*api.GetPolicyAssignmentResponse, error) {
- rsp := &api.GetPolicyAssignmentResponse{}
+// temporarily
+type PolicyAssignment struct {
+ Default table.RouteType
+ PolicyDefinitions []*config.PolicyDefinition
+}
+
+func (server *BgpServer) handleGrpcGetPolicyAssignment(grpcReq *GrpcRequest) (*PolicyAssignment, error) {
id, dir, err := server.getPolicyInfo(grpcReq.Data.(*api.GetPolicyAssignmentRequest).Assignment)
if err != nil {
- return rsp, err
- }
- rsp.Assignment = &api.PolicyAssignment{
- Default: server.policy.GetDefaultPolicy(id, dir).ToApiStruct(),
- }
- ps := server.policy.GetPolicy(id, dir)
- rsp.Assignment.Policies = make([]*api.Policy, 0, len(ps))
- for _, x := range ps {
- rsp.Assignment.Policies = append(rsp.Assignment.Policies, x.ToApiStruct())
+ return nil, err
}
- return rsp, nil
+ return &PolicyAssignment{
+ Default: server.policy.GetDefaultPolicy(id, dir),
+ PolicyDefinitions: func() []*config.PolicyDefinition {
+ ps := server.policy.GetPolicy(id, dir)
+ l := make([]*config.PolicyDefinition, 0, len(ps))
+ for _, p := range ps {
+ l = append(l, p.ToConfig())
+ }
+ return l
+ }(),
+ }, nil
}
func (server *BgpServer) handleGrpcAddPolicyAssignment(grpcReq *GrpcRequest) (*api.AddPolicyAssignmentResponse, error) {