SEBA-261
refactor to support serialize/deserialize

Change-Id: Icdc0bc2bb06a9d1c3240c0f46e1de02953a0b017
diff --git a/api/abstract_olt_api.proto b/api/abstract_olt_api.proto
index fb4aee7..6604703 100644
--- a/api/abstract_olt_api.proto
+++ b/api/abstract_olt_api.proto
@@ -81,6 +81,12 @@
 message DeleteOntReturn{
    bool Success=1;
 }
+message OutputMessage{
+   string Something=1;
+}
+message OutputReturn{
+   bool Success=1;
+}
 service AbstractOLT{
    rpc Echo(EchoMessage) returns (EchoReplyMessage){
       option(google.api.http)={
@@ -112,5 +118,11 @@
 	body:"*"
       };
    }
+   rpc Output(OutputMessage)returns(OutputReturn){
+      option(google.api.http)={
+        post:"/v1/Output"
+	body:"*"
+      };
+   }
 }
 
diff --git a/api/handler.go b/api/handler.go
index 54115a6..2e6b9cc 100644
--- a/api/handler.go
+++ b/api/handler.go
@@ -21,6 +21,7 @@
 	"fmt"
 	"log"
 	"net"
+	"os"
 	"strings"
 
 	"gerrit.opencord.org/abstract-olt/internal/pkg/settings"
@@ -36,6 +37,9 @@
 type Server struct {
 }
 
+/*
+Echo - Tester function which just returns same string sent to it
+*/
 func (s *Server) Echo(ctx context.Context, in *EchoMessage) (*EchoReplyMessage, error) {
 	ping := in.GetPing()
 	pong := EchoReplyMessage{Pong: ping}
@@ -46,23 +50,24 @@
 CreateChassis - allocates a new Chassis struct and stores it in chassisMap
 */
 func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) {
-	phyChassisMap := models.GetPhyChassisMap()
-	absChassisMap := models.GetAbstractChassisMap()
+	chassisMap := models.GetChassisMap()
 	clli := in.GetCLLI()
-	chassis := (*phyChassisMap)[clli]
-	if chassis != nil {
-		return &AddChassisReturn{DeviceID: chassis.CLLI}, nil
+	chassisHolder := (*chassisMap)[clli]
+	if chassisHolder != nil {
+		return &AddChassisReturn{DeviceID: chassisHolder.AbstractChassis.CLLI}, nil
 	}
+
 	abstractChassis := abstract.GenerateChassis(clli, int(in.GetRack()), int(in.GetShelf()))
-	phyChassis := &physical.Chassis{CLLI: clli, VCoreAddress: net.TCPAddr{IP: net.ParseIP(in.GetVCoreIP()), Port: int(in.GetVCorePort())}, Rack: int(in.GetRack()), Shelf: int(in.GetShelf())}
+	phyChassis := physical.Chassis{CLLI: clli, VCoreAddress: net.TCPAddr{IP: net.ParseIP(in.GetVCoreIP()),
+		Port: int(in.GetVCorePort())}, Rack: int(in.GetRack()), Shelf: int(in.GetShelf())}
+
+	chassisHolder = &models.ChassisHolder{AbstractChassis: abstractChassis, PhysicalChassis: phyChassis}
 	if settings.GetDebug() {
 		output := fmt.Sprintf("%v", abstractChassis)
 		formatted := strings.Replace(output, "{", "\n{", -1)
 		log.Printf("new chassis %s\n", formatted)
 	}
-	(*phyChassisMap)[clli] = phyChassis
-	fmt.Printf("phy %v, abs %v\n", phyChassisMap, absChassisMap)
-	(*absChassisMap)[clli] = abstractChassis
+	(*chassisMap)[clli] = chassisHolder
 	return &AddChassisReturn{DeviceID: clli}, nil
 }
 
@@ -71,64 +76,47 @@
 */
 func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) {
 	fmt.Printf(" CreateOLTChassis %v \n", *in)
-	phyChassisMap := models.GetPhyChassisMap()
+	chassisMap := models.GetChassisMap()
 	clli := in.GetCLLI()
-	chassis := (*phyChassisMap)[clli]
-	if chassis == nil {
+	chassisHolder := (*chassisMap)[clli]
+	if chassisHolder == nil {
 		errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
 		return &AddOLTChassisReturn{DeviceID: "", ChassisDeviceID: ""}, errors.New(errString)
 	}
 	oltType := in.GetType()
 	address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())}
