# file: gini_nw.py
from ImImagePlugin import number

import os
import xml.dom.minidom
from gini_components import *

class GINI_NW:
    switches = []
    vm = []
    vrm = []
    vmb = []
    vr = []
    cr=[]
    vwr = []


    def __init__(self, docDOM):
        "Initialize the GINI_NW class"
        self.getSwitches(docDOM.getElementsByTagName("vs"))
        self.getVMs(docDOM.getElementsByTagName('vm'))
        self.getVRMs(docDOM.getElementsByTagName('vrm'))
        self.getVMBs(docDOM.getElementsByTagName("vmb"))
        self.getVRs(docDOM.getElementsByTagName("vr"))
	self.getVWRs(docDOM.getElementsByTagName("vwr"))
    
    def getSwitches(self, elements):
        "get the switch configuration"
        for switch in elements:
            newSwitch = Switch(switch.getAttribute("name"))
            for para in switch.childNodes:
                if (para.nodeType == para.ELEMENT_NODE):
                    if (para.tagName.lower() == "priority"):
                        newSwitch.priority = self.getTextPart(para)
                    if (para.tagName.lower() == "mac"):
                        newSwitch.mac = self.getTextPart(para)
                    if (para.tagName.lower() == "target"):
                        newSwitch.targets.append(self.getTextPart(para))
                    if (para.tagName.lower() == "port"):
                        newSwitch.port = self.getTextPart(para)
                    if (para.tagName.lower() == "remote"):
                        newSwitch.remote = self.getTextPart(para)
                    if (para.tagName.lower() == "hub"):
                        newSwitch.hub = True
            self.switches.append(newSwitch)
        return True
        
    def getVMs(self, elements):
        "get virtual machine configurations"
        for vm in elements:
            newVM = VM(vm.getAttribute("name"))
            for para in vm.childNodes:
                if (para.nodeType == para.ELEMENT_NODE):
                    if (para.tagName.lower() == "filesystem"):
                        newVM.fileSystem = FileSystem()
                        newVM.fileSystem.type = para.getAttribute("type")
                        newVM.fileSystem.name = os.environ["GINI_SHARE"] + "/filesystem/" + self.getTextPart(para)
                    if (para.tagName.lower() == "mem"):
                        newVM.mem = self.getTextPart(para)
                    if (para.tagName.lower() == "kernel"):
                        newVM.kernel = self.getTextPart(para)
                    if (para.tagName.lower() == "boot"):
                        newVM.boot = self.getBoot(para)
                    if (para.tagName.lower() == "if"):
                        newIF = self.getVMIF(para, len(newVM.interfaces))
                        newVM.addInterface(newIF)
            self.vm.append(newVM)
        return True
                                
    def getVRMs(self, elements):
        "get remote machine configurations"
        for vrm in elements:
            newVRM = VRM(vrm.getAttribute("name"))
            for para in vrm.childNodes:
                if (para.nodeType == para.ELEMENT_NODE):
                    if (para.tagName.lower() == "filesystem"):
                        newVRM.fileSystem = FileSystem()
                        newVRM.fileSystem.type = para.getAttribute("type")
                        newVRM.fileSystem.name = os.environ["GINI_HOME"] + "/" + self.getTextPart(para)
                    if (para.tagName.lower() == "mem"):
                        newVRM.mem = self.getTextPart(para)
                    if (para.tagName.lower() == "kernel"):
                        newVRM.kernel = self.getTextPart(para)
                    if (para.tagName.lower() == "boot"):
                        newVRM.boot = self.getBoot(para)
                    if (para.tagName.lower() == "if"):
                        newIF = self.getVMIF(para, len(newVRM.interfaces))
                        newVRM.addInterface(newIF)
            self.vrm.append(newVRM)
        return True

    def getCRs(self, elemments):
        a=5