diff options
-rw-r--r-- | api/gobgp.pb.go | 348 | ||||
-rw-r--r-- | api/gobgp.proto | 54 | ||||
-rw-r--r-- | gobgp/policy.go | 124 | ||||
-rw-r--r-- | policy/policy.go | 106 | ||||
-rw-r--r-- | server/grpc_server.go | 191 | ||||
-rw-r--r-- | server/server.go | 441 |
6 files changed, 350 insertions, 914 deletions
diff --git a/api/gobgp.pb.go b/api/gobgp.pb.go index 132701c1..78a9b6a3 100644 --- a/api/gobgp.pb.go +++ b/api/gobgp.pb.go @@ -41,7 +41,6 @@ It has these top-level messages: Actions Statement PolicyDefinition - RoutingPolicy ApplyPolicy */ package api @@ -526,33 +525,17 @@ type PolicyArguments struct { Operation Operation `protobuf:"varint,2,opt,name=operation,enum=api.Operation" json:"operation,omitempty"` RouterId string `protobuf:"bytes,3,opt,name=router_id" json:"router_id,omitempty"` Name string `protobuf:"bytes,4,opt,name=name" json:"name,omitempty"` - PrefixSet *PrefixSet `protobuf:"bytes,5,opt,name=prefix_set" json:"prefix_set,omitempty"` - NeighborSet *NeighborSet `protobuf:"bytes,6,opt,name=neighbor_set" json:"neighbor_set,omitempty"` - PolicyDifinition *PolicyDefinition `protobuf:"bytes,7,opt,name=policy_difinition" json:"policy_difinition,omitempty"` - ApplyPolicy *ApplyPolicy `protobuf:"bytes,8,opt,name=apply_policy" json:"apply_policy,omitempty"` + PolicyDefinition *PolicyDefinition `protobuf:"bytes,6,opt,name=policy_definition" json:"policy_definition,omitempty"` + ApplyPolicy *ApplyPolicy `protobuf:"bytes,7,opt,name=apply_policy" json:"apply_policy,omitempty"` } func (m *PolicyArguments) Reset() { *m = PolicyArguments{} } func (m *PolicyArguments) String() string { return proto.CompactTextString(m) } func (*PolicyArguments) ProtoMessage() {} -func (m *PolicyArguments) GetPrefixSet() *PrefixSet { +func (m *PolicyArguments) GetPolicyDefinition() *PolicyDefinition { if m != nil { - return m.PrefixSet - } - return nil -} - -func (m *PolicyArguments) GetNeighborSet() *NeighborSet { - if m != nil { - return m.NeighborSet - } - return nil -} - -func (m *PolicyArguments) GetPolicyDifinition() *PolicyDefinition { - if m != nil { - return m.PolicyDifinition + return m.PolicyDefinition } return nil } @@ -1091,21 +1074,6 @@ func (m *PolicyDefinition) GetStatementList() []*Statement { return nil } -type RoutingPolicy struct { - PolicyDifinition []*PolicyDefinition `protobuf:"bytes,1,rep,name=policy_difinition" json:"policy_difinition,omitempty"` -} - -func (m *RoutingPolicy) Reset() { *m = RoutingPolicy{} } -func (m *RoutingPolicy) String() string { return proto.CompactTextString(m) } -func (*RoutingPolicy) ProtoMessage() {} - -func (m *RoutingPolicy) GetPolicyDifinition() []*PolicyDefinition { - if m != nil { - return m.PolicyDifinition - } - return nil -} - type ApplyPolicy struct { ImportPolicies []*PolicyDefinition `protobuf:"bytes,1,rep,name=import_policies" json:"import_policies,omitempty"` DefaultImportPolicy string `protobuf:"bytes,2,opt,name=default_import_policy" json:"default_import_policy,omitempty"` @@ -1164,12 +1132,6 @@ type GrpcClient interface { ModPath(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPathClient, error) GetNeighborPolicy(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (*ApplyPolicy, error) ModNeighborPolicy(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModNeighborPolicyClient, error) - GetPolicyPrefixes(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (Grpc_GetPolicyPrefixesClient, error) - GetPolicyPrefix(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*PrefixSet, error) - ModPolicyPrefix(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPolicyPrefixClient, error) - GetPolicyNeighbors(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (Grpc_GetPolicyNeighborsClient, error) - GetPolicyNeighbor(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*NeighborSet, error) - ModPolicyNeighbor(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPolicyNeighborClient, error) GetPolicyRoutePolicies(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (Grpc_GetPolicyRoutePoliciesClient, error) GetPolicyRoutePolicy(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*PolicyDefinition, error) ModPolicyRoutePolicy(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPolicyRoutePolicyClient, error) @@ -1422,152 +1384,8 @@ func (x *grpcModNeighborPolicyClient) Recv() (*Error, error) { return m, nil } -func (c *grpcClient) GetPolicyPrefixes(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (Grpc_GetPolicyPrefixesClient, error) { - stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[5], c.cc, "/api.Grpc/GetPolicyPrefixes", opts...) - if err != nil { - return nil, err - } - x := &grpcGetPolicyPrefixesClient{stream} - if err := x.ClientStream.SendMsg(in); err != nil { - return nil, err - } - if err := x.ClientStream.CloseSend(); err != nil { - return nil, err - } - return x, nil -} - -type Grpc_GetPolicyPrefixesClient interface { - Recv() (*PrefixSet, error) - grpc.ClientStream -} - -type grpcGetPolicyPrefixesClient struct { - grpc.ClientStream -} - -func (x *grpcGetPolicyPrefixesClient) Recv() (*PrefixSet, error) { - m := new(PrefixSet) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func (c *grpcClient) GetPolicyPrefix(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*PrefixSet, error) { - out := new(PrefixSet) - err := grpc.Invoke(ctx, "/api.Grpc/GetPolicyPrefix", in, out, c.cc, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -func (c *grpcClient) ModPolicyPrefix(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPolicyPrefixClient, error) { - stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[6], c.cc, "/api.Grpc/ModPolicyPrefix", opts...) - if err != nil { - return nil, err - } - x := &grpcModPolicyPrefixClient{stream} - return x, nil -} - -type Grpc_ModPolicyPrefixClient interface { - Send(*PolicyArguments) error - Recv() (*Error, error) - grpc.ClientStream -} - -type grpcModPolicyPrefixClient struct { - grpc.ClientStream -} - -func (x *grpcModPolicyPrefixClient) Send(m *PolicyArguments) error { - return x.ClientStream.SendMsg(m) -} - -func (x *grpcModPolicyPrefixClient) Recv() (*Error, error) { - m := new(Error) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func (c *grpcClient) GetPolicyNeighbors(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (Grpc_GetPolicyNeighborsClient, error) { - stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[7], c.cc, "/api.Grpc/GetPolicyNeighbors", opts...) - if err != nil { - return nil, err - } - x := &grpcGetPolicyNeighborsClient{stream} - if err := x.ClientStream.SendMsg(in); err != nil { - return nil, err - } - if err := x.ClientStream.CloseSend(); err != nil { - return nil, err - } - return x, nil -} - -type Grpc_GetPolicyNeighborsClient interface { - Recv() (*NeighborSet, error) - grpc.ClientStream -} - -type grpcGetPolicyNeighborsClient struct { - grpc.ClientStream -} - -func (x *grpcGetPolicyNeighborsClient) Recv() (*NeighborSet, error) { - m := new(NeighborSet) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func (c *grpcClient) GetPolicyNeighbor(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*NeighborSet, error) { - out := new(NeighborSet) - err := grpc.Invoke(ctx, "/api.Grpc/GetPolicyNeighbor", in, out, c.cc, opts...) - if err != nil { - return nil, err - } - return out, nil -} - -func (c *grpcClient) ModPolicyNeighbor(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPolicyNeighborClient, error) { - stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[8], c.cc, "/api.Grpc/ModPolicyNeighbor", opts...) - if err != nil { - return nil, err - } - x := &grpcModPolicyNeighborClient{stream} - return x, nil -} - -type Grpc_ModPolicyNeighborClient interface { - Send(*PolicyArguments) error - Recv() (*Error, error) - grpc.ClientStream -} - -type grpcModPolicyNeighborClient struct { - grpc.ClientStream -} - -func (x *grpcModPolicyNeighborClient) Send(m *PolicyArguments) error { - return x.ClientStream.SendMsg(m) -} - -func (x *grpcModPolicyNeighborClient) Recv() (*Error, error) { - m := new(Error) - if err := x.ClientStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - func (c *grpcClient) GetPolicyRoutePolicies(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (Grpc_GetPolicyRoutePoliciesClient, error) { - stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[9], c.cc, "/api.Grpc/GetPolicyRoutePolicies", opts...) + stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[5], c.cc, "/api.Grpc/GetPolicyRoutePolicies", opts...) if err != nil { return nil, err } @@ -1608,7 +1426,7 @@ func (c *grpcClient) GetPolicyRoutePolicy(ctx context.Context, in *PolicyArgumen } func (c *grpcClient) ModPolicyRoutePolicy(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPolicyRoutePolicyClient, error) { - stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[10], c.cc, "/api.Grpc/ModPolicyRoutePolicy", opts...) + stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[6], c.cc, "/api.Grpc/ModPolicyRoutePolicy", opts...) if err != nil { return nil, err } @@ -1655,12 +1473,6 @@ type GrpcServer interface { ModPath(Grpc_ModPathServer) error GetNeighborPolicy(context.Context, *Arguments) (*ApplyPolicy, error) ModNeighborPolicy(Grpc_ModNeighborPolicyServer) error - GetPolicyPrefixes(*PolicyArguments, Grpc_GetPolicyPrefixesServer) error - GetPolicyPrefix(context.Context, *PolicyArguments) (*PrefixSet, error) - ModPolicyPrefix(Grpc_ModPolicyPrefixServer) error - GetPolicyNeighbors(*PolicyArguments, Grpc_GetPolicyNeighborsServer) error - GetPolicyNeighbor(context.Context, *PolicyArguments) (*NeighborSet, error) - ModPolicyNeighbor(Grpc_ModPolicyNeighborServer) error GetPolicyRoutePolicies(*PolicyArguments, Grpc_GetPolicyRoutePoliciesServer) error GetPolicyRoutePolicy(context.Context, *PolicyArguments) (*PolicyDefinition, error) ModPolicyRoutePolicy(Grpc_ModPolicyRoutePolicyServer) error @@ -1893,124 +1705,6 @@ func (x *grpcModNeighborPolicyServer) Recv() (*PolicyArguments, error) { return m, nil } -func _Grpc_GetPolicyPrefixes_Handler(srv interface{}, stream grpc.ServerStream) error { - m := new(PolicyArguments) - if err := stream.RecvMsg(m); err != nil { - return err - } - return srv.(GrpcServer).GetPolicyPrefixes(m, &grpcGetPolicyPrefixesServer{stream}) -} - -type Grpc_GetPolicyPrefixesServer interface { - Send(*PrefixSet) error - grpc.ServerStream -} - -type grpcGetPolicyPrefixesServer struct { - grpc.ServerStream -} - -func (x *grpcGetPolicyPrefixesServer) Send(m *PrefixSet) error { - return x.ServerStream.SendMsg(m) -} - -func _Grpc_GetPolicyPrefix_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) { - in := new(PolicyArguments) - if err := codec.Unmarshal(buf, in); err != nil { - return nil, err - } - out, err := srv.(GrpcServer).GetPolicyPrefix(ctx, in) - if err != nil { - return nil, err - } - return out, nil -} - -func _Grpc_ModPolicyPrefix_Handler(srv interface{}, stream grpc.ServerStream) error { - return srv.(GrpcServer).ModPolicyPrefix(&grpcModPolicyPrefixServer{stream}) -} - -type Grpc_ModPolicyPrefixServer interface { - Send(*Error) error - Recv() (*PolicyArguments, error) - grpc.ServerStream -} - -type grpcModPolicyPrefixServer struct { - grpc.ServerStream -} - -func (x *grpcModPolicyPrefixServer) Send(m *Error) error { - return x.ServerStream.SendMsg(m) -} - -func (x *grpcModPolicyPrefixServer) Recv() (*PolicyArguments, error) { - m := new(PolicyArguments) - if err := x.ServerStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - -func _Grpc_GetPolicyNeighbors_Handler(srv interface{}, stream grpc.ServerStream) error { - m := new(PolicyArguments) - if err := stream.RecvMsg(m); err != nil { - return err - } - return srv.(GrpcServer).GetPolicyNeighbors(m, &grpcGetPolicyNeighborsServer{stream}) -} - -type Grpc_GetPolicyNeighborsServer interface { - Send(*NeighborSet) error - grpc.ServerStream -} - -type grpcGetPolicyNeighborsServer struct { - grpc.ServerStream -} - -func (x *grpcGetPolicyNeighborsServer) Send(m *NeighborSet) error { - return x.ServerStream.SendMsg(m) -} - -func _Grpc_GetPolicyNeighbor_Handler(srv interface{}, ctx context.Context, codec grpc.Codec, buf []byte) (interface{}, error) { - in := new(PolicyArguments) - if err := codec.Unmarshal(buf, in); err != nil { - return nil, err - } - out, err := srv.(GrpcServer).GetPolicyNeighbor(ctx, in) - if err != nil { - return nil, err - } - return out, nil -} - -func _Grpc_ModPolicyNeighbor_Handler(srv interface{}, stream grpc.ServerStream) error { - return srv.(GrpcServer).ModPolicyNeighbor(&grpcModPolicyNeighborServer{stream}) -} - -type Grpc_ModPolicyNeighborServer interface { - Send(*Error) error - Recv() (*PolicyArguments, error) - grpc.ServerStream -} - -type grpcModPolicyNeighborServer struct { - grpc.ServerStream -} - -func (x *grpcModPolicyNeighborServer) Send(m *Error) error { - return x.ServerStream.SendMsg(m) -} - -func (x *grpcModPolicyNeighborServer) Recv() (*PolicyArguments, error) { - m := new(PolicyArguments) - if err := x.ServerStream.RecvMsg(m); err != nil { - return nil, err - } - return m, nil -} - func _Grpc_GetPolicyRoutePolicies_Handler(srv interface{}, stream grpc.ServerStream) error { m := new(PolicyArguments) if err := stream.RecvMsg(m); err != nil { @@ -2111,14 +1805,6 @@ var _Grpc_serviceDesc = grpc.ServiceDesc{ Handler: _Grpc_GetNeighborPolicy_Handler, }, { - MethodName: "GetPolicyPrefix", - Handler: _Grpc_GetPolicyPrefix_Handler, - }, - { - MethodName: "GetPolicyNeighbor", - Handler: _Grpc_GetPolicyNeighbor_Handler, - }, - { MethodName: "GetPolicyRoutePolicy", Handler: _Grpc_GetPolicyRoutePolicy_Handler, }, @@ -2152,28 +1838,6 @@ var _Grpc_serviceDesc = grpc.ServiceDesc{ ClientStreams: true, }, { - StreamName: "GetPolicyPrefixes", - Handler: _Grpc_GetPolicyPrefixes_Handler, - ServerStreams: true, - }, - { - StreamName: "ModPolicyPrefix", - Handler: _Grpc_ModPolicyPrefix_Handler, - ServerStreams: true, - ClientStreams: true, - }, - { - StreamName: "GetPolicyNeighbors", - Handler: _Grpc_GetPolicyNeighbors_Handler, - ServerStreams: true, - }, - { - StreamName: "ModPolicyNeighbor", - Handler: _Grpc_ModPolicyNeighbor_Handler, - ServerStreams: true, - ClientStreams: true, - }, - { StreamName: "GetPolicyRoutePolicies", Handler: _Grpc_GetPolicyRoutePolicies_Handler, ServerStreams: true, diff --git a/api/gobgp.proto b/api/gobgp.proto index 0d38ad58..29038ea8 100644 --- a/api/gobgp.proto +++ b/api/gobgp.proto @@ -34,12 +34,6 @@ service Grpc { rpc ModPath(stream ModPathArguments) returns (stream Error) {} rpc GetNeighborPolicy(Arguments) returns (ApplyPolicy) {} rpc ModNeighborPolicy(stream PolicyArguments) returns (stream Error) {} - rpc GetPolicyPrefixes(PolicyArguments) returns (stream PrefixSet) {} - rpc GetPolicyPrefix(PolicyArguments) returns (PrefixSet) {} - rpc ModPolicyPrefix(stream PolicyArguments) returns (stream Error) {} - rpc GetPolicyNeighbors(PolicyArguments) returns (stream NeighborSet) {} - rpc GetPolicyNeighbor(PolicyArguments) returns (NeighborSet) {} - rpc ModPolicyNeighbor(stream PolicyArguments) returns (stream Error) {} rpc GetPolicyRoutePolicies(PolicyArguments) returns (stream PolicyDefinition) {} rpc GetPolicyRoutePolicy(PolicyArguments) returns (PolicyDefinition) {} rpc ModPolicyRoutePolicy(stream PolicyArguments) returns (stream Error) {} @@ -70,10 +64,8 @@ message PolicyArguments { Operation operation = 2; string router_id = 3; string name = 4; - PrefixSet prefix_set = 5; - NeighborSet neighbor_set = 6; - PolicyDefinition policy_difinition = 7; - ApplyPolicy apply_policy = 8; + PolicyDefinition policy_definition = 6; + ApplyPolicy apply_policy = 7; } enum Resource { @@ -357,54 +349,50 @@ message Peer { } message Prefix { - string address = 1; - uint32 mask_length = 2; - string mask_length_range = 3; + string address = 1; + uint32 mask_length = 2; + string mask_length_range = 3; } message PrefixSet { - string prefix_set_name = 1; + string prefix_set_name = 1; repeated Prefix prefix_list = 2; } message Neighbor { - string address = 1; + string address = 1; } message NeighborSet { - string neighbor_set_name = 1; - repeated Neighbor neighbor_list = 2; + string neighbor_set_name = 1; + repeated Neighbor neighbor_list = 2; } message AsPathLength { - string value = 1; - string operator = 2; + string value = 1; + string operator = 2; } message Conditions { - PrefixSet match_prefix_set = 1; - NeighborSet match_neighbor_set = 2; - AsPathLength match_as_path_length = 3; - string match_set_options = 4; + PrefixSet match_prefix_set = 1; + NeighborSet match_neighbor_set = 2; + AsPathLength match_as_path_length = 3; + string match_set_options = 4; } message Actions { - string route_action = 1; + string route_action = 1; } message Statement { - string statement_neme = 1; - Conditions conditions = 2; - Actions actions = 3; + string statement_neme = 1; + Conditions conditions = 2; + Actions actions = 3; } message PolicyDefinition { - string policy_definition_name = 1; - repeated Statement statement_list = 2; -} - -message RoutingPolicy { - repeated PolicyDefinition policy_difinition = 1; + string policy_definition_name = 1; + repeated Statement statement_list = 2; } message ApplyPolicy { diff --git a/gobgp/policy.go b/gobgp/policy.go index 7fad0640..eda45afc 100644 --- a/gobgp/policy.go +++ b/gobgp/policy.go @@ -55,9 +55,8 @@ func showPolicyPrefixes() error { arg := &api.PolicyArguments{ Resource: api.Resource_POLICY_PREFIX, } - stream, e := client.GetPolicyPrefixes(context.Background(), arg) + stream, e := client.GetPolicyRoutePolicies(context.Background(), arg) if e != nil { - fmt.Println(e) return e } m := prefixes{} @@ -68,7 +67,7 @@ func showPolicyPrefixes() error { } else if e != nil { return e } - m = append(m, p) + m = append(m, p.StatementList[0].Conditions.MatchPrefixSet) } if globalOpts.Json { @@ -105,11 +104,11 @@ func showPolicyPrefix(args []string) error { Resource: api.Resource_POLICY_PREFIX, Name: args[0], } - ps, e := client.GetPolicyPrefix(context.Background(), arg) + pd, e := client.GetPolicyRoutePolicy(context.Background(), arg) if e != nil { return e } - + ps := pd.StatementList[0].Conditions.MatchPrefixSet if globalOpts.Json { j, _ := json.Marshal(ps) fmt.Println(string(j)) @@ -184,6 +183,44 @@ func parsePrefixSet(eArgs []string) (*api.PrefixSet, error) { } return prefixSet, nil } +func modPolicy(resource api.Resource, op api.Operation, data interface{}) error { + pd := &api.PolicyDefinition{} + if resource != api.Resource_POLICY_ROUTEPOLICY { + co := &api.Conditions{} + switch resource { + case api.Resource_POLICY_PREFIX: + co.MatchPrefixSet = data.(*api.PrefixSet) + case api.Resource_POLICY_NEIGHBOR: + co.MatchNeighborSet = data.(*api.NeighborSet) + } + pd.StatementList = []*api.Statement{{Conditions: co}} + } else { + pd = data.(*api.PolicyDefinition) + } + arg := &api.PolicyArguments{ + Resource: resource, + Operation: op, + PolicyDefinition: pd, + } + stream, err := client.ModPolicyRoutePolicy(context.Background()) + if err != nil { + return err + } + err = stream.Send(arg) + if err != nil { + return err + } + stream.CloseSend() + + res, e := stream.Recv() + if e != nil { + return e + } + if res.Code != api.Error_SUCCESS { + return fmt.Errorf("error: code: %d, msg: %s", res.Code, res.Msg) + } + return nil +} func modPolicyPrefix(modtype string, eArgs []string) error { prefixSet := &api.PrefixSet{} @@ -215,29 +252,9 @@ func modPolicyPrefix(modtype string, eArgs []string) error { operation = api.Operation_DEL } } - - arg := &api.PolicyArguments{ - Resource: api.Resource_POLICY_PREFIX, - Operation: operation, - PrefixSet: prefixSet, - } - stream, err := client.ModPolicyPrefix(context.Background()) - if err != nil { - return err - } - err = stream.Send(arg) - if err != nil { - return err - } - stream.CloseSend() - - res, e := stream.Recv() - if e != nil { + if e = modPolicy(api.Resource_POLICY_PREFIX, operation, prefixSet); e != nil { return e } - if res.Code != api.Error_SUCCESS { - return fmt.Errorf("error: code: %d, msg: %s", res.Code, res.Msg) - } return nil } @@ -262,20 +279,19 @@ func showPolicyNeighbors() error { arg := &api.PolicyArguments{ Resource: api.Resource_POLICY_NEIGHBOR, } - stream, e := client.GetPolicyNeighbors(context.Background(), arg) + stream, e := client.GetPolicyRoutePolicies(context.Background(), arg) if e != nil { - fmt.Println(e) return e } m := neighbors{} for { - n, e := stream.Recv() + p, e := stream.Recv() if e == io.EOF { break } else if e != nil { return e } - m = append(m, n) + m = append(m, p.StatementList[0].Conditions.MatchNeighborSet) } if globalOpts.Json { @@ -311,11 +327,11 @@ func showPolicyNeighbor(args []string) error { Resource: api.Resource_POLICY_NEIGHBOR, Name: args[0], } - ns, e := client.GetPolicyNeighbor(context.Background(), arg) + pd, e := client.GetPolicyRoutePolicy(context.Background(), arg) if e != nil { return e } - + ns := pd.StatementList[0].Conditions.MatchNeighborSet if globalOpts.Json { j, _ := json.Marshal(ns) fmt.Println(string(j)) @@ -389,29 +405,9 @@ func modPolicyNeighbor(modtype string, eArgs []string) error { operation = api.Operation_DEL } } - - arg := &api.PolicyArguments{ - Resource: api.Resource_POLICY_NEIGHBOR, - Operation: operation, - NeighborSet: neighborSet, - } - stream, err := client.ModPolicyNeighbor(context.Background()) - if err != nil { - return err - } - err = stream.Send(arg) - if err != nil { - return err - } - stream.CloseSend() - - res, e := stream.Recv() - if e != nil { + if e = modPolicy(api.Resource_POLICY_NEIGHBOR, operation, neighborSet); e != nil { return e } - if res.Code != api.Error_SUCCESS { - return fmt.Errorf("error: code: %d, msg: %s", res.Code, res.Msg) - } return nil } @@ -464,7 +460,6 @@ func showPolicyRoutePolicies() error { } stream, e := client.GetPolicyRoutePolicies(context.Background(), arg) if e != nil { - fmt.Println(e) return e } m := policyDefinitions{} @@ -631,28 +626,9 @@ func modPolicyRoutePolicy(modtype string, eArgs []string) error { default: return fmt.Errorf("invalid modType %s", modtype) } - arg := &api.PolicyArguments{ - Resource: api.Resource_POLICY_ROUTEPOLICY, - Operation: operation, - PolicyDifinition: pd, - } - stream, err := client.ModPolicyNeighbor(context.Background()) - if err != nil { - return err - } - err = stream.Send(arg) - if err != nil { - return err - } - stream.CloseSend() - - res, e := stream.Recv() - if e != nil { + if e := modPolicy(api.Resource_POLICY_ROUTEPOLICY, operation, pd); e != nil { return e } - if res.Code != api.Error_SUCCESS { - return fmt.Errorf("error: code: %d, msg: %s", res.Code, res.Msg) - } return nil } diff --git a/policy/policy.go b/policy/policy.go index 32799d43..cff79aae 100644 --- a/policy/policy.go +++ b/policy/policy.go @@ -939,91 +939,6 @@ func MatchSetOptionToString(option config.MatchSetOptionsType) string { return op } -func (p *Policy) ToApiStruct() *api.PolicyDefinition { - resStatements := make([]*api.Statement, 0) - for _, st := range p.Statements { - resPrefixSet := &api.PrefixSet{} - resNeighborSet := &api.NeighborSet{} - resAsPathLength := &api.AsPathLength{} - for _, condition := range st.Conditions { - switch reflect.TypeOf(condition) { - case reflect.TypeOf(&PrefixCondition{}): - prefixCondition := condition.(*PrefixCondition) - resPrefixList := make([]*api.Prefix, 0) - for _, prefix := range prefixCondition.PrefixList { - - resPrefix := &api.Prefix{ - Address: prefix.Address.String(), - MaskLength: uint32(prefix.Masklength), - } - if min, ok := prefix.MasklengthRange[MASK_LENGTH_RANGE_MIN]; ok { - if max, ok := prefix.MasklengthRange[MASK_LENGTH_RANGE_MAX]; ok { - resPrefix.MaskLengthRange = fmt.Sprintf("%d..%d", min, max) - } - } - - resPrefixList = append(resPrefixList, resPrefix) - } - resPrefixSet = &api.PrefixSet{ - PrefixSetName: prefixCondition.PrefixConditionName, - PrefixList: resPrefixList, - } - case reflect.TypeOf(&NeighborCondition{}): - neighborCondition := condition.(*NeighborCondition) - resNeighborList := make([]*api.Neighbor, 0) - for _, neighbor := range neighborCondition.NeighborList { - resNeighbor := &api.Neighbor{ - Address: neighbor.String(), - } - resNeighborList = append(resNeighborList, resNeighbor) - } - resNeighborSet = &api.NeighborSet{ - NeighborSetName: neighborCondition.NeighborConditionName, - NeighborList: resNeighborList, - } - case reflect.TypeOf(&AsPathLengthCondition{}): - asPathLengthCondition := condition.(*AsPathLengthCondition) - var op string - switch asPathLengthCondition.Operator { - case ATTRIBUTE_EQ: - op = "eq" - case ATTRIBUTE_GE: - op = "ge" - case ATTRIBUTE_LE: - op = "le" - } - resAsPathLength = &api.AsPathLength{ - Value: fmt.Sprintf("%d", asPathLengthCondition.Value), - Operator: op, - } - } - } - resConditions := &api.Conditions{ - MatchPrefixSet: resPrefixSet, - MatchNeighborSet: resNeighborSet, - MatchAsPathLength: resAsPathLength, - MatchSetOptions: MatchSetOptionToString(st.MatchSetOptions), - } - resActions := &api.Actions{ - RouteAction: ROUTE_REJECT, - } - if st.routingAction.AcceptRoute { - resActions.RouteAction = ROUTE_ACCEPT - } - resStatement := &api.Statement{ - StatementNeme: st.Name, - Conditions: resConditions, - Actions: resActions, - } - resStatements = append(resStatements, resStatement) - } - - return &api.PolicyDefinition{ - PolicyDefinitionName: p.Name, - StatementList: resStatements, - } -} - // find index PrefixSet of request from PrefixSet of configuration file. // Return the idxPrefixSet of the location where the name of PrefixSet matches, // and idxPrefix of the location where element of PrefixSet matches @@ -1208,11 +1123,8 @@ func ConditionsToConfigStruct(reqConditions *api.Conditions) config.Conditions { conditions.MatchNeighborSet = reqConditions.MatchNeighborSet.NeighborSetName } if reqConditions.MatchAsPathLength != nil { - asPathLength := AsPathLengthToConfigStruct(reqConditions.MatchAsPathLength) - bgpConditions := config.BgpConditions{ - AsPathLength: asPathLength, - } - conditions.BgpConditions = bgpConditions + conditions.BgpConditions.AsPathLength = + AsPathLengthToConfigStruct(reqConditions.MatchAsPathLength) } var setOption config.MatchSetOptionsType switch reqConditions.MatchSetOptions { @@ -1228,17 +1140,12 @@ func ConditionsToConfigStruct(reqConditions *api.Conditions) config.Conditions { } func ActionsToConfigStruct(reqActions *api.Actions) config.Actions { - acceptRoute := false - rejectRoute := false + actions := config.Actions{} switch reqActions.RouteAction { case ROUTE_ACCEPT: - acceptRoute = true + actions.AcceptRoute = true case ROUTE_REJECT: - rejectRoute = true - } - actions := config.Actions{ - AcceptRoute: acceptRoute, - RejectRoute: rejectRoute, + actions.RejectRoute = true } return actions } @@ -1295,11 +1202,10 @@ func PolicyDefinitionToApiStruct(pd config.PolicyDefinition, df config.DefinedSe if idxNeighborSet != -1 { neighborSet = NeighborSetToApiStruct(conNeighborSetList[idxNeighborSet]) } - asPathLength := AsPathLengthToApiStruct(st.Conditions.BgpConditions.AsPathLength) resConditions := &api.Conditions{ MatchPrefixSet: prefixSet, MatchNeighborSet: neighborSet, - MatchAsPathLength: asPathLength, + MatchAsPathLength: AsPathLengthToApiStruct(st.Conditions.BgpConditions.AsPathLength), MatchSetOptions: MatchSetOptionToString(conditions.MatchSetOptions), } resActions := &api.Actions{ diff --git a/server/grpc_server.go b/server/grpc_server.go index 19a2ba28..618344fd 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -346,57 +346,6 @@ func (s *Server) ModNeighborPolicy(stream api.Grpc_ModNeighborPolicyServer) erro } } -func (s *Server) getPolicies(reqType int, arg *api.PolicyArguments, stream interface{}) error { - var rf bgp.RouteFamily - req := NewGrpcRequest(reqType, "", rf, nil) - s.bgpServerCh <- req - for res := range req.ResponseCh { - if err := res.Err(); err != nil { - log.Debug(err.Error()) - return err - } - var err error - switch arg.Resource { - case api.Resource_POLICY_PREFIX: - err = stream.(api.Grpc_GetPolicyPrefixesServer).Send(res.Data.(*api.PrefixSet)) - case api.Resource_POLICY_NEIGHBOR: - err = stream.(api.Grpc_GetPolicyNeighborsServer).Send(res.Data.(*api.NeighborSet)) - case api.Resource_POLICY_ROUTEPOLICY: - err = stream.(api.Grpc_GetPolicyRoutePoliciesServer).Send(res.Data.(*api.PolicyDefinition)) - default: - return fmt.Errorf("unsupported resource type: %v", arg.Resource) - } - if err != nil { - return err - } - } - return nil -} - -func (s *Server) getPolicy(arg *api.PolicyArguments) (interface{}, error) { - var rf bgp.RouteFamily - var reqType int - switch arg.Resource { - case api.Resource_POLICY_PREFIX: - reqType = REQ_POLICY_PREFIX - case api.Resource_POLICY_NEIGHBOR: - reqType = REQ_POLICY_NEIGHBOR - case api.Resource_POLICY_ROUTEPOLICY: - reqType = REQ_POLICY_ROUTEPOLICY - default: - return nil, fmt.Errorf("unsupported resource type: %v", arg.Resource) - } - req := NewGrpcRequest(reqType, "", rf, arg.Name) - s.bgpServerCh <- req - - res := <-req.ResponseCh - if err := res.Err(); err != nil { - log.Debug(err.Error()) - return nil, err - } - return res.Data, nil -} - func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { var rf bgp.RouteFamily var reqType int @@ -413,17 +362,6 @@ func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { default: return fmt.Errorf("unsupported operation: %s", arg.Operation) } - req := NewGrpcRequest(reqType, "", rf, arg.PrefixSet) - s.bgpServerCh <- req - - res := <-req.ResponseCh - if err := res.Err(); err != nil { - log.Debug(err.Error()) - return err - } - err = stream.(api.Grpc_ModPolicyPrefixServer).Send(&api.Error{ - Code: api.Error_SUCCESS, - }) case api.Resource_POLICY_NEIGHBOR: switch arg.Operation { case api.Operation_ADD: @@ -435,17 +373,6 @@ func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { default: return fmt.Errorf("unsupported operation: %s", arg.Operation) } - req := NewGrpcRequest(reqType, "", rf, arg.NeighborSet) - s.bgpServerCh <- req - - res := <-req.ResponseCh - if err := res.Err(); err != nil { - log.Debug(err.Error()) - return err - } - err = stream.(api.Grpc_ModPolicyNeighborServer).Send(&api.Error{ - Code: api.Error_SUCCESS, - }) case api.Resource_POLICY_ROUTEPOLICY: switch arg.Operation { case api.Operation_ADD: @@ -457,101 +384,73 @@ func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { default: return fmt.Errorf("unsupported operation: %s", arg.Operation) } - req := NewGrpcRequest(reqType, "", rf, arg.PolicyDifinition) - s.bgpServerCh <- req - - res := <-req.ResponseCh - if err := res.Err(); err != nil { - log.Debug(err.Error()) - return err - } - err = stream.(api.Grpc_ModPolicyRoutePolicyServer).Send(&api.Error{ - Code: api.Error_SUCCESS, - }) - default: - return fmt.Errorf("unsupported resource type: %v", arg.Resource) } + req := NewGrpcRequest(reqType, "", rf, arg.PolicyDefinition) + s.bgpServerCh <- req - if err != nil { - return err - } - return nil - -} - -func (s *Server) GetPolicyPrefixes(arg *api.PolicyArguments, stream api.Grpc_GetPolicyPrefixesServer) error { - if err := s.getPolicies(REQ_POLICY_PREFIXES, arg, stream); err != nil { + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) return err } - return nil -} - -func (s *Server) GetPolicyPrefix(ctx context.Context, arg *api.PolicyArguments) (*api.PrefixSet, error) { - data, err := s.getPolicy(arg) + err = stream.(api.Grpc_ModPolicyRoutePolicyServer).Send(&api.Error{ + Code: api.Error_SUCCESS, + }) if err != nil { - return nil, err - } - return data.(*api.PrefixSet), nil -} - -func (s *Server) ModPolicyPrefix(stream api.Grpc_ModPolicyPrefixServer) error { - for { - arg, err := stream.Recv() - if err == io.EOF { - return nil - } else if err != nil { - return err - } - if err := s.modPolicy(arg, stream); err != nil { - return err - } - return nil - } -} - -func (s *Server) GetPolicyNeighbors(arg *api.PolicyArguments, stream api.Grpc_GetPolicyNeighborsServer) error { - if err := s.getPolicies(REQ_POLICY_NEIGHBORS, arg, stream); err != nil { return err } return nil } -func (s *Server) GetPolicyNeighbor(ctx context.Context, arg *api.PolicyArguments) (*api.NeighborSet, error) { - data, err := s.getPolicy(arg) - if err != nil { - return nil, err +func (s *Server) GetPolicyRoutePolicies(arg *api.PolicyArguments, stream api.Grpc_GetPolicyRoutePoliciesServer) error { + var rf bgp.RouteFamily + var reqType int + switch arg.Resource { + case api.Resource_POLICY_PREFIX: + reqType = REQ_POLICY_PREFIXES + case api.Resource_POLICY_NEIGHBOR: + reqType = REQ_POLICY_NEIGHBORS + case api.Resource_POLICY_ROUTEPOLICY: + reqType = REQ_POLICY_ROUTEPOLICIES + default: + return fmt.Errorf("unsupported resource type: %v", arg.Resource) } - return data.(*api.NeighborSet), nil -} - -func (s *Server) ModPolicyNeighbor(stream api.Grpc_ModPolicyNeighborServer) error { - for { - arg, err := stream.Recv() - if err == io.EOF { - return nil - } else if err != nil { + req := NewGrpcRequest(reqType, "", rf, nil) + s.bgpServerCh <- req + for res := range req.ResponseCh { + if err := res.Err(); err != nil { + log.Debug(err.Error()) return err } - if err := s.modPolicy(arg, stream); err != nil { + if err := stream.(api.Grpc_GetPolicyRoutePoliciesServer).Send(res.Data.(*api.PolicyDefinition)); err != nil { return err } - return nil - } -} - -func (s *Server) GetPolicyRoutePolicies(arg *api.PolicyArguments, stream api.Grpc_GetPolicyRoutePoliciesServer) error { - if err := s.getPolicies(REQ_POLICY_ROUTEPOLICIES, arg, stream); err != nil { - return err } return nil } func (s *Server) GetPolicyRoutePolicy(ctx context.Context, arg *api.PolicyArguments) (*api.PolicyDefinition, error) { - data, err := s.getPolicy(arg) - if err != nil { + var rf bgp.RouteFamily + var reqType int + switch arg.Resource { + case api.Resource_POLICY_PREFIX: + reqType = REQ_POLICY_PREFIX + case api.Resource_POLICY_NEIGHBOR: + reqType = REQ_POLICY_NEIGHBOR + case api.Resource_POLICY_ROUTEPOLICY: + reqType = REQ_POLICY_ROUTEPOLICY + default: + return nil, fmt.Errorf("unsupported resource type: %v", arg.Resource) + } + req := NewGrpcRequest(reqType, "", rf, arg.Name) + s.bgpServerCh <- req + + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Debug(err.Error()) return nil, err } - return data.(*api.PolicyDefinition), nil + return res.Data.(*api.PolicyDefinition), nil } func (s *Server) ModPolicyRoutePolicy(stream api.Grpc_ModPolicyRoutePolicyServer) error { diff --git a/server/server.go b/server/server.go index 43d0e51e..3ff104d5 100644 --- a/server/server.go +++ b/server/server.go @@ -950,12 +950,6 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { if err != nil { break } - - resInPolicies := []*api.PolicyDefinition{} - resOutPolicies := []*api.PolicyDefinition{} - // Add importpolies that has been set in the configuration file to the list. - // However, peer haven't target importpolicy when add PolicyDefinition of name only to the list. - conInPolicyNames := peer.config.ApplyPolicy.ImportPolicies loc := server.localRibMap[peer.config.NeighborAddress.String()] if loc == nil { result := &GrpcResponse{ @@ -965,12 +959,19 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { close(grpcReq.ResponseCh) break } + resInPolicies := []*api.PolicyDefinition{} + resOutPolicies := []*api.PolicyDefinition{} + pdList := server.routingPolicy.PolicyDefinitionList + df := server.routingPolicy.DefinedSets + // Add importpolies that has been set in the configuration file to the list. + // However, peer haven't target importpolicy when add PolicyDefinition of name only to the list. + conInPolicyNames := peer.config.ApplyPolicy.ImportPolicies for _, conInPolicyName := range conInPolicyNames { match := false - for _, inPolicy := range loc.importPolicies { - if conInPolicyName == inPolicy.Name { + for _, pd := range pdList { + if conInPolicyName == pd.Name { match = true - resInPolicies = append(resInPolicies, inPolicy.ToApiStruct()) + resInPolicies = append(resInPolicies, policy.PolicyDefinitionToApiStruct(pd, df)) break } } @@ -983,10 +984,10 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { conOutPolicyNames := peer.config.ApplyPolicy.ExportPolicies for _, conOutPolicyName := range conOutPolicyNames { match := false - for _, outPolicy := range loc.exportPolicies { - if conOutPolicyName == outPolicy.Name { + for _, pd := range pdList { + if conOutPolicyName == pd.Name { match = true - resOutPolicies = append(resOutPolicies, outPolicy.ToApiStruct()) + resOutPolicies = append(resOutPolicies, policy.PolicyDefinitionToApiStruct(pd, df)) break } } @@ -994,6 +995,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { resOutPolicies = append(resOutPolicies, &api.PolicyDefinition{PolicyDefinitionName: conOutPolicyName}) } } + defaultInPolicy := policy.ROUTE_REJECT defaultOutPolicy := policy.ROUTE_REJECT if loc.defaultImportPolicy == config.DEFAULT_POLICY_TYPE_ACCEPT_ROUTE { @@ -1019,8 +1021,6 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { break } reqApplyPolicy := grpcReq.Data.(*api.ApplyPolicy) - grpcReq.Data = []interface{}{reqApplyPolicy, server.policyMap} - reqPolicyMap := server.policyMap applyPolicy := &peer.config.ApplyPolicy var defInPolicy, defOutPolicy config.DefaultPolicyType @@ -1048,14 +1048,39 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { grpcReq.ResponseCh <- &GrpcResponse{} close(grpcReq.ResponseCh) + case REQ_POLICY_PREFIXES, REQ_POLICY_NEIGHBORS, REQ_POLICY_ROUTEPOLICIES: + server.handleGrpcShowPolicies(grpcReq) + case REQ_POLICY_PREFIX, REQ_POLICY_NEIGHBOR, REQ_POLICY_ROUTEPOLICY: + server.handleGrpcShowPolicy(grpcReq) + case REQ_POLICY_PREFIX_ADD, REQ_POLICY_NEIGHBOR_ADD, REQ_POLICY_ROUTEPOLICY_ADD: + server.handleGrpcAddPolicy(grpcReq) + case REQ_POLICY_PREFIX_DELETE, REQ_POLICY_NEIGHBOR_DELETE, REQ_POLICY_ROUTEPOLICY_DELETE: + server.handleGrpcDelPolicy(grpcReq) + case REQ_POLICY_PREFIXES_DELETE, REQ_POLICY_NEIGHBORS_DELETE, REQ_POLICY_ROUTEPOLICIES_DELETE: + server.handleGrpcDelPolicies(grpcReq) + default: + errmsg := "Unknown request type" + result := &GrpcResponse{ + ResponseErr: fmt.Errorf(errmsg), + } + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + } + return msgs +} + +func (server *BgpServer) handleGrpcShowPolicies(grpcReq *GrpcRequest) { + result := &GrpcResponse{} + switch grpcReq.RequestType { case REQ_POLICY_PREFIXES: info := server.routingPolicy.DefinedSets.PrefixSetList - result := &GrpcResponse{} if len(info) > 0 { for _, ps := range info { resPrefixSet := policy.PrefixSetToApiStruct(ps) + pd := &api.PolicyDefinition{} + pd.StatementList = []*api.Statement{{Conditions: &api.Conditions{MatchPrefixSet: resPrefixSet}}} result = &GrpcResponse{ - Data: resPrefixSet, + Data: pd, } grpcReq.ResponseCh <- result } @@ -1063,12 +1088,47 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { result.ResponseErr = fmt.Errorf("Policy prefix doesn't exist.") grpcReq.ResponseCh <- result } - close(grpcReq.ResponseCh) - + case REQ_POLICY_NEIGHBORS: + info := server.routingPolicy.DefinedSets.NeighborSetList + if len(info) > 0 { + for _, ns := range info { + resNeighborSet := policy.NeighborSetToApiStruct(ns) + pd := &api.PolicyDefinition{} + pd.StatementList = []*api.Statement{{Conditions: &api.Conditions{MatchNeighborSet: resNeighborSet}}} + result = &GrpcResponse{ + Data: pd, + } + grpcReq.ResponseCh <- result + } + } else { + result.ResponseErr = fmt.Errorf("Policy neighbor doesn't exist.") + grpcReq.ResponseCh <- result + } + case REQ_POLICY_ROUTEPOLICIES: + info := server.routingPolicy.PolicyDefinitionList + df := server.routingPolicy.DefinedSets + result := &GrpcResponse{} + if len(info) > 0 { + for _, pd := range info { + resPolicyDefinition := policy.PolicyDefinitionToApiStruct(pd, df) + result = &GrpcResponse{ + Data: resPolicyDefinition, + } + grpcReq.ResponseCh <- result + } + } else { + result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") + grpcReq.ResponseCh <- result + } + } + close(grpcReq.ResponseCh) +} +func (server *BgpServer) handleGrpcShowPolicy(grpcReq *GrpcRequest) { + name := grpcReq.Data.(string) + result := &GrpcResponse{} + switch grpcReq.RequestType { case REQ_POLICY_PREFIX: - name := grpcReq.Data.(string) info := server.routingPolicy.DefinedSets.PrefixSetList - result := &GrpcResponse{} resPrefixSet := &api.PrefixSet{} for _, ps := range info { if ps.PrefixSetName == name { @@ -1077,20 +1137,62 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { } } if len(resPrefixSet.PrefixList) > 0 { + pd := &api.PolicyDefinition{} + pd.StatementList = []*api.Statement{{Conditions: &api.Conditions{MatchPrefixSet: resPrefixSet}}} result = &GrpcResponse{ - Data: resPrefixSet, + Data: pd, } - grpcReq.ResponseCh <- result } else { - result.ResponseErr = fmt.Errorf("Policy prefix that has %v doesn't exist.", name) - grpcReq.ResponseCh <- result + result.ResponseErr = fmt.Errorf("policy prefix that has %v doesn't exist.", name) } - close(grpcReq.ResponseCh) + case REQ_POLICY_NEIGHBOR: + info := server.routingPolicy.DefinedSets.NeighborSetList + resNeighborSet := &api.NeighborSet{} + for _, ns := range info { + if ns.NeighborSetName == name { + resNeighborSet = policy.NeighborSetToApiStruct(ns) + break + } + } + if len(resNeighborSet.NeighborList) > 0 { + pd := &api.PolicyDefinition{} + pd.StatementList = []*api.Statement{{Conditions: &api.Conditions{MatchNeighborSet: resNeighborSet}}} + result = &GrpcResponse{ + Data: pd, + } + } else { + result.ResponseErr = fmt.Errorf("policy neighbor that has %v doesn't exist.", name) + } + case REQ_POLICY_ROUTEPOLICY: + log.Error("IN RoutePolicy") + info := server.routingPolicy.PolicyDefinitionList + df := server.routingPolicy.DefinedSets + resPolicyDefinition := &api.PolicyDefinition{} + for _, pd := range info { + if pd.Name == name { + resPolicyDefinition = policy.PolicyDefinitionToApiStruct(pd, df) + break + } + } + log.Error("IN RoutePolicy: ",len(resPolicyDefinition.StatementList)) + if len(resPolicyDefinition.StatementList) > 0 { + result = &GrpcResponse{ + Data: resPolicyDefinition, + } + } else { + result.ResponseErr = fmt.Errorf("Route Policy that has %v doesn't exist.", name) + } + } + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) +} +func (server *BgpServer) handleGrpcAddPolicy(grpcReq *GrpcRequest) { + result := &GrpcResponse{} + switch grpcReq.RequestType { case REQ_POLICY_PREFIX_ADD: - reqPrefixSet := grpcReq.Data.(*api.PrefixSet) + reqPrefixSet := grpcReq.Data.(*api.PolicyDefinition).StatementList[0].Conditions.MatchPrefixSet conPrefixSetList := server.routingPolicy.DefinedSets.PrefixSetList - result := &GrpcResponse{} isReqPrefixSet, prefixSet := policy.PrefixSetToConfigStruct(reqPrefixSet) if !isReqPrefixSet { result.ResponseErr = fmt.Errorf("doesn't reqest of policy prefix.") @@ -1104,22 +1206,88 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { conPrefixSetList = append(conPrefixSetList, prefixSet) } else { if idxPrefix == -1 { - conPrefixSetList[idxPrefixSet].PrefixList = append(conPrefixSetList[idxPrefixSet].PrefixList, prefixSet.PrefixList[0]) + conPrefixSetList[idxPrefixSet].PrefixList = + append(conPrefixSetList[idxPrefixSet].PrefixList, prefixSet.PrefixList[0]) } } server.routingPolicy.DefinedSets.PrefixSetList = conPrefixSetList - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) + case REQ_POLICY_NEIGHBOR_ADD: + reqNeighborSet := grpcReq.Data.(*api.PolicyDefinition).StatementList[0].Conditions.MatchNeighborSet + conNeighborSetList := server.routingPolicy.DefinedSets.NeighborSetList + isReqNeighborSet, neighborSet := policy.NeighborSetToConfigStruct(reqNeighborSet) + if !isReqNeighborSet { + result.ResponseErr = fmt.Errorf("doesn't reqest of policy neighbor.") + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) + } + // If the same NeighborSet is not set, add NeighborSet of request to the end. + // If only name of the NeighborSet is same, overwrite with NeighborSet of request + idxNeighborSet, idxNeighbor := policy.IndexOfNeighborSet(conNeighborSetList, neighborSet) + if idxNeighborSet == -1 { + conNeighborSetList = append(conNeighborSetList, neighborSet) + } else { + if idxNeighbor == -1 { + conNeighborSetList[idxNeighborSet].NeighborInfoList = + append(conNeighborSetList[idxNeighborSet].NeighborInfoList, neighborSet.NeighborInfoList[0]) + } + } + server.routingPolicy.DefinedSets.NeighborSetList = conNeighborSetList + case REQ_POLICY_ROUTEPOLICY_ADD: + reqPolicy := grpcReq.Data.(*api.PolicyDefinition) + reqConditions := reqPolicy.StatementList[0].Conditions + reqActions := reqPolicy.StatementList[0].Actions + conPolicyList := server.routingPolicy.PolicyDefinitionList + _, policyDef := policy.PolicyDefinitionToConfigStruct(reqPolicy) + idxPolicy, idxStatement := policy.IndexOfPolicyDefinition(conPolicyList, policyDef) + if idxPolicy == -1 { + conPolicyList = append(conPolicyList, policyDef) + } else { + statement := policyDef.StatementList[0] + if idxStatement == -1 { + conPolicyList[idxPolicy].StatementList = + append(conPolicyList[idxPolicy].StatementList, statement) + } else { + conStatement := &conPolicyList[idxPolicy].StatementList[idxStatement] + if reqConditions != nil { + if reqConditions.MatchPrefixSet != nil { + conStatement.Conditions.MatchPrefixSet = statement.Conditions.MatchPrefixSet + } + if reqConditions.MatchNeighborSet != nil { + conStatement.Conditions.MatchNeighborSet = statement.Conditions.MatchNeighborSet + } + if reqConditions.MatchSetOptions != "" { + conStatement.Conditions.MatchSetOptions = statement.Conditions.MatchSetOptions + } + if reqConditions.MatchAsPathLength != nil { + conStatement.Conditions.BgpConditions.AsPathLength = statement.Conditions.BgpConditions.AsPathLength + } + } + if reqActions != nil { + if reqActions.RouteAction != "" { + conStatement.Actions.AcceptRoute = statement.Actions.AcceptRoute + conStatement.Actions.RejectRoute = statement.Actions.RejectRoute + } + conStatement.Actions = statement.Actions + } + } + } + server.routingPolicy.PolicyDefinitionList = conPolicyList + } + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) +} +func (server *BgpServer) handleGrpcDelPolicy(grpcReq *GrpcRequest) { + result := &GrpcResponse{} + switch grpcReq.RequestType { case REQ_POLICY_PREFIX_DELETE: - reqPrefixSet := grpcReq.Data.(*api.PrefixSet) + reqPrefixSet := grpcReq.Data.(*api.PolicyDefinition).StatementList[0].Conditions.MatchPrefixSet conPrefixSetList := server.routingPolicy.DefinedSets.PrefixSetList - result := &GrpcResponse{} isReqPrefixSet, prefixSet := policy.PrefixSetToConfigStruct(reqPrefixSet) if isReqPrefixSet { // If only name of the PrefixSet is same, delete all of the elements of the PrefixSet. - //If the same element PrefixSet, delete the it's element from PrefixSet. + // If the same element PrefixSet, delete the it's element from PrefixSet. idxPrefixSet, idxPrefix := policy.IndexOfPrefixSet(conPrefixSetList, prefixSet) prefix := prefixSet.PrefixList[0] if idxPrefixSet == -1 { @@ -1131,7 +1299,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { prefix.Address, prefix.Masklength, prefix.MasklengthRange) } else { conPrefixSetList[idxPrefixSet].PrefixList = - append(conPrefixSetList[idxPrefixSet].PrefixList[:idxPrefix], conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix+1:]...) + append(conPrefixSetList[idxPrefixSet].PrefixList[:idxPrefix], conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix+1:]...) } } } else { @@ -1149,85 +1317,9 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { } } server.routingPolicy.DefinedSets.PrefixSetList = conPrefixSetList - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) - - case REQ_POLICY_PREFIXES_DELETE: - result := &GrpcResponse{} - server.routingPolicy.DefinedSets.PrefixSetList = make([]config.PrefixSet, 0) - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) - - case REQ_POLICY_NEIGHBORS: - info := server.routingPolicy.DefinedSets.NeighborSetList - result := &GrpcResponse{} - if len(info) > 0 { - for _, ns := range info { - resNeighborSet := policy.NeighborSetToApiStruct(ns) - result = &GrpcResponse{ - Data: resNeighborSet, - } - grpcReq.ResponseCh <- result - } - } else { - result.ResponseErr = fmt.Errorf("Policy neighbor doesn't exist.") - grpcReq.ResponseCh <- result - } - close(grpcReq.ResponseCh) - - case REQ_POLICY_NEIGHBOR: - name := grpcReq.Data.(string) - info := server.routingPolicy.DefinedSets.NeighborSetList - result := &GrpcResponse{} - resNeighborSet := &api.NeighborSet{} - for _, ns := range info { - if ns.NeighborSetName == name { - resNeighborSet = policy.NeighborSetToApiStruct(ns) - break - } - } - if len(resNeighborSet.NeighborList) > 0 { - result = &GrpcResponse{ - Data: resNeighborSet, - } - grpcReq.ResponseCh <- result - } else { - result.ResponseErr = fmt.Errorf("Policy neighbor that has %v doesn't exist.", name) - grpcReq.ResponseCh <- result - } - close(grpcReq.ResponseCh) - - case REQ_POLICY_NEIGHBOR_ADD: - reqNeighborSet := grpcReq.Data.(*api.NeighborSet) - conNeighborSetList := server.routingPolicy.DefinedSets.NeighborSetList - result := &GrpcResponse{} - isReqNeighborSet, neighborSet := policy.NeighborSetToConfigStruct(reqNeighborSet) - if !isReqNeighborSet { - result.ResponseErr = fmt.Errorf("doesn't reqest of policy neighbor.") - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) - } - // If the same NeighborSet is not set, add NeighborSet of request to the end. - // If only name of the NeighborSet is same, overwrite with NeighborSet of request - idxNeighborSet, idxNeighbor := policy.IndexOfNeighborSet(conNeighborSetList, neighborSet) - if idxNeighborSet == -1 { - conNeighborSetList = append(conNeighborSetList, neighborSet) - } else { - if idxNeighbor == -1 { - conNeighborSetList[idxNeighborSet].NeighborInfoList = - append(conNeighborSetList[idxNeighborSet].NeighborInfoList, neighborSet.NeighborInfoList[0]) - } - } - server.routingPolicy.DefinedSets.NeighborSetList = conNeighborSetList - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) case REQ_POLICY_NEIGHBOR_DELETE: - reqNeighborSet := grpcReq.Data.(*api.NeighborSet) + reqNeighborSet := grpcReq.Data.(*api.PolicyDefinition).StatementList[0].Conditions.MatchNeighborSet conNeighborSetList := server.routingPolicy.DefinedSets.NeighborSetList - result := &GrpcResponse{} isReqNeighborSet, neighborSet := policy.NeighborSetToConfigStruct(reqNeighborSet) if isReqNeighborSet { // If only name of the NeighborSet is same, delete all of the elements of the NeighborSet. @@ -1242,8 +1334,8 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { neighborSet.NeighborInfoList[0].Address) } else { conNeighborSetList[idxNeighborSet].NeighborInfoList = - append(conNeighborSetList[idxNeighborSet].NeighborInfoList[:idxNeighbor], - conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor+1:]...) + append(conNeighborSetList[idxNeighborSet].NeighborInfoList[:idxNeighbor], + conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor+1:]...) } } } else { @@ -1261,95 +1353,6 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { } } server.routingPolicy.DefinedSets.NeighborSetList = conNeighborSetList - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) - case REQ_POLICY_NEIGHBORS_DELETE: - result := &GrpcResponse{} - server.routingPolicy.DefinedSets.NeighborSetList = make([]config.NeighborSet, 0) - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) - case REQ_POLICY_ROUTEPOLICIES: - info := server.routingPolicy.PolicyDefinitionList - df := server.routingPolicy.DefinedSets - result := &GrpcResponse{} - if len(info) > 0 { - for _, pd := range info { - resPolicyDefinition := policy.PolicyDefinitionToApiStruct(pd, df) - result = &GrpcResponse{ - Data: resPolicyDefinition, - } - grpcReq.ResponseCh <- result - } - } else { - result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") - grpcReq.ResponseCh <- result - } - close(grpcReq.ResponseCh) - case REQ_POLICY_ROUTEPOLICY: - name := grpcReq.Data.(string) - info := server.routingPolicy.PolicyDefinitionList - df := server.routingPolicy.DefinedSets - result := &GrpcResponse{} - resPolicyDefinition := &api.PolicyDefinition{} - for _, pd := range info { - if pd.Name == name { - resPolicyDefinition = policy.PolicyDefinitionToApiStruct(pd, df) - break - } - } - if len(resPolicyDefinition.StatementList) > 0 { - result = &GrpcResponse{ - Data: resPolicyDefinition, - } - grpcReq.ResponseCh <- result - } else { - result.ResponseErr = fmt.Errorf("Route Policy that has %v doesn't exist.", name) - grpcReq.ResponseCh <- result - } - close(grpcReq.ResponseCh) - case REQ_POLICY_ROUTEPOLICY_ADD: - reqPolicy := grpcReq.Data.(*api.PolicyDefinition) - reqConditions := reqPolicy.StatementList[0].Conditions - reqActions := reqPolicy.StatementList[0].Actions - conPolicyList := server.routingPolicy.PolicyDefinitionList - result := &GrpcResponse{} - _, policyDef := policy.PolicyDefinitionToConfigStruct(reqPolicy) - idxPolicy, idxStatement := policy.IndexOfPolicyDefinition(conPolicyList, policyDef) - if idxPolicy == -1 { - conPolicyList = append(conPolicyList, policyDef) - } else { - statement := policyDef.StatementList[0] - if idxStatement == -1 { - conPolicyList[idxPolicy].StatementList = - append(conPolicyList[idxPolicy].StatementList, statement) - } else { - conStatement := &conPolicyList[idxPolicy].StatementList[idxStatement] - if reqConditions != nil { - if reqConditions.MatchPrefixSet != nil { - conStatement.Conditions.MatchPrefixSet = statement.Conditions.MatchPrefixSet - } - if reqConditions.MatchNeighborSet != nil { - conStatement.Conditions.MatchNeighborSet = statement.Conditions.MatchNeighborSet - } - if reqConditions.MatchSetOptions != "" { - conStatement.Conditions.MatchSetOptions = statement.Conditions.MatchSetOptions - } - if reqConditions.MatchAsPathLength != nil { - conStatement.Conditions.BgpConditions.AsPathLength = statement.Conditions.BgpConditions.AsPathLength - } - } - if reqActions != nil { - conStatement.Actions = statement.Actions - } - } - } - server.routingPolicy.PolicyDefinitionList = conPolicyList - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) - case REQ_POLICY_ROUTEPOLICY_DELETE: reqPolicy := grpcReq.Data.(*api.PolicyDefinition) conPolicyList := server.routingPolicy.PolicyDefinitionList @@ -1364,7 +1367,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { result.ResponseErr = fmt.Errorf("Policy Statment that has %v doesn't exist.", policyDef.StatementList[0].Name) } else { conPolicyList[idxPolicy].StatementList = - append(conPolicyList[idxPolicy].StatementList[:idxStatement], conPolicyList[idxPolicy].StatementList[idxStatement+1:]...) + append(conPolicyList[idxPolicy].StatementList[:idxStatement], conPolicyList[idxPolicy].StatementList[idxStatement+1:]...) } } } else { @@ -1382,23 +1385,23 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg { } } server.routingPolicy.PolicyDefinitionList = conPolicyList - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) + } + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) +} +func (server *BgpServer) handleGrpcDelPolicies(grpcReq *GrpcRequest) { + result := &GrpcResponse{} + switch grpcReq.RequestType { + case REQ_POLICY_PREFIXES_DELETE: + server.routingPolicy.DefinedSets.PrefixSetList = make([]config.PrefixSet, 0) + case REQ_POLICY_NEIGHBORS_DELETE: + server.routingPolicy.DefinedSets.NeighborSetList = make([]config.NeighborSet, 0) case REQ_POLICY_ROUTEPOLICIES_DELETE: - result := &GrpcResponse{} server.routingPolicy.PolicyDefinitionList = make([]config.PolicyDefinition, 0) - server.handlePolicy(server.routingPolicy) - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) - default: - errmsg := "Unknown request type" - result := &GrpcResponse{ - ResponseErr: fmt.Errorf(errmsg), - } - grpcReq.ResponseCh <- result - close(grpcReq.ResponseCh) } - return msgs + server.handlePolicy(server.routingPolicy) + grpcReq.ResponseCh <- result + close(grpcReq.ResponseCh) } |