Search in sources :

Example 31 with VIF

use of com.xensource.xenapi.VIF in project cosmic by MissionCriticalCloud.

the class CitrixPlugNicCommandWrapper method execute.

@Override
public Answer execute(final PlugNicCommand command, final CitrixResourceBase citrixResourceBase) {
    final Connection conn = citrixResourceBase.getConnection();
    final String vmName = command.getVmName();
    try {
        final Set<VM> vms = VM.getByNameLabel(conn, vmName);
        if (vms == null || vms.isEmpty()) {
            return new PlugNicAnswer(command, false, "Can not find VM " + vmName);
        }
        final VM vm = vms.iterator().next();
        final NicTO nic = command.getNic();
        String mac = nic.getMac();
        final Set<VIF> routerVIFs = vm.getVIFs(conn);
        mac = mac.trim();
        int counter = 0;
        for (final VIF vif : routerVIFs) {
            final String lmac = vif.getMAC(conn);
            if (lmac.trim().equals(mac)) {
                counter++;
            }
        }
        // redundant.
        if (counter > 2) {
            final String msg = " Plug Nic failed due to a VIF with the same mac " + nic.getMac() + " exists in more than 2 routers.";
            s_logger.error(msg);
            return new PlugNicAnswer(command, false, msg);
        }
        // Wilder Rodrigues - replaced this code with the code above.
        // VIF vif = getVifByMac(conn, vm, nic.getMac());
        // if (vif != null) {
        // final String msg = " Plug Nic failed due to a VIF with the same mac " + nic.getMac() + " exists";
        // s_logger.warn(msg);
        // return new PlugNicAnswer(cmd, false, msg);
        // }
        final VIF vif = citrixResourceBase.createVif(conn, vmName, vm, null, nic);
        // vif = createVif(conn, vmName, vm, null, nic);
        vif.plug(conn);
        return new PlugNicAnswer(command, true, "success");
    } catch (final Exception e) {
        final String msg = " Plug Nic failed due to " + e.toString();
        s_logger.error(msg, e);
        return new PlugNicAnswer(command, false, msg);
    }
}
Also used : VIF(com.xensource.xenapi.VIF) VM(com.xensource.xenapi.VM) Connection(com.xensource.xenapi.Connection) PlugNicAnswer(com.cloud.legacymodel.communication.answer.PlugNicAnswer) NicTO(com.cloud.legacymodel.to.NicTO)

Aggregations

VIF (com.xensource.xenapi.VIF)31 VM (com.xensource.xenapi.VM)22 Connection (com.xensource.xenapi.Connection)19 XenAPIException (com.xensource.xenapi.Types.XenAPIException)18 Network (com.xensource.xenapi.Network)17 XmlRpcException (org.apache.xmlrpc.XmlRpcException)17 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)12 NicTO (com.cloud.agent.api.to.NicTO)10 IOException (java.io.IOException)10 MalformedURLException (java.net.MalformedURLException)10 URISyntaxException (java.net.URISyntaxException)10 HashMap (java.util.HashMap)10 TimeoutException (java.util.concurrent.TimeoutException)10 ConfigurationException (javax.naming.ConfigurationException)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 SAXException (org.xml.sax.SAXException)10 InternalErrorException (com.cloud.exception.InternalErrorException)8 SR (com.xensource.xenapi.SR)8 ArrayList (java.util.ArrayList)8 URLConnection (java.net.URLConnection)7