[VOL-5291] - On demand onu stats from olt

Change-Id: I65e7b5a8bd93ec862726a7302dcc18be16855b4f
Signed-off-by: Akash Reddy Kankanala <akash.kankanala@radisys.com>
diff --git a/internal/pkg/core/openolt.go b/internal/pkg/core/openolt.go
index 44ac313..aa63387 100644
--- a/internal/pkg/core/openolt.go
+++ b/internal/pkg/core/openolt.go
@@ -406,6 +406,8 @@
 // GetSingleValue handles get uni status on ONU and ondemand metric on OLT
 func (oo *OpenOLT) GetSingleValue(ctx context.Context, request *extension.SingleGetValueRequest) (*extension.SingleGetValueResponse, error) {
 	logger.Infow(ctx, "single_get_value_request", log.Fields{"request": request})
+	var handler *DeviceHandler
+	var onuDevice *voltha.Device
 
 	errResp := func(status extension.GetValueResponse_Status,
 		reason extension.GetValueResponse_ErrorReason) *extension.SingleGetValueResponse {
@@ -416,7 +418,15 @@
 			},
 		}
 	}
-	if handler := oo.getDeviceHandler(request.TargetId); handler != nil {
+
+	switch request.GetRequest().GetRequest().(type) {
+	case *extension.GetValueRequest_OnuStatsFromOlt:
+		handler, onuDevice = oo.GetDeviceHandlerFromChild(ctx, request.TargetId)
+	default:
+		handler = oo.getDeviceHandler(request.TargetId)
+	}
+
+	if handler != nil {
 		switch reqType := request.GetRequest().GetRequest().(type) {
 		case *extension.GetValueRequest_OltPortInfo:
 			return handler.getOltPortCounters(ctx, reqType.OltPortInfo), nil
@@ -428,6 +438,8 @@
 			return handler.getPONRxPower(ctx, reqType.OltRxPower), nil
 		case *extension.GetValueRequest_OffloadedAppsStats:
 			return handler.getOltOffloadStats(ctx, reqType.OffloadedAppsStats), nil
+		case *extension.GetValueRequest_OnuStatsFromOlt:
+			return handler.getOnuStatsFromOlt(ctx, reqType.OnuStatsFromOlt, onuDevice), nil
 		default:
 			return errResp(extension.GetValueResponse_ERROR, extension.GetValueResponse_UNSUPPORTED), nil
 		}
@@ -465,6 +477,23 @@
 	return errResp(extension.SetValueResponse_ERROR, extension.SetValueResponse_INVALID_DEVICE_ID), nil
 }
 
+func (oo *OpenOLT) GetDeviceHandlerFromChild(ctx context.Context, deviceId string) (*DeviceHandler, *voltha.Device) {
+	oo.lockDeviceHandlersMap.Lock()
+	defer oo.lockDeviceHandlersMap.Unlock()
+
+	for parentId, handler := range oo.deviceHandlers {
+		devices, _ := handler.getChildDevicesFromCore(ctx, parentId)
+		if devices != nil {
+			for _, onuDevice := range devices.Items {
+				if onuDevice.Id == deviceId {
+					return handler, onuDevice
+				}
+			}
+		}
+	}
+	return nil, nil
+}
+
 /*
  *  OLT Inter-adapter service
  */