blob: 3a434b2ea7e5dec959cb0454e4671ab5cf619462 [file] [log] [blame]
Matteo Scandolo8df63df2019-09-12 10:34:32 -07001/*
2 * Portions copyright 2019-present Open Networking Foundation
3 * Original copyright 2019-present Ciena Corporation
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package commands
19
20import (
21 "context"
22 "fmt"
23 "github.com/jessevdk/go-flags"
24 pb "github.com/opencord/bbsim/api/bbsim"
25 "github.com/opencord/bbsim/internal/bbsimctl/config"
26 "github.com/opencord/cordctl/pkg/format"
27 log "github.com/sirupsen/logrus"
28 "google.golang.org/grpc"
29 "os"
30)
31
32const (
33 DEFAULT_OLT_DEVICE_HEADER_FORMAT = "table{{ .ID }}\t{{ .SerialNumber }}\t{{ .OperState }}\t{{ .InternalState }}"
34 DEFAULT_PORT_HEADER_FORMAT = "table{{ .ID }}\t{{ .OperState }}"
35)
36
37type OltGet struct{}
38
39type OltNNIs struct{}
40
41type OltPONs struct{}
42
43// TODO add autocomplete
44type oltOptions struct {
45 Get OltGet `command:"get"`
46 NNI OltNNIs `command:"nnis"`
47 PON OltPONs `command:"pons"`
48}
49
50func RegisterOltCommands(parser *flags.Parser) {
51 parser.AddCommand("olt", "OLT Commands", "Commands to query and manipulate the OLT device", &oltOptions{})
52}
53
54func getOLT() *pb.Olt {
55 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
56 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070057 log.Fatalf("did not connect: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070058 return nil
59 }
60 defer conn.Close()
61 c := pb.NewBBSimClient(conn)
62
63 // Contact the server and print out its response.
64
65 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
66 defer cancel()
67 olt, err := c.GetOlt(ctx, &pb.Empty{})
68 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070069 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070070 return nil
71 }
72 return olt
73}
74
75func printOltHeader(prefix string, o *pb.Olt) {
76 fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber))
77 fmt.Println()
78}
79
80// TODO use voltctl or cordctl parser to print tables (this needs to be moved out of the internals package)
81func (o *OltGet) Execute(args []string) error {
82 olt := getOLT()
83
84 // print out
85 tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT)
86 tableFormat.Execute(os.Stdout, true, olt)
87
88 return nil
89}
90
91func (o *OltNNIs) Execute(args []string) error {
92 olt := getOLT()
93
94 printOltHeader("NNI Ports for", olt)
95
96 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
97 tableFormat.Execute(os.Stdout, true, olt.NNIPorts)
98
99 return nil
100}
101
102func (o *OltPONs) Execute(args []string) error {
103 olt := getOLT()
104
105 printOltHeader("PON Ports for", olt)
106
107 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
108 tableFormat.Execute(os.Stdout, true, olt.PONPorts)
109
110 return nil
111}