Binary API generator improvements
[govpp.git] / examples / union-example / union_example.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         "git.fd.io/govpp.git/codec"
21         "git.fd.io/govpp.git/examples/binapi/interfaces"
22         "git.fd.io/govpp.git/examples/binapi/ip"
23         "git.fd.io/govpp.git/examples/binapi/ip_types"
24         "log"
25         "reflect"
26 )
27
28 func init() {
29         log.SetFlags(0)
30 }
31
32 func main() {
33         constructExample()
34
35         encodingExampleIP()
36
37         // convert IP from string form into Address type containing union
38         convertIP("10.10.1.1")
39         convertIP("ff80::1")
40
41         // convert IP from string form into Prefix type
42         convertIPPrefix("20.10.1.1/24")
43         convertIPPrefix("21.10.1.1")
44         convertIPPrefix("ff90::1/64")
45         convertIPPrefix("ff91::1")
46
47         // convert MAC address from string into MacAddress
48         convertToMacAddress("00:10:ab:4f:00:01")
49 }
50
51 func constructExample() {
52         var union ip_types.AddressUnion
53
54         // create AddressUnion with AdressUnionXXX constructors
55         union = ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 1, 10})
56         union = ip_types.AddressUnionIP6(ip_types.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02})
57
58         // set AddressUnion with SetXXX methods
59         union.SetIP4(ip_types.IP4Address{192, 168, 1, 10})
60         union.SetIP6(ip_types.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02})
61 }
62
63 func encodingExampleIP() {
64         var c = codec.DefaultCodec
65
66         // encode this message
67         var msg = ip.IPPuntRedirect{
68                 Punt: ip.PuntRedirect{
69                         Nh: ip_types.Address{
70                                 Af: ip_types.ADDRESS_IP4,
71                                 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 1, 10}),
72                         },
73                 },
74                 IsAdd: true,
75         }
76         log.Printf("encoding message: %+v", msg)
77
78         b, err := c.EncodeMsg(&msg, 1)
79         if err != nil {
80                 log.Fatal(err)
81         }
82
83         // decode into this message
84         var msg2 ip.IPPuntRedirect
85         if err := c.DecodeMsg(b, &msg2); err != nil {
86                 log.Fatal(err)
87         }
88         log.Printf("decoded message: %+v", msg2)
89
90         // compare the messages
91         if !reflect.DeepEqual(msg, msg2) {
92                 log.Fatal("messages are not equal")
93         }
94 }
95
96 func convertIP(ip string) {
97         addr, err := ip_types.ParseAddress(ip)
98         if err != nil {
99                 log.Printf("error converting IP to Address: %v", err)
100                 return
101         }
102         fmt.Printf("converted IP %q to: %+v\n", ip, addr)
103
104         ipStr := addr.ToString()
105         fmt.Printf("Address converted back to string IP %+v to: %q\n", addr, ipStr)
106 }
107
108 func convertIPPrefix(ip string) {
109         prefix, err := ip_types.ParsePrefix(ip)
110         if err != nil {
111                 log.Printf("error converting prefix to IP4Prefix: %v", err)
112                 return
113         }
114         fmt.Printf("converted prefix %q to: %+v\n", ip, prefix)
115
116         ipStr := prefix.ToString()
117         fmt.Printf("IP4Prefix converted back to string prefix %+v to: %q\n", prefix, ipStr)
118 }
119
120 func convertToMacAddress(mac string) {
121         parsedMac, err := interfaces.ParseMAC(mac)
122         if err != nil {
123                 log.Printf("error converting MAC to MacAddress: %v", err)
124                 return
125         }
126         fmt.Printf("converted prefix %q to: %+v\n", mac, parsedMac)
127
128         macStr := parsedMac.ToString()
129         fmt.Printf("MacAddress converted back to string %+v to: %q\n", parsedMac, macStr)
130 }