package org.interonet.ldm.SwitchManager;

import org.interonet.ldm.ConfigurationCenter.IConfigurationCenter;
import org.interonet.ldm.Core.LDMCore;

import java.io.IOException;
import java.util.Map;
import java.util.logging.Logger;

public class SwitchManager implements ISwitchManager {
    private NFSManager nfsManager;
    private BootImageManager bootImageManager;
    private FPGAManager fpgaManager;
    private IConfigurationCenter configurationCenter;

    public SwitchManager(LDMCore ldmCore) {
        configurationCenter = ldmCore.getConfigurationCenter();
        nfsManager = new NFSManager();
        bootImageManager = new BootImageManager();
        fpgaManager = new FPGAManager();
    }

    @Override
    public void changeConnectionPropertyFromNFS(Integer switchID, String controllerIP, int controllerPort) throws IOException, InterruptedException {
        Map<String, String> nfsMapping = configurationCenter.getSwitchId2NFSRootDirectoryMapping();
        String nfsRootPath = nfsMapping.get(switchID.toString());
        // nfsRootPath equals like /export/0
        //TODO move to nfsManager
        Process process2Copy = Runtime.getRuntime().exec("cp -r /export/backup /export/" + switchID.toString());
        process2Copy.waitFor();
        Logger.getAnonymousLogger().info("cp -r /export/backup /export/" + switchID.toString());
        nfsManager.changeConnecitonPropertyFromNFS(switchID, nfsRootPath, controllerIP, controllerPort);
    }

    @Override
    public void resetSwitchConf(Integer switchID) throws InterruptedException, IOException {
        //TODO move to nfsManager
        Process process2Remove = Runtime.getRuntime().exec("rm -rf /export/" + switchID.toString());
        process2Remove.waitFor();
        Logger.getAnonymousLogger().info("rm -rf /export/" + switchID.toString());
    }
}

@@ -1457,16 +1457,240 @@
 rPort);%0A
+        Process process2Chmod = Runtime.getRuntime().exec(%22chmod -R 777 %22 + switchID.toString() + %22/%22);%0A        process2Chmod.waitFor();%0A        Logger.getAnonymousLogger().info(%22chmod -R 777 %22 + switchID.toString() + %22/%22);%0A
     %7D%0A%0A 

modify the 777 for every directory.