package org.interonet.ldm.Core;

import org.interonet.ldm.ConfigurationCenter.ConfigurationCenter;
import org.interonet.ldm.ConfigurationCenter.IConfigurationCenter;
import org.interonet.ldm.SwitchManager.ISwitchManager;
import org.interonet.ldm.SwitchManager.SwitchManager;
import org.interonet.ldm.VMM.*;
import org.libvirt.Connect;
import org.libvirt.LibvirtException;
import org.org.interonet.ldm.PowerManager;

import java.io.IOException;

public class LDMCore {
    @SuppressWarnings("FieldCanBeLocal")
    private LDMAgent ldmAgent;
    private Connect connect;
    private ICreateVirtualMachine iCreateVirtualMachine;
    private IDeleteVirtualMachine iDeleteVirtualMachine;
    @SuppressWarnings("FieldCanBeLocal")
    private IBridgeAndVlan iBridgeAndVlan;

    private PowerManager SwitchControl;
    private ISwitchManager switchManager;
    private IConfigurationCenter configurationCenter;

    public void start() {
        ldmAgent = new LDMAgent(this);
        iCreateVirtualMachine = new CreateVirtualMachine();
        iDeleteVirtualMachine = new DeleteVirtualMachine();
        iBridgeAndVlan = new BridgeAndVlan();
//        iBridgeAndVlan.bridgeAndvlan();

        // VMM initiation.

        try {
            connect = new Connect("qemu+tcp://400@202.117.15.94/system", false);
        } catch (LibvirtException e) {
            e.printStackTrace();
        }

        // ConfigurationCenter initiation.
        configurationCenter = new ConfigurationCenter(this);

        // SwitchManager initiation.
        switchManager = new SwitchManager(this);
    }

    public LDMAgent getAgent() {
        return ldmAgent;
    }

    public String powerOnVM(Integer vmID) {
        String powerOnVMResult = "failure";
        String vmCloneTest = iCreateVirtualMachine.vmclone(vmID);
        String vmStartTest = iCreateVirtualMachine.vmstart(connect, vmID);
        String vmtestOn = "Clone 'vmm"+vmID+"' created successfully.";
       if(vmCloneTest.equals(vmtestOn) && vmStartTest.equals("success"))

            powerOnVMResult="success";

       return powerOnVMResult;
       // return vmStartTest;
    }

    public String powerOffVM(Integer vmID) {
        String powerOffVMResult = "failure";
        String vmDestroyTest = iDeleteVirtualMachine.vmdestroy(connect, vmID);
        String vmDeleteTest = iDeleteVirtualMachine.vmdelete(vmID);
        String vmtestOff="Domain vmm"+vmID+" has been undefined";
        if(vmDestroyTest.equals("success") && vmDeleteTest.equals(vmtestOff))
            powerOffVMResult = "success";
        return powerOffVMResult;
    }
    
    
    public String powerOnSwitch(Integer switchID) {
    	String powerOnSwitchResult = "failure";
    	byte buff[] = { (byte) 0x55, (byte) 0x01, (byte) 0x12, (byte) 0x00,
				(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
    	int ID = switchID ;
    	byte openWayy =(byte)ID;
    	SwitchControl.CreateSendCommand(buff , openWayy , true);
    	int sendStatement = SwitchControl.senBufWithSocket("10.0.0.2" , 3000 , buff , 8);
        if( sendStatement == 0 ) powerOnSwitchResult ="success" ;
    	return powerOnSwitchResult;
    }
    
    
    public String powerOffSwitch(Integer switchID) {
    	String powerOffSwitchResult = "failure";
    	byte buff[] = { (byte) 0x55, (byte) 0x01, (byte) 0x12, (byte) 0x00,
				(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00 };
    	int ID = switchID ;
    	byte closeWayy =(byte)ID;
    	SwitchControl.CreateSendCommand(buff , closeWayy , false);
    	int sendStatement = SwitchControl.senBufWithSocket("10.0.0.2" , 3000 , buff , 8);
        if( sendStatement == 0 ) powerOffSwitchResult ="success" ;
    	return powerOffSwitchResult;
    }

    public void addSwitchConf(Integer switchID, String controllerIP, int controllerPort) throws IOException, InterruptedException {
        switchManager.changeConnectionPropertyFromNFS(switchID, controllerIP, controllerPort);
    }

    public IConfigurationCenter getConfigurationCenter() {
        return configurationCenter;
    }

    public void resetSwitchConf(Integer switchID) throws IOException, InterruptedException {
        switchManager.resetSwitchConf(switchID);
    }
}

@@ -151,32 +151,84 @@
 gurationCenter;%0A
+import org.interonet.ldm.PowerManager.PowerManager;%0A
 import org.inter
@@ -258,32 +258,32 @@
 ISwitchManager;%0A
-
 import org.inter
@@ -420,51 +420,8 @@
 ion;
-%0Aimport org.org.interonet.ldm.PowerManager;
 %0A%0Aim
@@ -795,29 +795,28 @@
 Manager 
-SwitchControl
+powerManager
 ;%0A    pr
@@ -1382,24 +1382,92 @@
 %0A        %7D%0A%0A
+        // PowerManager%0A        powerManager = new PowerManager();%0A%0A
         // C
@@ -2955,37 +2955,36 @@
 te)ID;%0A    %09
-SwitchControl
+powerManager
 .CreateSendC
@@ -2994,17 +2994,16 @@
 and(buff
- 
 , openWa
@@ -3004,17 +3004,16 @@
 openWayy
- 
 , true);
@@ -3034,37 +3034,36 @@
 Statement = 
-SwitchControl
+powerManager
 .senBufWithS
@@ -3505,29 +3505,28 @@
 D;%0A    %09
-SwitchControl
+powerManager
 .CreateS
@@ -3540,17 +3540,16 @@
 and(buff
- 
 , closeW
@@ -3551,17 +3551,16 @@
 loseWayy
- 
 , false)
@@ -3590,21 +3590,20 @@
 t = 
-SwitchControl
+powerManager
 .sen

fix bug for JiaMing Zhou.