seba-321 implemented reflow logic

Change-Id: I4a67a38104dbcb9bbfa830b38fc4fc21c42c2dbd
diff --git a/api/abstract_olt_api.proto b/api/abstract_olt_api.proto
index 0a031bb..7a8cb69 100644
--- a/api/abstract_olt_api.proto
+++ b/api/abstract_olt_api.proto
@@ -103,6 +103,12 @@
 message DeleteOntReturn{
    bool Success=1;
 }
+message ReflowMessage{
+
+}
+message ReflowReturn{
+    bool Success=1;
+}
 message OutputMessage{
    string Something=1;
 }
@@ -152,10 +158,17 @@
 	body:"*"
       };
    }
+   rpc Reflow(ReflowMessage)returns (ReflowReturn){
+       option(google.api.http)={
+           post:"/v1/Reflow"
+           body:"*"
+
+       };
+   }
    rpc Output(OutputMessage)returns(OutputReturn){
       option(google.api.http)={
         post:"/v1/Output"
-	body:"*"
+	    body:"*"
       };
    }
 }
diff --git a/api/handler.go b/api/handler.go
index 593afed..ee70d74 100644
--- a/api/handler.go
+++ b/api/handler.go
@@ -19,21 +19,10 @@
 import (
 	"errors"
 	"fmt"
-	"io/ioutil"
-	"log"
 	"net"
-	"net/http"
-	"os"
-	"strings"
 	"sync"
 
-	"gerrit.opencord.org/abstract-olt/internal/pkg/settings"
-	"gerrit.opencord.org/abstract-olt/models"
-	"gerrit.opencord.org/abstract-olt/models/abstract"
-	"gerrit.opencord.org/abstract-olt/models/physical"
-	"github.com/mongodb/mongo-go-driver/bson"
-	"github.com/mongodb/mongo-go-driver/mongo"
-	"github.com/mongodb/mongo-go-driver/mongo/updateopt"
+	"gerrit.opencord.org/abstract-olt/internal/pkg/impl"
 	context "golang.org/x/net/context"
 )
 
@@ -77,11 +66,6 @@
 CreateChassis - allocates a new Chassis struct and stores it in chassisMap
 */
 func (s *Server) CreateChassis(ctx context.Context, in *AddChassisMessage) (*AddChassisReturn, error) {
-	myChan := getSyncChannel()
-	fmt.Println("after getSyncChannel")
-	<-myChan
-	defer done(myChan, true)
-	chassisMap := models.GetChassisMap()
 	clli := in.GetCLLI()
 
 	xosIP := net.ParseIP(in.GetXOSIP())
@@ -90,70 +74,33 @@
 		errStr := fmt.Sprintf("Invalid IP %s supplied for XOSIP", in.GetXOSIP())
 		return nil, errors.New(errStr)
 	}
-
 	xosAddress := net.TCPAddr{IP: xosIP, Port: xosPort}
-
 	xosUser := in.GetXOSUser()
 	xosPassword := in.GetXOSPassword()
 	if xosUser == "" || xosPassword == "" {
 		return nil, errors.New("Either XOSUser or XOSPassword supplied were empty")
 	}
-	loginWorked := testLogin(xosUser, xosPassword, xosIP, xosPort)
-	if !loginWorked {
-		return nil, errors.New("Unable to validate login not creating Abstract Chassis")
-	}
 	shelf := int(in.GetShelf())
 	rack := int(in.GetRack())