-	sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: chassis}
-
-	var olt physical.OLT
+	physicalChassis := &chassisHolder.PhysicalChassis
+	sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: physicalChassis}
 	switch oltType {
 	case AddOLTChassisMessage_edgecore:
-		olt = physical.CreateEdgecore(&sOlt)
+		sOlt.CreateEdgecore()
 	case AddOLTChassisMessage_adtran:
 	case AddOLTChassisMessage_tibit:
 	}
-	err := AddCard(chassis, olt)
-	if err != nil {
-		//TODO do something
+	physicalChassis.AddOLTChassis(sOlt)
+	ports := sOlt.GetPorts()
+	for i := 0; i < len(ports); i++ {
+		absPort, _ := chassisHolder.AbstractChassis.NextPort()
+
+		absPort.PhysPort = &ports[i]
+		//AssignTraits(&ports[i], absPort)
 	}
 	return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil
 
 }
 
 /*
-AddCard Adds an OLT card to an existing physical chassis, allocating ports
-in the physical card to those in the abstract model
-*/
-func AddCard(physChassis *physical.Chassis, olt physical.OLT) error {
-	physChassis.AddOLTChassis(olt)
-
-	ports := olt.GetPorts()
-	absChassis := (*models.GetAbstractChassisMap())[physChassis.CLLI]
-
-	for i := 0; i < len(ports); i++ {
-		absPort, _ := absChassis.NextPort()
-
-		absPort.PhysPort = &ports[i]
-		//AssignTraits(&ports[i], absPort)
-	}
-	//should probably worry about error at some point
-	return nil
-}
-
-/*
 ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port
 */
 func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) {
-	absChassisMap := models.GetAbstractChassisMap()
+	chassisMap := models.GetChassisMap()
 	clli := in.GetCLLI()
-	chassis := (*absChassisMap)[clli]
-	if chassis == nil {
+	chassisHolder := (*chassisMap)[clli]
+	if chassisHolder == nil {
 		errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
 		return &AddOntReturn{Success: false}, errors.New(errString)
 	}
-	err := chassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
+	err := chassisHolder.AbstractChassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
 
 	if err != nil {
 		return nil, err
@@ -140,12 +128,27 @@
 DeleteOnt - deletes a previously provision ont
 */
 func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) {
-	absChassisMap := models.GetAbstractChassisMap()
+	chassisMap := models.GetChassisMap()
 	clli := in.GetCLLI()
-	chassis := (*absChassisMap)[clli]
-	err := chassis.DeleteONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
+	chassisHolder := (*chassisMap)[clli]
+	err := chassisHolder.AbstractChassis.DeleteONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
 	if err != nil {
 		return nil, err
 	}
 	return &DeleteOntReturn{Success: true}, nil
 }
+
+func (s *Server) Output(ctx context.Context, in *OutputMessage) (*OutputReturn, error) {
+	chassisMap := models.GetChassisMap()
+	for clli, chassisHolder := range *chassisMap {
+
+		json, _ := (chassisHolder).Serialize()
+		backupFile := fmt.Sprintf("backup/%s", clli)
+		f, _ := os.Create(backupFile)
+		defer f.Close()
+		_, _ = f.WriteString(string(json))
+		f.Sync()
+	}
+	return &OutputReturn{Success: true}, nil
+
+}