github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/optiontemplatemap/option_template_interface.go (about) 1 package optiontemplatemap 2 3 import ( 4 "encoding/binary" 5 "errors" 6 "fmt" 7 "sync" 8 ) 9 10 // InterfaceData : Interface Template 11 // _____________________________________________________________________________ 12 // | Field | ID | Ent.ID | Offset | Size | 13 // ----------------------------------------------------------------------------- 14 // | INTERFACE INPUT SNMP | 10 | | 0 | 4 | 15 // | interface name short | 82 | | 4 | 32 | 16 // | interface name long | 83 | | 36 | 64 | 17 // ----------------------------------------------------------------------------- 18 19 type InterfaceData struct { 20 Name string 21 Description string 22 } 23 24 type InterfaceMapKey struct { 25 AgentID string 26 Ifindex uint32 27 } 28 29 var InterfaceTable sync.Map 30 31 func UpdateInterfaceDatabase(AgentID string, ifindex uint32, name string, Description string) error { 32 key := InterfaceMapKey{ 33 AgentID: AgentID, 34 Ifindex: ifindex, 35 } 36 data := InterfaceData{ 37 Name: name, 38 Description: Description, 39 } 40 /* 41 //store will cause map lock, so try to load and compare before store 42 olddata, _ := FetchInterfaceDatabaseByIndex(AgentID, ifindex) 43 if olddata != data { 44 InterfaceTable.Store(key, data) 45 } 46 */ 47 InterfaceTable.Store(key, data) 48 49 return nil 50 } 51 52 func FetchInterfaceDatabaseByIndex(AgentID string, ifindex uint32) (InterfaceData, error) { 53 var err error 54 var i InterfaceData 55 key := InterfaceMapKey{ 56 AgentID: AgentID, 57 Ifindex: ifindex, 58 } 59 60 value, ok := InterfaceTable.Load(key) 61 if ok { 62 i, valid := value.(InterfaceData) 63 if !valid { 64 err = errors.New("invalid type assertion") 65 return i, err 66 } else { 67 return i, nil 68 } 69 } else { 70 err = errors.New("Interface does not found in database") 71 return i, err 72 } 73 } 74 75 func ShowInterfaceDatabase() { 76 InterfaceTable.Range(func(k, v interface{}) bool { 77 value := v.(InterfaceData) 78 key := k.(InterfaceMapKey) 79 fmt.Printf("Interface Map[%10s:%-6d] | Name: %-20s | Description: %-50s \n", key.AgentID, key.Ifindex, value.Name, value.Description) 80 return true 81 }) 82 } 83 84 func UpdateInterfaceMap(d map[string]interface{}, AgentID string) error { 85 var err error 86 ingressIntf, hasInputIntf := d["ingressInterface"] 87 if hasInputIntf { 88 //isTemplate ? 89 intfName, hasNameShort := d["interfaceName"] 90 intfDescription, hasNameLong := d["interfaceDescription"] 91 if hasNameShort && hasNameLong { 92 err = UpdateInterfaceDatabase(AgentID, ingressIntf.(uint32), intfName.(string), intfDescription.(string)) 93 } else { 94 //data record contains interface field 95 r, err := FetchInterfaceDatabaseByIndex(AgentID, ingressIntf.(uint32)) 96 if err == nil { 97 //var ir fieldvalue.FieldValue 98 //ir.Name = "ingressInterfaceName" 99 //ir.Value = r.Description 100 //ir.Type = "string" 101 d["ingressInterfaceName"] = r.Description 102 } 103 } 104 } 105 egressIntf, egressOk := d["egressInterface"] 106 if egressOk { 107 r, err := FetchInterfaceDatabaseByIndex(AgentID, egressIntf.(uint32)) 108 if err == nil { 109 //var er fieldvalue.FieldValue 110 //er.Name = "egressInterfaceName" 111 //er.Type = "string" 112 //er.Value = r.Description 113 d["egressInterfaceName"] = r.Description 114 } 115 } 116 if _obvID, ok := d["observationPointId"]; ok && _obvID != nil { 117 if obvID, valid := _obvID.([]byte); valid { 118 value := binary.BigEndian.Uint32(obvID[4:8]) 119 r, rerr := FetchInterfaceDatabaseByIndex(AgentID, value) 120 if rerr == nil { 121 d["observationInterface"] = r.Description 122 d["observationPointId"] = value 123 } else { 124 d["observationInterface"] = "Unknown" 125 d["observationPointId"] = value 126 } 127 } 128 } 129 return err 130 }