Search in sources :

Example 6 with DpdkTO

use of com.cloud.agent.api.to.DpdkTO in project cloudstack by apache.

the class LibvirtMigrateCommandWrapper method replaceDpdkInterfaces.

/**
 * Replace DPDK source path and target before migrations
 */
protected String replaceDpdkInterfaces(String xmlDesc, Map<String, DpdkTO> dpdkPortsMapping) throws TransformerException, ParserConfigurationException, IOException, SAXException {
    InputStream in = IOUtils.toInputStream(xmlDesc);
    DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(in);
    // Get the root element
    Node domainNode = doc.getFirstChild();
    NodeList domainChildNodes = domainNode.getChildNodes();
    for (int i = 0; i < domainChildNodes.getLength(); i++) {
        Node domainChildNode = domainChildNodes.item(i);
        if ("devices".equals(domainChildNode.getNodeName())) {
            NodeList devicesChildNodes = domainChildNode.getChildNodes();
            for (int x = 0; x < devicesChildNodes.getLength(); x++) {
                Node deviceChildNode = devicesChildNodes.item(x);
                if ("interface".equals(deviceChildNode.getNodeName())) {
                    Node interfaceNode = deviceChildNode;
                    NamedNodeMap attributes = interfaceNode.getAttributes();
                    Node interfaceTypeAttr = attributes.getNamedItem("type");
                    if ("vhostuser".equals(interfaceTypeAttr.getNodeValue())) {
                        NodeList diskChildNodes = interfaceNode.getChildNodes();
                        String mac = null;
                        for (int y = 0; y < diskChildNodes.getLength(); y++) {
                            Node diskChildNode = diskChildNodes.item(y);
                            if (!"mac".equals(diskChildNode.getNodeName())) {
                                continue;
                            }
                            mac = diskChildNode.getAttributes().getNamedItem("address").getNodeValue();
                        }
                        if (StringUtils.isNotBlank(mac)) {
                            DpdkTO to = dpdkPortsMapping.get(mac);
                            for (int z = 0; z < diskChildNodes.getLength(); z++) {
                                Node diskChildNode = diskChildNodes.item(z);
                                if ("target".equals(diskChildNode.getNodeName())) {
                                    Node targetNode = diskChildNode;
                                    Node targetNodeAttr = targetNode.getAttributes().getNamedItem("dev");
                                    targetNodeAttr.setNodeValue(to.getPort());
                                } else if ("source".equals(diskChildNode.getNodeName())) {
                                    Node sourceNode = diskChildNode;
                                    NamedNodeMap attrs = sourceNode.getAttributes();
                                    Node path = attrs.getNamedItem("path");
                                    path.setNodeValue(to.getPath() + "/" + to.getPort());
                                    Node mode = attrs.getNamedItem("mode");
                                    mode.setNodeValue(to.getMode());
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    return getXml(doc);
}
Also used : DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) NamedNodeMap(org.w3c.dom.NamedNodeMap) DocumentBuilder(javax.xml.parsers.DocumentBuilder) InputStream(java.io.InputStream) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) DpdkTO(com.cloud.agent.api.to.DpdkTO)

Aggregations

DpdkTO (com.cloud.agent.api.to.DpdkTO)6 VirtualMachineTO (com.cloud.agent.api.to.VirtualMachineTO)3 Connect (org.libvirt.Connect)3 LibvirtException (org.libvirt.LibvirtException)3 PrepareForMigrationAnswer (com.cloud.agent.api.PrepareForMigrationAnswer)2 StopAnswer (com.cloud.agent.api.StopAnswer)2 DiskDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.DiskDef)2 InterfaceDef (com.cloud.hypervisor.kvm.resource.LibvirtVMDef.InterfaceDef)2 VifDriver (com.cloud.hypervisor.kvm.resource.VifDriver)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 URISyntaxException (java.net.URISyntaxException)2 HashMap (java.util.HashMap)2 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)1 Answer (com.cloud.agent.api.Answer)1 CheckVirtualMachineAnswer (com.cloud.agent.api.CheckVirtualMachineAnswer)1 ClusterVMMetaDataSyncAnswer (com.cloud.agent.api.ClusterVMMetaDataSyncAnswer)1 MigrateAnswer (com.cloud.agent.api.MigrateAnswer)1 MigrateCommand (com.cloud.agent.api.MigrateCommand)1 MigrateDiskInfo (com.cloud.agent.api.MigrateCommand.MigrateDiskInfo)1 MigrateVmToPoolAnswer (com.cloud.agent.api.MigrateVmToPoolAnswer)1