2dbaa3ec3c2ba186787f707503ac1548973df66e
[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 // union-example is an example to show how to use unions in VPP binary API.
16 package main
17
18 import (
19         "fmt"
20         "log"
21         "time"
22
23         "git.fd.io/govpp.git/binapi/ethernet_types"
24         "git.fd.io/govpp.git/binapi/ip"
25         "git.fd.io/govpp.git/binapi/ip_types"
26         "git.fd.io/govpp.git/binapi/vpe_types"
27         "git.fd.io/govpp.git/codec"
28 )
29
30 func init() {
31 }
32
33 func main() {
34         log.SetFlags(0)
35
36         usageUnion()
37         usageAddress()
38
39         // convert IP address in string form into ip_types.Address
40         convertIPAddress("10.10.1.1")
41         convertIPAddress("ff80::1")
42
43         // convert IP address / CIDR in string form into ip_types.Prefix
44         convertIPPrefix("20.10.1.1/24")
45         convertIPPrefix("21.10.1.1")
46         convertIPPrefix("ff90::1/64")
47         convertIPPrefix("ff91::1")
48
49         // convert MAC address in string form into ethernet_types.MacAddress
50         convertToMacAddress("00:10:ab:4f:00:01")
51
52         // convert time.Time into vpe_types.Timestamp
53         convertToTimestamp(time.Now())
54 }
55
56 func usageUnion() {
57         var union ip_types.AddressUnion
58
59         // initialize union using constructors
60         union = ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 1, 10})
61         union = ip_types.AddressUnionIP6(ip_types.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02})
62
63         // get union value using getters
64         ip4 := union.GetIP4()
65         ip6 := union.GetIP6()
66
67         // set union value using setters
68         union.SetIP4(ip4)
69         union.SetIP6(ip6)
70 }
71
72 func usageAddress() {
73         // parse string into IP address
74         addr, err := ip_types.ParseAddress("192.168.1.10")
75         if err != nil {
76                 panic(err)
77         }
78
79         var msg = ip.IPPuntRedirect{
80                 IsAdd: true,
81                 Punt: ip.PuntRedirect{
82                         Nh: addr,
83                 },
84         }
85
86         log.Printf("encoding message: %#v", msg)
87
88         var c = codec.DefaultCodec
89
90         b, err := c.EncodeMsg(&msg, 1)
91         if err != nil {
92                 log.Fatal(err)
93         }
94
95         // decode into this message
96         var msg2 ip.IPPuntRedirect
97         if err := c.DecodeMsg(b, &msg2); err != nil {
98                 log.Fatal(err)
99         }
100         log.Printf("decoded message: %#v", msg2)
101
102         // compare the messages
103         if !msg.Punt.Nh.ToIP().Equal(msg2.Punt.Nh.ToIP()) {
104                 log.Fatal("messages are not equal")
105         }
106 }
107
108 func convertIPAddress(ip string) {
109         addr, err := ip_types.ParseAddress(ip)
110         if err != nil {
111                 log.Printf("error converting IP to Address: %v", err)
112                 return
113         }
114         fmt.Printf("converted IP %q to: %#v\n", ip, addr)
115
116         fmt.Printf("Address converted back to string IP %#v to: %s\n", addr, addr)
117 }
118
119 func convertIPPrefix(ip string) {
120         prefix, err := ip_types.ParsePrefix(ip)
121         if err != nil {
122                 log.Printf("error converting prefix to IP4Prefix: %v", err)
123                 return
124         }
125         fmt.Printf("converted prefix %q to: %#v\n", ip, prefix)
126
127         fmt.Printf("IP4Prefix converted back to string prefix %#v to: %s\n", prefix, prefix)
128 }
129
130 func convertToMacAddress(mac string) {
131         parsedMac, err := ethernet_types.ParseMacAddress(mac)
132         if err != nil {
133                 log.Printf("error converting MAC to MacAddress: %v", err)
134                 return
135         }
136         fmt.Printf("converted mac %q to: %#v\n", mac, parsedMac)
137
138         fmt.Printf("MacAddress converted back to string %#v to: %s\n", parsedMac, parsedMac)
139 }
140
141 func convertToTimestamp(t time.Time) {
142         timestamp := vpe_types.NewTimestamp(t)
143         fmt.Printf("converted time %v to: %#v\n", t, timestamp)
144
145         fmt.Printf("Timestamp converted back to string %#v to: %s\n", timestamp, timestamp)
146 }