Binary API generator improvements
[govpp.git] / examples / binapi / ip_types / ip_types.ba.go
index e812b8b..fc7fa87 100644 (file)
@@ -1,12 +1,15 @@
 // Code generated by GoVPP's binapi-generator. DO NOT EDIT.
+// versions:
+//  binapi-generator: v0.4.0-dev
+//  VPP:              20.05-release
 // source: /usr/share/vpp/api/core/ip_types.api.json
 
 /*
-Package ip_types is a generated VPP binary API for 'ip_types' module.
+Package ip_types contains generated code for VPP API file ip_types.api (3.0.0).
 
 It consists of:
-         4 enums
          5 aliases
+         4 enums
          6 types
          1 union
 */
@@ -15,46 +18,59 @@ package ip_types
 import (
        "bytes"
        "context"
+       "encoding/binary"
+       "fmt"
        "io"
+       "math"
+       "net"
        "strconv"
+       "strings"
 
        api "git.fd.io/govpp.git/api"
+       codec "git.fd.io/govpp.git/codec"
        struc "github.com/lunixbochs/struc"
 )
 
+// This is a compile-time assertion to ensure that this generated file
+// is compatible with the GoVPP api package it is being compiled against.
+// A compilation error at this line likely means your copy of the
+// GoVPP api package needs to be updated.
+const _ = api.GoVppAPIPackageIsVersion2 // please upgrade the GoVPP api package
+
 const (
        // ModuleName is the name of this module.
        ModuleName = "ip_types"
        // APIVersion is the API version of this module.
        APIVersion = "3.0.0"
        // VersionCrc is the CRC of this module.
-       VersionCrc = 0x80424562
+       VersionCrc = 0x9cb1c2e2
 )
 
 // AddressFamily represents VPP binary API enum 'address_family'.
-type AddressFamily uint32
+type AddressFamily uint8
 
 const (
        ADDRESS_IP4 AddressFamily = 0
        ADDRESS_IP6 AddressFamily = 1
 )
 
-var AddressFamily_name = map[uint32]string{
-       0: "ADDRESS_IP4",
-       1: "ADDRESS_IP6",
-}
-
-var AddressFamily_value = map[string]uint32{
-       "ADDRESS_IP4": 0,
-       "ADDRESS_IP6": 1,
-}
+var (
+       AddressFamily_name = map[uint8]string{
+               0: "ADDRESS_IP4",
+               1: "ADDRESS_IP6",
+       }
+       AddressFamily_value = map[string]uint8{
+               "ADDRESS_IP4": 0,
+               "ADDRESS_IP6": 1,
+       }
+)
 
 func (x AddressFamily) String() string {
-       s, ok := AddressFamily_name[uint32(x)]
+       s, ok := AddressFamily_name[uint8(x)]
        if ok {
                return s
        }
-       return strconv.Itoa(int(x))
+       return "AddressFamily(" + strconv.Itoa(int(x)) + ")"
 }
 
 // IPDscp represents VPP binary API enum 'ip_dscp'.
@@ -84,60 +100,61 @@ const (
        IP_API_DSCP_CS7  IPDscp = 50
 )
 
