aboutsummaryrefslogtreecommitdiffstats
path: root/examples/binapi-types/binapi_types.go
blob: 75bf9b411c406fccef61ea2cd063332a56100c95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright (c) 2018 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

// binapi-types is an example showing how to use and convert data with
// helper methods from *-types packages in VPP binary API.
package main

import (
	"fmt"
	"log"
	"time"

	"go.fd.io/govpp/binapi/ethernet_types"
	"go.fd.io/govpp/binapi/ip"
	"go.fd.io/govpp/binapi/ip_types"
	"go.fd.io/govpp/binapi/vpe_types"
	"go.fd.io/govpp/codec"
)

func init() {
}

func main() {
	log.SetFlags(0)

	usageUnion()
	usageAddress()

	// convert IP address in string form into ip_types.Address
	convertIPAddress("10.10.1.1")
	convertIPAddress("ff80::1")

	// convert IP address / CIDR in string form into ip_types.Prefix
	convertIPPrefix("20.10.1.1/24")
	convertIPPrefix("21.10.1.1")
	convertIPPrefix("ff90::1/64")
	convertIPPrefix("ff91::1")

	// convert MAC address in string form into ethernet_types.MacAddress
	convertToMacAddress("00:10:ab:4f:00:01")

	// convert time.Time into vpe_types.Timestamp
	convertToTimestamp(time.Now())
}

func usageUnion() {
	var union ip_types.AddressUnion

	// initialize union using constructors
	union = ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 1, 10})
	union = ip_types.AddressUnionIP6(ip_types.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02})

	// get union value using getters
	ip4 := union.GetIP4()
	ip6 := union.GetIP6()

	// set union value using setters
	union.SetIP4(ip4)
	union.SetIP6(ip6)
}

func usageAddress() {
	// parse string into IP address
	addr, err := ip_types.ParseAddress("192.168.1.10")
	if err != nil {
		panic(err)
	}

	var msg = ip.IPPuntRedirect{
		IsAdd: true,
		Punt: ip.PuntRedirect{
			Nh: addr,
		},
	}

	log.Printf("encoding message: %#v", msg)

	var c = codec.DefaultCodec

	b, err := c.EncodeMsg(&msg, 1)
	if err != nil {
		log.Fatal(err)
	}

	// decode into this message
	var msg2 ip.IPPuntRedirect
	if err := c.DecodeMsg(b, &msg2); err != nil {
		log.Fatal(err)
	}
	log.Printf("decoded message: %#v", msg2)

	// compare the messages
	if !msg.Punt.Nh.ToIP().Equal(msg2.Punt.Nh.ToIP()) {
		log.Fatal("messages are not equal")
	}
}

func convertIPAddress(ip string) {
	addr, err := ip_types.ParseAddress(ip)
	if err != nil {
		log.Printf("error converting IP to Address: %v", err)
		return
	}
	fmt.Printf("converted IP %q to: %#v\n", ip, addr)

	fmt.Printf("Address converted back to string IP %#v to: %s\n", addr, addr)
}

func convertIPPrefix(ip string) {
	prefix, err := ip_types.ParsePrefix(ip)
	if err != nil {
		log.Printf("error converting prefix to IP4Prefix: %v", err)
		return
	}
	fmt.Printf("converted prefix %q to: %#v\n", ip, prefix)

	fmt.Printf("IP4Prefix converted back to string prefix %#v to: %s\n", prefix, prefix)
}

func convertToMacAddress(mac string) {
	parsedMac, err := ethernet_types.ParseMacAddress(mac)
	if err != nil {
		log.Printf("error converting MAC to MacAddress: %v", err)
		return
	}
	fmt.Printf("converted mac %q to: %#v\n", mac, parsedMac)

	fmt.Printf("MacAddress converted back to string %#v to: %s\n", parsedMac, parsedMac)
}

func convertToTimestamp(t time.Time) {
	timestamp := vpe_types.NewTimestamp(t)
	fmt.Printf("converted time %v to: %#v\n", t, timestamp)

	fmt.Printf("Timestamp converted back to string %#v to: %s\n", timestamp, timestamp)
}