blob: 8d8ea4c2353dae4fef26f26dca3174177985606f [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?")
38 addOlt := flag.Bool("s", false, "addOlt?")
39 provOnt := flag.Bool("o", false, "provisionOnt?")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040040 deleteOnt := flag.Bool("d", false, "deleteOnt")
41 /* END COMMAND FLAGS */
42
43 /* CREATE CHASSIS FLAGS */
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040044 xosAddress := flag.String("xos_address", "", "xos address")
45 xosPort := flag.Uint("xos_port", 0, "xos port")
46 rack := flag.Uint("rack", 1, "rack number for chassis")
47 shelf := flag.Uint("shelf", 1, "shelf number for chassis")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040048 /* END CREATE CHASSIS FLAGS */
49
50 /* ADD OLT FLAGS */
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040051 oltAddress := flag.String("olt_address", "", "ip address for olt chassis")
52 oltPort := flag.Uint("olt_port", 0, "listen port for olt chassis")
53 name := flag.String("name", "", "friendly name for olt chassis")
54 driver := flag.String("driver", "", "driver to use with olt chassis")
55 oltType := flag.String("type", "", "olt chassis type")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040056 /* END ADD OLT FLAGS */
57
58 /* PROVISION / DELETE ONT FLAGS */
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040059 slot := flag.Uint("slot", 1, "slot number 1-16 to provision ont to")
60 port := flag.Uint("port", 1, "port number 1-16 to provision ont to")
61 ont := flag.Uint("ont", 1, "ont number 1-64")
62 serial := flag.String("serial", "", "serial number of ont")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040063 /* END PROVISION / DELETE ONT FLAGS */
64
65 /*GENERIC FLAGS */
66 clli := flag.String("clli", "", "clli of abstract chassis")
donNewtonAlphab3279ea2018-09-18 15:55:32 -040067 useSsl := flag.Bool("ssl", false, "use ssl")
68 useAuth := flag.Bool("auth", false, "use auth")
69 crtFile := flag.String("cert", "cert/server.crt", "Public cert for server to establish tls session")
70 serverAddressPort := flag.String("server", "localhost:7777", "address and port of AbstractOLT server")
71 fqdn := flag.String("fqdn", "", "FQDN of the service to match what is in server.crt")
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040072 /*GENERIC FLAGS */
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040073
74 flag.Parse()
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040075
donNewtonAlphab3279ea2018-09-18 15:55:32 -040076 if *useSsl {
77 if *fqdn == "" {
78 fqdn = &(strings.Split(*serverAddressPort, ":")[0])
79 fmt.Printf("using %s as the FQDN for the AbstractOLT server", *fqdn)
80 }
81 }
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040082
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040083 cmdFlags := []*bool{echo, addOlt, create, provOnt, deleteOnt, output}
84 cmdCount := 0
85 for _, flag := range cmdFlags {
86 if *flag {
87 cmdCount++
88 }
89 }
90 if cmdCount > 1 {
91 fmt.Println("CMD You can only call one method at a time")
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040092 usage()
93 return
94 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -040095 if cmdCount < 1 {
96 fmt.Println("CMD You didn't specify an operation to perform")
donNewtonAlpha1d2d6812018-09-14 16:00:02 -040097 usage()
98 return
99 }
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400100
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400101 var conn *grpc.ClientConn
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400102 var err error
103
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400104 // Setup the login/pass
105 auth := Authentication{
106 Login: "john",
107 Password: "doe",
108 }
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400109 if *useSsl && *useAuth {
110
111 creds, err := credentials.NewClientTLSFromFile(*crtFile, *fqdn)
112 conn, err = grpc.Dial(*serverAddressPort, grpc.WithTransportCredentials(creds), grpc.WithPerRPCCredentials(&auth))
113 if err != nil {
114 log.Fatalf("could not load tls cert: %s", err)
115 }
116 } else if *useSsl {
117 creds, err := credentials.NewClientTLSFromFile("cert/server.crt", *fqdn)
118 conn, err = grpc.Dial(*serverAddressPort, grpc.WithTransportCredentials(creds))
119 if err != nil {
120 log.Fatalf("could not load tls cert: %s", err)
121 }
122 } else if *useAuth {
123 conn, err = grpc.Dial(*serverAddressPort, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth))
124 } else {
125 conn, err = grpc.Dial(*serverAddressPort, grpc.WithInsecure())
126 }
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400127 if err != nil {
128 log.Fatalf("did not connect: %s", err)
129 }
130 defer conn.Close()
131
132 c := api.NewAbstractOLTClient(conn)
133 if *create {
134 createChassis(c, clli, xosAddress, xosPort, rack, shelf)
135 } else if *addOlt {
136 addOltChassis(c, clli, oltAddress, oltPort, name, driver, oltType)
137 } else if *provOnt {
138 provisionONT(c, clli, slot, port, ont, serial)
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400139 } else if *echo {
140 ping(c, *message)
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400141 } else if *output {
142 Output(c)
143 } else if *deleteOnt {
144 deleteONT(c, clli, slot, port, ont, serial)
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400145 }
146
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400147}
148
Author Namea594e632018-08-10 11:33:58 -0400149// Authentication holds the login/password
150type Authentication struct {
151 Login string
152 Password string
153}
154
155// GetRequestMetadata gets the current request metadata
156func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
157 return map[string]string{
158 "login": a.Login,
159 "password": a.Password,
160 }, nil
161}
162
163// RequireTransportSecurity indicates whether the credentials requires transport security
164func (a *Authentication) RequireTransportSecurity() bool {
165 return true
166}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400167func Output(c api.AbstractOLTClient) error {
168 response, err := c.Output(context.Background(), &api.OutputMessage{Something: "wtf"})
169 if err != nil {
170 fmt.Printf("Error when calling Echo: %s", err)
171 return err
172 }
173 log.Printf("Response from server: %v", response.GetSuccess())
174
175 return nil
176}
177
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400178func ping(c api.AbstractOLTClient, message string) error {
179 response, err := c.Echo(context.Background(), &api.EchoMessage{Ping: message})
180 if err != nil {
181 fmt.Printf("Error when calling Echo: %s", err)
182 return err
183 }
184 log.Printf("Response from server: %s", response.GetPong())
185 return nil
186}
Author Namea594e632018-08-10 11:33:58 -0400187
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400188func createChassis(c api.AbstractOLTClient, clli *string, xosAddress *string, xosPort *uint, rack *uint, shelf *uint) error {
189 fmt.Println("Calling Create Chassis")
190 fmt.Println("clli", *clli)
191 fmt.Println("xos_address", *xosAddress)
192 fmt.Println("xos_port", *xosPort)
193 fmt.Println("rack", *rack)
194 fmt.Println("shelf", *shelf)
195 response, err := c.CreateChassis(context.Background(), &api.AddChassisMessage{CLLI: *clli, VCoreIP: *xosAddress, VCorePort: int32(*xosPort), Rack: int32(*rack), Shelf: int32(*shelf)})
Author Namea594e632018-08-10 11:33:58 -0400196 if err != nil {
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400197 fmt.Printf("Error when calling CreateChassis: %s", err)
198 return err
Author Namea594e632018-08-10 11:33:58 -0400199 }
200 log.Printf("Response from server: %s", response.GetDeviceID())
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400201 return nil
202}
203func addOltChassis(c api.AbstractOLTClient, clli *string, oltAddress *string, oltPort *uint, name *string, driver *string, oltType *string) error {
204 fmt.Println("clli", *clli)
205 fmt.Println("olt_address", *oltAddress)
206 fmt.Println("olt_port", *oltPort)
207 fmt.Println("name", *name)
208 fmt.Println("driver", *driver)
209 fmt.Println("type", *oltType)
210 var driverType api.AddOLTChassisMessage_OltDriver
211 var chassisType api.AddOLTChassisMessage_OltType
212 switch *oltType {
213 case "edgecore":
214 chassisType = api.AddOLTChassisMessage_edgecore
215 }
216 switch *driver {
217 case "openolt":
218 driverType = api.AddOLTChassisMessage_openoltDriver
219
220 }
221
222 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 -0400223 if err != nil {
224 debug.PrintStack()
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400225 fmt.Printf("Error when calling CreateOLTChassis: %s", err)
226 return err
Author Namea594e632018-08-10 11:33:58 -0400227 }
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400228 log.Printf("Response from server: %s", res.GetDeviceID())
229 return nil
230}
231func provisionONT(c api.AbstractOLTClient, clli *string, slot *uint, port *uint, ont *uint, serial *string) error {
232 fmt.Println("clli", *clli)
233 fmt.Println("slot", *slot)
234 fmt.Println("port", *port)
235 fmt.Println("ont", *ont)
236 fmt.Println("serial", *serial)
237 res, err := c.ProvisionOnt(context.Background(), &api.AddOntMessage{CLLI: *clli, SlotNumber: int32(*slot), PortNumber: int32(*port), OntNumber: int32(*ont), SerialNumber: *serial})
238 if err != nil {
239 debug.PrintStack()
240 fmt.Printf("Error when calling ProvsionOnt %s", err)
241 return err
242 }
243 log.Printf("Response from server: %t", res.GetSuccess())
244 return nil
245
246 return nil
247}
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400248func deleteONT(c api.AbstractOLTClient, clli *string, slot *uint, port *uint, ont *uint, serial *string) error {
249 fmt.Println("clli", *clli)
250 fmt.Println("slot", *slot)
251 fmt.Println("port", *port)
252 fmt.Println("ont", *ont)
253 fmt.Println("serial", *serial)
254 res, err := c.DeleteOnt(context.Background(), &api.DeleteOntMessage{CLLI: *clli, SlotNumber: int32(*slot), PortNumber: int32(*port), OntNumber: int32(*ont), SerialNumber: *serial})
255 if err != nil {
256 debug.PrintStack()
257 fmt.Printf("Error when calling ProvsionOnt %s", err)
258 return err
259 }
260 log.Printf("Response from server: %t", res.GetSuccess())
261 return nil
262}
263
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400264func usage() {
265 var output = `
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400266 Usage ./client -server=[serverAddress:port] -[methodFlag] params
267 ./client -ssl -fqdn=FQDN_OF_ABSTRACT_OLT_SERVER.CRT -cert PATH_TO_SERVER.CRT -server=[serverAddress:port] -[methodFlag] params : use ssl
268 ./client -auth -server=[serverAddress:port] -[methodFlag] params : Authenticate session
269
270 methodFlags:
271 -e echo # used to test connectivity to server NOOP
272 params:
273 -message string to be echoed back from the server
274 e.g. ./client -server=localhost:7777 -e -message MESSAGE_TO_BE_ECHOED
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400275 -c create chassis
276 params:
277 -clli CLLI_NAME
278 -xos_address XOS_TOSCA_IP
279 -xos_port XOS_TOSCA_LISTEN_PORT
280 -rack [optional default 1]
281 -shelf [optional default 1]
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400282 e.g. ./client -server=localhost:7777 -c -clli MY_CLLI -xos_address 192.168.0.1 -xos_port 30007 -rack 1 -shelf 1
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400283 -s add physical olt chassis to chassis
284 params:
285 -clli CLLI_NAME - identifies abstract chassis to assign olt chassis to
286 -olt_address - OLT_CHASSIS_IP_ADDRESS
287 -olt_port - OLT_CHASSIS_LISTEN_PORT
288 -name - OLT_NAME internal human readable name to identify OLT_CHASSIS
289 -driver [openolt,asfvolt16,adtran,tibits] - used to tell XOS which driver should be used to manange chassis
290 -type [edgecore,adtran,tibit] - used to tell AbstractOLT how many ports are available on olt chassis
donNewtonAlphab3279ea2018-09-18 15:55:32 -0400291 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 -0400292 -o provision ont - adds ont to whitelist in XOS on a specific port on a specific olt chassis based on abstract -> phyisical mapping
293 params:
294 -clli CLLI_NAME
295 -slot SLOT_NUMBER [1-16]
296 -port OLT_PORT_NUMBER [1-16]
297 -ont ONT_NUMBER [1-64]
298 -serial ONT_SERIAL_NUM
donNewtonAlphae7ab5b92018-09-27 15:09:14 -0400299 e.g. ./client -server=localhost:7777 -o -clli=MY_CLLI -slot=1 -port=1 -ont=22 -serial=aer900jasdf
300 -d delete ont - removes ont from service
301 params:
302 -clli CLLI_NAME
303 -slot SLOT_NUMBER [1-16]
304 -port OLT_PORT_NUMBER [1-16]
305 -ont ONT_NUMBER [1-64]
306 -serial ONT_SERIAL_NUM
307 e.g. ./client -server=localhost:7777 -d -clli=MY_CLLI -slot=1 -port=1 -ont=22 -serial=aer900jasdf
308 -output (TEMPORARY) causes AbstractOLT to serialize all chassis to JSON file in $WorkingDirectory/backups
309 e.g. ./client -server=localhost:7777 -output
310 `
donNewtonAlpha1d2d6812018-09-14 16:00:02 -0400311
312 fmt.Println(output)
Author Namea594e632018-08-10 11:33:58 -0400313}