-var IPDscp_name = map[uint8]string{
-       0:  "IP_API_DSCP_CS0",
-       8:  "IP_API_DSCP_CS1",
-       10: "IP_API_DSCP_AF11",
-       12: "IP_API_DSCP_AF12",
-       14: "IP_API_DSCP_AF13",
-       16: "IP_API_DSCP_CS2",
-       18: "IP_API_DSCP_AF21",
-       20: "IP_API_DSCP_AF22",
-       22: "IP_API_DSCP_AF23",
-       24: "IP_API_DSCP_CS3",
-       26: "IP_API_DSCP_AF31",
-       28: "IP_API_DSCP_AF32",
-       30: "IP_API_DSCP_AF33",
-       32: "IP_API_DSCP_CS4",
-       34: "IP_API_DSCP_AF41",
-       36: "IP_API_DSCP_AF42",
-       38: "IP_API_DSCP_AF43",
-       40: "IP_API_DSCP_CS5",
-       46: "IP_API_DSCP_EF",
-       48: "IP_API_DSCP_CS6",
-       50: "IP_API_DSCP_CS7",
-}
-
-var IPDscp_value = map[string]uint8{
-       "IP_API_DSCP_CS0":  0,
-       "IP_API_DSCP_CS1":  8,
-       "IP_API_DSCP_AF11": 10,
-       "IP_API_DSCP_AF12": 12,
-       "IP_API_DSCP_AF13": 14,
-       "IP_API_DSCP_CS2":  16,
-       "IP_API_DSCP_AF21": 18,
-       "IP_API_DSCP_AF22": 20,
-       "IP_API_DSCP_AF23": 22,
-       "IP_API_DSCP_CS3":  24,
-       "IP_API_DSCP_AF31": 26,
-       "IP_API_DSCP_AF32": 28,
-       "IP_API_DSCP_AF33": 30,
-       "IP_API_DSCP_CS4":  32,
-       "IP_API_DSCP_AF41": 34,
-       "IP_API_DSCP_AF42": 36,
-       "IP_API_DSCP_AF43": 38,
-       "IP_API_DSCP_CS5":  40,
-       "IP_API_DSCP_EF":   46,
-       "IP_API_DSCP_CS6":  48,
-       "IP_API_DSCP_CS7":  50,
-}
+var (
+       IPDscp_name = map[uint8]string{
+               0:  "IP_API_DSCP_CS0",
+               8:  "IP_API_DSCP_CS1",
+               10: "IP_API_DSCP_AF11",
+               12: "IP_API_DSCP_AF12",
+               14: "IP_API_DSCP_AF13",
+               16: "IP_API_DSCP_CS2",
+               18: "IP_API_DSCP_AF21",
+               20: "IP_API_DSCP_AF22",
+               22: "IP_API_DSCP_AF23",
+               24: "IP_API_DSCP_CS3",
+               26: "IP_API_DSCP_AF31",
+               28: "IP_API_DSCP_AF32",
+               30: "IP_API_DSCP_AF33",
+               32: "IP_API_DSCP_CS4",
+               34: "IP_API_DSCP_AF41",
+               36: "IP_API_DSCP_AF42",
+               38: "IP_API_DSCP_AF43",
+               40: "IP_API_DSCP_CS5",
+               46: "IP_API_DSCP_EF",
+               48: "IP_API_DSCP_CS6",
+               50: "IP_API_DSCP_CS7",
+       }
+       IPDscp_value = map[string]uint8{
+               "IP_API_DSCP_CS0":  0,
+               "IP_API_DSCP_CS1":  8,
+               "IP_API_DSCP_AF11": 10,
+               "IP_API_DSCP_AF12": 12,
+               "IP_API_DSCP_AF13": 14,
+               "IP_API_DSCP_CS2":  16,
+               "IP_API_DSCP_AF21": 18,
+               "IP_API_DSCP_AF22": 20,
+               "IP_API_DSCP_AF23": 22,
+               "IP_API_DSCP_CS3":  24,
+               "IP_API_DSCP_AF31": 26,
+               "IP_API_DSCP_AF32": 28,
+               "IP_API_DSCP_AF33": 30,
+               "IP_API_DSCP_CS4":  32,
+               "IP_API_DSCP_AF41": 34,
+               "IP_API_DSCP_AF42": 36,
+               "IP_API_DSCP_AF43": 38,
+               "IP_API_DSCP_CS5":  40,
+               "IP_API_DSCP_EF":   46,
+               "IP_API_DSCP_CS6":  48,
+               "IP_API_DSCP_CS7":  50,
+       }
+)
 
 func (x IPDscp) String() string {
        s, ok := IPDscp_name[uint8(x)]
        if ok {
                return s
        }
-       return strconv.Itoa(int(x))
+       return "IPDscp(" + strconv.Itoa(int(x)) + ")"
 }
 
 // IPEcn represents VPP binary API enum 'ip_ecn'.
