ca0c295e99af350db8d74aeee056a34beeca5b57
[govpp.git] / examples / rpc-service / rpc_service.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         "flag"
23         "fmt"
24         "io"
25         "log"
26
27         "git.fd.io/govpp.git"
28         "git.fd.io/govpp.git/adapter/socketclient"
29         "git.fd.io/govpp.git/api"
30         "git.fd.io/govpp.git/examples/binapi/interfaces"
31         "git.fd.io/govpp.git/examples/binapi/vpe"
32 )
33
34 var (
35         sockAddr = flag.String("sock", socketclient.DefaultSocketName, "Path to VPP binary API socket file")
36 )
37
38 func main() {
39         flag.Parse()
40
41         fmt.Println("Starting RPC service example")
42
43         // connect to VPP
44         conn, err := govpp.Connect(*sockAddr)
45         if err != nil {
46                 log.Fatalln("ERROR: connecting to VPP failed:", err)
47         }
48         defer conn.Disconnect()
49
50         // create a channel
51         ch, err := conn.NewAPIChannel()
52         if err != nil {
53                 log.Fatalln("ERROR: creating channel failed:", err)
54         }
55         defer ch.Close()
56
57         showVersion(ch)
58         interfaceDump(ch)
59 }
60
61 // showVersion shows an example of simple request with services.
62 func showVersion(ch api.Channel) {
63         c := vpe.NewServiceClient(ch)
64
65         version, err := c.ShowVersion(context.Background(), &vpe.ShowVersion{})
66         if err != nil {
67                 log.Fatalln("ERROR: ShowVersion failed:", err)
68         }
69
70         fmt.Printf("Version: %v\n", version.Version)
71 }
72
73 // interfaceDump shows an example of multi request with services.
74 func interfaceDump(ch api.Channel) {
75         c := interfaces.NewServiceClient(ch)
76
77         stream, err := c.DumpSwInterface(context.Background(), &interfaces.SwInterfaceDump{})
78         if err != nil {
79                 log.Fatalln("ERROR: DumpSwInterface failed:", err)
80         }
81
82         fmt.Println("Dumping interfaces")
83         for {
84                 iface, err := stream.Recv()
85                 if err == io.EOF {
86                         break
87                 }
88                 if err != nil {
89                         log.Fatalln("ERROR: DumpSwInterface failed:", err)
90                 }
91                 fmt.Printf("- interface: %s\n", bytes.Trim(iface.InterfaceName, "\x00"))
92         }
93 }