-
-	chassisHolder := (*chassisMap)[clli]
-	if chassisHolder != nil {
-		return &AddChassisReturn{DeviceID: chassisHolder.AbstractChassis.CLLI}, nil
+	deviceID, err := impl.CreateChassis(clli, xosAddress, xosUser, xosPassword, shelf, rack)
+	if err != nil {
+		return nil, err
 	}
-
-	abstractChassis := abstract.GenerateChassis(clli, int(in.GetRack()), int(in.GetShelf()))
-	phyChassis := physical.Chassis{CLLI: clli, XOSUser: xosUser, XOSPassword: xosPassword, XOSAddress: xosAddress, Rack: rack, Shelf: shelf}
-
-	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)
-	}
-	(*chassisMap)[clli] = chassisHolder
-	isDirty = true
-	return &AddChassisReturn{DeviceID: clli}, nil
+	return &AddChassisReturn{DeviceID: deviceID}, nil
 }
 
 /*
 ChangeXOSUserPassword - allows update of xos credentials
 */
 func (s *Server) ChangeXOSUserPassword(ctx context.Context, in *ChangeXOSUserPasswordMessage) (*ChangeXOSUserPasswordReturn, error) {
-	myChan := getSyncChannel()
-	<-myChan
-	defer done(myChan, true)
 	clli := in.GetCLLI()
 	xosUser := in.GetXOSUser()
 	xosPassword := in.GetXOSPassword()
 	if xosUser == "" || xosPassword == "" {
 		return nil, errors.New("Either XOSUser or XOSPassword supplied were empty")
 	}
-	chassisMap := models.GetChassisMap()
-	chassisHolder := (*chassisMap)[clli]
-	if chassisHolder == nil {
-		errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
-		return nil, errors.New(errString)
-	}
-	xosIP := chassisHolder.PhysicalChassis.XOSAddress.IP
-	xosPort := chassisHolder.PhysicalChassis.XOSAddress.Port
-	loginWorked := testLogin(xosUser, xosPassword, xosIP, xosPort)
-	if !loginWorked {
-		return nil, errors.New("Unable to validate login when changing password")
-	}
-
-	chassisHolder.PhysicalChassis.XOSUser = xosUser
-	chassisHolder.PhysicalChassis.XOSPassword = xosPassword
-	isDirty = true
-	return &ChangeXOSUserPasswordReturn{Success: true}, nil
+	success, err := impl.ChangeXOSUserPassword(clli, xosUser, xosPassword)
+	return &ChangeXOSUserPasswordReturn{Success: success}, err
 
 }
 
@@ -161,201 +108,64 @@
 CreateOLTChassis adds an OLT chassis/line card to the Physical chassis
 */
 func (s *Server) CreateOLTChassis(ctx context.Context, in *AddOLTChassisMessage) (*AddOLTChassisReturn, error) {
-	myChan := getSyncChannel()
-	<-myChan
-	defer done(myChan, true)
-	fmt.Printf(" CreateOLTChassis %v \n", *in)
-	chassisMap := models.GetChassisMap()
 	clli := in.GetCLLI()
-	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()
+	oltType := in.GetType().String()
 	address := net.TCPAddr{IP: net.ParseIP(in.GetSlotIP()), Port: int(in.GetSlotPort())}
-	physicalChassis := &chassisHolder.PhysicalChassis
-	sOlt := physical.SimpleOLT{CLLI: clli, Hostname: in.GetHostname(), Address: address, Parent: physicalChassis}
-	switch oltType {
-	case AddOLTChassisMessage_edgecore:
-		sOlt.CreateEdgecore()
-	case AddOLTChassisMessage_adtran:
-	case AddOLTChassisMessage_tibit:
-	}
-	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)
-	}
-	isDirty = true
-	return &AddOLTChassisReturn{DeviceID: in.GetHostname(), ChassisDeviceID: clli}, nil
-
+	hostname := in.GetHostname()
+	clli, err := impl.CreateOLTChassis(clli, oltType, address, hostname)
+	return &AddOLTChassisReturn{DeviceID: hostname, ChassisDeviceID: clli}, err
 }
 
 /*
 ProvisionOnt provisions an ONT on a specific Chassis/LineCard/Port
 */
 func (s *Server) ProvisionOnt(ctx context.Context, in *AddOntMessage) (*AddOntReturn, error) {
-	myChan := getSyncChannel()
-	<-myChan
-	defer done(myChan, true)
-	chassisMap := models.GetChassisMap()
 	clli := in.GetCLLI()
-	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 := chassisHolder.AbstractChassis.ActivateONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
-
-	if err != nil {
-		return nil, err
-	}
-	isDirty = true
-	return &AddOntReturn{Success: true}, nil
+	slotNumber := int(in.GetSlotNumber())
+	portNumber := int(in.GetPortNumber())
+	ontNumber := int(in.GetOntNumber())
+	serialNumber := in.GetSerialNumber()
+	success, err := impl.ProvisionOnt(clli, slotNumber, portNumber, ontNumber, serialNumber)
+	return &AddOntReturn{Success: success}, err
 }