@@ -150,30 +167,31 @@ const (
        IP_API_ECN_CE   IPEcn = 3
 )
 
-var IPEcn_name = map[uint8]string{
-       0: "IP_API_ECN_NONE",
-       1: "IP_API_ECN_ECT0",
-       2: "IP_API_ECN_ECT1",
-       3: "IP_API_ECN_CE",
-}
-
-var IPEcn_value = map[string]uint8{
-       "IP_API_ECN_NONE": 0,
-       "IP_API_ECN_ECT0": 1,
-       "IP_API_ECN_ECT1": 2,
-       "IP_API_ECN_CE":   3,
-}
+var (
+       IPEcn_name = map[uint8]string{
+               0: "IP_API_ECN_NONE",
+               1: "IP_API_ECN_ECT0",
+               2: "IP_API_ECN_ECT1",
+               3: "IP_API_ECN_CE",
+       }
+       IPEcn_value = map[string]uint8{
+               "IP_API_ECN_NONE": 0,
+               "IP_API_ECN_ECT0": 1,
+               "IP_API_ECN_ECT1": 2,
+               "IP_API_ECN_CE":   3,
+       }
+)
 
 func (x IPEcn) String() string {
        s, ok := IPEcn_name[uint8(x)]
        if ok {
                return s
        }
-       return strconv.Itoa(int(x))
+       return "IPEcn(" + strconv.Itoa(int(x)) + ")"
 }
 
 // IPProto represents VPP binary API enum 'ip_proto'.
