Implemented the provision / activate ont workflow

Change-Id: Ife684f41e54e176879332922ad86f517358f15e7
diff --git a/api/abstract_olt_api.proto b/api/abstract_olt_api.proto
index cf6e378..86f9a72 100644
--- a/api/abstract_olt_api.proto
+++ b/api/abstract_olt_api.proto
@@ -65,8 +65,8 @@
    Error error = 2;
 }
 message AddOntMessage{
-   string ChassisDeviceID=1;
-   string SlotDeviceID=2;
+   string CLLI=1;
+   int32 SlotNumber=2;
    int32 PortNumber=3;
    int32 OntNumber=4;
    string SerialNumber=5;
@@ -76,32 +76,31 @@
    Error error = 2;
 }
 
-service AddChassis{
+service AbstractOLT{
    rpc CreateChassis(AddChassisMessage) returns (AddChassisReturn) {
       option(google.api.http) = {
-         post: "/1/CreateAbstractChassis"
+         post: "/v1/CreateAbstractChassis"
 	 body:"*"
       };
    }
-}
 
-service AddOLTChassis {
    rpc CreateOLTChassis(AddOLTChassisMessage) returns (AddOLTChassisReturn) {
       option(google.api.http) = {
-         post: "/1/CreateOLTChassis"
+         post: "/v1/CreateOLTChassis"
 	 body:"*"
       };
    }
-}
-service ActivateSlot{
    rpc EnableSlot(ActivateSlotMessage) returns (ActivateSlotReturn){
       option(google.api.http) = {
-         post: "/1/EnableSlot"
+         post: "/v1/EnableSlot"
 	 body:"*"
       };
    }
-}
-service ActivateOnt {
-   rpc ProvisionOnt(AddOntMessage) returns (AddOntReturn) {}
+   rpc ProvisionOnt(AddOntMessage) returns (AddOntReturn) {
+      option(google.api.http) = {
+         post:"/v1/ProvsionOnt"
+	 body:"*"
+      };
+   }
 }
 
diff --git a/api/handler.go b/api/handler.go
index 7414e19..4aeae13 100644
--- a/api/handler.go
+++ b/api/handler.go
@@ -17,6 +17,7 @@
 package api
 
 import (
+	"errors"
 	"fmt"
 	"log"
 	"net"
@@ -47,7 +48,7 @@
 		return &AddChassisReturn{DeviceID: chassis.CLLI}, nil
 	}
 	abstractChassis := abstract.GenerateChassis(clli)
-	phyChassis := &physical.Chassis{CLLI: clli}
+	phyChassis := &physical.Chassis{CLLI: clli, VCoreAddress: net.TCPAddr{IP: net.ParseIP(in.GetVCoreIP()), Port: int(in.GetVCorePort())}}
 	if settings.GetDebug() {
 		output := fmt.Sprintf("%v", abstractChassis)
 		formatted := strings.Replace(output, "{", "\n{", -1)
@@ -71,7 +72,7 @@
 	}
 	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}
+	sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: chassis}
 
 	var olt physical.OLT
 	switch oltType {
@@ -95,7 +96,7 @@
 in the physical card to those in the abstract model
 */
 func AddCard(physChassis *physical.Chassis, olt physical.OLT) error {
-	physChassis.Linecards = append(physChassis.Linecards, olt)
+	physChassis.AddOLTChassis(olt)
 
 	ports := olt.GetPorts()
 	absChassis := (*models.GetAbstractChassisMap())[physChassis.CLLI]
@@ -105,7 +106,28 @@
 		absPort.PhysPort = &ports[i]
 		//AssignTraits(&ports[i], absPort)
 	}
-
 	//should probably worry about error at some point
 	return nil
 }
+
+/*
+EnableSlot - activates an OLT Chassis
+*/
+func (s *Server) EnableSlot(ctx context.Context, in *ActivateSlotMessage) (*ActivateSlotReturn, error) {
+	return nil, errors.New("garbage error")
+}
+
+/*
+ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port
+*/
+func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) {
+	absChassisMap := models.GetAbstractChassisMap()
+	clli := in.GetCLLI()
+	chassis := (*absChassisMap)[clli]
+	err := chassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetPortNumber()), in.GetSerialNumber())
+
+	if err != nil {
+		return nil, err
+	}
+	return &AddOntReturn{Success: true}, nil
+}
diff --git a/api/handler_test.go b/api/handler_test.go
index 6121242..50c5355 100644
--- a/api/handler_test.go
+++ b/api/handler_test.go
@@ -21,10 +21,12 @@
 	"testing"
 
 	"gerrit.opencord.org/abstract-olt/api"
+	"gerrit.opencord.org/abstract-olt/models/abstract"
 	"golang.org/x/net/context"
 )
 
 var clli string
+var slotHostname = "SlotOne"
 var ctx context.Context
 var server api.Server
 
@@ -41,8 +43,9 @@
 
 }
 func TestHandler_CreateOLTChassis(t *testing.T) {
+	fmt.Println("in handlerTest_CreateChassis")
 	message := &api.AddOLTChassisMessage{CLLI: clli, SlotIP: "12.2.2.0", SlotPort: 9191,
-		Hostname: "SlotOne", Type: api.AddOLTChassisMessage_edgecore}
+		Hostname: slotHostname, Type: api.AddOLTChassisMessage_edgecore}
 	ret, err := server.CreateOLTChassis(ctx, message)
 	if err != nil {
 		t.Fatalf("CreateOLTChassis failed %v\n", err)
@@ -50,3 +53,30 @@
 	fmt.Printf("CreateOLTChassis success %v\n", ret)
 
 }
+func TestHandler_ProvisionOnt(t *testing.T) {
+	ctx = context.Background()
+	server = api.Server{}
+	fmt.Println("in handlerTest_CreateChassis")
+	// this one should succeed
+	message := &api.AddOntMessage{CLLI: clli, SlotNumber: 1, PortNumber: 3, OntNumber: 2, SerialNumber: "2033029402"}
+	ret, err := server.ProvisionOnt(ctx, message)
+	if err != nil {
+		t.Fatalf("ProvisionOnt failed %v\n", err)
+	}
+	// this one should fail
+	fmt.Println("here")
+	message = &api.AddOntMessage{CLLI: clli, SlotNumber: 2, PortNumber: 3, OntNumber: 2, SerialNumber: "2033029402"}
+	ret, err = server.ProvisionOnt(ctx, message)
+	if err != nil {
+		switch err.(type) {
+		case *abstract.UnprovisonedPortError:
+			fmt.Printf("ProvisionOnt failed as it should with %v\n", err)
+		default:
+			t.Fatalf("ProvsionOnt test failed with %v\n", err)
+		}
+	} else {
+		t.Fatalf("ProvsionOnt should have failed but didn't")
+	}
+	fmt.Printf("ProvisionOnt success %v\n", ret)
+
+}