api/cli: refactor api to retrieve policy

make it similar to other policy api

Signed-off-by: ISHIDA Wataru <[email protected]>
This commit is contained in:
ISHIDA Wataru
2015-10-20 10:33:10 +09:00
committed by FUJITA Tomonori
parent f834e42128
commit 95e68a8d88
8 changed files with 203 additions and 216 deletions
+109 -109
View File
@@ -32,7 +32,7 @@ It has these top-level messages:
AsPrependAction
Actions
Statement
PolicyDefinition
Policy
ApplyPolicy
MrtMessage
RPKIConf
@@ -233,19 +233,19 @@ func (m *ModPathArguments) GetPaths() []*Path {
}
type PolicyArguments struct {
Resource Resource `protobuf:"varint,1,opt,name=resource,enum=gobgpapi.Resource" json:"resource,omitempty"`
Operation Operation `protobuf:"varint,2,opt,name=operation,enum=gobgpapi.Operation" json:"operation,omitempty"`
NeighborAddress string `protobuf:"bytes,3,opt,name=neighbor_address" json:"neighbor_address,omitempty"`
Name string `protobuf:"bytes,4,opt,name=name" json:"name,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"`
Resource Resource `protobuf:"varint,1,opt,name=resource,enum=gobgpapi.Resource" json:"resource,omitempty"`
Operation Operation `protobuf:"varint,2,opt,name=operation,enum=gobgpapi.Operation" json:"operation,omitempty"`
NeighborAddress string `protobuf:"bytes,3,opt,name=neighbor_address" json:"neighbor_address,omitempty"`
Name string `protobuf:"bytes,4,opt,name=name" json:"name,omitempty"`
PolicyDefinition *Policy `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) GetPolicyDefinition() *PolicyDefinition {
func (m *PolicyArguments) GetPolicyDefinition() *Policy {
if m != nil {
return m.PolicyDefinition
}
@@ -617,16 +617,16 @@ func (m *Statement) GetActions() *Actions {
return nil
}
type PolicyDefinition struct {
type Policy struct {
Name string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
Statements []*Statement `protobuf:"bytes,2,rep,name=statements" json:"statements,omitempty"`
}
func (m *PolicyDefinition) Reset() { *m = PolicyDefinition{} }
func (m *PolicyDefinition) String() string { return proto.CompactTextString(m) }
func (*PolicyDefinition) ProtoMessage() {}
func (m *Policy) Reset() { *m = Policy{} }
func (m *Policy) String() string { return proto.CompactTextString(m) }
func (*Policy) ProtoMessage() {}
func (m *PolicyDefinition) GetStatements() []*Statement {
func (m *Policy) GetStatements() []*Statement {
if m != nil {
return m.Statements
}
@@ -743,8 +743,6 @@ type GobgpApiClient interface {
ModPath(ctx context.Context, opts ...grpc.CallOption) (GobgpApi_ModPathClient, error)
GetNeighborPolicy(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*ApplyPolicy, error)
ModNeighborPolicy(ctx context.Context, opts ...grpc.CallOption) (GobgpApi_ModNeighborPolicyClient, error)
GetPolicyRoutePolicies(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (GobgpApi_GetPolicyRoutePoliciesClient, error)
GetPolicyRoutePolicy(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*PolicyDefinition, error)
ModPolicyRoutePolicy(ctx context.Context, opts ...grpc.CallOption) (GobgpApi_ModPolicyRoutePolicyClient, error)
MonitorBestChanged(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_MonitorBestChangedClient, error)
MonitorPeerState(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_MonitorPeerStateClient, error)
@@ -759,6 +757,8 @@ type GobgpApiClient interface {
GetStatement(ctx context.Context, in *Statement, opts ...grpc.CallOption) (*Statement, error)
GetStatements(ctx context.Context, in *Statement, opts ...grpc.CallOption) (GobgpApi_GetStatementsClient, error)
ModStatement(ctx context.Context, in *ModStatementArguments, opts ...grpc.CallOption) (*Error, error)
GetPolicy(ctx context.Context, in *Policy, opts ...grpc.CallOption) (*Policy, error)
GetPolicies(ctx context.Context, in *Policy, opts ...grpc.CallOption) (GobgpApi_GetPoliciesClient, error)
}
type gobgpApiClient struct {
@@ -979,49 +979,8 @@ func (x *gobgpApiModNeighborPolicyClient) Recv() (*Error, error) {
return m, nil
}
func (c *gobgpApiClient) GetPolicyRoutePolicies(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (GobgpApi_GetPolicyRoutePoliciesClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[4], c.cc, "/gobgpapi.GobgpApi/GetPolicyRoutePolicies", opts...)
if err != nil {
return nil, err
}
x := &gobgpApiGetPolicyRoutePoliciesClient{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 GobgpApi_GetPolicyRoutePoliciesClient interface {
Recv() (*PolicyDefinition, error)
grpc.ClientStream
}
type gobgpApiGetPolicyRoutePoliciesClient struct {
grpc.ClientStream
}
func (x *gobgpApiGetPolicyRoutePoliciesClient) Recv() (*PolicyDefinition, error) {
m := new(PolicyDefinition)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
func (c *gobgpApiClient) GetPolicyRoutePolicy(ctx context.Context, in *PolicyArguments, opts ...grpc.CallOption) (*PolicyDefinition, error) {
out := new(PolicyDefinition)
err := grpc.Invoke(ctx, "/gobgpapi.GobgpApi/GetPolicyRoutePolicy", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *gobgpApiClient) ModPolicyRoutePolicy(ctx context.Context, opts ...grpc.CallOption) (GobgpApi_ModPolicyRoutePolicyClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[5], c.cc, "/gobgpapi.GobgpApi/ModPolicyRoutePolicy", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[4], c.cc, "/gobgpapi.GobgpApi/ModPolicyRoutePolicy", opts...)
if err != nil {
return nil, err
}
@@ -1052,7 +1011,7 @@ func (x *gobgpApiModPolicyRoutePolicyClient) Recv() (*Error, error) {
}
func (c *gobgpApiClient) MonitorBestChanged(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_MonitorBestChangedClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[6], c.cc, "/gobgpapi.GobgpApi/MonitorBestChanged", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[5], c.cc, "/gobgpapi.GobgpApi/MonitorBestChanged", opts...)
if err != nil {
return nil, err
}
@@ -1084,7 +1043,7 @@ func (x *gobgpApiMonitorBestChangedClient) Recv() (*Destination, error) {
}
func (c *gobgpApiClient) MonitorPeerState(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_MonitorPeerStateClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[7], c.cc, "/gobgpapi.GobgpApi/MonitorPeerState", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[6], c.cc, "/gobgpapi.GobgpApi/MonitorPeerState", opts...)
if err != nil {
return nil, err
}
@@ -1116,7 +1075,7 @@ func (x *gobgpApiMonitorPeerStateClient) Recv() (*Peer, error) {
}
func (c *gobgpApiClient) GetMrt(ctx context.Context, in *MrtArguments, opts ...grpc.CallOption) (GobgpApi_GetMrtClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[8], c.cc, "/gobgpapi.GobgpApi/GetMrt", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[7], c.cc, "/gobgpapi.GobgpApi/GetMrt", opts...)
if err != nil {
return nil, err
}
@@ -1148,7 +1107,7 @@ func (x *gobgpApiGetMrtClient) Recv() (*MrtMessage, error) {
}
func (c *gobgpApiClient) GetRPKI(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_GetRPKIClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[9], c.cc, "/gobgpapi.GobgpApi/GetRPKI", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[8], c.cc, "/gobgpapi.GobgpApi/GetRPKI", opts...)
if err != nil {
return nil, err
}
@@ -1180,7 +1139,7 @@ func (x *gobgpApiGetRPKIClient) Recv() (*RPKI, error) {
}
func (c *gobgpApiClient) GetROA(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_GetROAClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[10], c.cc, "/gobgpapi.GobgpApi/GetROA", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[9], c.cc, "/gobgpapi.GobgpApi/GetROA", opts...)
if err != nil {
return nil, err
}
@@ -1212,7 +1171,7 @@ func (x *gobgpApiGetROAClient) Recv() (*ROA, error) {
}
func (c *gobgpApiClient) GetVrfs(ctx context.Context, in *Arguments, opts ...grpc.CallOption) (GobgpApi_GetVrfsClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[11], c.cc, "/gobgpapi.GobgpApi/GetVrfs", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[10], c.cc, "/gobgpapi.GobgpApi/GetVrfs", opts...)
if err != nil {
return nil, err
}
@@ -1262,7 +1221,7 @@ func (c *gobgpApiClient) GetDefinedSet(ctx context.Context, in *DefinedSet, opts
}
func (c *gobgpApiClient) GetDefinedSets(ctx context.Context, in *DefinedSet, opts ...grpc.CallOption) (GobgpApi_GetDefinedSetsClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[12], c.cc, "/gobgpapi.GobgpApi/GetDefinedSets", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[11], c.cc, "/gobgpapi.GobgpApi/GetDefinedSets", opts...)
if err != nil {
return nil, err
}
@@ -1312,7 +1271,7 @@ func (c *gobgpApiClient) GetStatement(ctx context.Context, in *Statement, opts .
}
func (c *gobgpApiClient) GetStatements(ctx context.Context, in *Statement, opts ...grpc.CallOption) (GobgpApi_GetStatementsClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[13], c.cc, "/gobgpapi.GobgpApi/GetStatements", opts...)
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[12], c.cc, "/gobgpapi.GobgpApi/GetStatements", opts...)
if err != nil {
return nil, err
}
@@ -1352,6 +1311,47 @@ func (c *gobgpApiClient) ModStatement(ctx context.Context, in *ModStatementArgum
return out, nil
}
func (c *gobgpApiClient) GetPolicy(ctx context.Context, in *Policy, opts ...grpc.CallOption) (*Policy, error) {
out := new(Policy)
err := grpc.Invoke(ctx, "/gobgpapi.GobgpApi/GetPolicy", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *gobgpApiClient) GetPolicies(ctx context.Context, in *Policy, opts ...grpc.CallOption) (GobgpApi_GetPoliciesClient, error) {
stream, err := grpc.NewClientStream(ctx, &_GobgpApi_serviceDesc.Streams[13], c.cc, "/gobgpapi.GobgpApi/GetPolicies", opts...)
if err != nil {
return nil, err
}
x := &gobgpApiGetPoliciesClient{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 GobgpApi_GetPoliciesClient interface {
Recv() (*Policy, error)
grpc.ClientStream
}
type gobgpApiGetPoliciesClient struct {
grpc.ClientStream
}
func (x *gobgpApiGetPoliciesClient) Recv() (*Policy, error) {
m := new(Policy)
if err := x.ClientStream.RecvMsg(m); err != nil {
return nil, err
}
return m, nil
}
// Server API for GobgpApi service
type GobgpApiServer interface {
@@ -1368,8 +1368,6 @@ type GobgpApiServer interface {
ModPath(GobgpApi_ModPathServer) error
GetNeighborPolicy(context.Context, *PolicyArguments) (*ApplyPolicy, error)
ModNeighborPolicy(GobgpApi_ModNeighborPolicyServer) error
GetPolicyRoutePolicies(*PolicyArguments, GobgpApi_GetPolicyRoutePoliciesServer) error
GetPolicyRoutePolicy(context.Context, *PolicyArguments) (*PolicyDefinition, error)
ModPolicyRoutePolicy(GobgpApi_ModPolicyRoutePolicyServer) error
MonitorBestChanged(*Arguments, GobgpApi_MonitorBestChangedServer) error
MonitorPeerState(*Arguments, GobgpApi_MonitorPeerStateServer) error
@@ -1384,6 +1382,8 @@ type GobgpApiServer interface {
GetStatement(context.Context, *Statement) (*Statement, error)
GetStatements(*Statement, GobgpApi_GetStatementsServer) error
ModStatement(context.Context, *ModStatementArguments) (*Error, error)
GetPolicy(context.Context, *Policy) (*Policy, error)
GetPolicies(*Policy, GobgpApi_GetPoliciesServer) error
}
func RegisterGobgpApiServer(s *grpc.Server, srv GobgpApiServer) {
@@ -1592,39 +1592,6 @@ func (x *gobgpApiModNeighborPolicyServer) Recv() (*PolicyArguments, error) {
return m, nil
}
func _GobgpApi_GetPolicyRoutePolicies_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(PolicyArguments)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(GobgpApiServer).GetPolicyRoutePolicies(m, &gobgpApiGetPolicyRoutePoliciesServer{stream})
}
type GobgpApi_GetPolicyRoutePoliciesServer interface {
Send(*PolicyDefinition) error
grpc.ServerStream
}
type gobgpApiGetPolicyRoutePoliciesServer struct {
grpc.ServerStream
}
func (x *gobgpApiGetPolicyRoutePoliciesServer) Send(m *PolicyDefinition) error {
return x.ServerStream.SendMsg(m)
}
func _GobgpApi_GetPolicyRoutePolicy_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
in := new(PolicyArguments)
if err := dec(in); err != nil {
return nil, err
}
out, err := srv.(GobgpApiServer).GetPolicyRoutePolicy(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
func _GobgpApi_ModPolicyRoutePolicy_Handler(srv interface{}, stream grpc.ServerStream) error {
return srv.(GobgpApiServer).ModPolicyRoutePolicy(&gobgpApiModPolicyRoutePolicyServer{stream})
}
@@ -1879,6 +1846,39 @@ func _GobgpApi_ModStatement_Handler(srv interface{}, ctx context.Context, dec fu
return out, nil
}
func _GobgpApi_GetPolicy_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
in := new(Policy)
if err := dec(in); err != nil {
return nil, err
}
out, err := srv.(GobgpApiServer).GetPolicy(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
func _GobgpApi_GetPolicies_Handler(srv interface{}, stream grpc.ServerStream) error {
m := new(Policy)
if err := stream.RecvMsg(m); err != nil {
return err
}
return srv.(GobgpApiServer).GetPolicies(m, &gobgpApiGetPoliciesServer{stream})
}
type GobgpApi_GetPoliciesServer interface {
Send(*Policy) error
grpc.ServerStream
}
type gobgpApiGetPoliciesServer struct {
grpc.ServerStream
}
func (x *gobgpApiGetPoliciesServer) Send(m *Policy) error {
return x.ServerStream.SendMsg(m)
}
var _GobgpApi_serviceDesc = grpc.ServiceDesc{
ServiceName: "gobgpapi.GobgpApi",
HandlerType: (*GobgpApiServer)(nil),
@@ -1919,10 +1919,6 @@ var _GobgpApi_serviceDesc = grpc.ServiceDesc{
MethodName: "GetNeighborPolicy",
Handler: _GobgpApi_GetNeighborPolicy_Handler,
},
{
MethodName: "GetPolicyRoutePolicy",
Handler: _GobgpApi_GetPolicyRoutePolicy_Handler,
},
{
MethodName: "ModVrf",
Handler: _GobgpApi_ModVrf_Handler,
@@ -1943,6 +1939,10 @@ var _GobgpApi_serviceDesc = grpc.ServiceDesc{
MethodName: "ModStatement",
Handler: _GobgpApi_ModStatement_Handler,
},
{
MethodName: "GetPolicy",
Handler: _GobgpApi_GetPolicy_Handler,
},
},
Streams: []grpc.StreamDesc{
{
@@ -1966,11 +1966,6 @@ var _GobgpApi_serviceDesc = grpc.ServiceDesc{
ServerStreams: true,
ClientStreams: true,
},
{
StreamName: "GetPolicyRoutePolicies",
Handler: _GobgpApi_GetPolicyRoutePolicies_Handler,
ServerStreams: true,
},
{
StreamName: "ModPolicyRoutePolicy",
Handler: _GobgpApi_ModPolicyRoutePolicy_Handler,
@@ -2017,5 +2012,10 @@ var _GobgpApi_serviceDesc = grpc.ServiceDesc{
Handler: _GobgpApi_GetStatements_Handler,
ServerStreams: true,
},
{
StreamName: "GetPolicies",
Handler: _GobgpApi_GetPolicies_Handler,
ServerStreams: true,
},
},
}
+4 -4
View File
@@ -33,8 +33,6 @@ service GobgpApi {
rpc ModPath(stream ModPathArguments) returns (Error) {}
rpc GetNeighborPolicy(PolicyArguments) returns (ApplyPolicy) {}
rpc ModNeighborPolicy(stream PolicyArguments) returns (stream Error) {}
rpc GetPolicyRoutePolicies(PolicyArguments) returns (stream PolicyDefinition) {}
rpc GetPolicyRoutePolicy(PolicyArguments) returns (PolicyDefinition) {}
rpc ModPolicyRoutePolicy(stream PolicyArguments) returns (stream Error) {}
rpc MonitorBestChanged(Arguments) returns (stream Destination) {}
rpc MonitorPeerState(Arguments) returns (stream Peer) {}
@@ -49,6 +47,8 @@ service GobgpApi {
rpc GetStatement(Statement) returns (Statement) {}
rpc GetStatements(Statement) returns (stream Statement) {}
rpc ModStatement(ModStatementArguments) returns (Error) {}
rpc GetPolicy(Policy) returns (Policy) {}
rpc GetPolicies(Policy) returns (stream Policy) {}
}
message Error {
@@ -77,7 +77,7 @@ message PolicyArguments {
Operation operation = 2;
string neighbor_address = 3;
string name = 4;
PolicyDefinition policy_definition = 6;
Policy policy_definition = 6;
ApplyPolicy apply_policy = 7;
}
@@ -257,7 +257,7 @@ message Statement {
Actions actions = 3;
}
message PolicyDefinition {
message Policy {
string name = 1;
repeated Statement statements = 2;
}
+4 -4
View File
@@ -325,17 +325,17 @@ func (n sets) Less(i, j int) bool {
return n[i].Name < n[j].Name
}
type policyDefinitions []*gobgpapi.PolicyDefinition
type policies []*gobgpapi.Policy
func (p policyDefinitions) Len() int {
func (p policies) Len() int {
return len(p)
}
func (p policyDefinitions) Swap(i, j int) {
func (p policies) Swap(i, j int) {
p[i], p[j] = p[j], p[i]
}
func (p policyDefinitions) Less(i, j int) bool {
func (p policies) Less(i, j int) bool {
return p[i].Name < p[j].Name
}
+3 -3
View File
@@ -647,12 +647,12 @@ func showNeighborPolicy(remoteIP net.IP, policyType string) error {
return nil
}
func parsePolicy(pNames string) []*api.PolicyDefinition {
func parsePolicy(pNames string) []*api.Policy {
pList := strings.Split(pNames, ",")
policyList := make([]*api.PolicyDefinition, 0, len(pList))
policyList := make([]*api.Policy, 0, len(pList))
for _, p := range pList {
if p != "" {
policy := &api.PolicyDefinition{
policy := &api.Policy{
Name: p,
}
policyList = append(policyList, policy)
+34 -57
View File
@@ -468,31 +468,39 @@ func printStatement(indent int, s *api.Statement) {
fmt.Printf("%s%s\n", sIndent(indent+4), s.Actions.RouteAction)
}
func showPolicyStatement(indent int, pd *api.PolicyDefinition) {
func printPolicy(indent int, pd *api.Policy) {
for _, s := range pd.Statements {
printStatement(indent, s)
}
}
func showPolicyRoutePolicies() error {
arg := &api.PolicyArguments{
Resource: api.Resource_POLICY_ROUTEPOLICY,
}
stream, e := client.GetPolicyRoutePolicies(context.Background(), arg)
if e != nil {
return e
}
m := policyDefinitions{}
for {
n, e := stream.Recv()
if e == io.EOF {
break
} else if e != nil {
func showPolicy(args []string) error {
m := policies{}
if len(args) > 0 {
arg := &api.Policy{
Name: args[0],
}
p, e := client.GetPolicy(context.Background(), arg)
if e != nil {
return e
}
m = append(m, n)
m = append(m, p)
} else {
arg := &api.Policy{}
stream, e := client.GetPolicies(context.Background(), arg)
if e != nil {
return e
}
for {
p, e := stream.Recv()
if e == io.EOF {
break
} else if e != nil {
return e
}
m = append(m, p)
}
}
if globalOpts.Json {
j, _ := json.Marshal(m)
fmt.Println(string(j))
@@ -505,42 +513,13 @@ func showPolicyRoutePolicies() error {
return nil
}
sort.Sort(m)
for _, pd := range m {
fmt.Printf("PolicyName %s:\n", pd.Name)
showPolicyStatement(4, pd)
fmt.Printf("Name %s:\n", pd.Name)
printPolicy(4, pd)
}
return nil
}
func showPolicyRoutePolicy(args []string) error {
arg := &api.PolicyArguments{
Resource: api.Resource_POLICY_ROUTEPOLICY,
Name: args[0],
}
pd, e := client.GetPolicyRoutePolicy(context.Background(), arg)
if e != nil {
return e
}
if globalOpts.Json {
j, _ := json.Marshal(pd)
fmt.Println(string(j))
return nil
}
if globalOpts.Quiet {
for _, st := range pd.Statements {
fmt.Println(st.Name)
}
return nil
}
fmt.Printf("PolicyName %s:\n", pd.Name)
showPolicyStatement(2, pd)
return nil
}
func parseConditions() (*api.Conditions, error) {
checkFormat := func(option string, isRestricted bool) (int32, string, error) {
regStr, _ := regexp.Compile("^(.*)\\[(.*)\\]$")
@@ -777,7 +756,7 @@ func parseActions() (*api.Actions, error) {
}
func modPolicy(resource api.Resource, op api.Operation, data interface{}) error {
pd := &api.PolicyDefinition{}
pd := &api.Policy{}
if resource != api.Resource_POLICY_ROUTEPOLICY {
co := &api.Conditions{}
switch resource {
@@ -794,7 +773,7 @@ func modPolicy(resource api.Resource, op api.Operation, data interface{}) error
}
pd.Statements = []*api.Statement{{Conditions: co}}
} else {
pd = data.(*api.PolicyDefinition)
pd = data.(*api.Policy)
}
arg := &api.PolicyArguments{
Resource: resource,
@@ -823,7 +802,7 @@ func modPolicy(resource api.Resource, op api.Operation, data interface{}) error
func modPolicyRoutePolicy(modtype string, eArgs []string) error {
var operation api.Operation
pd := &api.PolicyDefinition{}
pd := &api.Policy{}
if len(eArgs) > 0 {
pd.Name = eArgs[0]
}
@@ -1213,14 +1192,10 @@ func NewPolicyCmd() *cobra.Command {
policyCmd := &cobra.Command{
Use: CMD_POLICY,
Run: func(cmd *cobra.Command, args []string) {
var err error
if len(args) == 0 {
err = showPolicyRoutePolicies()
} else {
err = showPolicyRoutePolicy(args)
}
err := showPolicy(args)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
@@ -1231,6 +1206,7 @@ func NewPolicyCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
if err := showDefinedSet(cmd.Use, args); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
@@ -1240,6 +1216,7 @@ func NewPolicyCmd() *cobra.Command {
Run: func(c *cobra.Command, args []string) {
if err := modDefinedSet(cmd.Use, c.Use, args); err != nil {
fmt.Println(err)
os.Exit(1)
}
},
}
+17 -36
View File
@@ -63,6 +63,7 @@ const (
REQ_MOD_DEFINED_SET
REQ_STATEMENT
REQ_MOD_STATEMENT
REQ_POLICY
)
const GRPC_PORT = 8080
@@ -326,42 +327,6 @@ func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error {
return nil
}
func (s *Server) GetPolicyRoutePolicies(arg *api.PolicyArguments, stream api.GobgpApi_GetPolicyRoutePoliciesServer) error {
var rf bgp.RouteFamily
var reqType int
switch arg.Resource {
case api.Resource_POLICY_ROUTEPOLICY:
reqType = REQ_POLICY_ROUTEPOLICIES
default:
return fmt.Errorf("unsupported resource type: %v", arg.Resource)
}
req := NewGrpcRequest(reqType, "", rf, arg)
s.bgpServerCh <- req
return handleMultipleResponses(req, func(res *GrpcResponse) error {
return stream.Send(res.Data.(*api.PolicyDefinition))
})
}
func (s *Server) GetPolicyRoutePolicy(ctx context.Context, arg *api.PolicyArguments) (*api.PolicyDefinition, error) {
var rf bgp.RouteFamily
var reqType int
switch arg.Resource {
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)
s.bgpServerCh <- req
res := <-req.ResponseCh
if err := res.Err(); err != nil {
log.Debug(err.Error())
return nil, err
}
return res.Data.(*api.PolicyDefinition), nil
}
func (s *Server) ModPolicyRoutePolicy(stream api.GobgpApi_ModPolicyRoutePolicyServer) error {
for {
arg, err := stream.Recv()
@@ -486,6 +451,22 @@ func (s *Server) ModStatement(ctx context.Context, arg *api.ModStatementArgument
return s.mod(REQ_MOD_STATEMENT, arg)
}
func (s *Server) GetPolicy(ctx context.Context, arg *api.Policy) (*api.Policy, error) {
d, err := s.get(REQ_POLICY, arg)
if err != nil {
return nil, err
}
return d.(*api.Policy), nil
}
func (s *Server) GetPolicies(arg *api.Policy, stream api.GobgpApi_GetPoliciesServer) error {
req := NewGrpcRequest(REQ_POLICY, "", bgp.RouteFamily(0), arg)
s.bgpServerCh <- req
return handleMultipleResponses(req, func(res *GrpcResponse) error {
return stream.Send(res.Data.(*api.Policy))
})
}
type GrpcRequest struct {
RequestType int
Name string
+29
View File
@@ -1656,6 +1656,13 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) []*SenderMsg {
ResponseErr: err,
}
close(grpcReq.ResponseCh)
case REQ_POLICY:
if err := server.handleGrpcGetPolicy(grpcReq); err != nil {
grpcReq.ResponseCh <- &GrpcResponse{
ResponseErr: err,
}
}
close(grpcReq.ResponseCh)
case REQ_POLICY_ROUTEPOLICY, REQ_POLICY_ROUTEPOLICIES:
info := server.policy.PolicyMap
typ := grpcReq.RequestType
@@ -1819,6 +1826,28 @@ func (server *BgpServer) handleGrpcModStatement(grpcReq *GrpcRequest) error {
}
func (server *BgpServer) handleGrpcGetPolicy(grpcReq *GrpcRequest) error {
arg := grpcReq.Data.(*api.Policy)
name := arg.Name
found := false
for _, s := range server.policy.PolicyMap {
if name != "" && name != s.Name() {
continue
}
grpcReq.ResponseCh <- &GrpcResponse{
Data: s.ToApiStruct(),
}
found = true
if name != "" {
break
}
}
if !found {
return fmt.Errorf("not found %s", name)
}
return nil
}
func (server *BgpServer) handleMrt(grpcReq *GrpcRequest) {
now := uint32(time.Now().Unix())
view := ""
+3 -3
View File
@@ -2251,12 +2251,12 @@ func (p *Policy) Apply(path *Path) (RouteType, *Path) {
return ROUTE_TYPE_NONE, path
}
func (p *Policy) ToApiStruct() *api.PolicyDefinition {
func (p *Policy) ToApiStruct() *api.Policy {
ss := make([]*api.Statement, 0, len(p.Statements))
for _, s := range p.Statements {
ss = append(ss, s.ToApiStruct())
}
return &api.PolicyDefinition{
return &api.Policy{
Name: p.name,
Statements: ss,
}
@@ -2412,7 +2412,7 @@ func CanImportToVrf(v *Vrf, path *Path) bool {
return c.Evaluate(path)
}
func PoliciesToString(ps []*api.PolicyDefinition) []string {
func PoliciesToString(ps []*api.Policy) []string {
names := make([]string, 0, len(ps))
for _, p := range ps {
names = append(names, p.Name)