|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "net" |
| 7 | + |
| 8 | + "go.fd.io/govpp/binapi/interface_types" |
| 9 | + "go.fd.io/govpp/binapi/vpe" |
| 10 | +) |
| 11 | + |
| 12 | +type jsonVppDetails struct { |
| 13 | + Program string `json:"program"` |
| 14 | + Version string `json:"version"` |
| 15 | + BuildDate string `json:"build_date"` |
| 16 | + BuildDirectory string `json:"build_directory"` |
| 17 | +} |
| 18 | + |
| 19 | +type jsonVppInterface struct { |
| 20 | + Index uint32 `json:"if_index"` |
| 21 | + Name string `json:"if_name"` |
| 22 | + Tag string `json:"if_tag"` |
| 23 | + MacAddress string `json:"if_mac"` |
| 24 | + AdminState bool `json:"if_admin_state"` |
| 25 | + LinkState bool `json:"if_link_state"` |
| 26 | + LinkMTU uint16 `json:"if_link_mtu"` |
| 27 | + SubID uint32 `json:"if_sub_id"` |
| 28 | + |
| 29 | + TxBytes uint64 `json:"if_tx_bytes"` |
| 30 | + TxPackets uint64 `json:"if_tx_packets"` |
| 31 | + TxErrors uint64 `json:"if_tx_errors"` |
| 32 | + RxBytes uint64 `json:"if_rx_bytes"` |
| 33 | + RxPackets uint64 `json:"if_rx_packets"` |
| 34 | + RxErrors uint64 `json:"if_rx_errors"` |
| 35 | + Drops uint64 `json:"if_drops"` |
| 36 | + Punts uint64 `json:"if_punts"` |
| 37 | +} |
| 38 | + |
| 39 | +type jsonVppPayload struct { |
| 40 | + *jsonVppDetails `json:"vpp_details"` |
| 41 | + Interfaces []*jsonVppInterface `json:"interfaces"` |
| 42 | +} |
| 43 | + |
| 44 | + func toJSONVppDetails(svReply *vpe.ShowVersionReply) *jsonVppDetails { |
| 45 | + return &jsonVppDetails{ |
| 46 | + Program: svReply.Program, |
| 47 | + Version: svReply.Version, |
| 48 | + BuildDate: svReply.BuildDate, |
| 49 | + BuildDirectory: svReply.BuildDirectory, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func toJSONVppInterface(vppIf *vppInterface) *jsonVppInterface { |
| 54 | + return &jsonVppInterface{ |
| 55 | + Index: uint32(vppIf.SwIfIndex), |
| 56 | + Name: vppIf.InterfaceName, |
| 57 | + Tag: vppIf.Tag, |
| 58 | + MacAddress: net.HardwareAddr(vppIf.L2Address[:]).String(), |
| 59 | + AdminState: vppIf.Flags&interface_types.IF_STATUS_API_FLAG_ADMIN_UP != 0, |
| 60 | + LinkState: vppIf.Flags&interface_types.IF_STATUS_API_FLAG_LINK_UP != 0, |
| 61 | + LinkMTU: vppIf.LinkMtu, |
| 62 | + SubID: vppIf.SubID, |
| 63 | + TxBytes: vppIf.Stats.TxBytes, |
| 64 | + TxPackets: vppIf.Stats.TxPackets, |
| 65 | + TxErrors: vppIf.Stats.TxErrors, |
| 66 | + RxBytes: vppIf.Stats.RxBytes, |
| 67 | + RxPackets: vppIf.Stats.RxPackets, |
| 68 | + RxErrors: vppIf.Stats.RxErrors, |
| 69 | + Drops: vppIf.Stats.Drops, |
| 70 | + Punts: vppIf.Stats.Punts, |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +func toJSONVppPayload(svReply *vpe.ShowVersionReply, vppIfs []*vppInterface) *jsonVppPayload { |
| 75 | + p := &jsonVppPayload{ |
| 76 | + jsonVppDetails: toJSONVppDetails(svReply), |
| 77 | + Interfaces: make([]*jsonVppInterface, len(vppIfs)), |
| 78 | + } |
| 79 | + for index, vppIf := range vppIfs { |
| 80 | + p.Interfaces[index] = toJSONVppInterface(vppIf) |
| 81 | + } |
| 82 | + return p |
| 83 | +} |
| 84 | + |
| 85 | +func dumpToJSONString(v *vppConnector) (string, error) { |
| 86 | + payload := toJSONVppPayload(&v.VppDetails, v.Interfaces) |
| 87 | + jsonBytes, err := json.Marshal(payload) |
| 88 | + if err != nil { |
| 89 | + return "", fmt.Errorf("failed to dump to json: %v", err) |
| 90 | + } |
| 91 | + return string(jsonBytes), nil |
| 92 | +} |
0 commit comments