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