github.com/zartbot/goflow@v0.0.0-20221001080520-456f7dfad288/service/optiontemplatemap/option_template_c3pl.go (about) 1 package optiontemplatemap 2 3 import ( 4 "bytes" 5 "encoding/binary" 6 "errors" 7 "fmt" 8 "sync" 9 ) 10 11 /* 12 Client: Option classmap option table 13 _____________________________________________________________________________ 14 | Field | ID | Ent.ID | Offset | Size | 15 ----------------------------------------------------------------------------- 16 | C3PL CLASS CCE-ID | 8233 | 9 | 0 | 4 | 17 | c3pl class name | 8234 | 9 | 4 | 512 | 18 | c3pl class type | 8235 | 9 | 516 | 256 | 19 ----------------------------------------------------------------------------- 20 21 */ 22 23 type C3plClassData struct { 24 Name string 25 Type string 26 } 27 28 type C3plClassKey struct { 29 AgentID string 30 CCEID uint32 31 } 32 33 var C3plClassTable sync.Map 34 35 func UpdateC3plClassDatabase(AgentID string, cceid uint32, name string, t string) error { 36 key := C3plClassKey{ 37 AgentID: AgentID, 38 CCEID: cceid, 39 } 40 data := C3plClassData{ 41 Name: name, 42 Type: t, 43 } 44 C3plClassTable.Store(key, data) 45 return nil 46 } 47 48 func FetchC3plClassDatabaseByIndex(AgentID string, cceid uint32) (C3plClassData, error) { 49 var err error 50 var i C3plClassData 51 key := C3plClassKey{ 52 AgentID: AgentID, 53 CCEID: cceid, 54 } 55 56 value, ok := C3plClassTable.Load(key) 57 if ok { 58 i, valid := value.(C3plClassData) 59 if !valid { 60 err = errors.New("invalid type assertion") 61 return i, err 62 } else { 63 return i, nil 64 } 65 } else { 66 err = errors.New("Interface does not found in database") 67 return i, err 68 } 69 } 70 71 func ShowClassMapDatabase() { 72 C3plClassTable.Range(func(k, v interface{}) bool { 73 value := v.(C3plClassData) 74 key := k.(C3plClassKey) 75 fmt.Printf("C3PL Class Map[%10s:%-6d] | Name: %-20s | Type: %-50s \n", key.AgentID, key.CCEID, value.Name, value.Type) 76 return true 77 }) 78 } 79 80 /* 81 Client: Option policymap option table 82 _____________________________________________________________________________ 83 | Field | ID | Ent.ID | Offset | Size | 84 ----------------------------------------------------------------------------- 85 | C3PL POLICY CCE-ID | 8236 | 9 | 0 | 4 | 86 | c3pl policy name | 8237 | 9 | 4 | 512 | 87 | c3pl policy type | 8238 | 9 | 516 | 256 | 88 ----------------------------------------------------------------------------- 89 */ 90 type C3plPolicyData struct { 91 Name string 92 Type string 93 } 94 95 type C3plPolicyKey struct { 96 AgentID string 97 CCEID uint32 98 } 99 100 var C3plPolicyTable sync.Map 101 102 func UpdateC3plPolicyDatabase(AgentID string, cceid uint32, name string, t string) error { 103 key := C3plPolicyKey{ 104 AgentID: AgentID, 105 CCEID: cceid, 106 } 107 data := C3plPolicyData{ 108 Name: name, 109 Type: t, 110 } 111 C3plPolicyTable.Store(key, data) 112 return nil 113 } 114 115 func FetchC3plPolicyDatabaseByIndex(AgentID string, cceid uint32) (C3plPolicyData, error) { 116 var err error 117 var i C3plPolicyData 118 key := C3plPolicyKey{ 119 AgentID: AgentID, 120 CCEID: cceid, 121 } 122 123 value, ok := C3plPolicyTable.Load(key) 124 if ok { 125 i, valid := value.(C3plPolicyData) 126 if !valid { 127 err = errors.New("invalid type assertion") 128 return i, err 129 } else { 130 return i, nil 131 } 132 } else { 133 err = errors.New("Interface does not found in database") 134 return i, err 135 } 136 } 137 138 func ShowPolicyMapDatabase() { 139 C3plPolicyTable.Range(func(k, v interface{}) bool { 140 value := v.(C3plPolicyData) 141 key := k.(C3plPolicyKey) 142 fmt.Printf("C3PL Policy Map[%10s:%-6d] | Name: %-20s | Type: %-50s \n", key.AgentID, key.CCEID, value.Name, value.Type) 143 return true 144 }) 145 } 146 147 func ParseClassificationHierarchy(b []byte, AgentID string) string { 148 var result string 149 for idx := 0; idx < len(b); idx = idx + 4 { 150 value := binary.BigEndian.Uint32(b[idx : idx+4]) 151 if value != 0 { 152 if idx == 0 { 153 r, err := FetchC3plPolicyDatabaseByIndex(AgentID, value) 154 if err == nil { 155 result = r.Name 156 } else { 157 return "" 158 } 159 } else { 160 r, err := FetchC3plClassDatabaseByIndex(AgentID, value) 161 if err == nil { 162 result = result + "|->" + r.Name 163 } else { 164 return "" 165 } 166 } 167 } else { 168 break 169 } 170 } 171 return result 172 } 173 174 func UpdateC3PLMap(d map[string]interface{}, AgentID string) error { 175 var err error 176 177 /* 178 v, errv := map2value.MapToUInt32(d, "policy_qos_queue_index") 179 cd, errcd := map2value.MapToUInt64(d, "policy_qos_queue_drops") 180 181 if errv == nil && errcd == nil { 182 logrus.Warn(v, cd) 183 } 184 */ 185 classID, hasClassID := d["c3pl_class_cce_id"] 186 if hasClassID { 187 //isTemplate ? 188 className, hasName := d["c3pl_class_name"] 189 classType, hasType := d["c3pl_class_type"] 190 if hasName && hasType { 191 n := bytes.IndexByte(className.([]byte), 0) 192 namestr := string(className.([]byte)[:n]) 193 194 tn := bytes.IndexByte(classType.([]byte), 0) 195 typestr := string(classType.([]byte)[:tn]) 196 197 err = UpdateC3plClassDatabase(AgentID, classID.(uint32), namestr, typestr) 198 //fmt.Println("DEBUG----->:", AgentID, classID.(uint32), className.(string), classType.(string)) 199 } else { 200 //data record contains class field 201 r, err := FetchC3plClassDatabaseByIndex(AgentID, classID.(uint32)) 202 if err == nil { 203 d["class_map_name"] = r.Name 204 d["class_map_type"] = r.Type 205 } 206 } 207 } 208 policyID, hasPolicyID := d["c3pl_policy_cce_id"] 209 if hasPolicyID { 210 //isTemplate ? 211 policyName, hasName := d["c3pl_policy_name"] 212 policyType, hasType := d["c3pl_policy_type"] 213 if hasName && hasType { 214 n := bytes.IndexByte(policyName.([]byte), 0) 215 namestr := string(policyName.([]byte)[:n]) 216 217 tn := bytes.IndexByte(policyType.([]byte), 0) 218 typestr := string(policyType.([]byte)[:tn]) 219 220 err = UpdateC3plPolicyDatabase(AgentID, policyID.(uint32), namestr, typestr) 221 //fmt.Println("DEBUG--POLICY-MAP--->:", AgentID, policyID.(uint32), policyName.(string), policyType.(string)) 222 } else { 223 //data record contains policy map field 224 r, err := FetchC3plPolicyDatabaseByIndex(AgentID, policyID.(uint32)) 225 if err == nil { 226 d["policy_map_name"] = r.Name 227 d["policy_map_type"] = r.Type 228 } 229 } 230 } 231 232 if policyarray, ok := d["policy_qos_classification_hierarchy"]; ok { 233 d["policy_hierarchy_array"] = ParseClassificationHierarchy(policyarray.([]byte), AgentID) 234 delete(d, "policy_qos_classification_hierarchy") 235 //fmt.Printf("DEBUG:::------->:::::::POLICYARRAY:%+v\t %+v\n", policyarray.([]byte), r) 236 } 237 238 /* 239 if a, ok := d["policy_qos_queue_drops"]; ok { 240 fmt.Println(a) 241 } 242 */ 243 244 return err 245 }