-func (s *Server) ProvisionOntFull(ctx context.Context, in *AddOntFullMessage) (*AddOntReturn, error) {
-	myChan := getSyncChannel()
-	<-myChan
-	defer done(myChan, true)
-	chassisMap := models.GetChassisMap()
-	clli := in.GetCLLI()
-	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 := chassisHolder.AbstractChassis.ActivateONTFull(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber(),
-		in.GetCTag(), in.GetSTag(), in.GetNasPortID(), in.GetCircuitID())
 
-	if err != nil {
-		return nil, err
-	}
-	isDirty = true
-	return &AddOntReturn{Success: true}, nil
+/*
+ProvsionOntFull - provisions ont using sTag,cTag,NasPortID, and CircuitID passed in
+*/
+func (s *Server) ProvisionOntFull(ctx context.Context, in *AddOntFullMessage) (*AddOntReturn, error) {
+	clli := in.GetCLLI()
+	slotNumber := int(in.GetSlotNumber())
+	portNumber := int(in.GetPortNumber())
+	ontNumber := int(in.GetOntNumber())
+	serialNumber := in.GetSerialNumber()
+	cTag := in.GetCTag()
+	sTag := in.GetSTag()
+	nasPortID := in.GetNasPortID()
+	circuitID := in.GetCircuitID()
+	success, err := impl.ProvisionOntFull(clli, slotNumber, portNumber, ontNumber, serialNumber, cTag, sTag, nasPortID, circuitID)
+	return &AddOntReturn{Success: success}, err
 }
 
 /*
 DeleteOnt - deletes a previously provision ont
 */
 func (s *Server) DeleteOnt(ctx context.Context, in *DeleteOntMessage) (*DeleteOntReturn, error) {
-	myChan := getSyncChannel()
-	<-myChan
-	defer done(myChan, true)
-	chassisMap := models.GetChassisMap()
 	clli := in.GetCLLI()
-	chassisHolder := (*chassisMap)[clli]
-	err := chassisHolder.AbstractChassis.DeleteONT(int(in.GetSlotNumber()), int(in.GetPortNumber()), int(in.GetOntNumber()), in.GetSerialNumber())
-	if err != nil {
-		return nil, err
-	}
-	isDirty = true
-	return &DeleteOntReturn{Success: true}, nil
+	slotNumber := int(in.GetSlotNumber())
+	portNumber := int(in.GetPortNumber())
+	ontNumber := int(in.GetOntNumber())
+	serialNumber := in.GetSerialNumber()
+	success, err := impl.DeleteOnt(clli, slotNumber, portNumber, ontNumber, serialNumber)
+	return &DeleteOntReturn{Success: success}, err
 }
 
+func (s *Server) Reflow(ctx context.Context, in *ReflowMessage) (*ReflowReturn, error) {
+	success, err := impl.Reflow()
+	return &ReflowReturn{Success: success}, err
+
+}
 func (s *Server) Output(ctx context.Context, in *OutputMessage) (*OutputReturn, error) {
-	return DoOutput()
-
-}
-func DoOutput() (*OutputReturn, error) {
-	if isDirty {
-		myChan := getSyncChannel()
-		<-myChan
-		defer done(myChan, true)
-		chassisMap := models.GetChassisMap()
-		if settings.GetMongo() {
-			client, err := mongo.NewClient(settings.GetMongodb())
-			client.Connect(context.Background())
-			if err != nil {
-				log.Printf("client connect to mongo db @%s failed with %v\n", settings.GetMongodb(), err)
-			}
-			defer client.Disconnect(context.Background())
-			for clli, chassisHolder := range *chassisMap {
-				json, _ := (chassisHolder).Serialize()
-				collection := client.Database("AbstractOLT").Collection("backup")
-				doc := bson.NewDocument(bson.EC.String("_id", clli))
-				filter := bson.NewDocument(bson.EC.String("_id", clli))
-				doc.Append(bson.EC.Binary("body", json))
-
-				updateDoc := bson.NewDocument(bson.EC.SubDocument("$set", doc))
-				//update or insert if not existent
-				res, err := collection.UpdateOne(context.Background(), filter, updateDoc, updateopt.Upsert(true))
-				if err != nil {
-					log.Printf("collection.UpdateOne failed with %v\n", err)
-				} else {
-					id := res.UpsertedID
-					if settings.GetDebug() {
-						log.Printf("Update Succeeded with id %v\n", id)
-					}
-				}
-			}
-		} else {
-			for clli, chassisHolder := range *chassisMap {
-
-				json, _ := (chassisHolder).Serialize()
-				if settings.GetMongo() {
-
-				} else {
-					//TODO parameterize dump location
-					backupFile := fmt.Sprintf("backup/%s", clli)
-					f, _ := os.Create(backupFile)
-
-					defer f.Close()
-
-					_, _ = f.WriteString(string(json))
-					f.Sync()
-				}
-			}
-		}
-		isDirty = false
-	} else {
-		if settings.GetDebug() {
-			log.Print("Not dirty not dumping config")
-		}
-
-	}
-	return &OutputReturn{Success: true}, nil
-
-}
-func testLogin(xosUser string, xosPassword string, xosIP net.IP, xosPort int) bool {
-	if settings.GetDummy() {
-		return true
-	}
-	var dummyYaml = `
-tosca_definitions_version: tosca_simple_yaml_1_0
-imports:
-  - custom_types/site.yaml
-description: anything
-topology_template:
-  node_templates:
-    mysite:
-      type: tosca.nodes.Site
-      properties:
-        must-exist: true
-        name: mysite
-`
-	client := &http.Client{}
-	requestList := fmt.Sprintf("http://%s:%d/run", xosIP, xosPort)
-	req, err := http.NewRequest("POST", requestList, strings.NewReader(dummyYaml))
-	req.Header.Add("xos-username", xosUser)
-	req.Header.Add("xos-password", xosPassword)
-	resp, err := client.Do(req)
-	log.Printf("testLogin resp:%v", resp)
-	if err != nil {
-		log.Printf("Unable to validate XOS Login Information %v", err)
-		return false
-	}
-	defer resp.Body.Close()
-
-	if resp.StatusCode == http.StatusOK {
-		bodyBytes, _ := ioutil.ReadAll(resp.Body)
-		bodyString := string(bodyBytes)
-		fmt.Println(bodyString)
-		return true
-	}
-	return false
+	success, err := impl.DoOutput()
+	return &OutputReturn{Success: success}, err
 
 }
diff --git a/client/main.go b/client/main.go
index f4b7826..abecae7 100644
--- a/client/main.go
+++ b/client/main.go
@@ -39,6 +39,7 @@
 	provOntFull := flag.Bool("f", false, "provsionOntFull?")
 	deleteOnt := flag.Bool("d", false, "deleteOnt")
 	output := flag.Bool("output", false, "dump output")
+	reflow := flag.Bool("reflow", false, "reflow provisioning tosca")
 	/* END COMMAND FLAGS */
 
 	/* CREATE CHASSIS FLAGS */
@@ -94,7 +95,7 @@
 		}
 	}
 
