Newer
Older
"fmt"
"github.com/rivo/tview"
"google.golang.org/grpc"
"log"
"os"
"time"
)
const (
address = "localhost:55055"
defaultName = "gosdn-cli"
)
// Based on the helloworld example of grpc.io -- thx!
const refreshInterval = 500 * time.Millisecond
var (
view *tview.Modal
app *tview.Application
)
type cliClientConfig struct {
goSDNCLIAddr4 *string
goSDNCLIPort4 *int
interactive *bool
goSDNCommand *string
}
type commandOptions struct {
description string
command func(conn *grpc.ClientConn)
}
var commandList = map[string]commandOptions{
"hello": {"hello", "test connection to goSDN controller", goSDNSayHello},
"shutdown": {"shutdown", "request goSDN controller to shutdown", goSDNShutdown},
"testdb" : {"testdb", "test all database connections", goSDNTestDB},
"tapigetedge" : {"tapigetedge", "get list of edges", TAPIGetEdge},
"tapigetedgenode" : {"tapigetedgenode", "get list of edgenodes", TAPIGetEdgeNode},
"tapigetlink" : {"tapigetlink", "get list of links", TAPIGetLink},
}
/*
gosdn-cli allows to mode of operations:
- interactive: text GUI to operate goSDN
- non-interactive: basic CLI without text GUI
// This holds the basic configuration for gosdn-cli
var myConfiguration = new(cliClientConfig)
myConfiguration.goSDNCLIAddr4 = flag.String("cliServerAddr", "127.0.0.1", "The IPv4 Address of the grpcCLI.")
myConfiguration.goSDNCLIPort4 = flag.Int("cliServerPort", 55055, "The port number of the grpcCLI")
myConfiguration.interactive = flag.Bool("interactive", false, "interactive: text gui or just not")
var printCommandList = flag.Bool("commandlist", false, "interactive: print command list")
myConfiguration.goSDNCommand = flag.String("command", "", "-command: <your command> ; show commands with -commandlist")
flag.Parse()
// Print complete command list and exit
if *printCommandList == true {
for _, element := range commandList {
fmt.Println(element.name + "\t" + element.description)
}
os.Exit(0)
}
log.Println("Starting " + defaultName + " to access the goSDN controller")
// Prepare string with socket for connection to the goSDN controller
goSDNSocketAddress := fmt.Sprintf("%s:%d", *myConfiguration.goSDNCLIAddr4, *myConfiguration.goSDNCLIPort4)
log.Println("Connecting to the goSDN server at: " + goSDNSocketAddress)
conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
log.Println(("Connected to " + conn.Target()))
// Check for non-interactive or interactive mode
if *myConfiguration.interactive == false {
log.Println("starting in non-interactive mode")
// Lookup command or die
_, found := commandList[*myConfiguration.goSDNCommand]
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
// Excecute desired command
commandList[*myConfiguration.goSDNCommand].command(conn)
} else {
log.Fatalf("Your desired command %s is not available", commandList[*myConfiguration.goSDNCommand].name)
os.Exit(1)
}
} else {
log.Println("starting in interactive mode -- do not use yet")
os.Exit(1)
app = tview.NewApplication().EnableMouse(true)
flex := tview.NewFlex().
AddItem(tview.NewBox().SetBorder(true).SetTitle("Command List"), 0, 1, false).
AddItem(tview.NewFlex().SetDirection(tview.FlexRow).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Top"), 0, 1, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Middle (3 x height of Top)"), 0, 3, false).
AddItem(tview.NewBox().SetBorder(true).SetTitle("Bottom (5 rows)"), 5, 1, false), 0, 2, false)
if err := app.SetRoot(flex, true).Run(); err != nil {
panic(err)
}
}
}
func goSDNSayHello (conn *grpc.ClientConn) {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[1]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})
if err != nil {
log.Fatalf("could not say hello: %v", err)
log.Printf("Greeting: %s", r.String())
}
func goSDNShutdown(conn *grpc.ClientConn) {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.Shutdown(ctx, &pb.ShutdownRequest{Name: name})
log.Fatalf("could not request shutdown: %v", err)
log.Printf("Greeting: %s", r.GetMessage())
func goSDNTestDB(conn *grpc.ClientConn) {
// TODO: fill with code and also see if grpc interface has this stub implemented.
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
func TAPIGetEdge(conn *grpc.ClientConn) {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[0]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.TAPIGetEdge(ctx, &pb.TAPIRequest{Name: name})
if err != nil {
log.Fatalf("could not request shutdown: %v", err)
}
log.Printf("TAPIGetEdge said: %s", r.GetMessage())
}
func TAPIGetEdgeNode(conn *grpc.ClientConn) {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[0]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.TAPIGetEdgeNode(ctx, &pb.TAPIRequest{Name: name})
if err != nil {
log.Fatalf("could not request shutdown: %v", err)
}
log.Printf("TAPIGetEdgeNode said: %s", r.GetMessage())
}
func TAPIGetLink(conn *grpc.ClientConn) {
c := pb.NewGrpcCliClient(conn)
// Contact the server and print out its response.
name := defaultName
if len(os.Args) > 1 {
name = os.Args[0]
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
r, err := c.TAPIGetLink(ctx, &pb.TAPIRequest{Name: name})
if err != nil {
log.Fatalf("could not request shutdown: %v", err)
}
log.Printf("TAPIGetLink said: %s", r.GetMessage())
}