diff options
author | Ondrej Fabry <ofabry@cisco.com> | 2020-07-17 10:36:28 +0200 |
---|---|---|
committer | Ondrej Fabry <ofabry@cisco.com> | 2020-07-17 11:43:41 +0200 |
commit | d1f24d37bd447b64e402298bb8eb2479681facf9 (patch) | |
tree | a3fc21ba730a91d8a402c7a5bf9c614e3677c4fc /examples/union-example/union_example.go | |
parent | 1548c7e12531e3d055567d761c580a1c7ff0ac40 (diff) |
Improve binapi generator
- simplified Size/Marshal/Unmarshal methods
- replace struc in unions with custom marshal/unmarshal
- fix imports in generated files
- fix mock adapter
- generate rpc service using low-level stream API (dumps generate control ping or stream msg..)
- move examples/binapi to binapi and generate all API for latest release
- add binapigen.Plugin for developing custom generator plugins
- optionally generate HTTP handlers (REST API) for RPC services
- add govpp program for browsing VPP API
Change-Id: I092e9ed2b0c17972b3476463c3d4b14dd76ed42b
Signed-off-by: Ondrej Fabry <ofabry@cisco.com>
Diffstat (limited to 'examples/union-example/union_example.go')
-rw-r--r-- | examples/union-example/union_example.go | 130 |
1 files changed, 0 insertions, 130 deletions
diff --git a/examples/union-example/union_example.go b/examples/union-example/union_example.go deleted file mode 100644 index cdba2fa..0000000 --- a/examples/union-example/union_example.go +++ /dev/null @@ -1,130 +0,0 @@ -// 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. - -// union-example is an example to show how to use unions in VPP binary API. -package main - -import ( - "fmt" - "git.fd.io/govpp.git/codec" - "git.fd.io/govpp.git/examples/binapi/interfaces" - "git.fd.io/govpp.git/examples/binapi/ip" - "git.fd.io/govpp.git/examples/binapi/ip_types" - "log" - "reflect" -) - -func init() { - log.SetFlags(0) -} - -func main() { - constructExample() - - encodingExampleIP() - - // convert IP from string form into Address type containing union - convertIP("10.10.1.1") - convertIP("ff80::1") - - // convert IP from string form into Prefix type - convertIPPrefix("20.10.1.1/24") - convertIPPrefix("21.10.1.1") - convertIPPrefix("ff90::1/64") - convertIPPrefix("ff91::1") - - // convert MAC address from string into MacAddress - convertToMacAddress("00:10:ab:4f:00:01") -} - -func constructExample() { - var union ip_types.AddressUnion - - // create AddressUnion with AdressUnionXXX 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}) - - // set AddressUnion with SetXXX methods - union.SetIP4(ip_types.IP4Address{192, 168, 1, 10}) - union.SetIP6(ip_types.IP6Address{0xff, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02}) -} - -func encodingExampleIP() { - var c = codec.DefaultCodec - - // encode this message - var msg = ip.IPPuntRedirect{ - Punt: ip.PuntRedirect{ - Nh: ip_types.Address{ - Af: ip_types.ADDRESS_IP4, - Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 1, 10}), - }, - }, - IsAdd: true, - } - log.Printf("encoding message: %+v", msg) - - 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 !reflect.DeepEqual(msg, msg2) { - log.Fatal("messages are not equal") - } -} - -func convertIP(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) - - ipStr := addr.ToString() - fmt.Printf("Address converted back to string IP %+v to: %q\n", addr, ipStr) -} - -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) - - ipStr := prefix.ToString() - fmt.Printf("IP4Prefix converted back to string prefix %+v to: %q\n", prefix, ipStr) -} - -func convertToMacAddress(mac string) { - parsedMac, err := interfaces.ParseMAC(mac) - if err != nil { - log.Printf("error converting MAC to MacAddress: %v", err) - return - } - fmt.Printf("converted prefix %q to: %+v\n", mac, parsedMac) - - macStr := parsedMac.ToString() - fmt.Printf("MacAddress converted back to string %+v to: %q\n", parsedMac, macStr) -}
\ No newline at end of file |