VOL-2463 : Adding enable/disable PON port.
2. Raising and clearing alarm on disable and enable PON port.
3. Avoiding statistics collection for disabled pon.

Change-Id: I794d002a9fbf906cfb6aacbee0c6fea758617a61
diff --git a/adaptercore/device_handler.go b/adaptercore/device_handler.go
index 3def7bc..2fe1175 100644
--- a/adaptercore/device_handler.go
+++ b/adaptercore/device_handler.go
@@ -53,10 +53,6 @@
 	MaxTimeOutInMs = 500
 )
 
-func init() {
-	_, _ = log.AddPackage(log.JSON, log.DebugLevel, nil)
-}
-
 //DeviceHandler will interact with the OLT device.
 type DeviceHandler struct {
 	deviceID      string
@@ -82,6 +78,7 @@
 	metrics            *pmmetrics.PmMetrics
 	stopCollector      chan bool
 	stopHeartbeatCheck chan bool
+	activePorts        map[uint32]bool
 }
 
 //OnuDevice represents ONU related info
@@ -136,6 +133,7 @@
 	dh.stopCollector = make(chan bool, 2)
 	dh.stopHeartbeatCheck = make(chan bool, 2)
 	dh.metrics = pmmetrics.NewPmMetrics(cloned.Id, pmmetrics.Frequency(150), pmmetrics.FrequencyOverride(false), pmmetrics.Grouped(false), pmmetrics.Metrics(pmNames))
+	dh.activePorts = make(map[uint32]bool)
 	//TODO initialize the support classes.
 	return &dh
 }
@@ -231,8 +229,11 @@
 	var operStatus common.OperStatus_Types
 	if state == "up" {
 		operStatus = voltha.OperStatus_ACTIVE
+		//populating the intfStatus map
+		dh.activePorts[intfID] = true
 	} else {
 		operStatus = voltha.OperStatus_DISCOVERED
+		dh.activePorts[intfID] = false
 	}
 	portNum := IntfIDToPortNo(intfID, portType)
 	label := GetportLabel(portNum, portType)
@@ -384,6 +385,7 @@
 			// TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
 			// Handle pon port update
 			go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_PON_OLT, intfOperInd.GetOperState())
+			go dh.eventMgr.oltIntfOperIndication(indication.GetIntfOperInd(), dh.deviceID, raisedTs)
 		}
 		log.Infow("Received interface oper indication ", log.Fields{"InterfaceOperInd": intfOperInd})
 	case *oop.Indication_OnuDiscInd:
@@ -545,11 +547,9 @@
 		log.Errorw("Failed to fetch device device", log.Fields{"err": err})
 		return err
 	}
-	cloned := proto.Clone(device).(*voltha.Device)
-	// Update the all ports (if available) on that device to ACTIVE.
-	// The ports do not normally exist, unless the device is coming back from a reboot
-	if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
-		log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
+	dh.populateActivePorts(device)
+	if err := dh.updatePortAdminState(device); err != nil {
+		log.Errorw("Error-on-updating-port-status", log.Fields{"device": device})
 		return err
 	}
 
@@ -642,7 +642,7 @@
 			log.Debugf("Publish-NNI-Metrics")
 			// PON Stats
 			NumPonPORTS := dh.resourceMgr.DevInfo.GetPonPorts()
-			for i := uint32(0); i < NumPonPORTS; i++ {
+			for i := uint32(0); i < NumPonPORTS && (dh.activePorts[i]); i++ {
 				cmpon := dh.portStats.collectPONMetrics(i)
 				log.Debugf("Collect-PON-Metrics %v", cmpon)
 
@@ -1224,11 +1224,11 @@
 
 	cloned := proto.Clone(device).(*voltha.Device)
 	// Update the all ports state on that device to enable
-	if err := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_ACTIVE); err != nil {
-		log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": err})
+
+	if err := dh.updatePortAdminState(device); err != nil {
+		log.Errorw("Error-on-updating-port-status-after-reenabling-olt", log.Fields{"device": device})
 		return err
 	}
-
 	//Update the device oper status as ACTIVE
 	cloned.OperStatus = voltha.OperStatus_ACTIVE
 	dh.device = cloned
@@ -1237,6 +1237,7 @@
 		log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err})
 		return err
 	}
+
 	log.Debugw("ReEnableDevice-end", log.Fields{"deviceID": device.Id})
 
 	return nil
@@ -1571,3 +1572,93 @@
 		return
 	}
 }
