VOL-2180 code changes for context addition
Integrating InterContainerProxy interface changes

Change-Id: Ia20c5ac3093b7845acf80cce801ec0c1d90c125f
diff --git a/adaptercore/device_handler.go b/adaptercore/device_handler.go
index b541978..10b0dbf 100644
--- a/adaptercore/device_handler.go
+++ b/adaptercore/device_handler.go
@@ -276,9 +276,9 @@
 }
 
 // readIndications to read the indications from the OLT device
-func (dh *DeviceHandler) readIndications() {
+func (dh *DeviceHandler) readIndications(ctx context.Context) {
 	defer log.Errorw("Indications ended", log.Fields{})
-	indications, err := dh.Client.EnableIndication(context.Background(), new(oop.Empty))
+	indications, err := dh.Client.EnableIndication(ctx, new(oop.Empty))
 	if err != nil {
 		log.Errorw("Failed to read indications", log.Fields{"err": err})
 		return
@@ -288,7 +288,7 @@
 		return
 	}
 	/* get device state */
-	device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
+	device, err := dh.coreProxy.GetDevice(ctx, dh.device.Id, dh.device.Id)
 	if err != nil || device == nil {
 		/*TODO: needs to handle error scenarios */
 		log.Errorw("Failed to fetch device info", log.Fields{"err": err})
@@ -323,7 +323,7 @@
 				indicationBackoff.Reset()
 			}
 			time.Sleep(indicationBackoff.NextBackOff())
-			indications, err = dh.Client.EnableIndication(context.Background(), new(oop.Empty))
+			indications, err = dh.Client.EnableIndication(ctx, new(oop.Empty))
 			if err != nil {
 				log.Errorw("Failed to read indications", log.Fields{"err": err})
 				return
@@ -336,8 +336,8 @@
 				log.Debug("Device deleted stoping the read indication thread")
 				break
 			}
-			dh.transitionMap.Handle(DeviceDownInd)
-			dh.transitionMap.Handle(DeviceInit)
+			dh.transitionMap.Handle(ctx, DeviceDownInd)
+			dh.transitionMap.Handle(ctx, DeviceInit)
 			break
 		}
 		// Reset backoff if we have a successful receive
@@ -351,27 +351,27 @@
 			log.Infow("olt is admin down, ignore indication", log.Fields{"indication": indication})
 			continue
 		}
-		dh.handleIndication(indication)
+		dh.handleIndication(ctx, indication)
 
 	}
 }
 
