summaryrefslogtreecommitdiffhomepage
path: root/policy/policy.go
diff options
context:
space:
mode:
authorNaoto Hanaue <hanaue.naoto@po.ntts.co.jp>2015-06-24 20:34:37 +0900
committerNaoto Hanaue <hanaue.naoto@po.ntts.co.jp>2015-06-26 13:38:02 +0900
commit16f582dfd2b9ddc9d9434a7831f7392df13707cb (patch)
tree89464d465e20fc1af7c23fb4b0e853e4de89c6ba /policy/policy.go
parentb3bd568b8dd4a5536428a1bc9a3612b0f1315a39 (diff)
policy: add unit test for med action and bug fix
Diffstat (limited to 'policy/policy.go')
-rw-r--r--policy/policy.go65
1 files changed, 26 insertions, 39 deletions
diff --git a/policy/policy.go b/policy/policy.go
index 4f56fab6..49d210aa 100644
--- a/policy/policy.go
+++ b/policy/policy.go
@@ -791,28 +791,29 @@ func (a *CommunityAction) apply(path table.Path) table.Path {
type MedAction struct {
DefaultAction
- Value uint32
+ Value int64
action ActionType
}
const (
MED_ACTION_NONE ActionType = iota
+ MED_ACTION_REPLACE
MED_ACTION_ADD
MED_ACTION_SUB
-// MED_ACTION_IGP
)
-//const (
-// MED_IGP string = "IGP"
-//)
// NewMedAction creates MedAction object.
// If it cannot parse med string, then return nil.
-// Similarly, if option string is invalid, return nil.
func NewMedAction(med config.BgpSetMedType) *MedAction {
+ if med == ""{
+ return nil
+ }
+
m := &MedAction{}
- matched, value, action := getMedValue(fmt.Sprintf("%v", med))
+
+ matched, value, action := getMedValue(fmt.Sprintf("%s", med))
if !matched {
log.WithFields(log.Fields{
"Topic": "Policy",
@@ -826,59 +827,45 @@ func NewMedAction(med config.BgpSetMedType) *MedAction {
}
// getMedValue returns uint32 med value and action type (+ or -).
-// if the string doesn't match a number or operator or well known med name,
-// it returns false and 0 and MED_ACTION_NONE.
-func getMedValue(medStr string) (bool, uint32, ActionType) {
- regUint, _ := regexp.Compile("^([0-9]+)$")
- regUintAc, _ := regexp.Compile("^(\\+|\\-)([0-9]+)$")
-// regIGP, _ := regexp.Compile("^(IGP)$")
- if regUint.MatchString(medStr) {
- val, err := strconv.ParseUint(medStr, 10, 32)
- if err != nil {
- log.WithFields(log.Fields{
- "Topic": "Policy",
- "Type": "Med Action",
- }).Error("failed to parser as number or med value.")
- }
- return true, uint32(val), MED_ACTION_NONE
- }
- if regUintAc.MatchString(medStr) {
- group := regUintAc.FindStringSubmatch(medStr)
- action := MED_ACTION_ADD
- if group[1] == "-" {
+// if the string doesn't match a number or operator,
+// it returns false and 0.
+func getMedValue(medStr string) (bool, int64, ActionType) {
+ regMed, _ := regexp.Compile("^(\\+|\\-)?([0-9]+)$")
+ if regMed.MatchString(medStr) {
+ group := regMed.FindStringSubmatch(medStr)
+ action := MED_ACTION_REPLACE
+ if group[1] == "+" {
+ action = MED_ACTION_ADD
+ } else if group[1] == "-" {
action = MED_ACTION_SUB
}
- val, err := strconv.ParseUint(group[2], 10, 32)
+ val, err := strconv.ParseInt(medStr, 10, 64)
if err != nil {
log.WithFields(log.Fields{
"Topic": "Policy",
"Type": "Med Action",
}).Error("failed to parser as number or med value.")
}
- return true, uint32(val), action
+ return true, int64(val), action
}
-// if regIGP.MatchString(medStr) {
-// return true, uint32(0), MED_IGP
-// }
- return false, uint32(0), MED_ACTION_NONE
+ return false, int64(0), MED_ACTION_NONE
}
-
func (a *MedAction) apply(path table.Path) table.Path {
var err error
switch a.action {
- case MED_ACTION_NONE:
- err = path.SetMed(a.Value, true, false)
+ case MED_ACTION_REPLACE:
+ err = path.SetMed(a.Value, true)
case MED_ACTION_ADD:
- err = path.SetMed(a.Value, false, false)
+ err = path.SetMed(a.Value, false)
case MED_ACTION_SUB:
- err = path.SetMed(a.Value, false, true)
+ err = path.SetMed(a.Value, false)
}
if err != nil {
log.WithFields(log.Fields{
"Topic": "Policy",
"Type": "Med Action",
- }).Error(err)
+ }).Warn(err)
}
return path