SEBA-261
refactor to support serialize/deserialize

Change-Id: Icdc0bc2bb06a9d1c3240c0f46e1de02953a0b017
diff --git a/cmd/AbstractOLT/AbstractOLT.go b/cmd/AbstractOLT/AbstractOLT.go
index 2148cc9..eee3b0f 100644
--- a/cmd/AbstractOLT/AbstractOLT.go
+++ b/cmd/AbstractOLT/AbstractOLT.go
@@ -19,6 +19,7 @@
 import (
 	"flag"
 	"fmt"
+	"io/ioutil"
 	"log"
 	"net"
 	"net/http"
@@ -27,6 +28,7 @@
 
 	"gerrit.opencord.org/abstract-olt/api"
 	"gerrit.opencord.org/abstract-olt/internal/pkg/settings"
+	"gerrit.opencord.org/abstract-olt/models"
 	"github.com/grpc-ecosystem/grpc-gateway/runtime"
 	"golang.org/x/net/context"
 	"google.golang.org/grpc"
@@ -96,28 +98,27 @@
 	}
 	// create a listener on TCP port
 	lis, err := net.Listen("tcp", address)
-	if err != nil {
-		log.Printf("startGRPCServer failed to start with %v\n", err)
-		return fmt.Errorf("failed to listen: %v", err)
-	}
 
 	// create a server instance
 	s := api.Server{}
 
 	// Create the TLS credentials
-	creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)
-	if err != nil {
-		return fmt.Errorf("could not load TLS keys: %s", err)
-	}
 
 	// Create an array of gRPC options with the credentials
 	var opts []grpc.ServerOption
+	creds, err := credentials.NewClientTLSFromFile(certFile, "")
 	if *useSsl && *useAuthentication {
+		if err != nil {
+			return fmt.Errorf("could not load TLS keys: %s", err)
+		}
 		opts = []grpc.ServerOption{grpc.Creds(creds),
 			grpc.UnaryInterceptor(unaryInterceptor)}
 	} else if *useAuthentication {
 		opts = []grpc.ServerOption{grpc.UnaryInterceptor(unaryInterceptor)}
 	} else if *useSsl {
+		if err != nil {
+			return fmt.Errorf("could not load TLS keys: %s", err)
+		}
 		opts = []grpc.ServerOption{grpc.Creds(creds)}
 	} else {
 		opts = []grpc.ServerOption{}
@@ -146,19 +147,23 @@
 	defer cancel()
 	var mux *runtime.ServeMux
 
+	var opts []grpc.DialOption
 	if *useAuthentication {
 		mux = runtime.NewServeMux(runtime.WithIncomingHeaderMatcher(credMatcher))
 	} else {
 		mux = runtime.NewServeMux()
 	}
-	creds, err := credentials.NewClientTLSFromFile(certFile, "")
-	if err != nil {
-		return fmt.Errorf("could not load TLS certificate: %s", err)
+	if *useSsl {
+		creds, err := credentials.NewClientTLSFromFile(certFile, "")
+		opts = []grpc.DialOption{grpc.WithTransportCredentials(creds)}
+		if err != nil {
+			return fmt.Errorf("could not load TLS certificate: %s", err)
+		}
+	} else {
+		opts = []grpc.DialOption{grpc.WithInsecure()}
 	}
-
 	// Setup the client gRPC options
-	opts := []grpc.DialOption{grpc.WithTransportCredentials(creds)}
-	err = api.RegisterAbstractOLTHandlerFromEndpoint(ctx, mux, grpcAddress, opts)
+	err := api.RegisterAbstractOLTHandlerFromEndpoint(ctx, mux, grpcAddress, opts)
 	if err != nil {
 		return fmt.Errorf("could not register service Ping: %s", err)
 	}
@@ -226,7 +231,7 @@
 	go func() {
 		err := startGRPCServer(grpcAddress, certFile, keyFile)
 		if err != nil {
-			log.Fatalf("failed to start gRPC server: %s", err)
+			log.Printf("failed to start gRPC server: %s", err)
 		}
 	}()
 
@@ -234,11 +239,28 @@
 	go func() {
 		err := startRESTServer(restAddress, grpcAddress, certFile)
 		if err != nil {
-			log.Fatalf("failed to start gRPC server: %s", err)
+			log.Printf("failed to start REST server: %s", err)
 		}
 	}()
 
 	// infinite loop
+	files, err := ioutil.ReadDir("backup")
+	if err != nil {
+		log.Fatal(err)
+	}
+	for _, file := range files {
+		fmt.Println(file.Name())
+		chassisHolder := models.ChassisHolder{}
+		fileName := fmt.Sprintf("backup/%s", file.Name())
+		json, _ := ioutil.ReadFile(fileName)
+		err := chassisHolder.Deserialize([]byte(json))
+		if err != nil {
+			fmt.Printf("Deserialize threw an error %v\n", err)
+		}
+		chassisMap := models.GetChassisMap()
+		(*chassisMap)[file.Name()] = &chassisHolder
+	}
+
 	log.Printf("Entering infinite loop")
 	select {}
 	//TODO publish periodic stats etc