VOL-1549: Soft Reboot Edgecore asfvolt16 OLT when no ONUs are connected to it

Change-Id: Ifd3c8d1b7fd4b1df19e55c2568115a385a0da166
diff --git a/adaptercore/device_handler.go b/adaptercore/device_handler.go
index f397d21..44d9eee 100644
--- a/adaptercore/device_handler.go
+++ b/adaptercore/device_handler.go
@@ -344,6 +344,35 @@
 // doStateConnected get the device info and update to voltha core
 func (dh *DeviceHandler) doStateConnected() error {
 	log.Debug("OLT device has been connected")
+
+	// Case where OLT is disabled and then rebooted.
+	if dh.adminState == "down" {
+		log.Debugln("do-state-connected--device-admin-state-down")
+		device, err := dh.coreProxy.GetDevice(nil, dh.device.Id, dh.device.Id)
+		if err != nil || device == nil {
+			/*TODO: needs to handle error scenarios */
+			log.Errorw("Failed to fetch device device", log.Fields{"err": err})
+		}
+
+		cloned := proto.Clone(device).(*voltha.Device)
+		cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
+		cloned.OperStatus = voltha.OperStatus_UNKNOWN
+		dh.device = cloned
+		if err := dh.coreProxy.DeviceStateUpdate(nil, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
+			log.Errorw("error-updating-device-state", log.Fields{"deviceId": dh.device.Id, "error": err})
+		}
+
+		// Since the device was disabled before the OLT was rebooted, enfore the OLT to be Disabled after re-connection.
+		_, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
+		if err != nil {
+			log.Errorw("Failed to disable olt ", log.Fields{"err": err})
+		}
+
+		// Start reading indications
+		go dh.readIndications()
+		return nil
+	}
+
 	deviceInfo, err := dh.Client.GetDeviceInfo(context.Background(), new(oop.Empty))
 	if err != nil {
 		log.Errorw("Failed to fetch device info", log.Fields{"err": err})
@@ -357,7 +386,6 @@
 	dh.device.Root = true
 	dh.device.Vendor = deviceInfo.Vendor
 	dh.device.Model = deviceInfo.Model
-	dh.device.ConnectStatus = voltha.ConnectStatus_REACHABLE
 	dh.device.SerialNumber = deviceInfo.DeviceSerialNumber
 	dh.device.HardwareVersion = deviceInfo.HardwareVersion
 	dh.device.FirmwareVersion = deviceInfo.FirmwareVersion
@@ -776,6 +804,17 @@
 	return nil
 }
 
+func (dh *DeviceHandler) RebootDevice(device *voltha.Device) error {
+	if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
+		log.Errorw("Failed to reboot olt ", log.Fields{"err": err})
+		return err
+	}
+
+	log.Debugw("rebooted-device-successfully", log.Fields{"deviceId": device.Id})
+
+	return nil
+}
+
 func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
 	log.Debugw("Received packet-in", log.Fields{"packet-indication": *packetIn})
 	logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
diff --git a/adaptercore/openolt.go b/adaptercore/openolt.go
index 594ceb3..81639be 100644
--- a/adaptercore/openolt.go
+++ b/adaptercore/openolt.go
@@ -203,7 +203,13 @@
 }
 
 func (oo *OpenOLT) Reboot_device(device *voltha.Device) error {
-	return errors.New("UnImplemented")
+	log.Infow("reboot-device", log.Fields{"deviceId": device.Id})
+	if handler := oo.getDeviceHandler(device.Id); handler != nil {
+		return handler.RebootDevice(device)
+	}
+	log.Errorw("device-handler-not-set", log.Fields{"deviceId": device.Id})
+	return errors.New("device-handler-not-found")
+
 }
 
 func (oo *OpenOLT) Self_test_device(device *voltha.Device) error {