Change module name to go.fd.io/govpp
[govpp.git] / examples / binapi-types / binapi_types.go
1 // Copyright (c) 2018 Cisco and/or its affiliates.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // binapi-types is an example showing how to use and convert data with
16 // helper methods from *-types packages in VPP binary API.
17 package main
18
19 import (
20         "fmt"
21         "log"
22         "time"
23
24         "go.fd.io/govpp/binapi/ethernet_types"
25         "go.fd.io/govpp/binapi/ip"
26         "go.fd.io/govpp/binapi/ip_types"
27         "go.fd.io/govpp/binapi/vpe_types"
28         "go.fd.io/govpp/codec"
29 )
30
31 func init() {
32 }
33
34 func main() {
35         log.SetFlags(0)
36
37         usageUnion()
38         usageAddress()
39
40         // convert IP address in string form into ip_types.Address
41         convertIPAddress("10.10.1.1")
42         convertIPAddress("ff80::1")
43
44         // convert IP address / CIDR in string form into ip_types.Prefix
45         convertIPPrefix("20.10.1.1/24")
46         convertIPPrefix("21.10.1.1")
47         convertIPPrefix("ff90::1/64")
48         convertIPPrefix("ff91::1")
49
50         // convert MAC address in string form into ethernet_types.MacAddress
51         convertToMacAddress("00:10:ab:4f:00:01")
52
53         // convert time.Time into vpe_types.Timestamp
54         convertToTimestamp(time.Now())
55 }
56
57 func usageUnion() {
58         var union ip_types.AddressUnion
59
60         // initialize union using constructors
61         union = ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 1, 10})
62         union = ip_types.AddressUnionIP6(ip_types.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02})
63
64         // get union value using getters
65         ip4 := union.GetIP4()
66         ip6 := union.GetIP6()
67
68         // set union value using setters
69         union.SetIP4(ip4)
70         union.SetIP6(ip6)
71 }
72
73 func usageAddress() {
74         // parse string into IP address
75         addr, err := ip_types.ParseAddress("192.168.1.10")
76         if err != nil {
77                 panic(err)
78         }
79
80         var msg = ip.IPPuntRedirect{
81                 IsAdd: true,
82                 Punt: ip.PuntRedirect{
83                         Nh: addr,
84                 },
85         }
86
87         log.Printf("encoding message: %#v", msg)
88
89         var c = codec.DefaultCodec
90
91         b, err := c.EncodeMsg(&msg, 1)
92         if err != nil {
93                 log.Fatal(err)
94         }
95
96         // decode into this message
97         var msg2 ip.IPPuntRedirect
98         if err := c.DecodeMsg(b, &msg2); err != nil {
99                 log.Fatal(err)
100         }
101         log.Printf("decoded message: %#v", msg2)
102
103         // compare the messages
104         if !msg.Punt.Nh.ToIP().Equal(msg2.Punt.Nh.ToIP()) {
105                 log.Fatal("messages are not equal")
106         }
107 }
108
109 func convertIPAddress(ip string) {
110         addr, err := ip_types.ParseAddress(ip)
111         if err != nil {
112                 log.Printf("error converting IP to Address: %v", err)
113                 return
114         }
115         fmt.Printf("converted IP %q to: %#v\n", ip, addr)
116
117         fmt.Printf("Address converted back to string IP %#v to: %s\n", addr, addr)
118 }
119
120 func convertIPPrefix(ip string) {
121         prefix, err := ip_types.ParsePrefix(ip)
122         if err != nil {
123                 log.Printf("error converting prefix to IP4Prefix: %v", err)
124                 return
125         }
126         fmt.Printf("converted prefix %q to: %#v\n", ip, prefix)
127
128         fmt.Printf("IP4Prefix converted back to string prefix %#v to: %s\n", prefix, prefix)
129 }
130
131 func convertToMacAddress(mac string) {
132         parsedMac, err := ethernet_types.ParseMacAddress(mac)
133         if err != nil {
134                 log.Printf("error converting MAC to MacAddress: %v", err)
135                 return
136         }
137         fmt.Printf("converted mac %q to: %#v\n", mac, parsedMac)
138
139         fmt.Printf("MacAddress converted back to string %#v to: %s\n", parsedMac, parsedMac)
140 }
141
142 func convertToTimestamp(t time.Time) {
143         timestamp := vpe_types.NewTimestamp(t)
144         fmt.Printf("converted time %v to: %#v\n", t, timestamp)
145
146         fmt.Printf("Timestamp converted back to string %#v to: %s\n", timestamp, timestamp)
147 }