blob: d77cf779b9174e14aad0963994b01c68b50ae08f [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
Zdravko Bozakov681364d2019-11-10 14:28:46 +010043type OltShutdown struct{}
44
45type OltPoweron struct{}
46
47type OltReboot struct{}
48
Matteo Scandolo8df63df2019-09-12 10:34:32 -070049type oltOptions struct {
Anand S Katti86552f92020-03-03 21:56:32 +053050 Get OltGet `command:"get"`
51 NNI OltNNIs `command:"nnis"`
52 PON OltPONs `command:"pons"`
53 Shutdown OltShutdown `command:"shutdown"`
54 Poweron OltPoweron `command:"poweron"`
55 Reboot OltReboot `command:"reboot"`
56 Alarms OltAlarmOptions `command:"alarms"`
Matteo Scandolo8df63df2019-09-12 10:34:32 -070057}
58
59func RegisterOltCommands(parser *flags.Parser) {
60 parser.AddCommand("olt", "OLT Commands", "Commands to query and manipulate the OLT device", &oltOptions{})
61}
62
63func getOLT() *pb.Olt {
64 conn, err := grpc.Dial(config.GlobalConfig.Server, grpc.WithInsecure())
65 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070066 log.Fatalf("did not connect: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070067 return nil
68 }
69 defer conn.Close()
70 c := pb.NewBBSimClient(conn)
71
72 // Contact the server and print out its response.
73
74 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
75 defer cancel()
76 olt, err := c.GetOlt(ctx, &pb.Empty{})
77 if err != nil {
Matteo Scandolo2bf742a2019-10-01 11:33:34 -070078 log.Fatalf("could not get OLT: %v", err)
Matteo Scandolo8df63df2019-09-12 10:34:32 -070079 return nil
80 }
81 return olt
82}
83
84func printOltHeader(prefix string, o *pb.Olt) {
85 fmt.Println(fmt.Sprintf("%s : %s", prefix, o.SerialNumber))
86 fmt.Println()
87}
88
Matteo Scandolo8df63df2019-09-12 10:34:32 -070089func (o *OltGet) Execute(args []string) error {
90 olt := getOLT()
91
92 // print out
93 tableFormat := format.Format(DEFAULT_OLT_DEVICE_HEADER_FORMAT)
94 tableFormat.Execute(os.Stdout, true, olt)
95
96 return nil
97}
98
99func (o *OltNNIs) Execute(args []string) error {
100 olt := getOLT()
101
102 printOltHeader("NNI Ports for", olt)
103
104 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
105 tableFormat.Execute(os.Stdout, true, olt.NNIPorts)
106
107 return nil
108}
109
110func (o *OltPONs) Execute(args []string) error {
111 olt := getOLT()
112
113 printOltHeader("PON Ports for", olt)
114
115 tableFormat := format.Format(DEFAULT_PORT_HEADER_FORMAT)
116 tableFormat.Execute(os.Stdout, true, olt.PONPorts)
117
118 return nil
119}
Zdravko Bozakov681364d2019-11-10 14:28:46 +0100120
121func (o *OltShutdown) Execute(args []string) error {
122 client, conn := connect()
123 defer conn.Close()
124
125 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
126 defer cancel()
127
128 res, err := client.ShutdownOlt(ctx, &pb.Empty{})
129
130 if err != nil {
131 log.Fatalf("Cannot shut down OLT: %v", err)
132 return err
133 }
134
135 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
136 return nil
137}
138
139func (o *OltPoweron) Execute(args []string) error {
140 client, conn := connect()
141 defer conn.Close()
142
143 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
144 defer cancel()
145
146 res, err := client.PoweronOlt(ctx, &pb.Empty{})
147
148 if err != nil {
149 log.Fatalf("Cannot power on OLT: %v", err)
150 return err
151 }
152
153 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
154 return nil
155}
156
157func (o *OltReboot) Execute(args []string) error {
158 client, conn := connect()
159 defer conn.Close()
160
161 ctx, cancel := context.WithTimeout(context.Background(), config.GlobalConfig.Grpc.Timeout)
162 defer cancel()
163
164 res, err := client.RebootOlt(ctx, &pb.Empty{})
165
166 if err != nil {
167 log.Fatalf("Cannot reboot OLT: %v", err)
168 return err
169 }
170
171 fmt.Println(fmt.Sprintf("[Status: %d] %s", res.StatusCode, res.Message))
172 return nil
173}