diff options
-rw-r--r-- | api/gobgp.pb.go | 67 | ||||
-rw-r--r-- | api/gobgp.proto | 1 | ||||
-rw-r--r-- | gobgp/main.go | 194 | ||||
-rw-r--r-- | server/grpc_server.go | 46 | ||||
-rw-r--r-- | server/server.go | 97 |
5 files changed, 358 insertions, 47 deletions
diff --git a/api/gobgp.pb.go b/api/gobgp.pb.go index bb93a7c4..fda0a608 100644 --- a/api/gobgp.pb.go +++ b/api/gobgp.pb.go @@ -1143,6 +1143,7 @@ type GrpcClient interface { 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) } @@ -1476,8 +1477,39 @@ func (c *grpcClient) GetPolicyNeighbor(ctx context.Context, in *PolicyArguments, return out, nil } +func (c *grpcClient) ModPolicyNeighbor(ctx context.Context, opts ...grpc.CallOption) (Grpc_ModPolicyNeighborClient, error) { + stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[7], 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[7], c.cc, "/api.Grpc/GetPolicyRoutePolicies", opts...) + stream, err := grpc.NewClientStream(ctx, &_Grpc_serviceDesc.Streams[8], c.cc, "/api.Grpc/GetPolicyRoutePolicies", opts...) if err != nil { return nil, err } @@ -1538,6 +1570,7 @@ type GrpcServer interface { 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) } @@ -1835,6 +1868,32 @@ func _Grpc_GetPolicyNeighbor_Handler(srv interface{}, ctx context.Context, codec 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 { @@ -1960,6 +2019,12 @@ var _Grpc_serviceDesc = grpc.ServiceDesc{ 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 f9d66357..7c6bce1b 100644 --- a/api/gobgp.proto +++ b/api/gobgp.proto @@ -38,6 +38,7 @@ service Grpc { 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) {} } diff --git a/gobgp/main.go b/gobgp/main.go index 30218ef7..cda9aa1c 100644 --- a/gobgp/main.go +++ b/gobgp/main.go @@ -256,6 +256,10 @@ func requestGrpc(cmd string, eArgs []string, remoteIP net.IP) error { } else { return showPolicyNeighbor(eArgs) } + case CMD_POLICY + "_" + CMD_NEIGHBOR + "_" + CMD_ADD: + return modPolicyNeighbor(CMD_ADD, eArgs) + case CMD_POLICY + "_" + CMD_NEIGHBOR + "_" + CMD_DEL: + return modPolicyNeighbor(CMD_DEL, eArgs) case CMD_POLICY + "_" + CMD_ROUTEPOLICY: if len(eArgs) == 0 { return showPolicyRoutePolicies() @@ -1216,8 +1220,8 @@ func (x *PolicyCommand) Execute(args []string) error { parser := flags.NewParser(nil, flags.Default) parser.Usage = "policy" parser.AddCommand(CMD_PREFIX, "subcommand for prefix of policy", "", &PolicyPrefixCommand{}) - parser.AddCommand(CMD_NEIGHBOR, "subcommand for prefix of neighbor", "", &PolicyNeighborCommand{}) - parser.AddCommand(CMD_ROUTEPOLICY, "subcommand for prefix of routepolicy", "", &PolicyRoutePolicyCommand{}) + parser.AddCommand(CMD_NEIGHBOR, "subcommand for neighbor of policy", "", &PolicyNeighborCommand{}) + parser.AddCommand(CMD_ROUTEPOLICY, "subcommand for routepolicy of policy", "", &PolicyRoutePolicyCommand{}) if _, err := parser.ParseArgs(eArgs); err != nil { os.Exit(1) } @@ -1226,7 +1230,7 @@ func (x *PolicyCommand) Execute(args []string) error { type PolicyPrefixCommand struct{} -func formatPolicyPrefix(prefixSetList []*api.PrefixSet) string { +func formatPolicyPrefix(prefixSetList []*api.PrefixSet) (string, string) { maxNameLen := len("Name") maxPrefixLen := len("Prefix") maxRangeLen := len("MaskRange") @@ -1243,8 +1247,9 @@ func formatPolicyPrefix(prefixSetList []*api.PrefixSet) string { } } } - format := "%-" + fmt.Sprint(maxNameLen) + "s %-" + fmt.Sprint(maxPrefixLen) + "s %-" + fmt.Sprint(maxRangeLen) + "s\n" - return format + formatPrefixSet := "%-" + fmt.Sprint(maxNameLen) + "s %-" + fmt.Sprint(maxPrefixLen) + "s %-" + fmt.Sprint(maxRangeLen) + "s\n" + formatPrefixListOnly := "%-" + fmt.Sprint(maxPrefixLen) + "s %-" + fmt.Sprint(maxRangeLen) + "s\n" + return formatPrefixSet, formatPrefixListOnly } func showPolicyPrefixes() error { @@ -1281,7 +1286,7 @@ func showPolicyPrefixes() error { } sort.Sort(m) - format := formatPolicyPrefix(m) + format, _ := formatPolicyPrefix(m) fmt.Printf(format, "Name", "Prefix", "MaskRange") for _, ps := range m { for i, p := range ps.PrefixList { @@ -1312,7 +1317,7 @@ func showPolicyPrefix(args []string) error { return nil } - format := formatPolicyPrefix([]*api.PrefixSet{ps}) + format, _ := formatPolicyPrefix([]*api.PrefixSet{ps}) fmt.Printf(format, "Name", "Prefix", "MaskRange") for i, p := range ps.PrefixList { prefix := fmt.Sprintf("%s/%d", p.Address, p.MaskLength) @@ -1340,8 +1345,8 @@ func (x *PolicyPrefixCommand) Execute(args []string) error { } parser := flags.NewParser(nil, flags.Default) parser.Usage = "policy prefix [OPTIONS]\n gobgp policy prefix" - parser.AddCommand(CMD_ADD, "subcommand for add route to policy prefix", "", &PolicyPrefixAddCommand{}) - parser.AddCommand(CMD_DEL, "subcommand for delete route from policy prefix", "", &PolicyPrefixDelCommand{}) + parser.AddCommand(CMD_ADD, "subcommand for add prefix condition to policy", "", &PolicyPrefixAddCommand{}) + parser.AddCommand(CMD_DEL, "subcommand for delete prefix condition from policy", "", &PolicyPrefixDelCommand{}) parser.ParseArgs(eArgs) return nil } @@ -1407,7 +1412,7 @@ func modPolicyPrefix(modtype string, eArgs []string) error { switch modtype { case CMD_ADD: if len(eArgs) < 2 { - return fmt.Errorf("policy prefix add <prefix name> <prefix> [<mask length renge>]") + return fmt.Errorf("policy prefix add <prefix set name> <prefix> [<mask length renge>]") } if prefixSet, e = parsePrefixSet(eArgs); e != nil { return e @@ -1458,7 +1463,7 @@ func modPolicyPrefix(modtype string, eArgs []string) error { func (x *PolicyPrefixAddCommand) Execute(args []string) error { eArgs := extractArgs(CMD_ADD) if len(eArgs) == 0 || len(eArgs) > 3 { - return fmt.Errorf("policy prefix add <prefix name> <prefix> [<mask length renge>]") + return fmt.Errorf("policy prefix add <prefix set name> <prefix> [<mask length renge> ]") } else if !(eArgs[0] == "-h" || eArgs[0] == "--help") { if err := requestGrpc(CMD_POLICY+"_"+CMD_PREFIX+"_"+CMD_ADD, eArgs, nil); err != nil { return err @@ -1466,7 +1471,7 @@ func (x *PolicyPrefixAddCommand) Execute(args []string) error { return nil } parser := flags.NewParser(nil, flags.Default) - parser.Usage = "policy prefix add <prefix name> <prefix> [mask length renge]" + parser.Usage = "policy prefix add <prefix set name> <prefix> [mask length renge]" parser.ParseArgs(eArgs) return nil } @@ -1476,7 +1481,7 @@ type PolicyPrefixDelCommand struct{} func (x *PolicyPrefixDelCommand) Execute(args []string) error { eArgs := extractArgs(CMD_DEL) if len(eArgs) > 3 { - return fmt.Errorf("policy prefix del [<prefix name> [<prefix> [<mask length range>]]] ") + return fmt.Errorf("policy prefix del <prefix set name> [<prefix> [<mask length range>]] ") } else if len(eArgs) > 0 && !(eArgs[0] == "-h" || eArgs[0] == "--help" || eArgs[0] == "all") { if err := requestGrpc(CMD_POLICY+"_"+CMD_PREFIX+"_"+CMD_DEL, eArgs, nil); err != nil { return err @@ -1484,8 +1489,8 @@ func (x *PolicyPrefixDelCommand) Execute(args []string) error { return nil } parser := flags.NewParser(nil, flags.Default) - parser.Usage = "policy prefix del [ <prefix name> <prefix> ]\n policy prefix del" - parser.AddCommand(CMD_ALL, "subcommand for delete all route from policy prefix", "", &PolicyPrefixDelAllCommand{}) + parser.Usage = "policy prefix del <prefix set name> [<prefix> [<mask length range>]]" + parser.AddCommand(CMD_ALL, "subcommand for delete all prefix condition from policy", "", &PolicyPrefixDelAllCommand{}) parser.ParseArgs(eArgs) return nil } @@ -1495,7 +1500,7 @@ type PolicyPrefixDelAllCommand struct{} func (x *PolicyPrefixDelAllCommand) Execute(args []string) error { eArgs := extractArgs(CMD_ALL) if len(eArgs) > 0 && !(eArgs[0] == "-h" || eArgs[0] == "--help") { - return fmt.Errorf("Argument dose not input") + return fmt.Errorf("Argument can not be entered") } else if len(eArgs) == 0 { if err := requestGrpc(CMD_POLICY+"_"+CMD_PREFIX+"_"+CMD_DEL, eArgs, nil); err != nil { return err @@ -1619,6 +1624,138 @@ func (x *PolicyNeighborCommand) Execute(args []string) error { } parser := flags.NewParser(nil, flags.Default) parser.Usage = "policy neighbor [OPTIONS]\n gobgp policy neighbor" + parser.AddCommand(CMD_ADD, "subcommand for add neighbor condition to policy", "", &PolicyNeighborAddCommand{}) + parser.AddCommand(CMD_DEL, "subcommand for delete neighbor condition from policy", "", &PolicyNeighborDelCommand{}) + parser.ParseArgs(eArgs) + return nil +} + +type PolicyNeighborAddCommand struct{} + +func parseNeighborSet(eArgs []string) (*api.NeighborSet, error) { + address := net.ParseIP(eArgs[1]) + if address.To4() == nil { + if address.To16() == nil { + return nil, fmt.Errorf("address is invalid format %s\nplease enter ipv4 or ipv6 format", eArgs[1]) + } + } + + neighbor := &api.Neighbor{ + Address: address.String(), + } + neighborList := []*api.Neighbor{neighbor} + neighborSet := &api.NeighborSet{ + NeighborSetName: eArgs[0], + NeighborList: neighborList, + } + return neighborSet, nil +} + +func modPolicyNeighbor(modtype string, eArgs []string) error { + neighborSet := &api.NeighborSet{} + var e error + var operation api.Operation + + switch modtype { + case CMD_ADD: + if len(eArgs) < 2 { + return fmt.Errorf("policy neighbor add <neighbor set name> <address>") + } + if neighborSet, e = parseNeighborSet(eArgs); e != nil { + return e + } + operation = api.Operation_ADD + case CMD_DEL: + if len(eArgs) == 0 { + operation = api.Operation_DEL_ALL + } else if len(eArgs) == 1 { + neighborSet = &api.NeighborSet{ + NeighborSetName: eArgs[0], + NeighborList: nil, + } + operation = api.Operation_DEL + } else { + if neighborSet, e = parseNeighborSet(eArgs); e != nil { + return e + } + 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 { + return e + } + if res.Code != api.Error_SUCCESS { + return fmt.Errorf("error: code: %d, msg: %s", res.Code, res.Msg) + } + return nil +} + +func (x *PolicyNeighborAddCommand) Execute(args []string) error { + eArgs := extractArgs(CMD_ADD) + if len(eArgs) == 0 || len(eArgs) > 2 { + return fmt.Errorf("policy neighbor add <neighbor set name> <address>") + } else if !(eArgs[0] == "-h" || eArgs[0] == "--help") { + if err := requestGrpc(CMD_POLICY+"_"+CMD_NEIGHBOR+"_"+CMD_ADD, eArgs, nil); err != nil { + return err + } + return nil + } + parser := flags.NewParser(nil, flags.Default) + parser.Usage = "policy neighbor add <neighbor set name> <address>" + parser.ParseArgs(eArgs) + return nil +} + +type PolicyNeighborDelCommand struct{} + +func (x *PolicyNeighborDelCommand) Execute(args []string) error { + eArgs := extractArgs(CMD_DEL) + if len(eArgs) > 3 { + return fmt.Errorf("policy neighbor del [<neighbor set name> [<address>]] ") + } else if len(eArgs) > 0 && !(eArgs[0] == "-h" || eArgs[0] == "--help" || eArgs[0] == "all") { + if err := requestGrpc(CMD_POLICY+"_"+CMD_NEIGHBOR+"_"+CMD_DEL, eArgs, nil); err != nil { + return err + } + return nil + } + parser := flags.NewParser(nil, flags.Default) + parser.Usage = "policy neighbor del [<neighbor set name> [<address>]]" + parser.AddCommand(CMD_ALL, "subcommand for delete all neighbor condition from policy", "", &PolicyNeighborDelAllCommand{}) + parser.ParseArgs(eArgs) + return nil +} + +type PolicyNeighborDelAllCommand struct{} + +func (x *PolicyNeighborDelAllCommand) Execute(args []string) error { + eArgs := extractArgs(CMD_ALL) + if len(eArgs) > 0 && !(eArgs[0] == "-h" || eArgs[0] == "--help") { + return fmt.Errorf("Argument can not be entered") + } else if len(eArgs) == 0 { + if err := requestGrpc(CMD_POLICY+"_"+CMD_NEIGHBOR+"_"+CMD_DEL, eArgs, nil); err != nil { + return err + } + } + + parser := flags.NewParser(nil, flags.Default) + parser.Usage = "policy neighbor del all" parser.ParseArgs(eArgs) return nil } @@ -1630,34 +1767,31 @@ func showPolicyStatement(head string, pd *api.PolicyDefinition) { fmt.Printf("%s StatementName %s:\n", head, st.StatementNeme) fmt.Printf("%s Conditions:\n", head) prefixSet := st.Conditions.MatchPrefixSet - fmt.Printf("%s PrefixSet: ", head) + fmt.Printf("%s PrefixSet: %s ", head, prefixSet.PrefixSetName) if len(prefixSet.PrefixList) != 0 { - format := formatPolicyPrefix([]*api.PrefixSet{st.Conditions.MatchPrefixSet}) + nameFormat := "%-" + fmt.Sprint(len(prefixSet.PrefixSetName)+2) + "s" + _, format := formatPolicyPrefix([]*api.PrefixSet{st.Conditions.MatchPrefixSet}) for i, prefix := range prefixSet.PrefixList { p := fmt.Sprintf("%s/%d", prefix.Address, prefix.MaskLength) - if i == 0 { - fmt.Printf(format, prefixSet.PrefixSetName, p, prefix.MaskLengthRange) - } else { + if i != 0 { fmt.Printf("%s ", head) - fmt.Printf(format, "", p, prefix.MaskLengthRange) + fmt.Printf(nameFormat, "") } - + fmt.Printf(format, p, prefix.MaskLengthRange) } } else { fmt.Print("\n") } neighborSet := st.Conditions.MatchNeighborSet - fmt.Printf("%s NeighborSet: ", head) + fmt.Printf("%s NeighborSet: %s ", head, neighborSet.NeighborSetName) if len(neighborSet.NeighborList) != 0 { - format := formatPolicyNeighbor([]*api.NeighborSet{st.Conditions.MatchNeighborSet}) + nameFormat := "%-" + fmt.Sprint(len(neighborSet.NeighborSetName)+2) + "s" for i, neighbor := range neighborSet.NeighborList { - if i == 0 { - fmt.Printf(format, neighborSet.NeighborSetName, neighbor.Address) - } else { + if i != 0 { fmt.Printf("%s ", head) - fmt.Printf(format, "", neighbor.Address) + fmt.Printf(nameFormat, "") } - + fmt.Println(neighbor.Address) } } else { fmt.Print("\n") diff --git a/server/grpc_server.go b/server/grpc_server.go index d8188c56..dbd1be1e 100644 --- a/server/grpc_server.go +++ b/server/grpc_server.go @@ -51,6 +51,9 @@ const ( REQ_POLICY_PREFIXES_DELETE REQ_POLICY_NEIGHBOR REQ_POLICY_NEIGHBORS + REQ_POLICY_NEIGHBOR_ADD + REQ_POLICY_NEIGHBOR_DELETE + REQ_POLICY_NEIGHBORS_DELETE REQ_POLICY_ROUTEPOLICIES REQ_POLICY_ROUTEPOLICY ) @@ -341,6 +344,7 @@ func (s *Server) getPolicy(arg *api.PolicyArguments) (interface{}, error) { func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { var rf bgp.RouteFamily var reqType int + var err error switch arg.Resource { case api.Resource_POLICY_PREFIX: switch arg.Operation { @@ -361,17 +365,38 @@ func (s *Server) modPolicy(arg *api.PolicyArguments, stream interface{}) error { log.Debug(err.Error()) return err } - - err := stream.(api.Grpc_ModPolicyPrefixServer).Send(&api.Error{ + err = stream.(api.Grpc_ModPolicyPrefixServer).Send(&api.Error{ Code: api.Error_SUCCESS, }) + case api.Resource_POLICY_NEIGHBOR: + switch arg.Operation { + case api.Operation_ADD: + reqType = REQ_POLICY_NEIGHBOR_ADD + case api.Operation_DEL: + reqType = REQ_POLICY_NEIGHBOR_DELETE + case api.Operation_DEL_ALL: + reqType = REQ_POLICY_NEIGHBORS_DELETE + default: + return fmt.Errorf("unsupported operation: %s", arg.Operation) + } + req := NewGrpcRequest(reqType, "", rf, arg.NeighborSet) + s.bgpServerCh <- req - if err != nil { + 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, + }) default: return fmt.Errorf("unsupported resource type: %v", arg.Resource) } + + if err != nil { + return err + } return nil } @@ -421,6 +446,21 @@ func (s *Server) GetPolicyNeighbor(ctx context.Context, arg *api.PolicyArguments 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 { + return err + } + if err := s.modPolicy(arg, stream); 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 diff --git a/server/server.go b/server/server.go index cff4dbb2..adc2bc57 100644 --- a/server/server.go +++ b/server/server.go @@ -337,7 +337,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Prefix doesn't exist.") + result.ResponseErr = fmt.Errorf("Policy prefix doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -358,7 +358,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } grpcReq.ResponseCh <- result } else { - result.ResponseErr = fmt.Errorf("Prefix that has %v doesn't exist.", name) + result.ResponseErr = fmt.Errorf("Policy prefix that has %v doesn't exist.", name) grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -368,11 +368,10 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { result := &GrpcResponse{} isReqPrefixSet, prefixSet := policy.PrefixSetToConfigStruct(reqPrefixSet) if !isReqPrefixSet { - result.ResponseErr = fmt.Errorf("dose not reqest of policy prefix.") + result.ResponseErr = fmt.Errorf("doesn't reqest of policy prefix.") grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) } - // If the same PrefixSet is not set, add PrefixSet of request to the end. // If only name of the PrefixSet is same, overwrite with PrefixSet of request idxPrefixSet, idxPrefix := policy.IndexOfPrefixSet(conPrefixSetList, prefixSet) @@ -393,16 +392,15 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { 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. idxPrefixSet, idxPrefix := policy.IndexOfPrefixSet(conPrefixSetList, prefixSet) if idxPrefixSet == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, + result.ResponseErr = fmt.Errorf("Policy prefix that has %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, prefixSet.PrefixList[0].Address, prefixSet.PrefixList[0].Masklength, prefixSet.PrefixList[0].MasklengthRange) } else { if idxPrefix == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, + result.ResponseErr = fmt.Errorf("Policy prefix that has %v %v/%v %v doesn't exist.", prefixSet.PrefixSetName, prefixSet.PrefixList[0].Address, prefixSet.PrefixList[0].Masklength, prefixSet.PrefixList[0].MasklengthRange) } else { copy(conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix:], conPrefixSetList[idxPrefixSet].PrefixList[idxPrefix+1:]) @@ -418,7 +416,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } } if idxPrefixSet == -1 { - result.ResponseErr = fmt.Errorf("Prefix %v doesn't exist.", prefixSet.PrefixSetName) + result.ResponseErr = fmt.Errorf("Policy prefix that has %v doesn't exist.", prefixSet.PrefixSetName) } else { copy(conPrefixSetList[idxPrefixSet:], conPrefixSetList[idxPrefixSet+1:]) conPrefixSetList = conPrefixSetList[:len(conPrefixSetList)-1] @@ -430,8 +428,8 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { close(grpcReq.ResponseCh) case REQ_POLICY_PREFIXES_DELETE: result := &GrpcResponse{} - pl := config.RoutingPolicy{} - server.handlePolicy(pl) + server.routingPolicy.DefinedSets.PrefixSetList = make([]config.PrefixSet, 0) + server.handlePolicy(server.routingPolicy) grpcReq.ResponseCh <- result close(grpcReq.ResponseCh) case REQ_POLICY_NEIGHBORS: @@ -446,7 +444,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Neighbor doesn't exist.") + result.ResponseErr = fmt.Errorf("Policy neighbor doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) @@ -467,9 +465,82 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { } grpcReq.ResponseCh <- result } else { - result.ResponseErr = fmt.Errorf("Neighbor that has %v doesn't exist.", name) + 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) + 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. + // If the same element NeighborSet, delete the it's element from NeighborSet. + idxNeighborSet, idxNeighbor := policy.IndexOfNeighborSet(conNeighborSetList, neighborSet) + if idxNeighborSet == -1 { + result.ResponseErr = fmt.Errorf("Policy neighbor that has %v %v doesn't exist.", neighborSet.NeighborSetName, + neighborSet.NeighborInfoList[0].Address) + } else { + if idxNeighbor == -1 { + result.ResponseErr = fmt.Errorf("Policy neighbor that has %v %v doesn't exist.", neighborSet.NeighborSetName, + neighborSet.NeighborInfoList[0].Address) + } else { + copy(conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor:], + conNeighborSetList[idxNeighborSet].NeighborInfoList[idxNeighbor+1:]) + conNeighborSetList[idxNeighborSet].NeighborInfoList = + conNeighborSetList[idxNeighborSet].NeighborInfoList[:len(conNeighborSetList[idxNeighborSet].NeighborInfoList)-1] + } + } + } else { + idxNeighborSet := -1 + for i, conNeighborSet := range conNeighborSetList { + if conNeighborSet.NeighborSetName == reqNeighborSet.NeighborSetName { + idxNeighborSet = i + break + } + } + if idxNeighborSet == -1 { + result.ResponseErr = fmt.Errorf("Policy neighbor %v doesn't exist.", neighborSet.NeighborSetName) + } else { + copy(conNeighborSetList[idxNeighborSet:], conNeighborSetList[idxNeighborSet+1:]) + conNeighborSetList = conNeighborSetList[:len(conNeighborSetList)-1] + } } + 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 @@ -484,7 +555,7 @@ func (server *BgpServer) handleGrpc(grpcReq *GrpcRequest) { grpcReq.ResponseCh <- result } } else { - result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") + result.ResponseErr = fmt.Errorf("Route Policy doesn't exist.") grpcReq.ResponseCh <- result } close(grpcReq.ResponseCh) |