void DownlinkPacketScheduler::SelectFlowsToSchedule ()
{
#ifdef SCHEDULER_DEBUG
	std::cout << "\t Select Flows to schedule" << std::endl;
#endif

  ClearFlowsToSchedule ();

  RrcEntity *rrc = GetMacEntity ()->GetDevice ()->GetProtocolStack ()->GetRrcEntity ();
  RrcEntity::RadioBearersContainer* bearers = rrc->GetRadioBearerContainer ();


  for (std::vector<RadioBearer* >::iterator it = bearers->begin (); it != bearers->end (); it++)
	{
	  //SELECT FLOWS TO SCHEDULE
	  RadioBearer *bearer = (*it);
	  if (bearer->HasPackets () && bearer->GetDestination ()->GetNodeState () == NetworkNode::STATE_ACTIVE)
		{
		  //compute data to transmit
		  int dataToTransmit;
		  if (bearer->GetApplication ()->GetApplicationType () == Application::APPLICATION_TYPE_INFINITE_BUFFER)
			{
			  dataToTransmit = 100000000;
			}
		  else
			{
			  dataToTransmit = bearer->GetQueueSize ();
			}

		  //compute spectral efficiency
		  ENodeB *enb = (ENodeB*) GetMacEntity ()->GetDevice ();
		  ENodeB::UserEquipmentRecord *ueRecord = enb->GetUserEquipmentRecord (bearer->GetDestination ()->GetIDNetworkNode ());
		  std::vector<double> spectralEfficiency;
		  std::vector<int> cqiFeedbacks = ueRecord->GetCQI ();
		  int numberOfCqi = cqiFeedbacks.size ();
		  for (int i = 0; i < numberOfCqi; i++)
			{
			  double sEff = GetMacEntity ()->GetAmcModule ()->GetEfficiencyFromCQI (cqiFeedbacks.at (i));
			  spectralEfficiency.push_back (sEff);
			}

		  //create flow to scheduler record
		  InsertFlowToSchedule(bearer, dataToTransmit, spectralEfficiency, cqiFeedbacks);
		}
	  else
	    {}
	}
}
void
NetworkManager::TransferBearerInfo (UserEquipment* ue, NetworkNode* target)
{
#ifdef HANDOVER_DEBUG
              std::cout << "** HO ** \t TransferBearerInfo for user "
            		  <<  ue->GetIDNetworkNode () << std::endl;
#endif

  if ( (target->GetNodeType() == NetworkNode::TYPE_ENODEB && ue->GetTargetNode ()->GetNodeType() == NetworkNode::TYPE_ENODEB) ||
		 (target->GetNodeType() == NetworkNode::TYPE_HOME_BASE_STATION && ue->GetTargetNode ()->GetNodeType() == NetworkNode::TYPE_ENODEB) ||
		 (target->GetNodeType() == NetworkNode::TYPE_ENODEB && ue->GetTargetNode ()->GetNodeType() == NetworkNode::TYPE_HOME_BASE_STATION))
    {
	   ENodeB *oldTargetNode = (ENodeB*) ue->GetTargetNode ();
	   ENodeB *newTargetNode = (ENodeB*) target;

	   // 1 - update spectrum, channels and propagation loss model
#ifdef HANDOVER_DEBUG
	   std::cout << "update spectrum, channels and propagation loss model"<< std::endl;
#endif
	   ue->GetPhy ()->SetBandwidthManager (newTargetNode->GetPhy ()->GetBandwidthManager ());

	   LteChannel *oldDl = oldTargetNode->GetPhy ()->GetDlChannel ();
	   LteChannel *oldUl = oldTargetNode->GetPhy ()->GetUlChannel ();
	   LteChannel *newDl = newTargetNode->GetPhy ()->GetDlChannel ();
	   LteChannel *newUl = newTargetNode->GetPhy ()->GetUlChannel ();

	   ue->GetPhy ()->SetDlChannel (newDl);
	   if (oldDl->IsAttached (ue))
	     {
		   newDl->AddDevice (ue);
           oldDl->DelDevice (ue);
	     }
	   if (newDl->GetPropagationLossModel () != NULL)
	     {
		   newDl->GetPropagationLossModel ()->
				   AddChannelRealization (CreateChannelRealization (newTargetNode, ue));

		   oldDl->GetPropagationLossModel ()->DelChannelRealization (oldTargetNode,ue);

	     }

	   ue->GetPhy ()->SetUlChannel (newUl);
	   if (oldUl->IsAttached (ue))
	     {
		   newUl->AddDevice (ue);
           oldUl->DelDevice (ue);
	     }
	   if (newUl->GetPropagationLossModel () != NULL)
	     {
		   newUl->GetPropagationLossModel ()->AddChannelRealization (
				   CreateChannelRealization (ue, newTargetNode));

		   oldUl->GetPropagationLossModel ()->DelChannelRealization (ue,oldTargetNode);;
	     }


	   // 2 - add ue record to the new Enb
#ifdef HANDOVER_DEBUG
	   std::cout << "add ue record to the new Enb"<< std::endl;
#endif
       newTargetNode->RegisterUserEquipment (ue);

	   // 3 - delete ue record form the old enb
#ifdef HANDOVER_DEBUG
       std::cout << "delete ue record form the old enb"<< std::endl;
#endif
       oldTargetNode->DeleteUserEquipment (ue);

	   // 4 - update cell and new target enb for the ue
#ifdef HANDOVER_DEBUG
       std::cout << "update cell and new target enb for the ue"<< std::endl;
#endif
	   ue->SetTargetNode (newTargetNode);

	   // MOVE RRC CONTEXT FOR THE OLD TARGET NODE TO THE NEWER ONE
#ifdef HANDOVER_DEBUG
	   std::cout << "MOVE RRC CONTEXT"<< std::endl;
#endif
	   RrcEntity * oldEnbRrc = oldTargetNode->GetProtocolStack ()->GetRrcEntity ();
	   RrcEntity * newEnbRrc = newTargetNode->GetProtocolStack ()->GetRrcEntity ();
	   RrcEntity * ueRrc = ue->GetProtocolStack  ()->GetRrcEntity ();


	   // --> move dl bearers
#ifdef HANDOVER_DEBUG
	   std::cout << "\t DL radio bearers " << oldEnbRrc->GetRadioBearerContainer ()->size () << std::endl;
#endif
	   std::vector<RadioBearer* > *dlBearerToDelete = new std::vector<RadioBearer* > ();
	   for (std::vector<RadioBearer* >::iterator it = oldEnbRrc->GetRadioBearerContainer ()->begin();
			   it != oldEnbRrc->GetRadioBearerContainer ()->end (); it++)
	     {
		   RadioBearer *bearer = (*it);
		   if (bearer->GetDestination ()->GetIDNetworkNode () == ue->GetIDNetworkNode ())
		     {
			   bearer->SetSource (newTargetNode);
			   bearer->GetRlcEntity ()->SetDevice (newTargetNode);
			   newEnbRrc->AddRadioBearer (bearer);
			   dlBearerToDelete->push_back (bearer);
		     }
	     }
	   for (std::vector<RadioBearer* >::iterator it = dlBearerToDelete->begin();
			   it != dlBearerToDelete->end (); it++)
	     {
		   RadioBearer *bearer = (*it);
		   oldEnbRrc->DelRadioBearer (bearer);
	     }
	   dlBearerToDelete->clear ();
	   delete dlBearerToDelete;



	   // --> move ul bearers
#ifdef HANDOVER_DEBUG
	   std::cout << "\t UL radio bearers"<< std::endl;
#endif
	   std::vector<RadioBearerSink* > *ulBearerToDelete = new std::vector<RadioBearerSink* > ();
	   for (std::vector<RadioBearerSink* >::iterator it = oldEnbRrc->GetRadioBearerSinkContainer ()->begin();
			   it != oldEnbRrc->GetRadioBearerSinkContainer ()->end (); it++)
	     {
		   RadioBearerSink *bearer = (*it);
		   if (bearer->GetSource ()->GetIDNetworkNode () == ue->GetIDNetworkNode ())
		     {
			   bearer->SetDestination (newTargetNode);
			   newEnbRrc->AddRadioBearerSink (bearer);
			   ulBearerToDelete->push_back (bearer);
		     }
	     }
	   for (std::vector<RadioBearerSink* >::iterator it = ulBearerToDelete->begin();
			   it != ulBearerToDelete->end (); it++)
	     {
		   RadioBearerSink *bearer = (*it);
		   oldEnbRrc->DelRadioBearerSink (bearer);
	     }
	   ulBearerToDelete->clear ();
	   delete ulBearerToDelete;


	   // UPDATE THE RRC CONTEXT FOR THE UE
#ifdef HANDOVER_DEBUG
	   std::cout << "\t UE updates DL radio bearers "<< std::endl;
#endif
	   for (std::vector<RadioBearerSink* >::iterator it = ueRrc->GetRadioBearerSinkContainer ()->begin();
			   it != ueRrc->GetRadioBearerSinkContainer ()->end (); it++)
	     {
		   RadioBearerSink *bearer = (*it);
		   bearer->SetSource (newTargetNode);
	     }
#ifdef HANDOVER_DEBUG
	   std::cout << "\t UE updates UL radio bearers "<< std::endl;
#endif
	   for (std::vector<RadioBearer* >::iterator it = ueRrc->GetRadioBearerContainer ()->begin();
			   it != ueRrc->GetRadioBearerContainer ()->end (); it++)
	     {
		   RadioBearer *bearer = (*it);
		   bearer->SetDestination (newTargetNode);
	     }
    }

}