-func (dh *DeviceHandler) handleOltIndication(oltIndication *oop.OltIndication) {
+func (dh *DeviceHandler) handleOltIndication(ctx context.Context, oltIndication *oop.OltIndication) {
 	raisedTs := time.Now().UnixNano()
 	if oltIndication.OperState == "up" && dh.transitionMap.currentDeviceState != deviceStateUp {
-		dh.transitionMap.Handle(DeviceUpInd)
+		dh.transitionMap.Handle(ctx, DeviceUpInd)
 	} else if oltIndication.OperState == "down" {
-		dh.transitionMap.Handle(DeviceDownInd)
+		dh.transitionMap.Handle(ctx, DeviceDownInd)
 	}
 	// Send or clear Alarm
 	dh.eventMgr.oltUpDownIndication(oltIndication, dh.deviceID, raisedTs)
 }
 
-func (dh *DeviceHandler) handleIndication(indication *oop.Indication) {
+func (dh *DeviceHandler) handleIndication(ctx context.Context, indication *oop.Indication) {
 	raisedTs := time.Now().UnixNano()
 	switch indication.Data.(type) {
 	case *oop.Indication_OltInd:
-		dh.handleOltIndication(indication.GetOltInd())
+		dh.handleOltIndication(ctx, indication.GetOltInd())
 	case *oop.Indication_IntfInd:
 		intfInd := indication.GetIntfInd()
 		go dh.addPort(intfInd.GetIntfId(), voltha.Port_PON_OLT, intfInd.GetOperState())
@@ -380,7 +380,7 @@
 		intfOperInd := indication.GetIntfOperInd()
 		if intfOperInd.GetType() == "nni" {
 			go dh.addPort(intfOperInd.GetIntfId(), voltha.Port_ETHERNET_NNI, intfOperInd.GetOperState())
-			dh.resourceMgr.AddNNIToKVStore(intfOperInd.GetIntfId())
+			dh.resourceMgr.AddNNIToKVStore(ctx, intfOperInd.GetIntfId())
 		} else if intfOperInd.GetType() == "pon" {
 			// TODO: Check what needs to be handled here for When PON PORT down, ONU will be down
 			// Handle pon port update
@@ -392,7 +392,7 @@
 		onuDiscInd := indication.GetOnuDiscInd()
 		log.Infow("Received Onu discovery indication ", log.Fields{"OnuDiscInd": onuDiscInd})
 		sn := dh.stringifySerialNumber(onuDiscInd.SerialNumber)
-		go dh.onuDiscIndication(onuDiscInd, sn)
+		go dh.onuDiscIndication(ctx, onuDiscInd, sn)
 	case *oop.Indication_OnuInd:
 		onuInd := indication.GetOnuInd()
 		log.Infow("Received Onu indication ", log.Fields{"OnuInd": onuInd})
@@ -404,7 +404,7 @@
 	case *oop.Indication_PktInd:
 		pktInd := indication.GetPktInd()
 		log.Infow("Received pakcet indication ", log.Fields{"PktInd": pktInd})
-		go dh.handlePacketIndication(pktInd)
+		go dh.handlePacketIndication(ctx, pktInd)
 	case *oop.Indication_PortStats:
 		portStats := indication.GetPortStats()
 		go dh.portStats.PortStatisticsIndication(portStats, dh.resourceMgr.DevInfo.GetPonPorts())
@@ -419,9 +419,9 @@
 }
 
 // doStateUp handle the olt up indication and update to voltha core
-func (dh *DeviceHandler) doStateUp() error {
+func (dh *DeviceHandler) doStateUp(ctx context.Context) error {
 	// Synchronous call to update device state - this method is run in its own go routine
-	if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
+	if err := dh.coreProxy.DeviceStateUpdate(ctx, dh.device.Id, voltha.ConnectStatus_REACHABLE,
 		voltha.OperStatus_ACTIVE); err != nil {
 		log.Errorw("Failed to update device with OLT UP indication", log.Fields{"deviceID": dh.device.Id, "error": err})
 		return err
@@ -430,12 +430,12 @@
 }
 
 // doStateDown handle the olt down indication
-func (dh *DeviceHandler) doStateDown() error {
+func (dh *DeviceHandler) doStateDown(ctx context.Context) error {
 	dh.lockDevice.Lock()
 	defer dh.lockDevice.Unlock()
 	log.Debug("do-state-down-start")
 
-	device, err := dh.coreProxy.GetDevice(context.TODO(), dh.device.Id, dh.device.Id)
+	device, err := dh.coreProxy.GetDevice(ctx, 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})
@@ -444,7 +444,7 @@
 
 	cloned := proto.Clone(device).(*voltha.Device)
 	// Update the all ports state on that device to disable
-	if er := dh.coreProxy.PortsStateUpdate(context.TODO(), cloned.Id, voltha.OperStatus_UNKNOWN); er != nil {
+	if er := dh.coreProxy.PortsStateUpdate(ctx, cloned.Id, voltha.OperStatus_UNKNOWN); er != nil {
 		log.Errorw("updating-ports-failed", log.Fields{"deviceID": device.Id, "error": er})
 		return er
 	}
@@ -454,13 +454,13 @@
 	cloned.ConnectStatus = common.ConnectStatus_UNREACHABLE
 	dh.device = cloned
 
-	if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
+	if er := dh.coreProxy.DeviceStateUpdate(ctx, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
 		log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": er})
 		return er
 	}
 
 	//get the child device for the parent device
-	onuDevices, err := dh.coreProxy.GetChildDevices(context.TODO(), dh.device.Id)
+	onuDevices, err := dh.coreProxy.GetChildDevices(ctx, dh.device.Id)
 	if err != nil {
 		log.Errorw("failed to get child devices information", log.Fields{"deviceID": dh.device.Id, "error": err})
 		return err
@@ -470,7 +470,7 @@
 		// Update onu state as down in onu adapter
 		onuInd := oop.OnuIndication{}
 		onuInd.OperState = "down"
-		er := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
+		er := dh.AdapterProxy.SendInterAdapterMessage(ctx, &onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
 			"openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
 		if er != nil {
 			log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
@@ -486,7 +486,7 @@
 }
 
 // doStateInit dial the grpc before going to init state
-func (dh *DeviceHandler) doStateInit() error {
+func (dh *DeviceHandler) doStateInit(ctx context.Context) error {
 	var err error
 	dh.clientCon, err = grpc.Dial(dh.device.GetHostAndPort(), grpc.WithInsecure(), grpc.WithBlock())
 	if err != nil {
@@ -497,20 +497,20 @@
 }
 
 // postInit create olt client instance to invoke RPC on the olt device
-func (dh *DeviceHandler) postInit() error {
+func (dh *DeviceHandler) postInit(ctx context.Context) error {
 	dh.Client = oop.NewOpenoltClient(dh.clientCon)
-	dh.transitionMap.Handle(GrpcConnected)
+	dh.transitionMap.Handle(ctx, GrpcConnected)
 	return nil
 }
 
 // doStateConnected get the device info and update to voltha core
-func (dh *DeviceHandler) doStateConnected() error {
+func (dh *DeviceHandler) doStateConnected(ctx context.Context) 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(context.TODO(), dh.device.Id, dh.device.Id)
+		device, err := dh.coreProxy.GetDevice(ctx, 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})
@@ -520,18 +520,18 @@
 		cloned.ConnectStatus = voltha.ConnectStatus_REACHABLE
 		cloned.OperStatus = voltha.OperStatus_UNKNOWN
 		dh.device = cloned
-		if er := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
+		if er := dh.coreProxy.DeviceStateUpdate(ctx, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); er != nil {
 			log.Errorw("error-updating-device-state", log.Fields{"deviceID": dh.device.Id, "error": er})
 		}
 
 		// Since the device was disabled before the OLT was rebooted, enforce the OLT to be Disabled after re-connection.
-		_, err = dh.Client.DisableOlt(context.Background(), new(oop.Empty))
+		_, err = dh.Client.DisableOlt(ctx, new(oop.Empty))
 		if err != nil {
 			log.Errorw("Failed to disable olt ", log.Fields{"err": err})
 		}
 
 		// Start reading indications
-		go dh.readIndications()
+		go dh.readIndications(ctx)
 		return nil
 	}
 
@@ -555,12 +555,12 @@
 
 	KVStoreHostPort := fmt.Sprintf("%s:%d", dh.openOLT.KVStoreHost, dh.openOLT.KVStorePort)
 	// Instantiate resource manager
-	if dh.resourceMgr = rsrcMgr.NewResourceMgr(dh.deviceID, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
+	if dh.resourceMgr = rsrcMgr.NewResourceMgr(ctx, dh.deviceID, KVStoreHostPort, dh.openOLT.KVStoreType, dh.deviceType, deviceInfo); dh.resourceMgr == nil {
 		log.Error("Error while instantiating resource manager")
 		return errors.New("instantiating resource manager failed")
 	}
 	// Instantiate flow manager
-	if dh.flowMgr = NewFlowManager(dh, dh.resourceMgr); dh.flowMgr == nil {
+	if dh.flowMgr = NewFlowManager(ctx, dh, dh.resourceMgr); dh.flowMgr == nil {
 		log.Error("Error while instantiating flow manager")
 		return errors.New("instantiating flow manager failed")
 	}
@@ -571,7 +571,7 @@
 	dh.portStats = NewOpenOltStatsMgr(dh)
 
 	// Start reading indications
-	go dh.readIndications()
+	go dh.readIndications(ctx)
 	return nil
 }
 
@@ -656,10 +656,10 @@
 }
 
 //AdoptDevice adopts the OLT device
-func (dh *DeviceHandler) AdoptDevice(device *voltha.Device) {
+func (dh *DeviceHandler) AdoptDevice(ctx context.Context, device *voltha.Device) {
 	dh.transitionMap = NewTransitionMap(dh)
 	log.Infow("Adopt_device", log.Fields{"deviceID": device.Id, "Address": device.GetHostAndPort()})
-	dh.transitionMap.Handle(DeviceInit)
+	dh.transitionMap.Handle(ctx, DeviceInit)
 
 	// Now, set the initial PM configuration for that device
 	if err := dh.coreProxy.DevicePMConfigUpdate(nil, dh.metrics.ToPmConfigs()); err != nil {
@@ -840,13 +840,13 @@
 	log.Debugw("Sent Omci message", log.Fields{"intfID": intfID, "onuID": onuID, "omciMsg": hex.EncodeToString(omciMsg.Message)})
 }
 
-func (dh *DeviceHandler) activateONU(intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) {
+func (dh *DeviceHandler) activateONU(ctx context.Context, intfID uint32, onuID int64, serialNum *oop.SerialNumber, serialNumber string) {
 	log.Debugw("activate-onu", log.Fields{"intfID": intfID, "onuID": onuID, "serialNum": serialNum, "serialNumber": serialNumber})
-	dh.flowMgr.UpdateOnuInfo(intfID, uint32(onuID), serialNumber)
+	dh.flowMgr.UpdateOnuInfo(ctx, intfID, uint32(onuID), serialNumber)
 	// TODO: need resource manager
 	var pir uint32 = 1000000
 	Onu := oop.Onu{IntfId: intfID, OnuId: uint32(onuID), SerialNumber: serialNum, Pir: pir}
-	if _, err := dh.Client.ActivateOnu(context.Background(), &Onu); err != nil {
+	if _, err := dh.Client.ActivateOnu(ctx, &Onu); err != nil {
 		st, _ := status.FromError(err)
 		if st.Code() == codes.AlreadyExists {
 			log.Debug("ONU activation is in progress", log.Fields{"SerialNumber": serialNumber})
@@ -858,7 +858,7 @@
 	}
 }
 
-func (dh *DeviceHandler) onuDiscIndication(onuDiscInd *oop.OnuDiscIndication, sn string) {
+func (dh *DeviceHandler) onuDiscIndication(ctx context.Context, onuDiscInd *oop.OnuDiscIndication, sn string) {
 	channelID := onuDiscInd.GetIntfId()
 	parentPortNo := IntfIDToPortNo(onuDiscInd.GetIntfId(), voltha.Port_PON_OLT)
 
@@ -880,19 +880,19 @@
 		return
 	}
 
-	onuDevice, err := dh.coreProxy.GetChildDevice(context.TODO(), dh.device.Id, kwargs)
+	onuDevice, err := dh.coreProxy.GetChildDevice(ctx, dh.device.Id, kwargs)
 	var onuID uint32
 	if onuDevice == nil || err != nil {
 		//This is the first time ONU discovered. Create an OnuID for it.
 		ponintfid := onuDiscInd.GetIntfId()
 		dh.lockDevice.Lock()
-		onuID, err = dh.resourceMgr.GetONUID(ponintfid)
+		onuID, err = dh.resourceMgr.GetONUID(ctx, ponintfid)
 		dh.lockDevice.Unlock()
 		if err != nil {
 			log.Errorw("failed to fetch onuID from resource manager", log.Fields{"pon-intf-id": ponintfid, "err": err})
 			return
 		}
-		if onuDevice, err = dh.coreProxy.ChildDeviceDetected(context.TODO(), dh.device.Id, int(parentPortNo),
+		if onuDevice, err = dh.coreProxy.ChildDeviceDetected(ctx, dh.device.Id, int(parentPortNo),
 			"", int(channelID),
 			string(onuDiscInd.SerialNumber.GetVendorId()), sn, int64(onuID)); onuDevice == nil {
 			log.Errorw("Create onu error",
@@ -916,7 +916,7 @@
 	dh.onus.Store(onuKey, onuDev)
 	log.Debugw("new-onu-device-discovered", log.Fields{"onu": onuDev})
 
-	err = dh.coreProxy.DeviceStateUpdate(context.TODO(), onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
+	err = dh.coreProxy.DeviceStateUpdate(ctx, onuDevice.Id, common.ConnectStatus_REACHABLE, common.OperStatus_DISCOVERED)
 	if err != nil {
 		log.Errorw("failed to update device state", log.Fields{"DeviceID": onuDevice.Id, "err": err})
 		return
@@ -926,7 +926,7 @@
 	//In onuIndication the operStatus of device is checked. If it is still not updated in KV store
 	//then the initialisation fails.
 	time.Sleep(1 * time.Second)
-	dh.activateONU(onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn)
+	dh.activateONU(ctx, onuDiscInd.IntfId, int64(onuID), onuDiscInd.SerialNumber, sn)
 	return
 }
 
@@ -982,13 +982,14 @@
 }
 
 func (dh *DeviceHandler) updateOnuStates(onuDevice *voltha.Device, onuInd *oop.OnuIndication, foundInCache bool) {
+	ctx := context.TODO()
 	log.Debugw("onu-indication-for-state", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState})
 	dh.updateOnuAdminState(onuInd)
 	// operState
 	if onuInd.OperState == "down" {
 		log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState})
 		// TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
-		err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
+		err := dh.AdapterProxy.SendInterAdapterMessage(ctx, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
 			"openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
 		if err != nil {
 			log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
@@ -1002,7 +1003,7 @@
 		}
 		log.Debugw("sending-interadapter-onu-indication", log.Fields{"onuIndication": onuInd, "DeviceId": onuDevice.Id, "operStatus": onuDevice.OperStatus, "adminStatus": onuDevice.AdminState})
 		// TODO NEW CORE do not hardcode adapter name. Handler needs Adapter reference
-		err := dh.AdapterProxy.SendInterAdapterMessage(context.TODO(), onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
+		err := dh.AdapterProxy.SendInterAdapterMessage(ctx, onuInd, ic.InterAdapterMessageType_ONU_IND_REQUEST,
 			"openolt", onuDevice.Type, onuDevice.Id, onuDevice.ProxyAddress.DeviceId, "")
 		if err != nil {
 			log.Errorw("Failed to send inter-adapter-message", log.Fields{"OnuInd": onuInd,
@@ -1103,17 +1104,17 @@
 }
 
 //UpdateFlowsIncrementally updates the device flow
-func (dh *DeviceHandler) UpdateFlowsIncrementally(device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error {
+func (dh *DeviceHandler) UpdateFlowsIncrementally(ctx context.Context, device *voltha.Device, flows *of.FlowChanges, groups *of.FlowGroupChanges, flowMetadata *voltha.FlowMetadata) error {
 	log.Debugw("Received-incremental-flowupdate-in-device-handler", log.Fields{"deviceID": device.Id, "flows": flows, "groups": groups, "flowMetadata": flowMetadata})
 	if flows != nil {
 		for _, flow := range flows.ToRemove.Items {
 			log.Debug("Removing flow", log.Fields{"deviceId": device.Id, "flowToRemove": flow})
-			dh.flowMgr.RemoveFlow(flow)
+			dh.flowMgr.RemoveFlow(ctx, flow)
 		}
 
 		for _, flow := range flows.ToAdd.Items {
 			log.Debug("Adding flow", log.Fields{"deviceId": device.Id, "flowToAdd": flow})
-			dh.flowMgr.AddFlow(flow, flowMetadata)
+			dh.flowMgr.AddFlow(ctx, flow, flowMetadata)
 		}
 	}
 	if groups != nil && flows != nil {
@@ -1125,10 +1126,10 @@
 
 	if groups != nil {
 		for _, group := range groups.ToAdd.Items {
-			dh.flowMgr.AddGroup(group)
+			dh.flowMgr.AddGroup(ctx, group)
 		}
 		for _, group := range groups.ToUpdate.Items {
-			dh.flowMgr.ModifyGroup(group)
+			dh.flowMgr.ModifyGroup(ctx, group)
 		}
 		if len(groups.ToRemove.Items) != 0 {
 			log.Debug("Group delete operation is not supported for now")
@@ -1245,45 +1246,45 @@
 	return nil
 }
 
-func (dh *DeviceHandler) clearUNIData(onu *rsrcMgr.OnuGemInfo) error {
+func (dh *DeviceHandler) clearUNIData(ctx context.Context, onu *rsrcMgr.OnuGemInfo) error {
 	var uniID uint32
 	var err error
 	for _, port := range onu.UniPorts {
 		uniID = UniIDFromPortNum(uint32(port))
 		log.Debugw("clearing-resource-data-for-uni-port", log.Fields{"port": port, "uniID": uniID})
 		/* Delete tech-profile instance from the KV store */
-		if err = dh.flowMgr.DeleteTechProfileInstances(onu.IntfID, onu.OnuID, uniID, onu.SerialNumber); err != nil {
+		if err = dh.flowMgr.DeleteTechProfileInstances(ctx, onu.IntfID, onu.OnuID, uniID, onu.SerialNumber); err != nil {
 			log.Debugw("Failed-to-remove-tech-profile-instance-for-onu", log.Fields{"onu-id": onu.OnuID})
 		}
 		log.Debugw("Deleted-tech-profile-instance-for-onu", log.Fields{"onu-id": onu.OnuID})
-		flowIDs := dh.resourceMgr.GetCurrentFlowIDsForOnu(onu.IntfID, int32(onu.OnuID), int32(uniID))
+		flowIDs := dh.resourceMgr.GetCurrentFlowIDsForOnu(ctx, onu.IntfID, int32(onu.OnuID), int32(uniID))
 		for _, flowID := range flowIDs {
-			dh.resourceMgr.FreeFlowID(onu.IntfID, int32(onu.OnuID), int32(uniID), flowID)
+			dh.resourceMgr.FreeFlowID(ctx, onu.IntfID, int32(onu.OnuID), int32(uniID), flowID)
 		}
-		tpIDList := dh.resourceMgr.GetTechProfileIDForOnu(onu.IntfID, onu.OnuID, uniID)
+		tpIDList := dh.resourceMgr.GetTechProfileIDForOnu(ctx, onu.IntfID, onu.OnuID, uniID)
 		for _, tpID := range tpIDList {
-			if err = dh.resourceMgr.RemoveMeterIDForOnu("upstream", onu.IntfID, onu.OnuID, uniID, tpID); err != nil {
+			if err = dh.resourceMgr.RemoveMeterIDForOnu(ctx, "upstream", onu.IntfID, onu.OnuID, uniID, tpID); err != nil {
 				log.Debugw("Failed-to-remove-meter-id-for-onu-upstream", log.Fields{"onu-id": onu.OnuID})
 			}
 			log.Debugw("Removed-meter-id-for-onu-upstream", log.Fields{"onu-id": onu.OnuID})
-			if err = dh.resourceMgr.RemoveMeterIDForOnu("downstream", onu.IntfID, onu.OnuID, uniID, tpID); err != nil {
+			if err = dh.resourceMgr.RemoveMeterIDForOnu(ctx, "downstream", onu.IntfID, onu.OnuID, uniID, tpID); err != nil {
 				log.Debugw("Failed-to-remove-meter-id-for-onu-downstream", log.Fields{"onu-id": onu.OnuID})
 			}
 			log.Debugw("Removed-meter-id-for-onu-downstream", log.Fields{"onu-id": onu.OnuID})
 		}
-		dh.resourceMgr.FreePONResourcesForONU(onu.IntfID, onu.OnuID, uniID)
-		if err = dh.resourceMgr.RemoveTechProfileIDsForOnu(onu.IntfID, onu.OnuID, uniID); err != nil {
+		dh.resourceMgr.FreePONResourcesForONU(ctx, onu.IntfID, onu.OnuID, uniID)
+		if err = dh.resourceMgr.RemoveTechProfileIDsForOnu(ctx, onu.IntfID, onu.OnuID, uniID); err != nil {
 			log.Debugw("Failed-to-remove-tech-profile-id-for-onu", log.Fields{"onu-id": onu.OnuID})
 		}
 		log.Debugw("Removed-tech-profile-id-for-onu", log.Fields{"onu-id": onu.OnuID})
-		if err = dh.resourceMgr.DelGemPortPktIn(onu.IntfID, onu.OnuID, uint32(port)); err != nil {
+		if err = dh.resourceMgr.DelGemPortPktIn(ctx, onu.IntfID, onu.OnuID, uint32(port)); err != nil {
 			log.Debugw("Failed-to-remove-gemport-pkt-in", log.Fields{"intfid": onu.IntfID, "onuid": onu.OnuID, "uniId": uniID})
 		}
 	}
 	return nil
 }
 
-func (dh *DeviceHandler) clearNNIData() error {
+func (dh *DeviceHandler) clearNNIData(ctx context.Context) error {
 	nniUniID := -1
 	nniOnuID := -1
 
@@ -1291,21 +1292,21 @@
 		return fmt.Errorf("no resource manager for deviceID %s", dh.deviceID)
 	}
 	//Free the flow-ids for the NNI port
-	nni, err := dh.resourceMgr.GetNNIFromKVStore()
+	nni, err := dh.resourceMgr.GetNNIFromKVStore(ctx)
 	if err != nil {
 		log.Error("Failed to fetch nni from kv store")
 		return err
 	}
 	log.Debugw("NNI are ", log.Fields{"nni": nni})
 	for _, nniIntfID := range nni {
-		flowIDs := dh.resourceMgr.GetCurrentFlowIDsForOnu(uint32(nniIntfID), int32(nniOnuID), int32(nniUniID))
+		flowIDs := dh.resourceMgr.GetCurrentFlowIDsForOnu(ctx, uint32(nniIntfID), int32(nniOnuID), int32(nniUniID))
 		log.Debugw("Current flow ids for nni", log.Fields{"flow-ids": flowIDs})
 		for _, flowID := range flowIDs {
-			dh.resourceMgr.FreeFlowID(uint32(nniIntfID), -1, -1, uint32(flowID))
+			dh.resourceMgr.FreeFlowID(ctx, uint32(nniIntfID), -1, -1, uint32(flowID))
 		}
-		dh.resourceMgr.RemoveResourceMap(nniIntfID, int32(nniOnuID), int32(nniUniID))
+		dh.resourceMgr.RemoveResourceMap(ctx, nniIntfID, int32(nniOnuID), int32(nniUniID))
 	}
-	if err = dh.resourceMgr.DelNNiFromKVStore(); err != nil {
+	if err = dh.resourceMgr.DelNNiFromKVStore(ctx); err != nil {
 		log.Error("Failed to clear nni from kv store")
 		return err
 	}
@@ -1313,7 +1314,7 @@
 }
 
 // DeleteDevice deletes the device instance from openolt handler array.  Also clears allocated resource manager resources.  Also reboots the OLT hardware!
-func (dh *DeviceHandler) DeleteDevice(device *voltha.Device) error {
+func (dh *DeviceHandler) DeleteDevice(ctx context.Context, device *voltha.Device) error {
 	log.Debug("Function entry delete device")
 	dh.lockDevice.Lock()
 	if dh.adminState == "deleted" {
@@ -1331,7 +1332,7 @@
 		var ponPort uint32
 		for ponPort = 0; ponPort < noOfPonPorts; ponPort++ {
 			var onuGemData []rsrcMgr.OnuGemInfo
-			err := dh.resourceMgr.ResourceMgrs[ponPort].GetOnuGemInfo(ponPort, &onuGemData)
+			err := dh.resourceMgr.ResourceMgrs[ponPort].GetOnuGemInfo(ctx, ponPort, &onuGemData)
 			if err != nil {
 				log.Errorw("Failed to get onu info for port ", log.Fields{"ponport": ponPort})
 				return err
@@ -1339,19 +1340,19 @@
 			for _, onu := range onuGemData {
 				onuID := make([]uint32, 1)
 				log.Debugw("onu data ", log.Fields{"onu": onu})
-				if err = dh.clearUNIData(&onu); err != nil {
+				if err = dh.clearUNIData(ctx, &onu); err != nil {
 					log.Errorw("Failed to clear data for onu", log.Fields{"onu-device": onu})
 				}
 				// Clear flowids for gem cache.
 				for _, gem := range onu.GemPorts {
-					dh.resourceMgr.DeleteFlowIDsForGem(ponPort, gem)
+					dh.resourceMgr.DeleteFlowIDsForGem(ctx, ponPort, gem)
 				}
 				onuID[0] = onu.OnuID
-				dh.resourceMgr.FreeonuID(ponPort, onuID)
+				dh.resourceMgr.FreeonuID(ctx, ponPort, onuID)
 			}
-			dh.resourceMgr.DeleteIntfIDGempMapPath(ponPort)
+			dh.resourceMgr.DeleteIntfIDGempMapPath(ctx, ponPort)
 			onuGemData = nil
-			err = dh.resourceMgr.DelOnuGemInfoForIntf(ponPort)
+			err = dh.resourceMgr.DelOnuGemInfoForIntf(ctx, ponPort)
 			if err != nil {
 				log.Errorw("Failed to update onugem info", log.Fields{"intfid": ponPort, "onugeminfo": onuGemData})
 			}
@@ -1359,12 +1360,12 @@
 		/* Clear the flows from KV store associated with NNI port.
 		   There are mostly trap rules from NNI port (like LLDP)
 		*/
-		if err := dh.clearNNIData(); err != nil {
+		if err := dh.clearNNIData(ctx); err != nil {
 			log.Errorw("Failed to clear data for NNI port", log.Fields{"device-id": dh.deviceID})
 		}
 
 		/* Clear the resource pool for each PON port in the background */
-		go dh.resourceMgr.Delete()
+		go dh.resourceMgr.Delete(ctx)
 	}
 
 	/*Delete ONU map for the device*/
@@ -1380,7 +1381,7 @@
 	dh.stopHeartbeatCheck <- true
 	//Reset the state
 	if dh.Client != nil {
-		if _, err := dh.Client.Reboot(context.Background(), new(oop.Empty)); err != nil {
+		if _, err := dh.Client.Reboot(ctx, new(oop.Empty)); err != nil {
 			log.Errorw("Failed-to-reboot-olt ", log.Fields{"deviceID": dh.deviceID, "err": err})
 			return err
 		}
@@ -1388,7 +1389,7 @@
 	cloned := proto.Clone(device).(*voltha.Device)
 	cloned.OperStatus = voltha.OperStatus_UNKNOWN
 	cloned.ConnectStatus = voltha.ConnectStatus_UNREACHABLE
-	if err := dh.coreProxy.DeviceStateUpdate(context.TODO(), cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
+	if err := dh.coreProxy.DeviceStateUpdate(ctx, cloned.Id, cloned.ConnectStatus, cloned.OperStatus); err != nil {
 		log.Errorw("error-updating-device-state", log.Fields{"deviceID": device.Id, "error": err})
 		return err
 	}
@@ -1407,12 +1408,12 @@
 	return nil
 }
 
-func (dh *DeviceHandler) handlePacketIndication(packetIn *oop.PacketIndication) {
+func (dh *DeviceHandler) handlePacketIndication(ctx context.Context, packetIn *oop.PacketIndication) {
 	log.Debugw("Received packet-in", log.Fields{
 		"packet-indication": *packetIn,
 		"packet":            hex.EncodeToString(packetIn.Pkt),
 	})
-	logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(packetIn)
+	logicalPortNum, err := dh.flowMgr.GetLogicalPortFromPacketIn(ctx, packetIn)
 	if err != nil {
 		log.Errorw("Error getting logical port from packet-in", log.Fields{
 			"error":  err,
@@ -1437,7 +1438,7 @@
 }
 
 // PacketOut sends packet-out from VOLTHA to OLT on the egress port provided
-func (dh *DeviceHandler) PacketOut(egressPortNo int, packet *of.OfpPacketOut) error {
+func (dh *DeviceHandler) PacketOut(ctx context.Context, egressPortNo int, packet *of.OfpPacketOut) error {
 	log.Debugw("incoming-packet-out", log.Fields{
 		"deviceID":       dh.deviceID,
 		"egress_port_no": egressPortNo,
@@ -1469,7 +1470,7 @@
 		onuID := OnuIDFromPortNum(uint32(egressPortNo))
 		uniID := UniIDFromPortNum(uint32(egressPortNo))
 
-		gemPortID, err := dh.flowMgr.GetPacketOutGemPortID(intfID, onuID, uint32(egressPortNo))
+		gemPortID, err := dh.flowMgr.GetPacketOutGemPortID(ctx, intfID, onuID, uint32(egressPortNo))
 		if err != nil {
 			// In this case the openolt agent will receive the gemPortID as 0.
 			// The agent tries to retrieve the gemPortID in this case.
@@ -1490,7 +1491,7 @@
 			"packet":         hex.EncodeToString(packet.Data),
 		})
 
-		if _, err := dh.Client.OnuPacketOut(context.Background(), &onuPkt); err != nil {
+		if _, err := dh.Client.OnuPacketOut(ctx, &onuPkt); err != nil {
 			log.Errorw("Error while sending packet-out to ONU", log.Fields{
 				"error":  err,
 				"packet": hex.EncodeToString(packet.Data),
@@ -1505,7 +1506,7 @@
 			"packet":     hex.EncodeToString(packet.Data),
 		})
 
-		if _, err := dh.Client.UplinkPacketOut(context.Background(), &uplinkPkt); err != nil {
+		if _, err := dh.Client.UplinkPacketOut(ctx, &uplinkPkt); err != nil {
 			log.Errorw("Error while sending packet-out to NNI", log.Fields{
 				"error":  err,
 				"packet": hex.EncodeToString(packet.Data),
@@ -1549,7 +1550,7 @@
 
 						log.Debug("We got hearbeat after the timeout expired, changing the states")
 						go dh.notifyChildDevices("up")
-						if err := dh.coreProxy.DeviceStateUpdate(context.Background(), dh.device.Id, voltha.ConnectStatus_REACHABLE,
+						if err := dh.coreProxy.DeviceStateUpdate(ctx, dh.device.Id, voltha.ConnectStatus_REACHABLE,
 							voltha.OperStatus_ACTIVE); err != nil {
 							log.Errorw("Failed to update device state", log.Fields{"deviceID": dh.device.Id, "error": err})
 						}
@@ -1588,6 +1589,7 @@
 }
 
 func (dh *DeviceHandler) invokeDisableorEnablePort(port *voltha.Port, enablePort bool) error {
+	ctx := context.Background()
 	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.
@@ -1601,7 +1603,7 @@
 	var operStatus voltha.OperStatus_Types
 	if enablePort {
 		operStatus = voltha.OperStatus_ACTIVE
-		out, err := dh.Client.EnablePonIf(context.Background(), ponIntf)
+		out, err := dh.Client.EnablePonIf(ctx, ponIntf)
 
 		if err != nil {
 			log.Errorw("error-while-enable-Pon-port", log.Fields{"DeviceID": dh.device, "Port": port, "error": err})
@@ -1612,7 +1614,7 @@
 		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)
+		out, err := dh.Client.DisablePonIf(ctx, ponIntf)
 		if err != nil {
 			log.Errorw("error-while-disabling-interface", log.Fields{"DeviceID": dh.device, "Port": port})
 			return err
@@ -1621,7 +1623,7 @@
 		dh.activePorts.Store(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 {
+	if errs := dh.coreProxy.PortStateUpdate(ctx, 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
 	}