Add various generator improvements
[govpp.git] / examples / service-client / service_client.go
1 // Copyright (c) 2017 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 // service-client is an example VPP management application that exercises the
16 // govpp API using generated service client.
17 package main
18
19 import (
20         "bytes"
21         "context"
22         "fmt"
23         "log"
24
25         "git.fd.io/govpp.git"
26         "git.fd.io/govpp.git/api"
27         "git.fd.io/govpp.git/examples/binapi/interfaces"
28         "git.fd.io/govpp.git/examples/binapi/vpe"
29 )
30
31 func main() {
32         fmt.Println("Starting VPP service client...")
33
34         // connect to VPP
35         conn, err := govpp.Connect("")
36         if err != nil {
37                 log.Fatalln("failed to connect:", err)
38         }
39         defer conn.Disconnect()
40
41         // create an API channel
42         ch, err := conn.NewAPIChannel()
43         if err != nil {
44                 log.Fatalln("failed to create channel:", err)
45         }
46         defer ch.Close()
47
48         showVersion(ch)
49         interfaceDump(ch)
50 }
51
52 // showVersion shows an example of simple request with services.
53 func showVersion(ch api.Channel) {
54         c := vpe.NewService(ch)
55
56         version, err := c.ShowVersion(context.Background(), &vpe.ShowVersion{})
57         if err != nil {
58                 log.Fatalln("ShowVersion failed:", err)
59         }
60
61         fmt.Printf("Version: %v\n", version.Version)
62 }
63
64 // interfaceDump shows an example of multi request with services.
65 func interfaceDump(ch api.Channel) {
66         c := interfaces.NewService(ch)
67
68         ifaces, err := c.DumpSwInterface(context.Background(), &interfaces.SwInterfaceDump{})
69         if err != nil {
70                 log.Fatalln("DumpSwInterface failed:", err)
71         }
72
73         fmt.Printf("Listing %d interfaces:\n", len(ifaces))
74         for _, d := range ifaces {
75                 fmt.Printf("- interface: %s\n", bytes.Trim(d.InterfaceName, "\x00"))
76         }
77 }