blob: ed2da71e11404d88edb08f0f53d226d7699a0ab0 [file] [log] [blame]
Author Namea594e632018-08-10 11:33:58 -04001/*
2 Copyright 2017 the original author or authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17package main
18
19import (
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040020 "context"
21 "flag"
22 "fmt"
Author Namea594e632018-08-10 11:33:58 -040023 "log"
Author Namea594e632018-08-10 11:33:58 -040024 "runtime/debug"
donNewtonAlphab3279ea2018-09-18 15:55:32 -040025 "strings"
Author Namea594e632018-08-10 11:33:58 -040026
27 "gerrit.opencord.org/abstract-olt/api"
Author Namea594e632018-08-10 11:33:58 -040028 "google.golang.org/grpc"
29 "google.golang.org/grpc/credentials"
30)
31
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040032func main() {
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040033 /* COMMAND FLAGS */
34 output := flag.Bool("output", false, "dump output")
donNewtonAlphab3279ea2018-09-18 15:55:32 -040035 echo := flag.Bool("e", false, "echo")
36 message := flag.String("message", "ping", "message to be echoed back")
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040037 create := flag.Bool("c", false, "create?")
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040038 update := flag.Bool("u", false, "update?")
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040039 addOlt := flag.Bool("s", false, "addOlt?")
40 provOnt := flag.Bool("o", false, "provisionOnt?")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040041 deleteOnt := flag.Bool("d", false, "deleteOnt")
42 /* END COMMAND FLAGS */
43
44 /* CREATE CHASSIS FLAGS */
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040045 xosUser := flag.String("xos_user", "", "xos_user")
46 xosPassword := flag.String("xos_password", "", "xos_password")
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040047 xosAddress := flag.String("xos_address", "", "xos address")
48 xosPort := flag.Uint("xos_port", 0, "xos port")
49 rack := flag.Uint("rack", 1, "rack number for chassis")
50 shelf := flag.Uint("shelf", 1, "shelf number for chassis")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040051 /* END CREATE CHASSIS FLAGS */
52
53 /* ADD OLT FLAGS */
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040054 oltAddress := flag.String("olt_address", "", "ip address for olt chassis")
55 oltPort := flag.Uint("olt_port", 0, "listen port for olt chassis")
56 name := flag.String("name", "", "friendly name for olt chassis")
57 driver := flag.String("driver", "", "driver to use with olt chassis")
58 oltType := flag.String("type", "", "olt chassis type")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040059 /* END ADD OLT FLAGS */
60
61 /* PROVISION / DELETE ONT FLAGS */
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040062 slot := flag.Uint("slot", 1, "slot number 1-16 to provision ont to")
63 port := flag.Uint("port", 1, "port number 1-16 to provision ont to")
64 ont := flag.Uint("ont", 1, "ont number 1-64")
65 serial := flag.String("serial", "", "serial number of ont")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040066 /* END PROVISION / DELETE ONT FLAGS */
67
68 /*GENERIC FLAGS */
69 clli := flag.String("clli", "", "clli of abstract chassis")
donNewtonAlphab3279ea2018-09-18 15:55:32 -040070 useSsl := flag.Bool("ssl", false, "use ssl")
71 useAuth := flag.Bool("auth", false, "use auth")
72 crtFile := flag.String("cert", "cert/server.crt", "Public cert for server to establish tls session")
73 serverAddressPort := flag.String("server", "localhost:7777", "address and port of AbstractOLT server")
74 fqdn := flag.String("fqdn", "", "FQDN of the service to match what is in server.crt")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040075 /*GENERIC FLAGS */
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040076
77 flag.Parse()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040078
donNewtonAlphab3279ea2018-09-18 15:55:32 -040079 if *useSsl {
80 if *fqdn == "" {
81 fqdn = &(strings.Split(*serverAddressPort, ":")[0])
82 fmt.Printf("using %s as the FQDN for the AbstractOLT server", *fqdn)
83 }
84 }
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040085
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -040086 cmdFlags := []*bool{echo, addOlt, update, create, provOnt, deleteOnt, output}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040087 cmdCount := 0
88 for _, flag := range cmdFlags {
89 if *flag {
90 cmdCount++
91 }
92 }
93 if cmdCount > 1 {
94 fmt.Println("CMD You can only call one method at a time")
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040095 usage()
96 return
97 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040098 if cmdCount < 1 {
99 fmt.Println("CMD You didn't specify an operation to perform")
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400100 usage()
101 return
102 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400103
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400104 var conn *grpc.ClientConn
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400105 var err error
106
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400107 // Setup the login/pass
108 auth := Authentication{
109 Login: "john",
110 Password: "doe",
111 }
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400112 if *useSsl && *useAuth {
113
114 creds, err := credentials.NewClientTLSFromFile(*crtFile, *fqdn)
115 conn, err = grpc.Dial(*serverAddressPort, grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
116 if err != nil {
117 log.Fatalf("could not load tls cert: %s", err)
118 }
119 } else if *useSsl {
120 creds, err := credentials.NewClientTLSFromFile("cert/server.crt", *fqdn)
121 conn, err = grpc.Dial(*serverAddressPort, grpc.WithTransportCredentials(creds))
122 if err != nil {
123 log.Fatalf("could not load tls cert: %s", err)
124 }
125 } else if *useAuth {
126 conn, err = grpc.Dial(*serverAddressPort, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
127 } else {
128 conn, err = grpc.Dial(*serverAddressPort, grpc.WithInsecure())
129 }
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400130 if err != nil {
131 log.Fatalf("did not connect: %s", err)
132 }
133 defer conn.Close()
134
135 c := api.NewAbstractOLTClient(conn)
136 if *create {
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400137 createChassis(c, clli, xosUser, xosPassword, xosAddress, xosPort, rack, shelf)
138 } else if *update {
139 updateXOSUserPassword(c, clli, xosUser, xosPassword)
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400140 } else if *addOlt {
141 addOltChassis(c, clli, oltAddress, oltPort, name, driver, oltType)
142 } else if *provOnt {
143 provisionONT(c, clli, slot, port, ont, serial)
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400144 } else if *echo {
145 ping(c, *message)
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400146 } else if *output {
147 Output(c)
148 } else if *deleteOnt {
149 deleteONT(c, clli, slot, port, ont, serial)
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400150 }
151
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400152}
153
Author Namea594e632018-08-10 11:33:58 -0400154// Authentication holds the login/password
155type Authentication struct {
156 Login string
157 Password string
158}
159
160// GetRequestMetadata gets the current request metadata
161func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
162 return map[string]string{
163 "login": a.Login,
164 "password": a.Password,
165 }, nil
166}
167
168// RequireTransportSecurity indicates whether the credentials requires transport security
169func (a *Authentication) RequireTransportSecurity() bool {
170 return true
171}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400172func Output(c api.AbstractOLTClient) error {
173 response, err := c.Output(context.Background(), &api.OutputMessage{Something: "wtf"})
174 if err != nil {
175 fmt.Printf("Error when calling Echo: %s", err)
176 return err
177 }
178 log.Printf("Response from server: %v", response.GetSuccess())
179
180 return nil
181}
182
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400183func ping(c api.AbstractOLTClient, message string) error {
184 response, err := c.Echo(context.Background(), &api.EchoMessage{Ping: message})
185 if err != nil {
186 fmt.Printf("Error when calling Echo: %s", err)
187 return err
188 }
189 log.Printf("Response from server: %s", response.GetPong())
190 return nil
191}
Author Namea594e632018-08-10 11:33:58 -0400192
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400193func createChassis(c api.AbstractOLTClient, clli *string, xosUser *string, xosPassword *string, xosAddress *string, xosPort *uint, rack *uint, shelf *uint) error {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400194 fmt.Println("Calling Create Chassis")
195 fmt.Println("clli", *clli)
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400196 fmt.Println("xos_user", *xosUser)
197 fmt.Println("xos_password", *xosPassword)
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400198 fmt.Println("xos_address", *xosAddress)
199 fmt.Println("xos_port", *xosPort)
200 fmt.Println("rack", *rack)
201 fmt.Println("shelf", *shelf)
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400202 response, err := c.CreateChassis(context.Background(), &api.AddChassisMessage{CLLI: *clli, XOSUser: *xosUser, XOSPassword: *xosPassword,
203 XOSIP: *xosAddress, XOSPort: int32(*xosPort), Rack: int32(*rack), Shelf: int32(*shelf)})
Author Namea594e632018-08-10 11:33:58 -0400204 if err != nil {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400205 fmt.Printf("Error when calling CreateChassis: %s", err)
206 return err
Author Namea594e632018-08-10 11:33:58 -0400207 }
208 log.Printf("Response from server: %s", response.GetDeviceID())
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400209 return nil
210}
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400211func updateXOSUserPassword(c api.AbstractOLTClient, clli *string, xosUser *string, xosPassword *string) error {
212 fmt.Println("Calling Update XOS USER/PASSWORD")
213 fmt.Println("clli", *clli)
214 fmt.Println("xos_user", *xosUser)
215 fmt.Println("xos_password", *xosPassword)
216 response, err := c.ChangeXOSUserPassword(context.Background(), &api.ChangeXOSUserPasswordMessage{CLLI: *clli, XOSUser: *xosUser, XOSPassword: *xosPassword})
217 if err != nil {
218 fmt.Printf("Error when calling UpdateXOSUserPassword: %s", err)
219 return err
220 }
donNewtonAlphab1466392018-10-02 10:42:05 -0400221 log.Printf("Response from server: %t", response.GetSuccess())
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400222 return nil
223}
224
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400225func addOltChassis(c api.AbstractOLTClient, clli *string, oltAddress *string, oltPort *uint, name *string, driver *string, oltType *string) error {
226 fmt.Println("clli", *clli)
227 fmt.Println("olt_address", *oltAddress)
228 fmt.Println("olt_port", *oltPort)
229 fmt.Println("name", *name)
230 fmt.Println("driver", *driver)
231 fmt.Println("type", *oltType)
232 var driverType api.AddOLTChassisMessage_OltDriver
233 var chassisType api.AddOLTChassisMessage_OltType
234 switch *oltType {
235 case "edgecore":
236 chassisType = api.AddOLTChassisMessage_edgecore
237 }
238 switch *driver {
239 case "openolt":
240 driverType = api.AddOLTChassisMessage_openoltDriver
241
242 }
243
244 res, err := c.CreateOLTChassis(context.Background(), &api.AddOLTChassisMessage{CLLI: *clli, SlotIP: *oltAddress, SlotPort: uint32(*oltPort), Hostname: *name, Type: chassisType, Driver: driverType})
Author Namea594e632018-08-10 11:33:58 -0400245 if err != nil {
246 debug.PrintStack()
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400247 fmt.Printf("Error when calling CreateOLTChassis: %s", err)
248 return err
Author Namea594e632018-08-10 11:33:58 -0400249 }
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400250 log.Printf("Response from server: %s", res.GetDeviceID())
251 return nil
252}
253func provisionONT(c api.AbstractOLTClient, clli *string, slot *uint, port *uint, ont *uint, serial *string) error {
254 fmt.Println("clli", *clli)
255 fmt.Println("slot", *slot)
256 fmt.Println("port", *port)
257 fmt.Println("ont", *ont)
258 fmt.Println("serial", *serial)
259 res, err := c.ProvisionOnt(context.Background(), &api.AddOntMessage{CLLI: *clli, SlotNumber: int32(*slot), PortNumber: int32(*port), OntNumber: int32(*ont), SerialNumber: *serial})
260 if err != nil {
261 debug.PrintStack()
262 fmt.Printf("Error when calling ProvsionOnt %s", err)
263 return err
264 }
265 log.Printf("Response from server: %t", res.GetSuccess())
266 return nil
267
268 return nil
269}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400270func deleteONT(c api.AbstractOLTClient, clli *string, slot *uint, port *uint, ont *uint, serial *string) error {
271 fmt.Println("clli", *clli)
272 fmt.Println("slot", *slot)
273 fmt.Println("port", *port)
274 fmt.Println("ont", *ont)
275 fmt.Println("serial", *serial)
276 res, err := c.DeleteOnt(context.Background(), &api.DeleteOntMessage{CLLI: *clli, SlotNumber: int32(*slot), PortNumber: int32(*port), OntNumber: int32(*ont), SerialNumber: *serial})
277 if err != nil {
278 debug.PrintStack()
279 fmt.Printf("Error when calling ProvsionOnt %s", err)
280 return err
281 }
282 log.Printf("Response from server: %t", res.GetSuccess())
283 return nil
284}
285
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400286func usage() {
287 var output = `
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400288 Usage ./client -server=[serverAddress:port] -[methodFlag] params
289 ./client -ssl -fqdn=FQDN_OF_ABSTRACT_OLT_SERVER.CRT -cert PATH_TO_SERVER.CRT -server=[serverAddress:port] -[methodFlag] params : use ssl
290 ./client -auth -server=[serverAddress:port] -[methodFlag] params : Authenticate session
291
292 methodFlags:
293 -e echo # used to test connectivity to server NOOP
294 params:
295 -message string to be echoed back from the server
296 e.g. ./client -server=localhost:7777 -e -message MESSAGE_TO_BE_ECHOED
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400297 -c create chassis
298 params:
299 -clli CLLI_NAME
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400300 -xos_user XOS_USER
301 -xos_password XOS_PASSWORD
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400302 -xos_address XOS_TOSCA_IP
303 -xos_port XOS_TOSCA_LISTEN_PORT
304 -rack [optional default 1]
305 -shelf [optional default 1]
donNewtonAlpha57aa2ff2018-10-01 16:45:32 -0400306 e.g. ./client -server=localhost:7777 -c -clli MY_CLLI -xos_user foundry -xos_password password -xos_address 192.168.0.1 -xos_port 30007 -rack 1 -shelf 1
307 -u update xos user/password
308 -clli CLLI_NAME
309 -xos_user XOS_USER
310 -xos_password XOS_PASSWORD
311 e.g. ./client -server=localhost:7777 -u -clli MY_CLLI -xos_user NEW_USER -xos_password NEW_PASSWORD
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400312 -s add physical olt chassis to chassis
313 params:
314 -clli CLLI_NAME - identifies abstract chassis to assign olt chassis to
315 -olt_address - OLT_CHASSIS_IP_ADDRESS
316 -olt_port - OLT_CHASSIS_LISTEN_PORT
317 -name - OLT_NAME internal human readable name to identify OLT_CHASSIS
318 -driver [openolt,asfvolt16,adtran,tibits] - used to tell XOS which driver should be used to manange chassis
319 -type [edgecore,adtran,tibit] - used to tell AbstractOLT how many ports are available on olt chassis
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400320 e.g. ./client -server abstractOltHost:7777 -s -clli MY_CLLI -olt_address 192.168.1.100 -olt_port=9191 -name=slot1 -driver=openolt -type=adtran
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400321 -o provision ont - adds ont to whitelist in XOS on a specific port on a specific olt chassis based on abstract -> phyisical mapping
322 params:
323 -clli CLLI_NAME
324 -slot SLOT_NUMBER [1-16]
325 -port OLT_PORT_NUMBER [1-16]
326 -ont ONT_NUMBER [1-64]
327 -serial ONT_SERIAL_NUM
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400328 e.g. ./client -server=localhost:7777 -o -clli=MY_CLLI -slot=1 -port=1 -ont=22 -serial=aer900jasdf
329 -d delete ont - removes ont from service
330 params:
331 -clli CLLI_NAME
332 -slot SLOT_NUMBER [1-16]
333 -port OLT_PORT_NUMBER [1-16]
334 -ont ONT_NUMBER [1-64]
335 -serial ONT_SERIAL_NUM
336 e.g. ./client -server=localhost:7777 -d -clli=MY_CLLI -slot=1 -port=1 -ont=22 -serial=aer900jasdf
337 -output (TEMPORARY) causes AbstractOLT to serialize all chassis to JSON file in $WorkingDirectory/backups
338 e.g. ./client -server=localhost:7777 -output
339 `
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400340
341 fmt.Println(output)
Author Namea594e632018-08-10 11:33:58 -0400342}