+
+// EnablePort to enable Pon interface
+func (dh *DeviceHandler) EnablePort(port *voltha.Port) error {
+	log.Debugw("enable-port", log.Fields{"Device": dh.device, "port": port})
+	return dh.invokeDisableorEnablePort(port, true)
+}
+
+// DisablePort to disable pon interface
+func (dh *DeviceHandler) DisablePort(port *voltha.Port) error {
+	log.Debugw("disable-port", log.Fields{"Device": dh.device, "port": port})
+	return dh.invokeDisableorEnablePort(port, false)
+}
+
+func (dh *DeviceHandler) invokeDisableorEnablePort(port *voltha.Port, enablePort bool) error {
+	log.Infow("invokeDisableorEnablePort", log.Fields{"port": port, "Enable": enablePort})
+	if port.GetType() == voltha.Port_ETHERNET_NNI {
+		// Bug is opened for VOL-2505 to support NNI disable feature.
+		log.Infow("voltha-supports-single-nni-hence-disable-of-nni-not-allowed",
+			log.Fields{"Device": dh.device, "port": port})
+		return fmt.Errorf("received-disable-enable-nni-port-request, received-port %s", port.GetType())
+	}
+	// fetch interfaceid from PortNo
+	ponID := PortNoToIntfID(port.GetPortNo(), voltha.Port_PON_OLT)
+	ponIntf := &oop.Interface{IntfId: ponID}
+	var operStatus voltha.OperStatus_Types
+	if enablePort {
+		operStatus = voltha.OperStatus_ACTIVE
+		out, err := dh.Client.EnablePonIf(context.Background(), ponIntf)
+
+		if err != nil {
+			log.Errorw("error-while-enable-Pon-port", log.Fields{"DeviceID": dh.device, "Port": port, "error": err})
+			return err
+		}
+		// updating interface local cache for collecting stats
+		dh.activePorts[ponID] = true
+		log.Infow("enabled-pon-port", log.Fields{"out": out, "DeviceID": dh.device, "Port": port})
+	} else {
+		operStatus = voltha.OperStatus_UNKNOWN
+		out, err := dh.Client.DisablePonIf(context.Background(), ponIntf)
+		if err != nil {
+			log.Errorw("error-while-disabling-interface", log.Fields{"DeviceID": dh.device, "Port": port})
+			return err
+		}
+		// updating interface local cache for collecting stats
+		dh.activePorts[ponID] = false
+		log.Infow("disabled-pon-port", log.Fields{"out": out, "DeviceID": dh.device, "Port": port})
+	}
+	if errs := dh.coreProxy.PortStateUpdate(context.Background(), dh.deviceID, voltha.Port_PON_OLT, port.PortNo, operStatus); errs != nil {
+		log.Errorw("portstate-update-failed", log.Fields{"Device": dh.deviceID, "port": port.PortNo, "error": errs})
+		return errs
+	}
+	return nil
+}
+
+//updatePortAdminState update the ports on reboot and re-enable device.
+func (dh *DeviceHandler) updatePortAdminState(device *voltha.Device) error {
+	cloned := proto.Clone(device).(*voltha.Device)
+	// Disable the port and update the oper_port_status to core
+	// if the Admin state of the port is disabled on reboot and re-enable device.
+	for _, port := range cloned.Ports {
+		if port.AdminState == common.AdminState_DISABLED {
+			if err := dh.invokeDisableorEnablePort(port, false); err != nil {
+				log.Errorw("error-occurred-while-disabling-port", log.Fields{"DeviceId": dh.deviceID, "port": port, "error": err})
+				return err
+			}
+		}
+	}
+	return nil
+}
+
+//populateActivePorts to populate activePorts map
+func (dh *DeviceHandler) populateActivePorts(device *voltha.Device) {
+	log.Info("populateActiveports", log.Fields{"Device": device})
+	for _, port := range device.Ports {
+		if port.Type == voltha.Port_ETHERNET_NNI {
+			if port.OperStatus == voltha.OperStatus_ACTIVE {
+				dh.activePorts[PortNoToIntfID(port.PortNo, voltha.Port_ETHERNET_NNI)] = true
+			} else {
+				dh.activePorts[PortNoToIntfID(port.PortNo, voltha.Port_ETHERNET_NNI)] = false
+			}
+		}
+		if port.Type == voltha.Port_PON_OLT {
+			if port.OperStatus == voltha.OperStatus_ACTIVE {
+				dh.activePorts[PortNoToIntfID(port.PortNo, voltha.Port_PON_OLT)] = true
+			} else {
+				dh.activePorts[PortNoToIntfID(port.PortNo, voltha.Port_PON_OLT)] = false
+			}
+		}
+	}
+}