Example #1
0
func ActionsToConfigStruct(reqActions *api.Actions) config.Actions {
	actions := config.Actions{}
	if reqActions == nil {
		return actions
	}
	if reqActions.Community != nil {
		actions.BgpActions.SetCommunity.Communities = reqActions.Community.Communities
		actions.BgpActions.SetCommunity.Options = reqActions.Community.Options
	}
	if reqActions.Med != "" {
		actions.BgpActions.SetMed = config.BgpSetMedType(reqActions.Med)
	}
	if reqActions.AsPrepend != nil {
		actions.BgpActions.SetAsPathPrepend.As = reqActions.AsPrepend.As
		actions.BgpActions.SetAsPathPrepend.RepeatN = uint8(reqActions.AsPrepend.Repeatn)
	}

	switch reqActions.RouteAction {
	case ROUTE_ACCEPT:
		actions.AcceptRoute = true
	case ROUTE_REJECT:
		actions.RejectRoute = true
	}
	return actions
}
Example #2
0
File: policy.go Project: osrg/gobgp
func modAction(name, op string, args []string) error {
	stmt := config.Statement{
		Name: name,
	}
	usage := fmt.Sprintf("usage: gobgp policy statement %s %s action", name, op)
	if len(args) < 1 {
		return fmt.Errorf("%s { reject | accept | community | ext-community | large-community | med | local-pref | as-prepend | next-hop }", usage)
	}
	typ := args[0]
	args = args[1:]
	switch typ {
	case "reject":
		stmt.Actions.RouteDisposition = config.ROUTE_DISPOSITION_REJECT_ROUTE
	case "accept":
		stmt.Actions.RouteDisposition = config.ROUTE_DISPOSITION_ACCEPT_ROUTE
	case "community":
		if len(args) < 1 {
			return fmt.Errorf("%s community { add | remove | replace } <value>...", usage)
		}
		stmt.Actions.BgpActions.SetCommunity.SetCommunityMethod.CommunitiesList = args[1:]
		switch strings.ToLower(args[0]) {
		case "add":
			stmt.Actions.BgpActions.SetCommunity.Options = string(config.BGP_SET_COMMUNITY_OPTION_TYPE_ADD)
		case "remove":
			stmt.Actions.BgpActions.SetCommunity.Options = string(config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE)
		case "replace":
			stmt.Actions.BgpActions.SetCommunity.Options = string(config.BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE)
		default:
			return fmt.Errorf("%s community { add | remove | replace } <value>...", usage)
		}
	case "ext-community":
		if len(args) < 1 {
			return fmt.Errorf("%s ext-community { add | remove | replace } <value>...", usage)
		}
		stmt.Actions.BgpActions.SetExtCommunity.SetExtCommunityMethod.CommunitiesList = args[1:]
		switch strings.ToLower(args[0]) {
		case "add":
			stmt.Actions.BgpActions.SetExtCommunity.Options = string(config.BGP_SET_COMMUNITY_OPTION_TYPE_ADD)
		case "remove":
			stmt.Actions.BgpActions.SetExtCommunity.Options = string(config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE)
		case "replace":
			stmt.Actions.BgpActions.SetExtCommunity.Options = string(config.BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE)
		default:
			return fmt.Errorf("%s ext-community { add | remove | replace } <value>...", usage)
		}
	case "large-community":
		if len(args) < 1 {
			return fmt.Errorf("%s large-community { add | remove | replace } <value>...", usage)
		}
		stmt.Actions.BgpActions.SetLargeCommunity.SetLargeCommunityMethod.CommunitiesList = args[1:]
		switch strings.ToLower(args[0]) {
		case "add":
			stmt.Actions.BgpActions.SetLargeCommunity.Options = config.BGP_SET_COMMUNITY_OPTION_TYPE_ADD
		case "remove":
			stmt.Actions.BgpActions.SetLargeCommunity.Options = config.BGP_SET_COMMUNITY_OPTION_TYPE_REMOVE
		case "replace":
			stmt.Actions.BgpActions.SetLargeCommunity.Options = config.BGP_SET_COMMUNITY_OPTION_TYPE_REPLACE
		default:
			return fmt.Errorf("%s large-community { add | remove | replace } <value>...", usage)
		}
	case "med":
		if len(args) < 2 {
			return fmt.Errorf("%s med { add | sub | set } <value>", usage)
		}
		med, err := strconv.Atoi(args[1])
		if err != nil {
			return err
		}
		switch strings.ToLower(args[0]) {
		case "add":
			stmt.Actions.BgpActions.SetMed = config.BgpSetMedType(fmt.Sprintf("+%d", med))
		case "sub":
			stmt.Actions.BgpActions.SetMed = config.BgpSetMedType(fmt.Sprintf("-%d", med))
		case "set":
			stmt.Actions.BgpActions.SetMed = config.BgpSetMedType(fmt.Sprintf("%d", med))
		default:
			return fmt.Errorf("%s med { add | sub | set } <value>", usage)
		}
	case "local-pref":
		if len(args) < 1 {
			return fmt.Errorf("%s local-pref <value>", usage)
		}
		value, err := strconv.ParseUint(args[0], 10, 32)
		if err != nil {
			return err
		}
		stmt.Actions.BgpActions.SetLocalPref = uint32(value)
	case "as-prepend":
		if len(args) < 2 {
			return fmt.Errorf("%s as-prepend { <asn> | last-as } <repeat-value>", usage)
		}
		stmt.Actions.BgpActions.SetAsPathPrepend.As = args[0]
		repeat, err := strconv.Atoi(args[1])
		if err != nil {
			return err
		}
		stmt.Actions.BgpActions.SetAsPathPrepend.RepeatN = uint8(repeat)
	case "next-hop":
		if len(args) != 1 {
			return fmt.Errorf("%s next-hop { <value> | self }", usage)
		}
		stmt.Actions.BgpActions.SetNextHop = config.BgpNextHopType(args[0])
	}
	t, err := table.NewStatement(stmt)
	switch op {
	case CMD_ADD:
		err = client.AddStatement(t)
	case CMD_DEL:
		err = client.DeleteStatement(t, false)
	case CMD_SET:
		err = client.ReplaceStatement(t)
	default:
		return fmt.Errorf("invalid operation: %s", op)
	}
	return err
}