e20e5c074be2e181a831e8bb85b48738494a8cf0
[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         "context"
21         "flag"
22         "fmt"
23         "io"
24         "log"
25         "strings"
26
27         "git.fd.io/govpp.git"
28         "git.fd.io/govpp.git/adapter/socketclient"
29         "git.fd.io/govpp.git/api"
30         interfaces "git.fd.io/govpp.git/binapi/interface"
31         "git.fd.io/govpp.git/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         showVersion(conn)
51         interfaceDump(conn)
52 }
53
54 // showVersion shows an example of simple request with services.
55 func showVersion(conn api.Connection) {
56         c := vpe.NewServiceClient(conn)
57
58         version, err := c.ShowVersion(context.Background(), &vpe.ShowVersion{})
59         if err != nil {
60                 log.Fatalln("ERROR: ShowVersion failed:", err)
61         }
62
63         fmt.Printf("Version: %v\n", version.Version)
64 }
65
66 // interfaceDump shows an example of multi request with services.
67 func interfaceDump(conn api.Connection) {
68         c := interfaces.NewServiceClient(conn)
69
70         stream, err := c.SwInterfaceDump(context.Background(), &interfaces.SwInterfaceDump{})
71         if err != nil {
72                 log.Fatalln("ERROR: DumpSwInterface failed:", err)
73         }
74
75         fmt.Println("Dumping interfaces")
76         for {
77                 iface, err := stream.Recv()
78                 if err == io.EOF {
79                         break
80                 }
81                 if err != nil {
82                         log.Fatalln("ERROR: DumpSwInterface failed:", err)
83                 }
84                 fmt.Printf("- interface: %s\n", strings.Trim(iface.InterfaceName, "\x00"))
85         }
86 }