@@ -5741,4 +5741,116 @@ private Pair<JobInfo.Status, String> orchestrateRestoreVirtualMachine(final VmWo
57415741 return new Pair <JobInfo .Status , String >(JobInfo .Status .SUCCEEDED , _jobMgr .marshallResultObject (passwordMap ));
57425742 }
57435743
5744+ @ Override
5745+ public Boolean updateDefaultNicForVM (final VirtualMachine vm , final Nic nic , final Nic defaultNic ) {
5746+
5747+ final AsyncJobExecutionContext jobContext = AsyncJobExecutionContext .getCurrentExecutionContext ();
5748+ if (jobContext .isJobDispatchedBy (VmWorkConstants .VM_WORK_JOB_DISPATCHER )) {
5749+ VmWorkJobVO placeHolder = null ;
5750+ placeHolder = createPlaceHolderWork (vm .getId ());
5751+ try {
5752+ return orchestrateUpdateDefaultNicForVM (vm , nic , defaultNic );
5753+ } finally {
5754+ if (placeHolder != null ) {
5755+ _workJobDao .expunge (placeHolder .getId ());
5756+ }
5757+ }
5758+ } else {
5759+ final Outcome <VirtualMachine > outcome = updateDefaultNicForVMThroughJobQueue (vm , nic , defaultNic );
5760+
5761+ try {
5762+ outcome .get ();
5763+ } catch (final InterruptedException e ) {
5764+ throw new RuntimeException ("Operation is interrupted" , e );
5765+ } catch (final java .util .concurrent .ExecutionException e ) {
5766+ throw new RuntimeException ("Execution exception" , e );
5767+ }
5768+
5769+ final Object jobResult = _jobMgr .unmarshallResultObject (outcome .getJob ());
5770+ if (jobResult != null ) {
5771+ if (jobResult instanceof Boolean ) {
5772+ return (Boolean )jobResult ;
5773+ }
5774+ }
5775+
5776+ throw new RuntimeException ("Unexpected job execution result" );
5777+ }
5778+ }
5779+
5780+ private Boolean orchestrateUpdateDefaultNicForVM (final VirtualMachine vm , final Nic nic , final Nic defaultNic ) {
5781+
5782+ s_logger .debug ("Updating default nic of vm " + vm + " from nic " + defaultNic .getUuid () + " to nic " + nic .getUuid ());
5783+ Integer chosenID = nic .getDeviceId ();
5784+ Integer existingID = defaultNic .getDeviceId ();
5785+ NicVO nicVO = _nicsDao .findById (nic .getId ());
5786+ NicVO defaultNicVO = _nicsDao .findById (defaultNic .getId ());
5787+
5788+ nicVO .setDefaultNic (true );
5789+ nicVO .setDeviceId (existingID );
5790+ defaultNicVO .setDefaultNic (false );
5791+ defaultNicVO .setDeviceId (chosenID );
5792+
5793+ _nicsDao .persist (nicVO );
5794+ _nicsDao .persist (defaultNicVO );
5795+ return true ;
5796+ }
5797+
5798+ public Outcome <VirtualMachine > updateDefaultNicForVMThroughJobQueue (final VirtualMachine vm , final Nic nic , final Nic defaultNic ) {
5799+
5800+ final CallContext context = CallContext .current ();
5801+ final User user = context .getCallingUser ();
5802+ final Account account = context .getCallingAccount ();
5803+
5804+ final List <VmWorkJobVO > pendingWorkJobs = _workJobDao .listPendingWorkJobs (
5805+ VirtualMachine .Type .Instance , vm .getId (),
5806+ VmWorkUpdateDefaultNic .class .getName ());
5807+
5808+ VmWorkJobVO workJob = null ;
5809+ if (pendingWorkJobs != null && pendingWorkJobs .size () > 0 ) {
5810+ assert pendingWorkJobs .size () == 1 ;
5811+ workJob = pendingWorkJobs .get (0 );
5812+ } else {
5813+
5814+ workJob = new VmWorkJobVO (context .getContextId ());
5815+
5816+ workJob .setDispatcher (VmWorkConstants .VM_WORK_JOB_DISPATCHER );
5817+ workJob .setCmd (VmWorkUpdateDefaultNic .class .getName ());
5818+
5819+ workJob .setAccountId (account .getId ());
5820+ workJob .setUserId (user .getId ());
5821+ workJob .setVmType (VirtualMachine .Type .Instance );
5822+ workJob .setVmInstanceId (vm .getId ());
5823+ workJob .setRelated (AsyncJobExecutionContext .getOriginJobId ());
5824+
5825+ final VmWorkUpdateDefaultNic workInfo = new VmWorkUpdateDefaultNic (user .getId (), account .getId (), vm .getId (),
5826+ VirtualMachineManagerImpl .VM_WORK_JOB_HANDLER , nic .getId (), defaultNic .getId ());
5827+ workJob .setCmdInfo (VmWorkSerializer .serialize (workInfo ));
5828+
5829+ _jobMgr .submitAsyncJob (workJob , VmWorkConstants .VM_WORK_QUEUE , vm .getId ());
5830+ }
5831+ AsyncJobExecutionContext .getCurrentExecutionContext ().joinJob (workJob .getId ());
5832+
5833+ return new VmJobVirtualMachineOutcome (workJob , vm .getId ());
5834+ }
5835+
5836+ @ ReflectionUse
5837+ private Pair <JobInfo .Status , String > orchestrateUpdateDefaultNic (final VmWorkUpdateDefaultNic work ) throws Exception {
5838+ final VMInstanceVO vm = _entityMgr .findById (VMInstanceVO .class , work .getVmId ());
5839+ if (vm == null ) {
5840+ s_logger .info ("Unable to find vm " + work .getVmId ());
5841+ }
5842+ assert vm != null ;
5843+ final NicVO nic = _entityMgr .findById (NicVO .class , work .getNicId ());
5844+ if (nic == null ) {
5845+ throw new CloudRuntimeException ("Unable to find nic " + work .getNicId ());
5846+ }
5847+ final NicVO defaultNic = _entityMgr .findById (NicVO .class , work .getDefaultNicId ());
5848+ if (defaultNic == null ) {
5849+ throw new CloudRuntimeException ("Unable to find default nic " + work .getDefaultNicId ());
5850+ }
5851+ final boolean result = orchestrateUpdateDefaultNicForVM (vm , nic , defaultNic );
5852+ return new Pair <JobInfo .Status , String >(JobInfo .Status .SUCCEEDED ,
5853+ _jobMgr .marshallResultObject (result ));
5854+ }
5855+
57445856}
0 commit comments