blob: fa847ff4117417dd614ee4425d943987e764d4c1 [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
Matteo Scandolo8df63df2019-09-12 10:34:32 -070043type oltOptions struct {
44 Get OltGet `command:"get"`
45 NNI OltNNIs `command:"nnis"`
46 PON OltPONs `command:"pons"`
47}
48
49func RegisterOltCommands(parser *flags.Parser) {
50 parser.AddCommand("olt", "OLT Commands", "Commands to query and manipulate the OLT device", &oltOptions{})
51}
52
53func getOLT() *pb.Olt {
54 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
55 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070056 log.Fatalf("did not connect: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070057 return nil
58 }
59 defer conn.Close()
60 c := pb.NewBBSimClient(conn)
61
62 // Contact the server and print out its response.
63
64 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
65 defer cancel()
66 olt, err := c.GetOlt(ctx, &pb.Empty{})
67 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070068 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070069 return nil
70 }
71 return olt
72}
73
74func printOltHeader(prefix string, o *pb.Olt) {
75 fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber))
76 fmt.Println()
77}
78
Matteo Scandolo8df63df2019-09-12 10:34:32 -070079func (o *OltGet) Execute(args []string) error {
80 olt := getOLT()
81
82 // print out
83 tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT)
84 tableFormat.Execute(os.Stdout, true, olt)
85
86 return nil
87}
88
89func (o *OltNNIs) Execute(args []string) error {
90 olt := getOLT()
91
92 printOltHeader("NNI Ports for", olt)
93
94 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
95 tableFormat.Execute(os.Stdout, true, olt.NNIPorts)
96
97 return nil
98}
99
100func (o *OltPONs) Execute(args []string) error {
101 olt := getOLT()
102
103 printOltHeader("PON Ports for", olt)
104
105 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
106 tableFormat.Execute(os.Stdout, true, olt.PONPorts)
107
108 return nil
109}