Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 20 | "context" |
| 21 | "flag" |
| 22 | "fmt" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 23 | "log" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 24 | "runtime/debug" |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 25 | "strings" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 26 | |
| 27 | "gerrit.opencord.org/abstract-olt/api" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 28 | "google.golang.org/grpc" |
| 29 | "google.golang.org/grpc/credentials" |
| 30 | ) |
| 31 | |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 32 | func main() { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 33 | /* COMMAND FLAGS */ |
| 34 | output := flag.Bool("output", false, "dump output") |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 35 | echo := flag.Bool("e", false, "echo") |
| 36 | message := flag.String("message", "ping", "message to be echoed back") |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 37 | create := flag.Bool("c", false, "create?") |
| 38 | addOlt := flag.Bool("s", false, "addOlt?") |
| 39 | provOnt := flag.Bool("o", false, "provisionOnt?") |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 40 | deleteOnt := flag.Bool("d", false, "deleteOnt") |
| 41 | /* END COMMAND FLAGS */ |
| 42 | |
| 43 | /* CREATE CHASSIS FLAGS */ |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 44 | 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") |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 48 | /* END CREATE CHASSIS FLAGS */ |
| 49 | |
| 50 | /* ADD OLT FLAGS */ |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 51 | 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") |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 56 | /* END ADD OLT FLAGS */ |
| 57 | |
| 58 | /* PROVISION / DELETE ONT FLAGS */ |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 59 | 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") |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 63 | /* END PROVISION / DELETE ONT FLAGS */ |
| 64 | |
| 65 | /*GENERIC FLAGS */ |
| 66 | clli := flag.String("clli", "", "clli of abstract chassis") |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 67 | 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") |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 72 | /*GENERIC FLAGS */ |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 73 | |
| 74 | flag.Parse() |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 75 | |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 76 | 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 | } |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 82 | |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 83 | 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") |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 92 | usage() |
| 93 | return |
| 94 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 95 | if cmdCount < 1 { |
| 96 | fmt.Println("CMD You didn't specify an operation to perform") |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 97 | usage() |
| 98 | return |
| 99 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 100 | |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 101 | var conn *grpc.ClientConn |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 102 | var err error |
| 103 | |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 104 | // Setup the login/pass |
| 105 | auth := Authentication{ |
| 106 | Login: "john", |
| 107 | Password: "doe", |
| 108 | } |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 109 | 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 | } |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 127 | 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) |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 139 | } else if *echo { |
| 140 | ping(c, *message) |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 141 | } else if *output { |
| 142 | Output(c) |
| 143 | } else if *deleteOnt { |
| 144 | deleteONT(c, clli, slot, port, ont, serial) |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 145 | } |
| 146 | |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 149 | // Authentication holds the login/password |
| 150 | type Authentication struct { |
| 151 | Login string |
| 152 | Password string |
| 153 | } |
| 154 | |
| 155 | // GetRequestMetadata gets the current request metadata |
| 156 | func (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 |
| 164 | func (a *Authentication) RequireTransportSecurity() bool { |
| 165 | return true |
| 166 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 167 | func 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 | |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 178 | func 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 Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 187 | |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 188 | func 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 Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 196 | if err != nil { |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 197 | fmt.Printf("Error when calling CreateChassis: %s", err) |
| 198 | return err |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 199 | } |
| 200 | log.Printf("Response from server: %s", response.GetDeviceID()) |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 201 | return nil |
| 202 | } |
| 203 | func 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 Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 223 | if err != nil { |
| 224 | debug.PrintStack() |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 225 | fmt.Printf("Error when calling CreateOLTChassis: %s", err) |
| 226 | return err |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 227 | } |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 228 | log.Printf("Response from server: %s", res.GetDeviceID()) |
| 229 | return nil |
| 230 | } |
| 231 | func 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 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 248 | func 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 | |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 264 | func usage() { |
| 265 | var output = ` |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 266 | 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 |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 275 | -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] |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 282 | e.g. ./client -server=localhost:7777 -c -clli MY_CLLI -xos_address 192.168.0.1 -xos_port 30007 -rack 1 -shelf 1 |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 283 | -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 |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 291 | 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 |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 292 | -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 |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame^] | 299 | 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 | ` |
donNewtonAlpha | 1d2d681 | 2018-09-14 16:00:02 -0400 | [diff] [blame] | 311 | |
| 312 | fmt.Println(output) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 313 | } |