-	cmdFlags := []*bool{echo, addOlt, update, create, provOnt, provOntFull, deleteOnt, output}
+	cmdFlags := []*bool{echo, addOlt, update, create, provOnt, provOntFull, deleteOnt, output, reflow}
 	cmdCount := 0
 	for _, flag := range cmdFlags {
 		if *flag {
@@ -160,6 +161,8 @@
 		Output(c)
 	} else if *deleteOnt {
 		deleteONT(c, clli, slot, port, ont, serial)
+	} else if *reflow {
+		reflowTosca(c)
 	}
 
 }
@@ -312,6 +315,16 @@
 	log.Printf("Response from server: %t", res.GetSuccess())
 	return nil
 }
+func reflowTosca(c api.AbstractOLTClient) error {
+	res, err := c.Reflow(context.Background(), &api.ReflowMessage{})
+	if err != nil {
+		debug.PrintStack()
+		fmt.Printf("Error when calling Reflow %s", err)
+		return err
+	}
+	log.Printf("Response from server: %t", res.GetSuccess())
+	return nil
+}
 
 func usage() {
 	var output = `
@@ -378,6 +391,8 @@
 	 e.g. ./client -server=localhost:7777 -d -clli=MY_CLLI -slot=1 -port=1 -ont=22 -serial=aer900jasdf
     -output (TEMPORARY) causes AbstractOLT to serialize all chassis to JSON file in $WorkingDirectory/backups
          e.g. ./client -server=localhost:7777 -output
+    -reflow causes tosca to be repushed to xos
+	e.g. ./client -server=localhost:7777 -reflow
 	 `
 
 	fmt.Println(output)
diff --git a/cmd/AbstractOLT/AbstractOLT.go b/cmd/AbstractOLT/AbstractOLT.go
index 3e50828..e94a218 100644
--- a/cmd/AbstractOLT/AbstractOLT.go
+++ b/cmd/AbstractOLT/AbstractOLT.go
@@ -28,6 +28,7 @@
 	"time"
 
 	"gerrit.opencord.org/abstract-olt/api"
+	"gerrit.opencord.org/abstract-olt/internal/pkg/impl"
 	"gerrit.opencord.org/abstract-olt/internal/pkg/settings"
 	"gerrit.opencord.org/abstract-olt/models"
 	"github.com/grpc-ecosystem/grpc-gateway/runtime"
@@ -315,7 +316,7 @@
 	for {
 		select {
 		case <-ticker.C:
-			api.DoOutput()
+			impl.DoOutput()
 		}
 	}
 
diff --git a/internal/pkg/impl/abstractChassis.go b/internal/pkg/impl/abstractChassis.go
new file mode 100644
index 0000000..a2e05b3
--- /dev/null
+++ b/internal/pkg/impl/abstractChassis.go
@@ -0,0 +1,63 @@
+/*
+ Copyright 2017 the original author or authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+package impl
+
+import (
+	"errors"
+	"fmt"
+	"log"
+	"net"
+	"strings"
+
+	"gerrit.opencord.org/abstract-olt/internal/pkg/settings"
+	"gerrit.opencord.org/abstract-olt/models"
+	"gerrit.opencord.org/abstract-olt/models/abstract"
+	"gerrit.opencord.org/abstract-olt/models/physical"
+)
+
+/*
+CreateChassis - allocates a new Chassis struct and stores it in chassisMap
+*/
+func CreateChassis(clli string, xosAddress net.TCPAddr, xosUser string, xosPassword string, shelf int, rack int) (string, error) {
+	myChan := getSyncChannel()
+	<-myChan
+	defer done(myChan, true)
+	chassisMap := models.GetChassisMap()
+
+	loginWorked := testLogin(xosUser, xosPassword, xosAddress.IP, xosAddress.Port)
+	if !loginWorked {
+		return "", errors.New("Unable to validate login not creating Abstract Chassis")
+	}
+
+	chassisHolder := (*chassisMap)[clli]
+	if chassisHolder != nil {
+		errMsg := fmt.Sprintf("AbstractChassis %s already exists", clli)
+		return "", errors.New(errMsg)
+	}
+
+	abstractChassis := abstract.GenerateChassis(clli, rack, shelf)
+	phyChassis := physical.Chassis{CLLI: clli, XOSUser: xosUser, XOSPassword: xosPassword, XOSAddress: xosAddress, Rack: rack, Shelf: shelf}
+
+	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)
+	}
+	(*chassisMap)[clli] = chassisHolder
+	isDirty = true
+	return clli, nil
+}
diff --git a/internal/pkg/impl/echo.go b/internal/pkg/impl/echo.go
new file mode 100644
index 0000000..1727afe
--- /dev/null
+++ b/internal/pkg/impl/echo.go
@@ -0,0 +1,26 @@
+/*
+ Copyright 2017 the original author or authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+package impl
+
+/*
+Echo - Tester function which just returns same string sent to it
+*/
+func Echo(ping string) (string, error) {
+	myChan := getSyncChannel()
+	<-myChan
+	defer done(myChan, true)
+	return ping, nil
+}
diff --git a/internal/pkg/impl/olt.go b/internal/pkg/impl/olt.go
new file mode 100644
index 0000000..b878a9b
--- /dev/null
+++ b/internal/pkg/impl/olt.go
@@ -0,0 +1,60 @@
+/*
+ Copyright 2017 the original author or authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+package impl
+
+import (
+	"errors"
+	"fmt"
+	"net"
+
+	"gerrit.opencord.org/abstract-olt/models"
+	"gerrit.opencord.org/abstract-olt/models/physical"
+)
+
+/*
+CreateOLTChassis adds an OLT chassis/line card to the Physical chassis
+*/
+func CreateOLTChassis(clli string, oltType string, address net.TCPAddr, hostname string) (string, error) {
+	myChan := getSyncChannel()
+	<-myChan
+	defer done(myChan, true)
+	chassisMap := models.GetChassisMap()
+	chassisHolder := (*chassisMap)[clli]
+	if chassisHolder == nil {
+		errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
+		return "", errors.New(errString)
+	}
+	physicalChassis := &chassisHolder.PhysicalChassis
+	sOlt := physical.SimpleOLT{CLLI: clli, Hostname: hostname, Address: address, Parent: physicalChassis}
+	switch oltType {
+	case "edgecore":
+		sOlt.CreateEdgecore()
+	case "adtran":
+	case "tibit":
+	}
+	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)
+	}
+	isDirty = true
+	return clli, nil
+
+}
diff --git a/internal/pkg/impl/ont.go b/internal/pkg/impl/ont.go
new file mode 100644
index 0000000..932ee5e
--- /dev/null
+++ b/internal/pkg/impl/ont.go
@@ -0,0 +1,74 @@
+/*
+ Copyright 2017 the original author or authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+package impl
+
+import (
+	"errors"
+	"fmt"
+
+	"gerrit.opencord.org/abstract-olt/models"
+)
+
+/*
+ProvsionOnt - provisions ont using sTag,cTag,NasPortID, and CircuitID generated internally
+*/
+func ProvisionOnt(clli string, slotNumber int, portNumber int, ontNumber int, serialNumber string) (bool, error) {
+	myChan := getSyncChannel()
+	<-myChan
+	defer done(myChan, true)
+	chassisMap := models.GetChassisMap()
+	chassisHolder := (*chassisMap)[clli]
+	if chassisHolder == nil {
+		errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
+		return false, errors.New(errString)
+	}
+	err := chassisHolder.AbstractChassis.ActivateONT(slotNumber, portNumber, ontNumber, serialNumber)
+	isDirty = true
+	return true, err
+}
+
+/*
+ProvsionOntFull - provisions ont using sTag,cTag,NasPortID, and CircuitID passed in
+*/
+func ProvisionOntFull(clli string, slotNumber int, portNumber int, ontNumber int, serialNumber string, cTag uint32, sTag uint32, nasPortID string, circuitID string) (bool, error) {
+	myChan := getSyncChannel()
+	<-myChan
+	defer done(myChan, true)
+	chassisMap := models.GetChassisMap()
+	chassisHolder := (*chassisMap)[clli]
+	if chassisHolder == nil {
+		errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
+		return false, errors.New(errString)
+	}
+	err := chassisHolder.AbstractChassis.ActivateONTFull(slotNumber, portNumber, ontNumber, serialNumber, cTag, sTag, nasPortID, circuitID)
+	isDirty = true
+	return true, err
+}
+
+/*
+DeleteOnt - deletes a previously provision ont
+*/
+func DeleteOnt(clli string, slotNumber int, portNumber int, ontNumber int, serialNumber string) (bool, error) {
+	myChan := getSyncChannel()
+	<-myChan
+	defer done(myChan, true)
+	chassisMap := models.GetChassisMap()
+	chassisHolder := (*chassisMap)[clli]
+	err := chassisHolder.AbstractChassis.DeleteONT(slotNumber, portNumber, ontNumber, serialNumber)
+	isDirty = true
+	return true, err
+}
diff --git a/internal/pkg/impl/ouput.go b/internal/pkg/impl/ouput.go
new file mode 100644
index 0000000..aa2ea7e
--- /dev/null
+++ b/internal/pkg/impl/ouput.go
@@ -0,0 +1,93 @@
+/*
+ Copyright 2017 the original author or authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+package impl
+
+import (
+	"fmt"
+	"log"
+	"os"
+
+	"gerrit.opencord.org/abstract-olt/internal/pkg/settings"
+	"gerrit.opencord.org/abstract-olt/models"
+	"github.com/mongodb/mongo-go-driver/bson"
+	"github.com/mongodb/mongo-go-driver/mongo"
+	"github.com/mongodb/mongo-go-driver/mongo/updateopt"
+	context "golang.org/x/net/context"
+)
+
+/*
+DoOutput - creates a backup and stores it to disk/mongodb
+*/
+func DoOutput() (bool, error) {
+	if isDirty {
+		myChan := getSyncChannel()
+		<-myChan
+		defer done(myChan, true)
+		chassisMap := models.GetChassisMap()
+		if settings.GetMongo() {
+			client, err := mongo.NewClient(settings.GetMongodb())
+			client.Connect(context.Background())
+			if err != nil {
+				log.Printf("client connect to mongo db @%s failed with %v\n", settings.GetMongodb(), err)
+			}
+			defer client.Disconnect(context.Background())
+			for clli, chassisHolder := range *chassisMap {
+				json, _ := (chassisHolder).Serialize()
+				collection := client.Database("AbstractOLT").Collection("backup")
+				doc := bson.NewDocument(bson.EC.String("_id", clli))
+				filter := bson.NewDocument(bson.EC.String("_id", clli))
+				doc.Append(bson.EC.Binary("body", json))
+
+				updateDoc := bson.NewDocument(bson.EC.SubDocument("$set", doc))
+				//update or insert if not existent
+				res, err := collection.UpdateOne(context.Background(), filter, updateDoc, updateopt.Upsert(true))
+				if err != nil {
+					log.Printf("collection.UpdateOne failed with %v\n", err)
+				} else {
+					id := res.UpsertedID
+					if settings.GetDebug() {
+						log.Printf("Update Succeeded with id %v\n", id)
+					}
+				}
+			}
+		} else {
+			for clli, chassisHolder := range *chassisMap {
+
+				json, _ := (chassisHolder).Serialize()
+				if settings.GetMongo() {
+
+				} else {
+					//TODO parameterize dump location
+					backupFile := fmt.Sprintf("backup/%s", clli)
+					f, _ := os.Create(backupFile)
+
+					defer f.Close()
+
+					_, _ = f.WriteString(string(json))
+					f.Sync()
+				}
+			}
+		}
+		isDirty = false
+	} else {
+		if settings.GetDebug() {
+			log.Print("Not dirty not dumping config")
+		}
+
+	}
+	return true, nil
+
+}
diff --git a/internal/pkg/impl/reflow.go b/internal/pkg/impl/reflow.go
new file mode 100644
index 0000000..e178054
--- /dev/null
+++ b/internal/pkg/impl/reflow.go
@@ -0,0 +1,50 @@
+/*
+ Copyright 2017 the original author or authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+package impl
+
+import "gerrit.opencord.org/abstract-olt/models"
+
+/*
+Reflow - takes internal config and resends to xos
+*/
+func Reflow() (bool, error) {
+	myChan := getSyncChannel()
+	<-myChan
+	defer done(myChan, true)
+	chassisMap := models.GetChassisMap()
+	for _, chassisHolder := range *chassisMap {
+		physical := chassisHolder.PhysicalChassis
+		for index := range physical.Linecards {
+			olt := physical.Linecards[index]
+			physical.SendOltTosca(olt)
+			for portIndex := range olt.Ports {
+				port := olt.Ports[portIndex]
+				for ontIndex := range port.Onts {
+					ont := port.Onts[ontIndex]
+					if ont.Active {
+						physical.SendOntTosca(ont)
+						physical.SendSubscriberTosca(ont)
+
+					}
+
+				}
+
+			}
+		}
+	}
+	return true, nil
+	//TODO lots of this could throw errors
+}
diff --git a/internal/pkg/impl/sync.go b/internal/pkg/impl/sync.go
new file mode 100644
index 0000000..65f5c35
--- /dev/null
+++ b/internal/pkg/impl/sync.go
@@ -0,0 +1,35 @@
+/*
+ Copyright 2017 the original author or authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+package impl
+
+import "sync"
+
+var syncChan chan bool
+var isDirty bool
+
+var runOnce sync.Once
+
+func getSyncChannel() chan bool {
+	runOnce.Do(func() {
+		syncChan = make(chan bool, 1)
+		syncChan <- true
+	})
+
+	return syncChan
+}
+func done(myChan chan bool, done bool) {
+	myChan <- done
+}
diff --git a/internal/pkg/impl/xosuser.go b/internal/pkg/impl/xosuser.go
new file mode 100644
index 0000000..77a9367
--- /dev/null
+++ b/internal/pkg/impl/xosuser.go
@@ -0,0 +1,96 @@
+/*
+ Copyright 2017 the original author or authors.
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+package impl
+
+import (
+	"errors"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"net"
+	"net/http"
+	"strings"
+
+	"gerrit.opencord.org/abstract-olt/internal/pkg/settings"
+	"gerrit.opencord.org/abstract-olt/models"
+)
+
+/*
+ChangeXOSUserPassword - allows update of xos credentials
+*/
+func ChangeXOSUserPassword(clli string, xosUser string, xosPassword string) (bool, error) {
+	myChan := getSyncChannel()
+	<-myChan
+	defer done(myChan, true)
+	chassisMap := models.GetChassisMap()
+	chassisHolder := (*chassisMap)[clli]
+	if chassisHolder == nil {
+		errString := fmt.Sprintf("There is no chassis with CLLI of %s", clli)
+		return false, errors.New(errString)
+	}
+	xosIP := chassisHolder.PhysicalChassis.XOSAddress.IP
+	xosPort := chassisHolder.PhysicalChassis.XOSAddress.Port
+	loginWorked := testLogin(xosUser, xosPassword, xosIP, xosPort)
+	if !loginWorked {
+		return false, errors.New("Unable to validate login when changing password")
+	}
+
+	chassisHolder.PhysicalChassis.XOSUser = xosUser
+	chassisHolder.PhysicalChassis.XOSPassword = xosPassword
+	isDirty = true
+	return true, nil
+
+}
+
+func testLogin(xosUser string, xosPassword string, xosIP net.IP, xosPort int) bool {
+	if settings.GetDummy() {
+		return true
+	}
+	var dummyYaml = `
+tosca_definitions_version: tosca_simple_yaml_1_0
+imports:
+  - custom_types/site.yaml
+description: anything
+topology_template:
+  node_templates:
+    mysite:
+      type: tosca.nodes.Site
+      properties:
+        must-exist: true
+        name: mysite
+`
+	client := &http.Client{}
+	requestList := fmt.Sprintf("http://%s:%d/run", xosIP, xosPort)
+	req, err := http.NewRequest("POST", requestList, strings.NewReader(dummyYaml))
+	req.Header.Add("xos-username", xosUser)
+	req.Header.Add("xos-password", xosPassword)
+	resp, err := client.Do(req)
+	log.Printf("testLogin resp:%v", resp)
+	if err != nil {
+		log.Printf("Unable to validate XOS Login Information %v", err)
+		return false
+	}
+	defer resp.Body.Close()
+
+	if resp.StatusCode == http.StatusOK {
+		bodyBytes, _ := ioutil.ReadAll(resp.Body)
+		bodyString := string(bodyBytes)
+		fmt.Println(bodyString)
+		return true
+	}
+	return false
+
+}
diff --git a/models/physical/chassis.go b/models/physical/chassis.go
index 8f76a5e..59c87b7 100644
--- a/models/physical/chassis.go
+++ b/models/physical/chassis.go
@@ -60,8 +60,14 @@
 func (chassis *Chassis) AddOLTChassis(olt SimpleOLT) {
 	olt.SetNumber((len(chassis.Linecards) + 1))
 	chassis.Linecards = append(chassis.Linecards, olt)
-	//TODO - api call to add olt i.e. preprovision_olt
-	//S>103 func NewOltProvision(name string, deviceType string, host string, port int) OltProvsion {
+	chassis.SendOltTosca(olt)
+
+}
+
+/*
+SendOltTosca - Broke above method apart to support Reflow
+*/
+func (chassis *Chassis) SendOltTosca(olt SimpleOLT) {
 	ipString := olt.GetAddress().IP.String()
 	webServerPort := olt.GetAddress().Port
 	oltStruct := tosca.NewOltProvision(chassis.CLLI, olt.GetHostname(), "openolt", ipString, webServerPort)
@@ -82,15 +88,16 @@
 		// handle error
 	}
 	log.Printf("Server response was %v\n", resp)
