package nebula;

import java.util.Iterator;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.opennebula.client.vm.VirtualMachine;
import org.opennebula.client.vm.VirtualMachinePool;

public class VirtualMachines extends Nebula {

	private VirtualMachinePool vmPool;
	
	public VirtualMachines(){
		super();
		vmPool = new VirtualMachinePool(client);
	}
	
	public void displayVM(){
		
		Iterator<VirtualMachine> vms = vmPool.iterator();
		while(vms.hasNext()){
			
			try {
				
				VirtualMachine vm = vms.next();
				System.out.print("ID : "+vm.getId()+"; ");
				System.out.print("NAME : "+vm.getName()+"; ");
				System.out.print("STATUS : "+vm.lcmStateStr()+"; ");
				String monitoring = vm.monitoring().getMessage();
				Document document = DocumentHelper.parseText(monitoring);
				if (vm.state() == 3){ // ie VM running
					System.out.print("HOST : "+document.selectSingleNode("//HOSTNAME").getText()+"; ");
					System.out.print("HYPERVISEUR : "+document.selectSingleNode("//VMMMAD").getText()+"; ");
					System.out.print("CPU used in %: "+document.selectSingleNode("//CPU").getText()+"; ");
					System.out.println("Memory used in kB: "+document.selectSingleNode("//MEMORY").getText()+"; ");
				}
				
			} catch (DocumentException e) {
				e.printStackTrace();
			}
			
		}
		
	}
	
	public void suspendVM(Integer id){
		VirtualMachine vm = new VirtualMachine(39, client);
		vm.suspend();
	}
	
	public void restartVM(Integer id){
		VirtualMachine vm = new VirtualMachine(39, client);
		vm.resume();
	}
	
}

@@ -1375,32 +1375,40 @@
 VM(Integer id)%7B%0A
+%09%09try%7B%0A%09
 %09%09VirtualMachine
@@ -1424,34 +1424,34 @@
  VirtualMachine(
-39
+id
 , client);%0A%09%09vm.
@@ -1441,24 +1441,25 @@
 d, client);%0A
+%09
 %09%09vm.suspend
@@ -1458,24 +1458,173 @@
 .suspend();%0A
+%09%09%09System.out.println(%22Virtual Machine suspended%22);%0A%09%09%7D catch (Exception e)%7B%0A%09%09%09System.out.println(%22Error while suspending Virtual Machine%22);%0A%09%09%7D%0A%09%09%0A
 %09%7D%0A%09%0A%09public
@@ -1652,16 +1652,24 @@
 er id)%7B%0A
+%09%09try%7B%0A%09
 %09%09Virtua
@@ -1705,10 +1705,10 @@
 ine(
-39
+id
 , cl
@@ -1716,16 +1716,17 @@
 ent);%0A%09%09
+%09
 vm.resum
@@ -1730,15 +1730,712 @@
 sume();%0A
+%09%09%09System.out.println(%22Virtual Machine restarted%22);%0A%09%09%7D catch (Exception e)%7B%0A%09%09%09System.out.println(%22Error while restarting Virtual Machine%22);%0A%09%09%7D%0A%09%09%0A%09%7D%0A%09%0A%09public void deleteVM(Integer id)%7B%0A%09%09try%7B%0A%09%09%09VirtualMachine vm = new VirtualMachine(id, client);%0A%09%09%09vm.delete();%0A%09%09%09System.out.println(%22Virtual Machine deleted%22);%0A%09%09%7D catch (Exception e)%7B%0A%09%09%09System.out.println(%22Error while deleting Virtual Machine%22);%0A%09%09%7D%0A%09%7D%0A%09%0A%09public void migrateVM(Integer idVM, Integer idHost)%7B%0A%09%09try%7B%0A%09%09%09VirtualMachine vm = new VirtualMachine(idVM, client);%0A%09%09%09vm.migrate(idHost);%0A%09%09%09System.out.println(%22Virtual Machine deleted%22);%0A%09%09%7D catch (Exception e)%7B%0A%09%09%09System.out.println(%22Error while deleting Virtual Machine%22);%0A%09%09%7D%0A
 %09%7D%0A%09%0A%7D%0A

Add migrate VM