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