-
 }
 func (chassis *Chassis) provisionONT(ont Ont) {
 	//TODO - api call to provison s/c vlans and ont serial number etc
 	log.Printf("chassis.provisionONT(%s,SVlan:%d,CVlan:%d)\n", ont.SerialNumber, ont.Svlan, ont.Cvlan)
+	chassis.SendOntTosca(ont)
+	chassis.SendSubscriberTosca(ont)
+}
+func (chassis *Chassis) SendOntTosca(ont Ont) {
 	ponPort := ont.Parent
 	slot := ponPort.Parent
-
-	//func NewOntProvision(serialNumber string, oltIP net.IP, ponPortNumber int) OntProvision {
 	ontStruct := tosca.NewOntProvision(ont.SerialNumber, slot.Address.IP, ponPort.Number)
 	yaml, _ := ontStruct.ToYaml()
 
@@ -110,22 +117,32 @@
 		// handle error
 	}
 	log.Printf("Response is %v\n", resp)
+}
+func (chassis *Chassis) SendSubscriberTosca(ont Ont) {
+	ponPort := ont.Parent
+	slot := ponPort.Parent
+	requestList := fmt.Sprintf("http://%s:%d/run", chassis.XOSAddress.IP.String(), chassis.XOSAddress.Port)
 	rgName := fmt.Sprintf("%s_%d_%d_%d_RG", chassis.CLLI, slot.Number, ponPort.Number, ont.Number)
 	subStruct := tosca.NewSubscriberProvision(rgName, ont.Cvlan, ont.Svlan, ont.SerialNumber, ont.NasPortID, ont.CircuitID, chassis.CLLI)
-	yaml, _ = subStruct.ToYaml()
-	log.Printf("yaml:%s\n", yaml)
-	req, err = http.NewRequest("POST", requestList, strings.NewReader(yaml))
-	req.Header.Add("xos-username", chassis.XOSUser)
-	req.Header.Add("xos-password", chassis.XOSPassword)
-	resp, err = client.Do(req)
-	if err != nil {
-		log.Printf("ERROR :) %v\n", err)
-		// handle error
+	yaml, _ := subStruct.ToYaml()
+	if settings.GetDummy() {
+		log.Printf("yaml:%s\n", yaml)
+		log.Println("YAML IS NOT BEING SET TO XOS")
+	} else {
+		req, err := http.NewRequest("POST", requestList, strings.NewReader(yaml))
+		req.Header.Add("xos-username", chassis.XOSUser)
+		req.Header.Add("xos-password", chassis.XOSPassword)
+		client := &http.Client{}
+		resp, err := client.Do(req)
+		if err != nil {
+			log.Printf("ERROR :) %v\n", err)
+			// handle error
+		}
+		log.Printf("Response is %v\n", resp)
+
 	}
 }
 func (chassis *Chassis) deleteONT(ont Ont) {
-	//TODO - api call to provison s/c vlans and ont serial number etc
-	//TODO - api call to provison s/c vlans and ont serial number etc
 	log.Printf("chassis.deleteONT(%s,SVlan:%d,CVlan:%d)\n", ont.SerialNumber, ont.Svlan, ont.Cvlan)
 	ponPort := ont.Parent
 	slot := ponPort.Parent