-type IPProto uint32
+type IPProto uint8
 
 const (
        IP_API_PROTO_HOPOPT   IPProto = 0
@@ -182,50 +200,54 @@ const (
        IP_API_PROTO_TCP      IPProto = 6
        IP_API_PROTO_UDP      IPProto = 17
        IP_API_PROTO_GRE      IPProto = 47
-       IP_API_PROTO_AH       IPProto = 50
-       IP_API_PROTO_ESP      IPProto = 51
+       IP_API_PROTO_ESP      IPProto = 50
+       IP_API_PROTO_AH       IPProto = 51
+       IP_API_PROTO_ICMP6    IPProto = 58
        IP_API_PROTO_EIGRP    IPProto = 88
        IP_API_PROTO_OSPF     IPProto = 89
        IP_API_PROTO_SCTP     IPProto = 132
        IP_API_PROTO_RESERVED IPProto = 255
 )
 
-var IPProto_name = map[uint32]string{
-       0:   "IP_API_PROTO_HOPOPT",
-       1:   "IP_API_PROTO_ICMP",
-       2:   "IP_API_PROTO_IGMP",
-       6:   "IP_API_PROTO_TCP",
-       17:  "IP_API_PROTO_UDP",
-       47:  "IP_API_PROTO_GRE",
-       50:  "IP_API_PROTO_AH",
-       51:  "IP_API_PROTO_ESP",
-       88:  "IP_API_PROTO_EIGRP",
-       89:  "IP_API_PROTO_OSPF",
-       132: "IP_API_PROTO_SCTP",
-       255: "IP_API_PROTO_RESERVED",
-}
-
-var IPProto_value = map[string]uint32{
-       "IP_API_PROTO_HOPOPT":   0,
-       "IP_API_PROTO_ICMP":     1,
-       "IP_API_PROTO_IGMP":     2,
-       "IP_API_PROTO_TCP":      6,
-       "IP_API_PROTO_UDP":      17,
-       "IP_API_PROTO_GRE":      47,
-       "IP_API_PROTO_AH":       50,
-       "IP_API_PROTO_ESP":      51,
-       "IP_API_PROTO_EIGRP":    88,
-       "IP_API_PROTO_OSPF":     89,
-       "IP_API_PROTO_SCTP":     132,
-       "IP_API_PROTO_RESERVED": 255,
-}
+var (
+       IPProto_name = map[uint8]string{
+               0:   "IP_API_PROTO_HOPOPT",
+               1:   "IP_API_PROTO_ICMP",
+               2:   "IP_API_PROTO_IGMP",
+               6:   "IP_API_PROTO_TCP",
+               17:  "IP_API_PROTO_UDP",
+               47:  "IP_API_PROTO_GRE",
+               50:  "IP_API_PROTO_ESP",
+               51:  "IP_API_PROTO_AH",
+               58:  "IP_API_PROTO_ICMP6",
+               88:  "IP_API_PROTO_EIGRP",
+               89:  "IP_API_PROTO_OSPF",
+               132: "IP_API_PROTO_SCTP",
+               255: "IP_API_PROTO_RESERVED",
+       }
+       IPProto_value = map[string]uint8{
+               "IP_API_PROTO_HOPOPT":   0,
+               "IP_API_PROTO_ICMP":     1,
+               "IP_API_PROTO_IGMP":     2,
+               "IP_API_PROTO_TCP":      6,
+               "IP_API_PROTO_UDP":      17,
+               "IP_API_PROTO_GRE":      47,
+               "IP_API_PROTO_ESP":      50,
+               "IP_API_PROTO_AH":       51,
+               "IP_API_PROTO_ICMP6":    58,
+               "IP_API_PROTO_EIGRP":    88,
+               "IP_API_PROTO_OSPF":     89,
+               "IP_API_PROTO_SCTP":     132,
+               "IP_API_PROTO_RESERVED": 255,
+       }
+)
 
 func (x IPProto) String() string {
-       s, ok := IPProto_name[uint32(x)]
+       s, ok := IPProto_name[uint8(x)]
        if ok {
                return s
        }
-       return strconv.Itoa(int(x))
+       return "IPProto(" + strconv.Itoa(int(x)) + ")"
 }
 
 // AddressWithPrefix represents VPP binary API alias 'address_with_prefix'.
@@ -245,50 +267,115 @@ type IP6AddressWithPrefix IP6Prefix
 
 // Address represents VPP binary API type 'address'.
 type Address struct {
-       Af AddressFamily
-       Un AddressUnion
+       Af AddressFamily `binapi:"address_family,name=af" json:"af,omitempty"`
+       Un AddressUnion  `binapi:"address_union,name=un" json:"un,omitempty"`
 }
 
 func (*Address) GetTypeName() string { return "address" }
 
+func ParseAddress(ip string) (Address, error) {
+       var address Address
+       netIP := net.ParseIP(ip)
+       if netIP == nil {
+               return address, fmt.Errorf("invalid address: %s", ip)
+       }
+       if ip4 := netIP.To4(); ip4 == nil {
+               address.Af = ADDRESS_IP6
+               var ip6addr IP6Address
+               copy(ip6addr[:], netIP.To16())
+               address.Un.SetIP6(ip6addr)
+       } else {
+               address.Af = ADDRESS_IP4
+               var ip4addr IP4Address
+               copy(ip4addr[:], netIP.To4())
+               address.Un.SetIP4(ip4addr)
+       }
+       return address, nil
+}
+
+func (a *Address) ToString() string {
+       var ip string
+       if a.Af == ADDRESS_IP6 {
+               ip6Address := a.Un.GetIP6()
+               ip = net.IP(ip6Address[:]).To16().String()
+       } else {
+               ip4Address := a.Un.GetIP4()
+               ip = net.IP(ip4Address[:]).To4().String()
+       }
+       return ip
+}
+
 // IP4Prefix represents VPP binary API type 'ip4_prefix'.
 type IP4Prefix struct {
-       Address IP4Address
-       Len     uint8
+       Address IP4Address `binapi:"ip4_address,name=address" json:"address,omitempty"`
+       Len     uint8      `binapi:"u8,name=len" json:"len,omitempty"`
 }
 
 func (*IP4Prefix) GetTypeName() string { return "ip4_prefix" }
 
 // IP6Prefix represents VPP binary API type 'ip6_prefix'.
 type IP6Prefix struct {
-       Address IP6Address
-       Len     uint8
+       Address IP6Address `binapi:"ip6_address,name=address" json:"address,omitempty"`
+       Len     uint8      `binapi:"u8,name=len" json:"len,omitempty"`
 }
 
 func (*IP6Prefix) GetTypeName() string { return "ip6_prefix" }
 
 // Mprefix represents VPP binary API type 'mprefix'.
 type Mprefix struct {
-       Af               AddressFamily
-       GrpAddressLength uint16
-       GrpAddress       AddressUnion
-       SrcAddress       AddressUnion
+       Af               AddressFamily `binapi:"address_family,name=af" json:"af,omitempty"`
+       GrpAddressLength uint16        `binapi:"u16,name=grp_address_length" json:"grp_address_length,omitempty"`
+       GrpAddress       AddressUnion  `binapi:"address_union,name=grp_address" json:"grp_address,omitempty"`
+       SrcAddress       AddressUnion  `binapi:"address_union,name=src_address" json:"src_address,omitempty"`
 }
 
 func (*Mprefix) GetTypeName() string { return "mprefix" }
 
 // Prefix represents VPP binary API type 'prefix'.
 type Prefix struct {
-       Address Address
-       Len     uint8
+       Address Address `binapi:"address,name=address" json:"address,omitempty"`
+       Len     uint8   `binapi:"u8,name=len" json:"len,omitempty"`
 }
 
 func (*Prefix) GetTypeName() string { return "prefix" }
 
+func ParsePrefix(ip string) (prefix Prefix, err error) {
+       hasPrefix := strings.Contains(ip, "/")
+       if hasPrefix {
+               netIP, network, err := net.ParseCIDR(ip)
+               if err != nil {
+                       return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err)
+               }
+               maskSize, _ := network.Mask.Size()
+               prefix.Len = byte(maskSize)
+               prefix.Address, err = ParseAddress(netIP.String())
+               if err != nil {
+                       return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err)
+               }
+       } else {
+               netIP := net.ParseIP(ip)
+               defaultMaskSize, _ := net.CIDRMask(32, 32).Size()
+               if netIP.To4() == nil {
+                       defaultMaskSize, _ = net.CIDRMask(128, 128).Size()
+               }
+               prefix.Len = byte(defaultMaskSize)
+               prefix.Address, err = ParseAddress(netIP.String())
+               if err != nil {
+                       return Prefix{}, fmt.Errorf("invalid IP %s: %v", ip, err)
+               }
+       }
+       return prefix, nil
+}
+
+func (p *Prefix) ToString() string {
+       ip := p.Address.ToString()
+       return ip + "/" + strconv.Itoa(int(p.Len))
+}
+
 // PrefixMatcher represents VPP binary API type 'prefix_matcher'.
 type PrefixMatcher struct {
-       Le uint8
-       Ge uint8
+       Le uint8 `binapi:"u8,name=le" json:"le,omitempty"`
+       Ge uint8 `binapi:"u8,name=ge" json:"ge,omitempty"`
 }
 
 func (*PrefixMatcher) GetTypeName() string { return "prefix_matcher" }
@@ -334,16 +421,16 @@ func (u *AddressUnion) GetIP6() (a IP6Address) {
        return
 }
 
-// This is a compile-time assertion to ensure that this generated file
-// is compatible with the GoVPP api package it is being compiled against.
-// A compilation error at this line likely means your copy of the
-// GoVPP api package needs to be updated.
-const _ = api.GoVppAPIPackageIsVersion1 // please upgrade the GoVPP api package
-
 // Reference imports to suppress errors if they are not otherwise used.
 var _ = api.RegisterMessage
+var _ = codec.DecodeString
 var _ = bytes.NewBuffer
 var _ = context.Background
 var _ = io.Copy
 var _ = strconv.Itoa
+var _ = strings.Contains
 var _ = struc.Pack
+var _ = binary.BigEndian
+var _ = math.Float32bits
+var _ = net.ParseIP
+var _ = fmt.Errorf