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 ( |
| 20 | "flag" |
| 21 | "fmt" |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 22 | "io/ioutil" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 23 | "log" |
| 24 | "net" |
| 25 | "net/http" |
| 26 | "os" |
| 27 | "strings" |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 28 | "time" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 29 | |
| 30 | "gerrit.opencord.org/abstract-olt/api" |
| 31 | "gerrit.opencord.org/abstract-olt/internal/pkg/settings" |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 32 | "gerrit.opencord.org/abstract-olt/models" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 33 | "github.com/grpc-ecosystem/grpc-gateway/runtime" |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 34 | "github.com/mongodb/mongo-go-driver/bson" |
| 35 | "github.com/mongodb/mongo-go-driver/mongo" |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 36 | "golang.org/x/net/context" |
| 37 | "google.golang.org/grpc" |
| 38 | "google.golang.org/grpc/credentials" |
| 39 | "google.golang.org/grpc/metadata" |
| 40 | ) |
| 41 | |
| 42 | // private type for Context keys |
| 43 | type contextKey int |
| 44 | |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 45 | var useSsl *bool |
| 46 | var useAuthentication *bool |
| 47 | var certDirectory *string |
| 48 | |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 49 | const ( |
| 50 | clientIDKey contextKey = iota |
| 51 | ) |
| 52 | |
| 53 | /* |
| 54 | GetLogger - returns the logger |
| 55 | */ |
| 56 | func credMatcher(headerName string) (mdName string, ok bool) { |
| 57 | if headerName == "Login" || headerName == "Password" { |
| 58 | return headerName, true |
| 59 | } |
| 60 | return "", false |
| 61 | } |
| 62 | |
| 63 | // authenticateAgent check the client credentials |
| 64 | func authenticateClient(ctx context.Context, s *api.Server) (string, error) { |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 65 | //TODO if we decide to handle Authentication with AbstractOLT this will need to be bound to an authentication service |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 66 | if md, ok := metadata.FromIncomingContext(ctx); ok { |
| 67 | clientLogin := strings.Join(md["login"], "") |
| 68 | clientPassword := strings.Join(md["password"], "") |
| 69 | |
| 70 | if clientLogin != "john" { |
| 71 | return "", fmt.Errorf("unknown user %s", clientLogin) |
| 72 | } |
| 73 | if clientPassword != "doe" { |
| 74 | return "", fmt.Errorf("bad password %s", clientPassword) |
| 75 | } |
| 76 | |
| 77 | log.Printf("authenticated client: %s", clientLogin) |
| 78 | return "42", nil |
| 79 | } |
| 80 | return "", fmt.Errorf("missing credentials") |
| 81 | } |
| 82 | |
| 83 | // unaryInterceptor call authenticateClient with current context |
| 84 | func unaryInterceptor(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { |
| 85 | s, ok := info.Server.(*api.Server) |
| 86 | if !ok { |
| 87 | return nil, fmt.Errorf("unable to cast server") |
| 88 | } |
| 89 | clientID, err := authenticateClient(ctx, s) |
| 90 | if err != nil { |
| 91 | return nil, err |
| 92 | } |
| 93 | |
| 94 | ctx = context.WithValue(ctx, clientIDKey, clientID) |
| 95 | return handler(ctx, req) |
| 96 | } |
| 97 | |
| 98 | func startGRPCServer(address, certFile, keyFile string) error { |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 99 | if settings.GetDebug() { |
| 100 | log.Printf("startGRPCServer(LisenAddress:%s,CertFile:%s,KeyFile:%s\n", address, certFile, keyFile) |
| 101 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 102 | // create a listener on TCP port |
| 103 | lis, err := net.Listen("tcp", address) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 104 | |
| 105 | // create a server instance |
| 106 | s := api.Server{} |
| 107 | |
| 108 | // Create the TLS credentials |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 109 | |
| 110 | // Create an array of gRPC options with the credentials |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 111 | var opts []grpc.ServerOption |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 112 | creds, err := credentials.NewClientTLSFromFile(certFile, "") |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 113 | if *useSsl && *useAuthentication { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 114 | if err != nil { |
| 115 | return fmt.Errorf("could not load TLS keys: %s", err) |
| 116 | } |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 117 | opts = []grpc.ServerOption{grpc.Creds(creds), |
| 118 | grpc.UnaryInterceptor(unaryInterceptor)} |
| 119 | } else if *useAuthentication { |
| 120 | opts = []grpc.ServerOption{grpc.UnaryInterceptor(unaryInterceptor)} |
| 121 | } else if *useSsl { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 122 | if err != nil { |
| 123 | return fmt.Errorf("could not load TLS keys: %s", err) |
| 124 | } |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 125 | opts = []grpc.ServerOption{grpc.Creds(creds)} |
| 126 | } else { |
| 127 | opts = []grpc.ServerOption{} |
| 128 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 129 | |
| 130 | // create a gRPC server object |
| 131 | grpcServer := grpc.NewServer(opts...) |
| 132 | |
| 133 | // attach the Ping service to the server |
donNewtonAlpha | 5234b13 | 2018-08-16 14:12:28 -0400 | [diff] [blame] | 134 | api.RegisterAbstractOLTServer(grpcServer, &s) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 135 | |
| 136 | // start the server |
| 137 | log.Printf("starting HTTP/2 gRPC server on %s", address) |
| 138 | if err := grpcServer.Serve(lis); err != nil { |
| 139 | return fmt.Errorf("failed to serve: %s", err) |
| 140 | } |
| 141 | |
| 142 | return nil |
| 143 | } |
| 144 | func startRESTServer(address, grpcAddress, certFile string) error { |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 145 | if settings.GetDebug() { |
| 146 | log.Printf("startRESTServer(Address:%s, GRPCAddress:%s,Cert File:%s\n", address, grpcAddress, certFile) |
| 147 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 148 | ctx := context.Background() |
| 149 | ctx, cancel := context.WithCancel(ctx) |
| 150 | defer cancel() |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 151 | var mux *runtime.ServeMux |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 152 | |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 153 | var opts []grpc.DialOption |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 154 | if *useAuthentication { |
| 155 | mux = runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(credMatcher)) |
| 156 | } else { |
| 157 | mux = runtime.NewServeMux() |
| 158 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 159 | if *useSsl { |
| 160 | creds, err := credentials.NewClientTLSFromFile(certFile, "") |
| 161 | opts = []grpc.DialOption{grpc.WithTransportCredentials(creds)} |
| 162 | if err != nil { |
| 163 | return fmt.Errorf("could not load TLS certificate: %s", err) |
| 164 | } |
| 165 | } else { |
| 166 | opts = []grpc.DialOption{grpc.WithInsecure()} |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 167 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 168 | // Setup the client gRPC options |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 169 | err := api.RegisterAbstractOLTHandlerFromEndpoint(ctx, mux, grpcAddress, opts) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 170 | if err != nil { |
| 171 | return fmt.Errorf("could not register service Ping: %s", err) |
| 172 | } |
| 173 | |
| 174 | log.Printf("starting HTTP/1.1 REST server on %s", address) |
| 175 | http.ListenAndServe(address, mux) |
| 176 | |
| 177 | return nil |
| 178 | } |
| 179 | func main() { |
| 180 | debugPtr := flag.Bool("d", false, "Log Level Debug") |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 181 | useAuthentication = flag.Bool("a", false, "Use Authentication") |
| 182 | useSsl = flag.Bool("s", false, "Use SSL") |
| 183 | certDirectory = flag.String("cert_dir", "cert", "Directory where key files exist") |
| 184 | listenAddress := flag.String("listenAddress", "localhost", "IP Address to listen on") |
| 185 | grpcPort := flag.String("grpc_port", "7777", "Port to listen for GRPC") |
| 186 | restPort := flag.String("rest_port", "7778", "Port to listen for Rest Server") |
| 187 | logFile := flag.String("log_file", "AbstractOLT.log", "Name of the LogFile to write to") |
| 188 | h := flag.Bool("h", false, "Show usage") |
| 189 | help := flag.Bool("help", false, "Show usage") |
donNewtonAlpha | af22974 | 2018-09-19 13:22:00 -0400 | [diff] [blame] | 190 | dummy := flag.Bool("dummy", false, "Run in dummy mode where YAML is not sent to XOS") |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 191 | |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 192 | useMongo := flag.Bool("useMongo", false, "use mongo db for backup/restore") |
| 193 | mongodb := flag.String("mongodb", "mongodb://foundry:foundry@localhost:27017", "connect string for mongodb backup/restore") |
| 194 | |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 195 | flag.Parse() |
donNewtonAlpha | af22974 | 2018-09-19 13:22:00 -0400 | [diff] [blame] | 196 | settings.SetDummy(*dummy) |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 197 | |
| 198 | if *help || *h { |
| 199 | var usage = `./AbstractOLT -d [default false] : Runs in Debug mode |
| 200 | Params: |
| 201 | -s [default false] -cert_dir [default $WORKING_DIR/cert] DIR : Runs in SSL mode with server.crt and server.key found in DIR |
| 202 | -a [default false] : Run in Authentication mode currently very basic |
| 203 | -listenAddress IP_ADDRESS [default localhost] -grpc_port [default 7777] PORT1 -rest_port [default 7778] PORT2: Listen for grpc on IP_ADDRESS:PORT1 and rest on IP_ADDRESS:PORT2 |
| 204 | -log_file [default $WORKING_DIR/AbstractOLT.log] LOG_FILE |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 205 | -mongo [default false] use mongodb for backup restore |
| 206 | -mongodb [default mongodb://foundry:foundry@localhost:27017] connect string for mongodb - Required if mongo == true |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 207 | -h(elp) print this usage |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 208 | |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 209 | ` |
| 210 | fmt.Println(usage) |
| 211 | return |
| 212 | } |
| 213 | settings.SetDebug(*debugPtr) |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 214 | settings.SetMongo(*useMongo) |
| 215 | settings.SetMongodb(*mongodb) |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 216 | fmt.Println("Startup Params: debug:", *debugPtr, " Authentication:", *useAuthentication, " SSL:", *useSsl, "Cert Directory", *certDirectory, |
| 217 | "ListenAddress:", *listenAddress, " grpc port:", *grpcPort, " rest port:", *restPort, "Logging to ", *logFile) |
| 218 | |
| 219 | file, err := os.OpenFile(*logFile, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 220 | if err != nil { |
| 221 | log.Fatalln("Failed to open log file", file, ":", err) |
| 222 | } |
| 223 | log.SetOutput(file) |
| 224 | log.SetFlags(log.Ldate | log.Ltime | log.Lmicroseconds | log.Lshortfile) |
donNewtonAlpha | af22974 | 2018-09-19 13:22:00 -0400 | [diff] [blame] | 225 | if *dummy { |
| 226 | fmt.Println("RUNNING IN DUMMY MODE NO YAML WILL BE SENT TO XOS") |
| 227 | log.Println("RUNNING IN DUMMY MODE NO YAML WILL BE SENT TO XOS") |
| 228 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 229 | log.Printf("Setting Debug to %t\n", settings.GetDebug()) |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 230 | if settings.GetDebug() { |
| 231 | log.Println("Startup Params: debug:", *debugPtr, " Authentication:", *useAuthentication, " SSL:", *useSsl, "Cert Directory", *certDirectory, |
| 232 | "ListenAddress:", *listenAddress, " grpc port:", *grpcPort, " rest port:", *restPort, "Logging to ", *logFile) |
| 233 | } |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 234 | |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 235 | grpcAddress := fmt.Sprintf("%s:%s", *listenAddress, *grpcPort) |
| 236 | restAddress := fmt.Sprintf("%s:%s", *listenAddress, *restPort) |
| 237 | |
| 238 | certFile := fmt.Sprintf("%s/server.crt", *certDirectory) |
| 239 | keyFile := fmt.Sprintf("%s/server.key", *certDirectory) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 240 | |
| 241 | // fire the gRPC server in a goroutine |
| 242 | go func() { |
| 243 | err := startGRPCServer(grpcAddress, certFile, keyFile) |
| 244 | if err != nil { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 245 | log.Printf("failed to start gRPC server: %s", err) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 246 | } |
| 247 | }() |
| 248 | |
| 249 | // fire the REST server in a goroutine |
| 250 | go func() { |
| 251 | err := startRESTServer(restAddress, grpcAddress, certFile) |
| 252 | if err != nil { |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 253 | log.Printf("failed to start REST server: %s", err) |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 254 | } |
| 255 | }() |
| 256 | |
| 257 | // infinite loop |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 258 | if *useMongo { |
| 259 | client, err := mongo.NewClient(*mongodb) |
| 260 | client.Connect(context.Background()) |
| 261 | defer client.Disconnect(context.Background()) |
| 262 | if err != nil { |
| 263 | log.Fatalf("unable to connect to mongodb with %v\n", err) |
| 264 | } |
| 265 | collection := client.Database("AbstractOLT").Collection("backup") |
| 266 | cur, err := collection.Find(context.Background(), nil) |
| 267 | if err != nil { |
| 268 | log.Fatalf("Unable to connect to collection with %v\n", err) |
| 269 | } |
| 270 | defer cur.Close(context.Background()) |
| 271 | for cur.Next(context.Background()) { |
| 272 | elem := bson.NewDocument() |
| 273 | err := cur.Decode(elem) |
donNewtonAlpha | b8f3075 | 2018-10-04 11:57:41 -0400 | [diff] [blame] | 274 | if err != nil { |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 275 | log.Fatal(err) |
donNewtonAlpha | b8f3075 | 2018-10-04 11:57:41 -0400 | [diff] [blame] | 276 | } |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 277 | clli := elem.LookupElement("_id").Value() |
| 278 | body := elem.LookupElement("body").Value() |
| 279 | _, bodyBin := (*body).Binary() |
| 280 | |
| 281 | chassisHolder := models.ChassisHolder{} |
| 282 | err = chassisHolder.Deserialize(bodyBin) |
| 283 | if err != nil { |
| 284 | log.Printf("Deserialize threw an error for clli %s %v\n", (*clli).StringValue(), err) |
| 285 | } else { |
| 286 | chassisMap := models.GetChassisMap() |
| 287 | (*chassisMap)[(*clli).StringValue()] = &chassisHolder |
| 288 | |
| 289 | } |
| 290 | } |
| 291 | } else { |
| 292 | files, err := ioutil.ReadDir("backup") |
| 293 | if err != nil { |
| 294 | log.Fatal(err) |
| 295 | } |
| 296 | for _, file := range files { |
| 297 | chassisHolder := models.ChassisHolder{} |
| 298 | if file.Name() != "BackupPlaceHolder" { |
| 299 | fileName := fmt.Sprintf("backup/%s", file.Name()) |
| 300 | json, _ := ioutil.ReadFile(fileName) |
| 301 | err := chassisHolder.Deserialize([]byte(json)) |
| 302 | if err != nil { |
| 303 | fmt.Printf("Deserialize threw an error %v\n", err) |
| 304 | } |
| 305 | chassisMap := models.GetChassisMap() |
| 306 | (*chassisMap)[file.Name()] = &chassisHolder |
| 307 | } else { |
| 308 | fmt.Println("Ignoring BackupPlaceHolder") |
| 309 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 310 | } |
donNewtonAlpha | e7ab5b9 | 2018-09-27 15:09:14 -0400 | [diff] [blame] | 311 | } |
| 312 | |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 313 | log.Printf("Entering infinite loop") |
donNewtonAlpha | c997d64 | 2018-10-17 13:22:48 -0400 | [diff] [blame] | 314 | var ticker = time.NewTicker(60 * time.Second) |
| 315 | for { |
| 316 | select { |
| 317 | case <-ticker.C: |
| 318 | api.DoOutput() |
| 319 | } |
| 320 | } |
| 321 | |
donNewtonAlpha | b3279ea | 2018-09-18 15:55:32 -0400 | [diff] [blame] | 322 | //TODO publish periodic stats etc |
Author Name | a594e63 | 2018-08-10 11:33:58 -0400 | [diff] [blame] | 323 | fmt.Println("AbstractOLT") |
| 324 | } |