github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/optiontemplatemap/option_template_fw_classmap.go (about) 1 package optiontemplatemap 2 3 import ( 4 "errors" 5 "fmt" 6 "sync" 7 ) 8 9 type FWClassMapMapKey struct { 10 AgentID string 11 classid uint8 12 } 13 14 var FWClassMap sync.Map 15 16 func UpdateFWClassMap(AgentID string, classid uint8, Description string) error { 17 key := FWClassMapMapKey{ 18 AgentID: AgentID, 19 classid: classid, 20 } 21 FWClassMap.Store(key, string(Description)) 22 23 return nil 24 } 25 26 func FetchFWClassMapByIndex(AgentID string, classid uint8) (string, error) { 27 var err error 28 var i string 29 key := FWClassMapMapKey{ 30 AgentID: AgentID, 31 classid: classid, 32 } 33 34 value, ok := FWClassMap.Load(key) 35 if ok { 36 i, valid := value.(string) 37 if !valid { 38 err = errors.New("invalid type assertion") 39 return i, err 40 } else { 41 return i, nil 42 } 43 } else { 44 err = errors.New("Event does not found in database") 45 return i, err 46 } 47 } 48 49 func ShowFWClassMap() { 50 FWClassMap.Range(func(k, v interface{}) bool { 51 value := v.(string) 52 key := k.(FWClassMapMapKey) 53 fmt.Printf("FW Class Map[%10s:%-6d] | Description: %-50s \n", key.AgentID, key.classid, value) 54 return true 55 }) 56 } 57 58 func UpdateFWClassMapMap(d map[string]interface{}, AgentID string) error { 59 var err error 60 classid, hasclassid := d["classId"] 61 if hasclassid { 62 //isTemplate ? 63 className, hasclassName := d["className"] 64 65 if hasclassid && hasclassName { 66 err = UpdateFWClassMap(AgentID, classid.(uint8), className.(string)) 67 } else { 68 //data record contains interface field 69 r, err := FetchFWClassMapByIndex(AgentID, classid.(uint8)) 70 if err == nil { 71 //var ir fieldvalue.FieldValue 72 //ir.Name = "ingressInterfaceName" 73 //ir.Value = r.Description 74 //ir.Type = "string" 75 d["fw_class_map"] = r 76 } 77 } 78 } 79 return err 80 }