From 84d5f0514ea6228956fbfd01dbdd4037c0f672b6 Mon Sep 17 00:00:00 2001 From: Alex Huang Date: Wed, 16 Nov 2011 13:12:51 -0800 Subject: [PATCH] Changed network tags to network name as we no longer need to send the entire network tags down to the resource. --- .../com/cloud/agent/api/to/IpAddressTO.java | 52 +- api/src/com/cloud/agent/api/to/NetworkTO.java | 11 +- api/src/com/cloud/vm/NicProfile.java | 13 +- .../xen/resource/CitrixResourceBase.java | 1340 ++++++++--------- .../cloud/hypervisor/HypervisorGuruBase.java | 4 +- .../src/com/cloud/network/NetworkManager.java | 38 +- .../com/cloud/network/NetworkManagerImpl.java | 1134 +++++++------- .../PhysicalNetworkTrafficTypeDaoImpl.java | 53 +- .../VirtualNetworkApplianceManagerImpl.java | 578 +++---- .../src/com/cloud/vm/UserVmManagerImpl.java | 227 ++- .../cloud/vm/VirtualMachineManagerImpl.java | 278 ++-- 11 files changed, 1851 insertions(+), 1877 deletions(-) diff --git a/api/src/com/cloud/agent/api/to/IpAddressTO.java b/api/src/com/cloud/agent/api/to/IpAddressTO.java index 35429dcc089..2f0be031470 100644 --- a/api/src/com/cloud/agent/api/to/IpAddressTO.java +++ b/api/src/com/cloud/agent/api/to/IpAddressTO.java @@ -18,14 +18,12 @@ package com.cloud.agent.api.to; -import java.util.List; - import com.cloud.network.Networks.TrafficType; public class IpAddressTO { - - private long accountId; + + private long accountId; private String publicIp; private boolean sourceNat; private boolean add; @@ -38,8 +36,8 @@ public class IpAddressTO { private String guestIp; private Integer networkRate; private TrafficType trafficType; - private String[] networkTags; - + private String networkName; + public IpAddressTO(long accountId, String ipAddress, boolean add, boolean firstIP, boolean sourceNat, String vlanId, String vlanGateway, String vlanNetmask, String vifMacAddress, String guestIp, Integer networkRate, boolean isOneToOneNat) { this.accountId = accountId; this.publicIp = ipAddress; @@ -54,12 +52,12 @@ public class IpAddressTO { this.networkRate = networkRate; this.oneToOneNat = isOneToOneNat; } - + protected IpAddressTO() { } - + public long getAccountId() { - return accountId; + return accountId; } public String getGuestIp(){ @@ -69,24 +67,20 @@ public class IpAddressTO { public String getPublicIp() { return publicIp; } - + public TrafficType getTrafficType() { return trafficType; } - - public void setNetworkTags(List tagsList) { - if (tagsList == null || tagsList.size() == 0) { - networkTags = null; - } else { - networkTags = tagsList.toArray(new String[tagsList.size()]); - } + + public void setNetworkName(String name) { + this.networkName = name; } - - public String[] getNetworkTags() { - return networkTags; + + public String getNetworkName() { + return networkName; } - - + + public void setTrafficType(TrafficType trafficType) { this.trafficType = trafficType; } @@ -94,11 +88,11 @@ public class IpAddressTO { public boolean isAdd() { return add; } - + public boolean isOneToOneNat(){ return this.oneToOneNat; } - + public boolean isFirstIP() { return firstIP; } @@ -110,23 +104,23 @@ public class IpAddressTO { public boolean isSourceNat() { return sourceNat; } - + public String getVlanId() { return vlanId; } - + public String getVlanGateway() { return vlanGateway; } - + public String getVlanNetmask() { return vlanNetmask; } - + public String getVifMacAddress() { return vifMacAddress; } - + public Integer getNetworkRate() { return networkRate; } diff --git a/api/src/com/cloud/agent/api/to/NetworkTO.java b/api/src/com/cloud/agent/api/to/NetworkTO.java index 363946a3d5a..fdafd952213 100644 --- a/api/src/com/cloud/agent/api/to/NetworkTO.java +++ b/api/src/com/cloud/agent/api/to/NetworkTO.java @@ -18,7 +18,6 @@ package com.cloud.agent.api.to; import java.net.URI; -import java.util.List; import com.cloud.network.Networks.BroadcastDomainType; import com.cloud.network.Networks.TrafficType; @@ -39,7 +38,7 @@ public class NetworkTO { protected URI broadcastUri; protected URI isolationUri; protected boolean isSecurityGroupEnabled; - protected String[] tags; + protected String name; public NetworkTO() { } @@ -88,12 +87,12 @@ public class NetworkTO { this.type = type; } - public void setTags(List tags) { - this.tags = tags.toArray(new String[tags.size()]); + public void setName(String name) { + this.name = name; } - public String[] getTags() { - return tags; + public String getName() { + return name; } public void setSecurityGroupEnabled(boolean enabled) { diff --git a/api/src/com/cloud/vm/NicProfile.java b/api/src/com/cloud/vm/NicProfile.java index 9c9c390f5a2..1d8705000e7 100644 --- a/api/src/com/cloud/vm/NicProfile.java +++ b/api/src/com/cloud/vm/NicProfile.java @@ -22,7 +22,6 @@ package com.cloud.vm; import java.net.URI; -import java.util.List; import com.cloud.network.Network; import com.cloud.network.Networks.AddressFormat; @@ -54,15 +53,15 @@ public class NicProfile { String dns2; Integer networkRate; boolean isSecurityGroupEnabled; - List tags; + String name; String requestedIp; public String getDns1() { return dns1; } - public List getTags() { - return tags; + public String getName() { + return name; } public String getDns2() { @@ -213,7 +212,7 @@ public class NicProfile { return strategy; } - public NicProfile(Nic nic, Network network, URI broadcastUri, URI isolationUri, Integer networkRate, boolean isSecurityGroupEnabled, List tags) { + public NicProfile(Nic nic, Network network, URI broadcastUri, URI isolationUri, Integer networkRate, boolean isSecurityGroupEnabled, String name) { this.id = nic.getId(); this.networkId = network.getId(); this.gateway = nic.getGateway(); @@ -233,7 +232,7 @@ public class NicProfile { this.netmask = nic.getNetmask(); this.isSecurityGroupEnabled = isSecurityGroupEnabled; this.vmId = nic.getInstanceId(); - this.tags = tags; + this.name = name; if (networkRate != null) { this.networkRate = networkRate; @@ -255,7 +254,7 @@ public class NicProfile { this.netmask = netmask; this.strategy = strategy; } - + public NicProfile(String requestedIp) { this.requestedIp = requestedIp; } diff --git a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java index 2c01472b06b..376f9120570 100755 --- a/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java +++ b/core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java @@ -125,7 +125,6 @@ import com.cloud.agent.api.StartAnswer; import com.cloud.agent.api.StartCommand; import com.cloud.agent.api.StartupCommand; import com.cloud.agent.api.StartupRoutingCommand; -import com.cloud.agent.api.StartupRoutingCommand.VmState; import com.cloud.agent.api.StartupStorageCommand; import com.cloud.agent.api.StopAnswer; import com.cloud.agent.api.StopCommand; @@ -232,8 +231,6 @@ import com.xensource.xenapi.VM; import com.xensource.xenapi.VMGuestMetrics; import com.xensource.xenapi.XenAPIObject; -import edu.emory.mathcs.backport.java.util.Arrays; - /** * CitrixResourceBase encapsulates the calls to the XenServer Xapi process * to perform the required functionalities for CloudStack. @@ -287,9 +284,9 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe public enum SRType { NFS, LVM, ISCSI, ISO, LVMOISCSI, LVMOHBA, EXT; - + String _str; - + private SRType() { _str = super.toString().toLowerCase(); } @@ -313,7 +310,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_statesTable.put(Types.VmPowerState.SUSPENDED, State.Running); s_statesTable.put(Types.VmPowerState.UNRECOGNIZED, State.Unknown); } - + protected boolean cleanupHaltedVms(Connection conn) throws XenAPIException, XmlRpcException { Host host = Host.getByUuid(conn, _host.uuid); @@ -325,7 +322,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if ( vmRec.isATemplate || vmRec.isControlDomain ) { continue; } - + if (VmPowerState.HALTED.equals(vmRec.powerState) && vmRec.affinity.equals(host)) { try { vm.destroy(conn); @@ -333,11 +330,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.warn("Catch Exception " + e.getClass().getName() + ": unable to destroy VM " + vmRec.nameLabel + " due to ", e); success = false; } - } + } } return success; } - + protected boolean isRefNull(XenAPIObject object) { return (object == null || object.toWireString().equals("OpaqueRef:NULL") || object.toWireString().equals("")); } @@ -357,7 +354,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return true; } - + protected boolean pingXenServer() { Session slaveSession = null; Connection slaveConn = null; @@ -379,7 +376,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return false; } - + protected String logX(XenAPIObject obj, String msg) { return new StringBuilder("Host ").append(_host.ip).append(" ").append(obj.toWireString()).append(": ").append(msg).toString(); } @@ -483,11 +480,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } else if (clazz == SecurityIngressRulesCmd.class) { return execute((SecurityIngressRulesCmd) cmd); } else if (clazz == OvsCreateGreTunnelCommand.class) { - return execute((OvsCreateGreTunnelCommand)cmd); + return execute((OvsCreateGreTunnelCommand)cmd); } else if (clazz == OvsSetTagAndFlowCommand.class) { - return execute((OvsSetTagAndFlowCommand)cmd); + return execute((OvsSetTagAndFlowCommand)cmd); } else if (clazz == OvsDeleteFlowCommand.class) { - return execute((OvsDeleteFlowCommand)cmd); + return execute((OvsDeleteFlowCommand)cmd); } else if (clazz == CleanupNetworkRulesCmd.class){ return execute((CleanupNetworkRulesCmd)cmd); } else if (clazz == NetworkRulesSystemVmCommand.class) { @@ -514,16 +511,16 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return Answer.createUnsupportedCommandAnswer(cmd); } } - - protected XsLocalNetwork getNativeNetworkForTraffic(Connection conn, TrafficType type, String tag) throws XenAPIException, XmlRpcException { - if (tag != null) { + + protected XsLocalNetwork getNativeNetworkForTraffic(Connection conn, TrafficType type, String name) throws XenAPIException, XmlRpcException { + if (name != null) { if (s_logger.isDebugEnabled()) { - s_logger.debug("Looking for network named " + tag); + s_logger.debug("Looking for network named " + name); } - return getNetworkByName(conn, tag); + return getNetworkByName(conn, name); } - + if (type == TrafficType.Guest) { return new XsLocalNetwork(Network.getByUuid(conn, _host.guestNetwork), null, PIF.getByUuid(conn, _host.guestPif), null); } else if (type == TrafficType.Control) { @@ -536,10 +533,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } else if (type == TrafficType.Storage) { return new XsLocalNetwork(Network.getByUuid(conn, _host.storageNetwork1), null, PIF.getByUuid(conn, _host.storagePif1), null); } - + throw new CloudRuntimeException("Unsupported network type: " + type); } - + /** * This is a tricky to create network in xenserver. * if you create a network then create bridge by brctl or openvswitch yourself, @@ -550,61 +547,61 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe * @throws XenAPIException */ private void enableXenServerNetwork(Connection conn, Network nw, - String vifNameLabel, String networkDesc) throws XenAPIException, XmlRpcException { - /* Make sure there is a physical bridge on this network */ + String vifNameLabel, String networkDesc) throws XenAPIException, XmlRpcException { + /* Make sure there is a physical bridge on this network */ VIF dom0vif = null; Pair vm = getControlDomain(conn); VM dom0 = vm.first(); - - s_logger.debug("Create a vif on dom0 for " + networkDesc); - VIF.Record vifr = new VIF.Record(); - vifr.VM = dom0; - vifr.device = getLowestAvailableVIFDeviceNum(conn, dom0); - if (vifr.device == null) { - s_logger.debug("Failed to create " + networkDesc + ", no vif available"); - return; - } - Map config = new HashMap(); - config.put("nameLabel", vifNameLabel); - vifr.otherConfig = config; - vifr.MAC = "FE:FF:FF:FF:FF:FF"; - vifr.network = nw; - dom0vif = VIF.create(conn, vifr); - dom0vif.plug(conn); - dom0vif.unplug(conn); - synchronized(_tmpDom0Vif) { - _tmpDom0Vif.add(dom0vif); - } + + s_logger.debug("Create a vif on dom0 for " + networkDesc); + VIF.Record vifr = new VIF.Record(); + vifr.VM = dom0; + vifr.device = getLowestAvailableVIFDeviceNum(conn, dom0); + if (vifr.device == null) { + s_logger.debug("Failed to create " + networkDesc + ", no vif available"); + return; + } + Map config = new HashMap(); + config.put("nameLabel", vifNameLabel); + vifr.otherConfig = config; + vifr.MAC = "FE:FF:FF:FF:FF:FF"; + vifr.network = nw; + dom0vif = VIF.create(conn, vifr); + dom0vif.plug(conn); + dom0vif.unplug(conn); + synchronized(_tmpDom0Vif) { + _tmpDom0Vif.add(dom0vif); + } } - + private synchronized Network setupvSwitchNetwork(Connection conn) { - try { - if (_host.vswitchNetwork == null) { - Network vswitchNw = null; - Network.Record rec = new Network.Record(); - String nwName = Networks.BroadcastScheme.VSwitch.toString(); - Set networks = Network.getByNameLabel(conn, nwName); - - if (networks.size() == 0) { - rec.nameDescription = "vswitch network for " + nwName; - rec.nameLabel = nwName; - vswitchNw = Network.create(conn, rec); - } else { - vswitchNw = networks.iterator().next(); - } + try { + if (_host.vswitchNetwork == null) { + Network vswitchNw = null; + Network.Record rec = new Network.Record(); + String nwName = Networks.BroadcastScheme.VSwitch.toString(); + Set networks = Network.getByNameLabel(conn, nwName); - enableXenServerNetwork(conn, vswitchNw, "vswitch", "vswicth network"); - _host.vswitchNetwork = vswitchNw; - } - return _host.vswitchNetwork; - } catch (Exception e) { - e.printStackTrace(); - } - - return null; + if (networks.size() == 0) { + rec.nameDescription = "vswitch network for " + nwName; + rec.nameLabel = nwName; + vswitchNw = Network.create(conn, rec); + } else { + vswitchNw = networks.iterator().next(); + } + + enableXenServerNetwork(conn, vswitchNw, "vswitch", "vswicth network"); + _host.vswitchNetwork = vswitchNw; + } + return _host.vswitchNetwork; + } catch (Exception e) { + e.printStackTrace(); + } + + return null; } - + private synchronized Network createTunnelNetwork(Connection conn, long account) { try { String nwName = "OVSTunnel" + account; @@ -619,7 +616,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } else { nw = networks.iterator().next(); } - + enableXenServerNetwork(conn, nw, nwName, "tunnel network for account " + account); return nw; } catch (Exception e) { @@ -628,10 +625,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } - + protected Network getNetwork(Connection conn, NicTO nic) throws XenAPIException, XmlRpcException { - String[] tags = nic.getTags(); - XsLocalNetwork network = getNativeNetworkForTraffic(conn, nic.getType(), tags != null && tags.length > 0 ? tags[0] : null); + String name = nic.getName(); + XsLocalNetwork network = getNativeNetworkForTraffic(conn, nic.getType(), name); if (network == null) { s_logger.error("Network is not configured on the backend for nic " + nic.toString()); throw new CloudRuntimeException("Network for the backend is not configured correctly for network broadcast domain: " + nic.getBroadcastUri()); @@ -649,47 +646,47 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe String broadcastUri = nic.getBroadcastUri().toString(); String header = broadcastUri.substring(Networks.BroadcastDomainType.Vswitch.scheme().length() + "://".length()); if (header.startsWith("vlan")) { - _isOvs = true; + _isOvs = true; return setupvSwitchNetwork(conn); } else { long account = Long.parseLong(nic.getBroadcastUri().getHost()); return createTunnelNetwork(conn, account); } } - + throw new CloudRuntimeException("Unable to support this type of network broadcast domain: " + nic.getBroadcastUri()); } - + protected VIF createVif(Connection conn, String vmName, VM vm, NicTO nic) throws XmlRpcException, XenAPIException { if (s_logger.isDebugEnabled()) { s_logger.debug("Creating VIF for " + vmName + " on nic " + nic); } - + VIF.Record vifr = new VIF.Record(); vifr.VM = vm; vifr.device = Integer.toString(nic.getDeviceId()); vifr.MAC = nic.getMac(); vifr.network = getNetwork(conn, nic); - + if (nic.getNetworkRateMbps() != null && nic.getNetworkRateMbps().intValue() != -1) { vifr.qosAlgorithmType = "ratelimit"; vifr.qosAlgorithmParams = new HashMap(); // convert mbs to kilobyte per second vifr.qosAlgorithmParams.put("kbps", Integer.toString(nic.getNetworkRateMbps() * 128)); } - + VIF vif = VIF.create(conn, vifr); if (s_logger.isDebugEnabled()) { vifr = vif.getRecord(conn); s_logger.debug("Created a vif " + vifr.uuid + " on " + nic.getDeviceId()); } - + return vif; } - + protected void prepareISO(Connection conn, String vmName) throws XmlRpcException, XenAPIException { - + Set vms = VM.getByNameLabel(conn, vmName); if( vms == null || vms.size() != 1) { throw new CloudRuntimeException("There are " + ((vms == null) ? "0" : vms.size()) + " VMs named " + vmName); @@ -722,13 +719,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } } - + protected VDI mount(Connection conn, String vmName, VolumeTO volume) throws XmlRpcException, XenAPIException { if (volume.getType() == Volume.Type.ISO) { - + String isopath = volume.getPath(); if (isopath == null) { - return null; + return null; } int index = isopath.lastIndexOf("/"); @@ -753,25 +750,25 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return VDI.getByUuid(conn, volume.getPath()); } } - + protected VBD createVbd(Connection conn, VolumeTO volume, String vmName, VM vm, BootloaderType bootLoaderType) throws XmlRpcException, XenAPIException { Volume.Type type = volume.getType(); - + VDI vdi = mount(conn, vmName, volume); - + VBD.Record vbdr = new VBD.Record(); vbdr.VM = vm; if (vdi != null) { vbdr.VDI = vdi; } else { - vbdr.empty = true; + vbdr.empty = true; } if (type == Volume.Type.ROOT && bootLoaderType == BootloaderType.PyGrub) { vbdr.bootable = true; }else if(type == Volume.Type.ISO && bootLoaderType == BootloaderType.CD) { - vbdr.bootable = true; + vbdr.bootable = true; } - + vbdr.userdevice = Long.toString(volume.getDeviceId()); if (volume.getType() == Volume.Type.ISO) { vbdr.mode = Types.VbdMode.RO; @@ -786,14 +783,14 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe vbdr.unpluggable = true; } VBD vbd = VBD.create(conn, vbdr); - + if (s_logger.isDebugEnabled()) { s_logger.debug("VBD " + vbd.getUuid(conn) + " created for " + volume); } - + return vbd; } - + protected VM createVmFromTemplate(Connection conn, VirtualMachineTO vmSpec, Host host) throws XenAPIException, XmlRpcException { String guestOsTypeName = getGuestOsType(vmSpec.getOs(), vmSpec.getBootloader() == BootloaderType.CD); if ( guestOsTypeName == null ) { @@ -805,17 +802,17 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe Set templates = VM.getByNameLabel(conn, guestOsTypeName); assert templates.size() == 1 : "Should only have 1 template but found " + templates.size(); VM template = templates.iterator().next(); - + VM vm = template.createClone(conn, vmSpec.getName()); VM.Record vmr = vm.getRecord(conn); if (s_logger.isDebugEnabled()) { s_logger.debug("Created VM " + vmr.uuid + " for " + vmSpec.getName()); } - + for (Console console : vmr.consoles) { console.destroy(conn); } - + vm.setIsATemplate(conn, false); vm.setAffinity(conn, host); vm.removeFromOtherConfig(conn, "disks"); @@ -823,12 +820,12 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe setMemory(conn, vm, vmSpec.getMinRam()); vm.setVCPUsMax(conn, (long)vmSpec.getCpus()); vm.setVCPUsAtStartup(conn, (long)vmSpec.getCpus()); - + Map vcpuParams = new HashMap(); Integer speed = vmSpec.getSpeed(); if (speed != null) { - + int cpuWeight = _maxWeight; //cpu_weight int utilization = 0; // max CPU cap, default is unlimited @@ -839,21 +836,21 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } if (vmSpec.getLimitCpuUse()) { - utilization = (int)((speed*0.99) / _host.speed * 100); + utilization = (int)((speed*0.99) / _host.speed * 100); } - + vcpuParams.put("weight", Integer.toString(cpuWeight)); vcpuParams.put("cap", Integer.toString(utilization)); - + } - + if (vcpuParams.size() > 0) { vm.setVCPUsParams(conn, vcpuParams); } vm.setActionsAfterCrash(conn, Types.OnCrashBehaviour.DESTROY); vm.setActionsAfterShutdown(conn, Types.OnNormalExit.DESTROY); - + String bootArgs = vmSpec.getBootArgs(); if (bootArgs != null && bootArgs.length() > 0) { String pvargs = vm.getPVArgs(conn); @@ -863,44 +860,44 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } vm.setPVArgs(conn, pvargs); } - + if (!(guestOsTypeName.startsWith("Windows") || guestOsTypeName.startsWith("Citrix") || guestOsTypeName.startsWith("Other"))) { if (vmSpec.getBootloader() == BootloaderType.CD) { - VolumeTO [] disks = vmSpec.getDisks(); - for (VolumeTO disk : disks) { - if (disk.getType() == Volume.Type.ISO && disk.getOsType() != null) { - String isoGuestOsName = getGuestOsType(disk.getOsType(), vmSpec.getBootloader() == BootloaderType.CD); - if (!isoGuestOsName.equals(guestOsTypeName)) { - vmSpec.setBootloader(BootloaderType.PyGrub); - } - } - } + VolumeTO [] disks = vmSpec.getDisks(); + for (VolumeTO disk : disks) { + if (disk.getType() == Volume.Type.ISO && disk.getOsType() != null) { + String isoGuestOsName = getGuestOsType(disk.getOsType(), vmSpec.getBootloader() == BootloaderType.CD); + if (!isoGuestOsName.equals(guestOsTypeName)) { + vmSpec.setBootloader(BootloaderType.PyGrub); + } + } + } } if (vmSpec.getBootloader() == BootloaderType.CD) { - vm.setPVBootloader(conn, "eliloader"); - Map otherConfig = vm.getOtherConfig(conn); + vm.setPVBootloader(conn, "eliloader"); + Map otherConfig = vm.getOtherConfig(conn); if ( ! vm.getOtherConfig(conn).containsKey("install-repository") ) { otherConfig.put( "install-repository", "cdrom"); } - vm.setOtherConfig(conn, otherConfig); + vm.setOtherConfig(conn, otherConfig); } else if (vmSpec.getBootloader() == BootloaderType.PyGrub ){ - vm.setPVBootloader(conn, "pygrub"); + vm.setPVBootloader(conn, "pygrub"); } else { - vm.destroy(conn); - throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader()); + vm.destroy(conn); + throw new CloudRuntimeException("Unable to handle boot loader type: " + vmSpec.getBootloader()); } } return vm; } - + protected String handleVmStartFailure(Connection conn, String vmName, VM vm, String message, Throwable th) { String msg = "Unable to start " + vmName + " due to " + message; s_logger.warn(msg, th); - + if (vm == null) { return msg; } - + try { VM.Record vmr = vm.getRecord(conn); List networks = new ArrayList(); @@ -950,12 +947,12 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } catch (Exception e) { s_logger.warn("VM getRecord failed due to ", e); } - + return msg; } - + protected VBD createPatchVbd(Connection conn, String vmName, VM vm) throws XmlRpcException, XenAPIException { - + if( _host.systemvmisouuid == null ) { Set srs = SR.getByNameLabel(conn, "XenServer Tools"); if( srs.size() != 1 ) { @@ -969,17 +966,17 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if( _host.systemvmisouuid == null ) { for( VDI vdi : srr.VDIs ) { VDI.Record vdir = vdi.getRecord(conn); - if(vdir.nameLabel.contains("systemvm.iso")){ - _host.systemvmisouuid = vdir.uuid; - break; - } + if(vdir.nameLabel.contains("systemvm.iso")){ + _host.systemvmisouuid = vdir.uuid; + break; + } } } if( _host.systemvmisouuid == null ) { throw new CloudRuntimeException("can not find systemvmiso"); } } - + VBD.Record cdromVBDR = new VBD.Record(); cdromVBDR.VM = vm; cdromVBDR.empty = true; @@ -989,16 +986,16 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe cdromVBDR.type = Types.VbdType.CD; VBD cdromVBD = VBD.create(conn, cdromVBDR); cdromVBD.insert(conn, VDI.getByUuid(conn, _host.systemvmisouuid)); - + return cdromVBD; } - + protected CheckSshAnswer execute(CheckSshCommand cmd) { Connection conn = getConnection(); String vmName = cmd.getName(); String privateIp = cmd.getIp(); int cmdPort = cmd.getPort(); - + if (s_logger.isDebugEnabled()) { s_logger.debug("Ping command port, " + privateIp + ":" + cmdPort); } @@ -1011,14 +1008,14 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } catch (Exception e) { return new CheckSshAnswer(cmd, e); } - + if (s_logger.isDebugEnabled()) { s_logger.debug("Ping command port succeeded for vm " + vmName); } - + return new CheckSshAnswer(cmd); } - + private HashMap parseDefaultOvsRuleComamnd(String str) { HashMap cmd = new HashMap(); String[] sarr = str.split("/"); @@ -1031,8 +1028,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe continue; } if (p[0].equalsIgnoreCase("vlans")) { - p[1] = p[1].replace("@", "["); - p[1] = p[1].replace("#", "]"); + p[1] = p[1].replace("@", "["); + p[1] = p[1].replace("#", "]"); } cmd.put(p[0], p[1]); } @@ -1040,26 +1037,26 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } private void cleanUpTmpDomVif(Connection conn) { - List vifs; - synchronized(_tmpDom0Vif) { - vifs = _tmpDom0Vif; - _tmpDom0Vif = new ArrayList(); - } - - for (VIF v : vifs) { - String vifName = "unkown"; - try { - VIF.Record vifr = v.getRecord(conn); - Map config = vifr.otherConfig; - vifName = config.get("nameLabel"); - v.destroy(conn); - s_logger.debug("Destroy temp dom0 vif" + vifName + " success"); - } catch (Exception e) { - s_logger.warn("Destroy temp dom0 vif " + vifName + "failed", e); - } - } + List vifs; + synchronized(_tmpDom0Vif) { + vifs = _tmpDom0Vif; + _tmpDom0Vif = new ArrayList(); + } + + for (VIF v : vifs) { + String vifName = "unkown"; + try { + VIF.Record vifr = v.getRecord(conn); + Map config = vifr.otherConfig; + vifName = config.get("nameLabel"); + v.destroy(conn); + s_logger.debug("Destroy temp dom0 vif" + vifName + " success"); + } catch (Exception e) { + s_logger.warn("Destroy temp dom0 vif " + vifName + "failed", e); + } + } } - + @Override public StartAnswer execute(StartCommand cmd) { Connection conn = getConnection(); @@ -1086,26 +1083,26 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } } - + s_vms.put(_cluster, _name, vmName, State.Starting); - + Host host = Host.getByUuid(conn, _host.uuid); vm = createVmFromTemplate(conn, vmSpec, host); - + for (VolumeTO disk : vmSpec.getDisks()) { createVbd(conn, disk, vmName, vm, vmSpec.getBootloader()); } - + if (vmSpec.getType() != VirtualMachine.Type.User) { createPatchVbd(conn, vmName, vm); } - + for (NicTO nic : vmSpec.getNics()) { createVif(conn, vmName, vm, nic); } - + startVM(conn, host, vm, vmName); - + if (_isOvs) { for (NicTO nic : vmSpec.getNics()) { if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vswitch) { @@ -1144,22 +1141,22 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } else { - //For user vm, program the rules for each nic if the isolation uri scheme is ec2 - NicTO[] nics = vmSpec.getNics(); - for (NicTO nic : nics) { - if (nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) { - result = callHostPlugin(conn, "vmops", "default_network_rules", "vmName", vmName, "vmIP", nic.getIp(), "vmMAC", nic.getMac(), "vmID", Long.toString(vmSpec.getId())); - - if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) { - s_logger.warn("Failed to program default network rules for " + vmName+" on nic with ip:"+nic.getIp()+" mac:"+nic.getMac()); - } else { - s_logger.info("Programmed default network rules for " + vmName+" on nic with ip:"+nic.getIp()+" mac:"+nic.getMac()); - } - } - } + //For user vm, program the rules for each nic if the isolation uri scheme is ec2 + NicTO[] nics = vmSpec.getNics(); + for (NicTO nic : nics) { + if (nic.getIsolationUri() != null && nic.getIsolationUri().getScheme().equalsIgnoreCase(IsolationType.Ec2.toString())) { + result = callHostPlugin(conn, "vmops", "default_network_rules", "vmName", vmName, "vmIP", nic.getIp(), "vmMAC", nic.getMac(), "vmID", Long.toString(vmSpec.getId())); + + if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) { + s_logger.warn("Failed to program default network rules for " + vmName+" on nic with ip:"+nic.getIp()+" mac:"+nic.getMac()); + } else { + s_logger.info("Programmed default network rules for " + vmName+" on nic with ip:"+nic.getIp()+" mac:"+nic.getMac()); + } + } + } } } - + state = State.Running; return new StartAnswer(cmd); } catch (Exception e) { @@ -1178,7 +1175,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } protected Answer execute(ModifySshKeysCommand cmd) { - return new Answer(cmd); + return new Answer(cmd); } private boolean doPingTest(Connection conn, final String computingHostIp) { @@ -1229,7 +1226,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return new CheckRouterAnswer(cmd, result, true); } - + private GetDomRVersionAnswer execute(GetDomRVersionCmd cmd) { Connection conn = getConnection(); String args = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); @@ -1243,7 +1240,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return new GetDomRVersionAnswer(cmd, result, lines[0], lines[1]); } - + private Answer execute(BumpUpPriorityCommand cmd) { Connection conn = getConnection(); String args = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); @@ -1299,11 +1296,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe protected SetPortForwardingRulesAnswer execute(SetPortForwardingRulesCommand cmd) { Connection conn = getConnection(); - + String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); String[] results = new String[cmd.getRules().length]; int i = 0; - + boolean endResult = true; for (PortForwardingRuleTO rule : cmd.getRules()) { StringBuilder args = new StringBuilder(); @@ -1314,7 +1311,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe args.append(" -p ").append(rule.getStringSrcPortRange()); args.append(" -r ").append(rule.getDstIp()); args.append(" -d ").append(rule.getStringDstPortRange()); - + String result = callHostPlugin(conn, "vmops", "setFirewallRule", "args", args.toString()); if (result == null || result.isEmpty()) { @@ -1327,10 +1324,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new SetPortForwardingRulesAnswer(cmd, results, endResult); } - + protected SetStaticNatRulesAnswer execute(SetStaticNatRulesCommand cmd) { Connection conn = getConnection(); - + String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); //String args = routerIp; String[] results = new String[cmd.getRules().length]; @@ -1345,12 +1342,12 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe args.append(" -r ").append(rule.getDstIp()); if (rule.getProtocol() != null) { - args.append(" -P ").append(rule.getProtocol().toLowerCase()); + args.append(" -P ").append(rule.getProtocol().toLowerCase()); } args.append(" -d ").append(rule.getStringSrcPortRange()); args.append(" -G "); - + String result = callHostPlugin(conn, "vmops", "setFirewallRule", "args", args.toString()); if (result == null || result.isEmpty()) { @@ -1371,7 +1368,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (routerIp == null) { return new Answer(cmd); } - + LoadBalancerConfigurator cfgtr = new HAProxyConfigurator(); String[] config = cfgtr.generateConfiguration(cmd); String[][] rules = cfgtr.generateFwRules(cmd); @@ -1391,7 +1388,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe String[] addRules = rules[LoadBalancerConfigurator.ADD]; String[] removeRules = rules[LoadBalancerConfigurator.REMOVE]; String[] statRules = rules[LoadBalancerConfigurator.STATS]; - + String args = ""; args += "-i " + routerIp; args += " -f " + tmpCfgFilePath; @@ -1413,7 +1410,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe args += " -d " + sb.toString(); } - + sb = new StringBuilder(); if (statRules.length > 0) { for (int i = 0; i < statRules.length; i++) { @@ -1422,7 +1419,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe args += " -s " + sb.toString(); } - + result = callHostPlugin(conn, "vmops", "setLoadBalancerRule", "args", args); if (result == null || result.isEmpty()) { @@ -1446,44 +1443,44 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return new Answer(cmd); } - + protected synchronized Answer execute(final RemoteAccessVpnCfgCommand cmd) { Connection conn = getConnection(); String args = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); if (cmd.isCreate()) { - args += " -r " + cmd.getIpRange(); - args += " -p " + cmd.getPresharedKey(); - args += " -s " + cmd.getVpnServerIp(); - args += " -l " + cmd.getLocalIp(); - args += " -c "; - + args += " -r " + cmd.getIpRange(); + args += " -p " + cmd.getPresharedKey(); + args += " -s " + cmd.getVpnServerIp(); + args += " -l " + cmd.getLocalIp(); + args += " -c "; + } else { - args += " -d "; - args += " -s " + cmd.getVpnServerIp(); + args += " -d "; + args += " -s " + cmd.getVpnServerIp(); } String result = callHostPlugin(conn, "vmops", "lt2p_vpn", "args", args); - if (result == null || result.isEmpty()) { - return new Answer(cmd, false, "Configure VPN failed"); - } - return new Answer(cmd); + if (result == null || result.isEmpty()) { + return new Answer(cmd, false, "Configure VPN failed"); + } + return new Answer(cmd); } - + protected synchronized Answer execute(final VpnUsersCfgCommand cmd) { Connection conn = getConnection(); for (VpnUsersCfgCommand.UsernamePassword userpwd: cmd.getUserpwds()) { String args = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); - if (!userpwd.isAdd()) { - args += " -U " + userpwd.getUsername(); - } else { - args += " -u " + userpwd.getUsernamePassword(); - } - String result = callHostPlugin(conn, "vmops", "lt2p_vpn", "args", args); - if (result == null || result.isEmpty()) { - return new Answer(cmd, false, "Configure VPN user failed for user " + userpwd.getUsername()); - } + if (!userpwd.isAdd()) { + args += " -U " + userpwd.getUsername(); + } else { + args += " -u " + userpwd.getUsernamePassword(); + } + String result = callHostPlugin(conn, "vmops", "lt2p_vpn", "args", args); + if (result == null || result.isEmpty()) { + return new Answer(cmd, false, "Configure VPN user failed for user " + userpwd.getUsername()); + } } - - return new Answer(cmd); + + return new Answer(cmd); } protected Answer execute(final VmDataCommand cmd) { @@ -1539,10 +1536,9 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } protected void assignPublicIpAddress(Connection conn, String vmName, String privateIpAddress, String publicIpAddress, boolean add, boolean firstIP, - boolean sourceNat, String vlanId, String vlanGateway, String vlanNetmask, String vifMacAddress, String guestIp, Integer networkRate, TrafficType trafficType, String[] tags) throws InternalErrorException { + boolean sourceNat, String vlanId, String vlanGateway, String vlanNetmask, String vifMacAddress, String guestIp, Integer networkRate, TrafficType trafficType, String name) throws InternalErrorException { try { - String tag = tags != null && tags.length > 0 ? tags[0] : null; VM router = getVM(conn, vmName); NicTO nic = new NicTO(); @@ -1556,12 +1552,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } nic.setDeviceId(0); nic.setNetworkRateMbps(networkRate); - if (tags != null) { - nic.setTags(Arrays.asList(tags)); - } - + nic.setName(name); + Network network = getNetwork(conn, nic); - + // Determine the correct VIF on DomR to associate/disassociate the // IP address with VIF correctVif = getCorrectVif(conn, router, network); @@ -1585,9 +1579,9 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (vifDeviceNum == null) { throw new InternalErrorException("There were no more available slots for a new VIF on router: " + router.getNameLabel(conn)); } - + nic.setDeviceId(Integer.parseInt(vifDeviceNum)); - + correctVif = createVif(conn, vmName, router, nic); correctVif.plug(conn); // Add iptables rule for network usage @@ -1599,7 +1593,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } String args = privateIpAddress; - + if (add) { args += " -A "; } else { @@ -1611,17 +1605,17 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe args += " -l "; args += publicIpAddress + "/" + cidrSize; } else if (firstIP) { - args += " -f"; - args += " -l "; + args += " -f"; + args += " -l "; args += publicIpAddress + "/" + cidrSize; } else { - args += " -l "; + args += " -l "; args += publicIpAddress; } - + args += " -c "; args += "eth" + correctVif.getDevice(conn); - + String result = callHostPlugin(conn, "vmops", "ipassoc", "args", args); if (result == null || result.isEmpty()) { @@ -1661,7 +1655,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return null; } - protected Answer execute(final IpAssocCommand cmd) { + protected Answer execute(IpAssocCommand cmd) { Connection conn = getConnection(); String[] results = new String[cmd.getIpAddresses().length]; int i = 0; @@ -1670,9 +1664,9 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe try { IpAddressTO[] ips = cmd.getIpAddresses(); for (IpAddressTO ip : ips) { - + assignPublicIpAddress(conn, routerName, routerIp, ip.getPublicIp(), ip.isAdd(), ip.isFirstIP(), ip.isSourceNat(), ip.getVlanId(), - ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getGuestIp(), ip.getNetworkRate(), ip.getTrafficType(), ip.getNetworkTags()); + ip.getVlanGateway(), ip.getVlanNetmask(), ip.getVifMacAddress(), ip.getGuestIp(), ip.getNetworkRate(), ip.getTrafficType(), ip.getNetworkName()); results[i++] = ip.getPublicIp() + " - success"; } } catch (InternalErrorException e) { @@ -1683,7 +1677,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new IpAssocAnswer(cmd, results); } - + protected GetVncPortAnswer execute(GetVncPortCommand cmd) { Connection conn = getConnection(); try { @@ -1801,21 +1795,21 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe hostStats.setCpuUtilization(hostStats.getCpuUtilization() + getDataAverage(dataNode, col, numRows)); } -/* + /* if (param.contains("loadavg")) { hostStats.setAverageLoad((hostStats.getAverageLoad() + getDataAverage(dataNode, col, numRows))); } -*/ + */ } } // add the host cpu utilization -/* + /* if (hostStats.getNumCpus() != 0) { hostStats.setCpuUtilization(hostStats.getCpuUtilization() / hostStats.getNumCpus()); s_logger.debug("Host cpu utilization " + hostStats.getCpuUtilization()); } -*/ + */ return hostStats; } @@ -1841,7 +1835,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if( vmStatsUUIDMap == null ) { return new GetVmStatsAnswer(cmd, vmStatsNameMap); } - + for (String vmUUID : vmStatsUUIDMap.keySet()) { vmStatsNameMap.put(vmNames.get(vmUUIDs.indexOf(vmUUID)), vmStatsUUIDMap.get(vmUUID)); } @@ -1908,9 +1902,9 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe vmStatsAnswer.setNumCPUs(vmStatsAnswer.getNumCPUs() + 1); vmStatsAnswer.setCPUUtilization(((vmStatsAnswer.getCPUUtilization() + getDataAverage(dataNode, col, numRows)))); } else if (param.matches("vif_\\d_rx")) { - vmStatsAnswer.setNetworkReadKBs(vmStatsAnswer.getNetworkReadKBs() + (getDataAverage(dataNode, col, numRows)/(8*2))); + vmStatsAnswer.setNetworkReadKBs(vmStatsAnswer.getNetworkReadKBs() + (getDataAverage(dataNode, col, numRows)/(8*2))); } else if (param.matches("vif_\\d_tx")) { - vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + (getDataAverage(dataNode, col, numRows)/(8*2))); + vmStatsAnswer.setNetworkWriteKBs(vmStatsAnswer.getNetworkWriteKBs() + (getDataAverage(dataNode, col, numRows)/(8*2))); } } @@ -1922,7 +1916,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (vmStatsAnswer.getNumCPUs() != 0) { vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization() / vmStatsAnswer.getNumCPUs()); } - + vmStatsAnswer.setCPUUtilization(vmStatsAnswer.getCPUUtilization()*100); if(s_logger.isDebugEnabled()) { s_logger.debug("Vm cpu utilization " + vmStatsAnswer.getCPUUtilization()); @@ -1958,7 +1952,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if(stats == null) { return null; } - + StringReader statsReader = new StringReader(stats); InputSource statsSource = new InputSource(statsReader); @@ -1966,15 +1960,15 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe try { doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(statsSource); } catch (Exception e) { - s_logger.warn("Exception caught whilst processing the document via document factory:", e); - return null; + s_logger.warn("Exception caught whilst processing the document via document factory:", e); + return null; } if(doc==null){ - s_logger.warn("Null document found after tryinh to parse the stats source"); - return null; + s_logger.warn("Null document found after tryinh to parse the stats source"); + return null; } - + NodeList firstLevelChildren = doc.getChildNodes(); NodeList secondLevelChildren = (firstLevelChildren.item(0)).getChildNodes(); Node metaNode = secondLevelChildren.item(0); @@ -2017,29 +2011,29 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if(numRowsUsed == 0) { - if((!Double.isInfinite(value))&&(!Double.isNaN(value))) - { - return value; - } - else - { - s_logger.warn("Found an invalid value (infinity/NaN) in getDataAverage(), numRows=0"); - return dummy; - } + if((!Double.isInfinite(value))&&(!Double.isNaN(value))) + { + return value; + } + else + { + s_logger.warn("Found an invalid value (infinity/NaN) in getDataAverage(), numRows=0"); + return dummy; + } } else { - if((!Double.isInfinite(value/numRowsUsed))&&(!Double.isNaN(value/numRowsUsed))) - { - return (value/numRowsUsed); - } - else - { - s_logger.warn("Found an invalid value (infinity/NaN) in getDataAverage(), numRows>0"); - return dummy; - } + if((!Double.isInfinite(value/numRowsUsed))&&(!Double.isNaN(value/numRowsUsed))) + { + return (value/numRowsUsed); + } + else + { + s_logger.warn("Found an invalid value (infinity/NaN) in getDataAverage(), numRows>0"); + return dummy; + } } - + } protected String getHostStatsRawXML(Connection conn) { @@ -2079,7 +2073,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } - + if (vm_map == null) { return null; } @@ -2171,28 +2165,28 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe protected PrepareForMigrationAnswer execute(PrepareForMigrationCommand cmd) { Connection conn = getConnection(); - + VirtualMachineTO vm = cmd.getVirtualMachine(); if (s_logger.isDebugEnabled()) { s_logger.debug("Preparing host for migrating " + vm); } - + NicTO[] nics = vm.getNics(); try { prepareISO(conn, vm.getName()); - + for (NicTO nic : nics) { getNetwork(conn, nic); } s_vms.put(_cluster, _name, vm.getName(), State.Migrating); - + return new PrepareForMigrationAnswer(cmd); } catch (Exception e) { s_logger.warn("Catch Exception " + e.getClass().getName() + " prepare for migration failed due to " + e.toString(), e); return new PrepareForMigrationAnswer(cmd, e); } } - + private String copy_vhd_to_secondarystorage(Connection conn, String mountpoint, String vdiuuid, String sruuid, int wait) { String results = callHostPluginAsync(conn, "vmopspremium", "copy_vhd_to_secondarystorage", wait, "mountpoint", mountpoint, "vdiuuid", vdiuuid, "sruuid", sruuid); @@ -2211,11 +2205,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException(tmp[1]); } } - + String upgradeSnapshot(Connection conn, String templatePath, String snapshotPath) { String results = callHostPluginAsync(conn, "vmopspremium", "upgrade_snapshot", 2 * 60 * 60, "templatePath", templatePath, "snapshotPath", snapshotPath); - + if (results == null || results.isEmpty()) { String msg = "upgrade_snapshot return null"; s_logger.warn(msg); @@ -2230,11 +2224,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException(results); } } - + String createTemplateFromSnapshot(Connection conn, String templatePath, String snapshotPath, int wait) { String results = callHostPluginAsync(conn, "vmopspremium", "create_privatetemplate_from_snapshot", wait, "templatePath", templatePath, "snapshotPath", snapshotPath); - + if (results == null || results.isEmpty()) { String msg = "create_privatetemplate_from_snapshot return null"; s_logger.warn(msg); @@ -2249,7 +2243,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException(results); } } - + String copy_vhd_from_secondarystorage(Connection conn, String mountpoint, String sruuid, int wait) { String results = callHostPluginAsync(conn, "vmopspremium", "copy_vhd_from_secondarystorage", wait, "mountpoint", mountpoint, "sruuid", sruuid); @@ -2400,7 +2394,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } s_logger.warn(logX(sr, "Unable to remove SR")); } - + private boolean isPVInstalled(Connection conn, VM vm) throws BadServerResponse, XenAPIException, XmlRpcException { VMGuestMetrics vmmetric = vm.getGuestMetrics(conn); if (isRefNull(vmmetric)) { @@ -2466,7 +2460,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.warn(msg); return new MigrateAnswer(cmd, false, msg, null); } - + Set vbds = vm.getVBDs(conn); for( VBD vbd : vbds) { VBD.Record vbdRec = vbd.getRecord(conn); @@ -2535,7 +2529,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException("Com'on no control domain? What the crap?!#@!##$@"); } - + protected ReadyAnswer execute(ReadyCommand cmd) { Connection conn = getConnection(); Long dcId = cmd.getDataCenterId(); @@ -2543,16 +2537,16 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe // snapshots dir fails, let Ready command // succeed. callHostPlugin(conn, "vmopsSnapshot", "unmountSnapshotsDir", "dcId", dcId.toString()); - + setupLinkLocalNetwork(conn); - + _localGateway = callHostPlugin(conn, "vmops", "getgateway", "mgmtIP", _host.ip); if (_localGateway == null || _localGateway.isEmpty()) { String msg = "can not get gateway for host :" + _host.uuid; s_logger.warn(msg); return new ReadyAnswer(cmd, msg); } - + try { boolean result = cleanupHaltedVms(conn); if (!result) { @@ -2565,7 +2559,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.warn("Unable to cleanup halted vms", e); return new ReadyAnswer(cmd, "Unable to cleanup halted vms"); } - + return new ReadyAnswer(cmd); } @@ -2716,7 +2710,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe vm.setMemoryDynamicMax(conn, memsize); vm.setMemoryStaticMax(conn, memsize); } - + private void waitForTask(Connection c, Task task, long pollInterval, long timeout) throws XenAPIException, XmlRpcException { long beginTime = System.currentTimeMillis(); while (task.getStatus(c) == Types.TaskStatusType.PENDING) { @@ -2732,7 +2726,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } } - + private void checkForSuccess(Connection c, Task task) throws XenAPIException, XmlRpcException { if (task.getStatus(c) == Types.TaskStatusType.SUCCESS) { return; @@ -2778,7 +2772,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } } - + void forceShutdownVM(Connection conn, VM vm) { try { Long domId = vm.getDomid(conn); @@ -2791,7 +2785,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException(msg); } } - + void shutdownVM(Connection conn, VM vm, String vmName) throws XmlRpcException { Task task = null; try { @@ -2844,7 +2838,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } } - + void startVM(Connection conn, Host host, VM vm, String vmName) throws XmlRpcException { Task task = null; try { @@ -2874,7 +2868,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } } - + private void migrateVM(Connection conn, Host destHost, VM vm, String vmName) throws XmlRpcException { Task task = null; @@ -2884,7 +2878,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe task = vm.poolMigrateAsync(conn, destHost, other); try { // poll every 1 seconds - long timeout = (long)(_migratewait) * 1000L; + long timeout = (_migratewait) * 1000L; waitForTask(conn, task, 1000, timeout); checkForSuccess(conn, task); } catch (Types.HandleInvalid e) { @@ -2931,14 +2925,14 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } } - + boolean swiftDownload(Connection conn, SwiftTO swift, String container, String rfilename, String dir, String lfilename, Boolean remote) { String result = null; try { result = callHostPluginAsync(conn, "swiftxen", "swift", 60 * 60, - "op", "download", "url", swift.getUrl(), "account", swift.getAccount(), - "username", swift.getUserName(), "key", swift.getKey(), "rfilename", rfilename, - "dir", dir, "lfilename", lfilename, "remote", remote.toString()); + "op", "download", "url", swift.getUrl(), "account", swift.getAccount(), + "username", swift.getUserName(), "key", swift.getKey(), "rfilename", rfilename, + "dir", dir, "lfilename", lfilename, "remote", remote.toString()); if( result != null && result.equals("true")) { return true; } @@ -2947,14 +2941,14 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return false; } - + boolean swiftUpload(Connection conn, SwiftTO swift, String container, String ldir, String lfilename, Boolean isISCSI, int wait) { String result = null; try { result = callHostPluginAsync(conn, "swiftxen", "swift", wait, - "op", "upload", "url", swift.getUrl(), "account", swift.getAccount(), - "username", swift.getUserName(), "key", swift.getKey(), "container", container, - "ldir", ldir, "lfilename", lfilename, "isISCSI", isISCSI.toString()); + "op", "upload", "url", swift.getUrl(), "account", swift.getAccount(), + "username", swift.getUserName(), "key", swift.getKey(), "container", container, + "ldir", ldir, "lfilename", lfilename, "isISCSI", isISCSI.toString()); if( result != null && result.equals("true")) { return true; } @@ -2963,13 +2957,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return false; } - + boolean swiftDelete(Connection conn, SwiftTO swift, String rfilename) { String result = null; try { result = callHostPlugin(conn, "swiftxen", "swift", - "op", "delete", "url", swift.getUrl(), "account", swift.getAccount(), - "username", swift.getUserName(), "key", swift.getKey(), "rfilename", rfilename); + "op", "delete", "url", swift.getUrl(), "account", swift.getAccount(), + "username", swift.getUserName(), "key", swift.getKey(), "rfilename", rfilename); if( result != null && result.equals("true")) { return true; } @@ -2978,7 +2972,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return false; } - + private void swiftBackupSnapshot(Connection conn, SwiftTO swift, String srUuid, String snapshotUuid, String container, Boolean isISCSI, int wait) { String lfilename; String ldir; @@ -2991,7 +2985,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } swiftUpload(conn, swift, container, ldir, lfilename, isISCSI, wait); } - + protected String backupSnapshot(Connection conn, String primaryStorageSRUuid, Long dcId, Long accountId, Long volumeId, String secondaryStorageMountPath, String snapshotUuid, String prevBackupUuid, Boolean isISCSI, int wait) { @@ -3005,7 +2999,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe // Using more lines does not harm the environment. String results = callHostPluginAsync(conn, "vmopsSnapshot", "backupSnapshot", wait, "primaryStorageSRUuid", primaryStorageSRUuid, "dcId", dcId.toString(), "accountId", accountId - .toString(), "volumeId", volumeId.toString(), "secondaryStorageMountPath", + .toString(), "volumeId", volumeId.toString(), "secondaryStorageMountPath", secondaryStorageMountPath, "snapshotUuid", snapshotUuid, "prevBackupUuid", prevBackupUuid, "isISCSI", isISCSI.toString()); @@ -3075,7 +3069,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return null; } - + @Override public StopAnswer execute(StopCommand cmd) { Connection conn = getConnection(); @@ -3183,7 +3177,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe String msg = "VM destroy failed in Stop " + vmName + " Command due to " + e.getMessage(); s_logger.warn(msg, e); } finally { - s_vms.put(_cluster, _name, vmName, state); + s_vms.put(_cluster, _name, vmName, state); } } } @@ -3199,7 +3193,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return new StopAnswer(cmd, "Stop VM failed"); } - + private List getVdis(Connection conn, VM vm) { List vdis = new ArrayList(); try { @@ -3272,7 +3266,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException("When check deviceId " + msg); } - + protected String getUnusedDeviceNum(Connection conn, VM vm) { // Figure out the disk number to attach the VM to try { @@ -3361,7 +3355,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe Bond bond = mgmtPifRec.bondSlaveOf; if ( !isRefNull(bond) ) { String msg = "Management interface is on slave(" +mgmtPifRec.uuid + ") of bond(" - + bond.getUuid(conn) + ") on host(" +_host.uuid + "), please move management interface to bond!"; + + bond.getUuid(conn) + ") on host(" +_host.uuid + "), please move management interface to bond!"; s_logger.warn(msg); throw new CloudRuntimeException(msg); } @@ -3369,8 +3363,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe Network.Record nkRec = nk.getRecord(conn); return new XsLocalNetwork(nk, nkRec, mgmtPif, mgmtPifRec); } - - + + protected VIF getCorrectVif(Connection conn, VM router, Network network) throws XmlRpcException, XenAPIException { Set routerVIFs = router.getVIFs(conn); for (VIF vif : routerVIFs) { @@ -3414,7 +3408,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe protected VDI mount(Connection conn, StoragePoolType pooltype, String volumeFolder, String volumePath) { return getVDIbyUuid(conn, volumePath); } - + /** * getNetworkByName() retrieves what the server thinks is the actual * network used by the XenServer host. This method should always be @@ -3459,11 +3453,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (networks.size() == 1) { return new XsLocalNetwork(networks.iterator().next(), null, null, null); } - + if (networks.size() == 0) { return null; } - + if (s_logger.isDebugEnabled()) { s_logger.debug("Found more than one network with the name " + name); } @@ -3473,7 +3467,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe int earliestRandom = Integer.MAX_VALUE; for (Network network : networks) { XsLocalNetwork nic = new XsLocalNetwork(network); - + if (nic.getPif(conn) != null) { return nic; } @@ -3485,7 +3479,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (stamp == null) { continue; } - + if (stamp.first() < earliestTimestamp || (stamp.first() == earliestTimestamp && stamp.second() < earliestRandom)) { earliestTimestamp = stamp.first(); earliestRandom = stamp.second(); @@ -3498,11 +3492,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return earliestNetwork != null ? new XsLocalNetwork(earliestNetwork, earliestNetworkRecord, null, null) : null; } - + protected String generateTimeStamp() { return new StringBuilder("CsCreateTime-").append(System.currentTimeMillis()).append("-").append(_rand.nextInt()).toString(); } - + protected Pair parseTimestamp(String timeStampStr) { String[] tokens = timeStampStr.split("-"); assert(tokens.length == 3) : "It's our timestamp but it doesn't fit our format: " + timeStampStr; @@ -3511,7 +3505,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return new Pair(Long.parseLong(tokens[1]), Integer.parseInt(tokens[2])); } - + /** * enableVlanNetwork creates a Network object, Vlan object, and thereby * a tagged PIF object in Xapi. @@ -3590,7 +3584,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe PIF nPif = network.getPif(conn); PIF.Record nPifr = network.getPifRecord(conn); - + vlanNetwork = vlanNic.getNetwork(); if (vlanNic.getPif(conn) != null) { return vlanNetwork; @@ -3604,10 +3598,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (s_logger.isDebugEnabled()) { s_logger.debug("VLAN is created for " + tag + ". The uuid is " + vlanr.uuid); } - + return vlanNetwork; } - + protected void disableVlanNetwork(Connection conn, Network network) { } @@ -3753,48 +3747,48 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new PingRoutingCommand(getType(), id, null); } else**/ if (_isOvs) { - List>ovsStates = ovsFullSyncStates(); - return new PingRoutingWithOvsCommand(getType(), id, null, ovsStates); + List>ovsStates = ovsFullSyncStates(); + return new PingRoutingWithOvsCommand(getType(), id, null, ovsStates); }else { - HashMap> nwGrpStates = syncNetworkGroups(conn, id); - return new PingRoutingWithNwGroupsCommand(getType(), id, null, nwGrpStates); + HashMap> nwGrpStates = syncNetworkGroups(conn, id); + return new PingRoutingWithNwGroupsCommand(getType(), id, null, nwGrpStates); } } catch (Exception e) { s_logger.warn("Unable to get current status", e); return null; } } - + private HashMap> syncNetworkGroups(Connection conn, long id) { - HashMap> states = new HashMap>(); - + HashMap> states = new HashMap>(); + String result = callHostPlugin(conn, "vmops", "get_rule_logs_for_vms", "host_uuid", _host.uuid); s_logger.trace("syncNetworkGroups: id=" + id + " got: " + result); String [] rulelogs = result != null ?result.split(";"): new String [0]; for (String rulesforvm: rulelogs){ - String [] log = rulesforvm.split(","); - if (log.length != 6) { - continue; - } - //output = ','.join([vmName, vmID, vmIP, domID, signature, seqno]) - try { - states.put(log[0], new Pair(Long.parseLong(log[1]), Long.parseLong(log[5]))); - } catch (NumberFormatException nfe) { - states.put(log[0], new Pair(-1L, -1L)); - } + String [] log = rulesforvm.split(","); + if (log.length != 6) { + continue; + } + //output = ','.join([vmName, vmID, vmIP, domID, signature, seqno]) + try { + states.put(log[0], new Pair(Long.parseLong(log[1]), Long.parseLong(log[5]))); + } catch (NumberFormatException nfe) { + states.put(log[0], new Pair(-1L, -1L)); + } } - return states; + return states; } @Override public Type getType() { return com.cloud.host.Host.Type.Routing; } - + protected boolean getHostInfo(Connection conn) throws IllegalArgumentException{ try { Host myself = Host.getByUuid(conn, _host.uuid); - + Set hcs = myself.getHostCPUs(conn); _host.cpus = hcs.size(); for (final HostCpu hc : hcs) { @@ -3806,17 +3800,17 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe _host.privatePif = privateNic.getPifRecord(conn).uuid; _host.privateNetwork = privateNic.getNetworkRecord(conn).uuid; _host.systemvmisouuid = null; - + XsLocalNetwork guestNic = null; if (_guestNetworkName != null && !_guestNetworkName.equals(_privateNetworkName)) { - guestNic = getNetworkByName(conn, _guestNetworkName); - if (guestNic == null) { - s_logger.warn("Unable to find guest network " + _guestNetworkName); - throw new IllegalArgumentException("Unable to find guest network " + _guestNetworkName + " for host " + _host.ip); - } + guestNic = getNetworkByName(conn, _guestNetworkName); + if (guestNic == null) { + s_logger.warn("Unable to find guest network " + _guestNetworkName); + throw new IllegalArgumentException("Unable to find guest network " + _guestNetworkName + " for host " + _host.ip); + } } else { - guestNic = privateNic; - _guestNetworkName = _privateNetworkName; + guestNic = privateNic; + _guestNetworkName = _privateNetworkName; } _host.guestNetwork = guestNic.getNetworkRecord(conn).uuid; _host.guestPif = guestNic.getPifRecord(conn).uuid; @@ -3856,13 +3850,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } _host.storageNetwork2 = storageNic2.getNetworkRecord(conn).uuid; _host.storagePif2 = storageNic2.getPifRecord(conn).uuid; - + s_logger.info("Private Network is " + _privateNetworkName + " for host " + _host.ip); s_logger.info("Guest Network is " + _guestNetworkName + " for host " + _host.ip); s_logger.info("Public Network is " + _publicNetworkName + " for host " + _host.ip); s_logger.info("Storage Network 1 is " + _storageNetworkName1 + " for host " + _host.ip); s_logger.info("Storage Network 2 is " + _storageNetworkName2 + " for host " + _host.ip); - + return true; } catch (XenAPIException e) { s_logger.warn("Unable to get host information for " + _host.ip, e); @@ -3899,39 +3893,39 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe VM dom0 = vm.first(); Set vifs = dom0.getVIFs(conn); if (vifs.size() != 0) { - for (VIF vif : vifs) { - Map otherConfig = vif.getOtherConfig(conn); - if (otherConfig != null) { - String nameLabel = otherConfig.get("nameLabel"); - if ((nameLabel != null) && nameLabel.equalsIgnoreCase("link_local_network_vif")) { - dom0vif = vif; - } - } - } + for (VIF vif : vifs) { + Map otherConfig = vif.getOtherConfig(conn); + if (otherConfig != null) { + String nameLabel = otherConfig.get("nameLabel"); + if ((nameLabel != null) && nameLabel.equalsIgnoreCase("link_local_network_vif")) { + dom0vif = vif; + } + } + } } /* create temp VIF0 */ if (dom0vif == null) { - s_logger.debug("Can't find a vif on dom0 for link local, creating a new one"); - VIF.Record vifr = new VIF.Record(); - vifr.VM = dom0; - vifr.device = getLowestAvailableVIFDeviceNum(conn, dom0); - if (vifr.device == null) { - s_logger.debug("Failed to create link local network, no vif available"); - return; - } - Map config = new HashMap(); - config.put("nameLabel", "link_local_network_vif"); - vifr.otherConfig = config; - vifr.MAC = "FE:FF:FF:FF:FF:FF"; - vifr.network = linkLocal; - dom0vif = VIF.create(conn, vifr); - dom0vif.plug(conn); + s_logger.debug("Can't find a vif on dom0 for link local, creating a new one"); + VIF.Record vifr = new VIF.Record(); + vifr.VM = dom0; + vifr.device = getLowestAvailableVIFDeviceNum(conn, dom0); + if (vifr.device == null) { + s_logger.debug("Failed to create link local network, no vif available"); + return; + } + Map config = new HashMap(); + config.put("nameLabel", "link_local_network_vif"); + vifr.otherConfig = config; + vifr.MAC = "FE:FF:FF:FF:FF:FF"; + vifr.network = linkLocal; + dom0vif = VIF.create(conn, vifr); + dom0vif.plug(conn); } else { - s_logger.debug("already have a vif on dom0 for link local network"); - if (!dom0vif.getCurrentlyAttached(conn)) { - dom0vif.plug(conn); - } + s_logger.debug("already have a vif on dom0 for link local network"); + if (!dom0vif.getCurrentlyAttached(conn)) { + dom0vif.plug(conn); + } } String brName = linkLocal.getBridge(conn); @@ -3944,7 +3938,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.warn("Unable to create local link network", e); } } - + protected boolean transferManagementNetwork(Connection conn, Host host, PIF src, PIF.Record spr, PIF dest) throws XmlRpcException, XenAPIException { dest.reconfigureIp(conn, spr.ipConfigurationMode, spr.IP, spr.netmask, spr.gateway, spr.DNS); Host.managementReconfigure(conn, dest); @@ -3974,7 +3968,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe src.reconfigureIp(conn, IpConfigurationMode.NONE, null, null, null, null); return true; } - + @Override public StartupCommand[] initialize() throws IllegalArgumentException{ Connection conn = getConnection(); @@ -3994,7 +3988,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return new StartupCommand[] { cmd }; } - + private void cleanupTemplateSR(Connection conn) { Set pbds = null; try { @@ -4031,7 +4025,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } } - + protected SetupAnswer execute(SetupCommand cmd) { Connection conn = getConnection(); setupServer(conn); @@ -4041,7 +4035,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return null; } _canBridgeFirewall = can_bridge_firewall(conn); - + String result = callHostPluginPremium(conn, "heartbeat", "host", _host.uuid, "interval", Integer .toString(_heartbeatInterval)); if (result == null || !result.contains("> DONE <")) { @@ -4066,7 +4060,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.warn("Unable to setup iscsi: " + result); return new SetupAnswer(cmd, result); } - + Pair mgmtPif = null; Set hostPifs = host.getPIFs(conn); for (PIF pif : hostPifs) { @@ -4075,8 +4069,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (rec.VLAN != null && rec.VLAN != -1) { String msg = new StringBuilder( "Unsupported configuration. Management network is on a VLAN. host=").append( - _host.uuid).append("; pif=").append(rec.uuid).append("; vlan=").append(rec.VLAN) - .toString(); + _host.uuid).append("; pif=").append(rec.uuid).append("; vlan=").append(rec.VLAN) + .toString(); s_logger.warn(msg); return new SetupAnswer(cmd, msg); } @@ -4087,13 +4081,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe break; } } - + if (mgmtPif == null) { String msg = "Unable to find management network for " + _host.uuid; s_logger.warn(msg); return new SetupAnswer(cmd, msg); } - + Map networks = Network.getAllRecords(conn); for (Network.Record network : networks.values()) { if (network.nameLabel.equals("cloud-private")) { @@ -4107,7 +4101,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (pr.VLAN != null && pr.VLAN != -1) { String msg = new StringBuilder( "Unsupported configuration. Network cloud-private is on a VLAN. Network=") - .append(network.uuid).append(" ; pif=").append(pr.uuid).toString(); + .append(network.uuid).append(" ; pif=").append(pr.uuid).toString(); s_logger.warn(msg); return new SetupAnswer(cmd, msg); } @@ -4115,7 +4109,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (pr.bondMasterOf.size() > 1) { String msg = new StringBuilder( "Unsupported configuration. Network cloud-private has more than one bond. Network=") - .append(network.uuid).append("; pif=").append(pr.uuid).toString(); + .append(network.uuid).append("; pif=").append(pr.uuid).toString(); s_logger.warn(msg); return new SetupAnswer(cmd, msg); } @@ -4127,8 +4121,8 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (!transferManagementNetwork(conn, host, slave, spr, pif)) { String msg = new StringBuilder( "Unable to transfer management network. slave=" + spr.uuid - + "; master=" + pr.uuid + "; host=" + _host.uuid) - .toString(); + + "; master=" + pr.uuid + "; host=" + _host.uuid) + .toString(); s_logger.warn(msg); return new SetupAnswer(cmd, msg); } @@ -4195,49 +4189,49 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } for( File file :files) { String path = file.getParentFile().getAbsolutePath() + "/"; - Properties props = new Properties(); - props.load(new FileInputStream(file)); - - for (Map.Entry entry : props.entrySet()) { - String k = (String) entry.getKey(); - String v = (String) entry.getValue(); - - assert (k != null && k.length() > 0 && v != null && v.length() > 0) : "Problems with " + k + "=" + v; - - String[] tokens = v.split(","); - String f = null; - if (tokens.length == 3 && tokens[0].length() > 0) { - if (tokens[0].startsWith("/")) { - f = tokens[0]; - } else if (tokens[0].startsWith("~")) { - String homedir = System.getenv("HOME"); - f = homedir + tokens[0].substring(1) + k; - } else { - f = path + tokens[0] + '/' + k; - } - } else { - f = path + k; - } - String d = tokens[tokens.length - 1]; - f = f.replace('/', File.separatorChar); - - String p = "0755"; - if (tokens.length == 3) { - p = tokens[1]; - } else if (tokens.length == 2) { - p = tokens[0]; - } - - if (!new File(f).exists()) { - s_logger.warn("We cannot locate " + f); - continue; - } - if (s_logger.isDebugEnabled()) { - s_logger.debug("Copying " + f + " to " + d + " on " + hr.address + " with permission " + p); - } - scp.put(f, d, p); - - } + Properties props = new Properties(); + props.load(new FileInputStream(file)); + + for (Map.Entry entry : props.entrySet()) { + String k = (String) entry.getKey(); + String v = (String) entry.getValue(); + + assert (k != null && k.length() > 0 && v != null && v.length() > 0) : "Problems with " + k + "=" + v; + + String[] tokens = v.split(","); + String f = null; + if (tokens.length == 3 && tokens[0].length() > 0) { + if (tokens[0].startsWith("/")) { + f = tokens[0]; + } else if (tokens[0].startsWith("~")) { + String homedir = System.getenv("HOME"); + f = homedir + tokens[0].substring(1) + k; + } else { + f = path + tokens[0] + '/' + k; + } + } else { + f = path + k; + } + String d = tokens[tokens.length - 1]; + f = f.replace('/', File.separatorChar); + + String p = "0755"; + if (tokens.length == 3) { + p = tokens[1]; + } else if (tokens.length == 2) { + p = tokens[0]; + } + + if (!new File(f).exists()) { + s_logger.warn("We cannot locate " + f); + continue; + } + if (s_logger.isDebugEnabled()) { + s_logger.debug("Copying " + f + " to " + d + " on " + hr.address + " with permission " + p); + } + scp.put(f, d, p); + + } } } catch (IOException e) { @@ -4258,14 +4252,14 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException("Unable to get host information ", e); } } - + protected CheckNetworkAnswer execute(CheckNetworkCommand cmd) { if (s_logger.isDebugEnabled()) { s_logger.debug("Checking if network name setup is done on the resource"); } - + List infoList = cmd.getPhysicalNetworkInfoList(); - + try{ boolean errorout = false; String msg = ""; @@ -4283,7 +4277,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe msg = "For Physical Network id:"+ info.getPhysicalNetworkId() + ", Public Network is not configured on the backend by name " + info.getGuestNetworkName(); errorout = true; break; - } + } if(!isNetworkSetupByName(info.getStorageNetworkName())){ msg = "For Physical Network id:"+ info.getPhysicalNetworkId() + ", Storage Network is not configured on the backend by name " + info.getGuestNetworkName(); errorout = true; @@ -4307,7 +4301,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new CheckNetworkAnswer(cmd, false, msg); } } - + protected boolean isNetworkSetupByName(String nameTag) throws XenAPIException, XmlRpcException{ if (nameTag != null) { if (s_logger.isDebugEnabled()) { @@ -4370,7 +4364,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new GetStorageStatsAnswer(cmd, msg); } } - + private void pbdPlug(Connection conn, PBD pbd, String uuid) { try { @@ -4380,7 +4374,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe pbd.plug(conn); } catch (Exception e) { String msg = "PBD " + uuid + " is not attached! and PBD plug failed due to " - + e.toString() + ". Please check this PBD in " + _host; + + e.toString() + ". Please check this PBD in " + _host; s_logger.warn(msg, e); throw new CloudRuntimeException(msg); } @@ -4456,7 +4450,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } - + protected String callHostPluginThroughMaster(Connection conn, String plugin, String cmd, String... params) { Map args = new HashMap(); @@ -4490,7 +4484,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return null; } - + protected String callHostPluginPremium(Connection conn, String cmd, String... params) { return callHostPlugin(conn, "vmopspremium", cmd, params); @@ -4535,7 +4529,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return srUuid; } - + protected Answer execute(ModifyStoragePoolCommand cmd) { Connection conn = getConnection(); StorageFilerTO pool = cmd.getPool(); @@ -4585,11 +4579,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } - + protected boolean can_bridge_firewall(Connection conn) { return Boolean.valueOf(callHostPlugin(conn, "vmops", "can_bridge_firewall", "host_uuid", _host.uuid, "instance", _instance)); } - + private Answer execute(OvsDestroyTunnelCommand cmd) { Connection conn = getConnection(); try { @@ -4597,7 +4591,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (nw == null) { return new Answer(cmd, false, "No network found"); } - + String bridge = nw.getBridge(conn); String result = callHostPlugin(conn, "ovstunnel", "destroy_tunnel", "bridge", bridge, "in_port", cmd.getInPortName()); if (result.equalsIgnoreCase("SUCCESS")) { @@ -4610,13 +4604,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new Answer(cmd, false, e.getMessage()); } } - - + + private Answer execute(UpdateHostPasswordCommand cmd) { _password.add(cmd.getNewPassword()); return new Answer(cmd, true, null); } - + private OvsCreateTunnelAnswer execute(OvsCreateTunnelCommand cmd) { Connection conn = getConnection(); String bridge = "unknown"; @@ -4625,11 +4619,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (nw == null) { return new OvsCreateTunnelAnswer(cmd, false, "Cannot create network", bridge); } - + bridge = nw.getBridge(conn); String result = callHostPlugin(conn, "ovstunnel", "create_tunnel", "bridge", bridge, "remote_ip", cmd.getRemoteIp(), "key", cmd.getKey(), "from", cmd.getFrom().toString(), "to", cmd .getTo().toString()); - + String[] res = result.split(":"); if (res.length == 2 && res[0].equalsIgnoreCase("SUCCESS")) { return new OvsCreateTunnelAnswer(cmd, true, result, res[1], bridge); @@ -4641,113 +4635,113 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new OvsCreateTunnelAnswer(cmd, false, e.getMessage(), bridge); } } - + private Answer execute(OvsDeleteFlowCommand cmd) { - _isOvs = true; - - Connection conn = getConnection(); - try { - Network nw = setupvSwitchNetwork(conn); - String bridge = nw.getBridge(conn); - String result = callHostPlugin(conn, "ovsgre", "ovs_delete_flow", "bridge", bridge, - "vmName", cmd.getVmName()); - - if (result.equalsIgnoreCase("SUCCESS")) { - return new Answer(cmd, true, "success to delete flows for " + cmd.getVmName()); - } else { - return new Answer(cmd, false, result); - } - } catch (Exception e) { - e.printStackTrace(); - } - return new Answer(cmd, false, "failed to delete flow for " + cmd.getVmName()); - } - - private List> ovsFullSyncStates() { - Connection conn = getConnection(); - try { - String result = callHostPlugin(conn, "ovsgre", "ovs_get_vm_log", "host_uuid", _host.uuid); - String [] logs = result != null ?result.split(";"): new String [0]; - List> states = new ArrayList>(); - for (String log: logs){ - String [] info = log.split(","); - if (info.length != 5) { - s_logger.warn("Wrong element number in ovs log(" + log +")"); - continue; - } - - //','.join([bridge, vmName, vmId, seqno, tag]) - try { - states.add(new Pair(info[0], Long.parseLong(info[3]))); - } catch (NumberFormatException nfe) { - states.add(new Pair(info[0], -1L)); - } + _isOvs = true; + + Connection conn = getConnection(); + try { + Network nw = setupvSwitchNetwork(conn); + String bridge = nw.getBridge(conn); + String result = callHostPlugin(conn, "ovsgre", "ovs_delete_flow", "bridge", bridge, + "vmName", cmd.getVmName()); + + if (result.equalsIgnoreCase("SUCCESS")) { + return new Answer(cmd, true, "success to delete flows for " + cmd.getVmName()); + } else { + return new Answer(cmd, false, result); } - - return states; - } catch (Exception e) { - e.printStackTrace(); - } - - return null; + } catch (Exception e) { + e.printStackTrace(); + } + return new Answer(cmd, false, "failed to delete flow for " + cmd.getVmName()); } - + + private List> ovsFullSyncStates() { + Connection conn = getConnection(); + try { + String result = callHostPlugin(conn, "ovsgre", "ovs_get_vm_log", "host_uuid", _host.uuid); + String [] logs = result != null ?result.split(";"): new String [0]; + List> states = new ArrayList>(); + for (String log: logs){ + String [] info = log.split(","); + if (info.length != 5) { + s_logger.warn("Wrong element number in ovs log(" + log +")"); + continue; + } + + //','.join([bridge, vmName, vmId, seqno, tag]) + try { + states.add(new Pair(info[0], Long.parseLong(info[3]))); + } catch (NumberFormatException nfe) { + states.add(new Pair(info[0], -1L)); + } + } + + return states; + } catch (Exception e) { + e.printStackTrace(); + } + + return null; + } + private OvsSetTagAndFlowAnswer execute(OvsSetTagAndFlowCommand cmd) { - _isOvs = true; - - Connection conn = getConnection(); - try { - Network nw = setupvSwitchNetwork(conn); - String bridge = nw.getBridge(conn); - - /*If VM is domainRouter, this will try to set flow and tag on its - * none guest network nic. don't worry, it will fail silently at host - * plugin side - */ - String result = callHostPlugin(conn, "ovsgre", "ovs_set_tag_and_flow", "bridge", bridge, - "vmName", cmd.getVmName(), "tag", cmd.getTag(), - "vlans", cmd.getVlans(), "seqno", cmd.getSeqNo()); - s_logger.debug("set flow for " + cmd.getVmName() + " " + result); - - if (result.equalsIgnoreCase("SUCCESS")) { - return new OvsSetTagAndFlowAnswer(cmd, true, result); - } else { - return new OvsSetTagAndFlowAnswer(cmd, false, result); - } - } catch (Exception e) { - e.printStackTrace(); - } - - return new OvsSetTagAndFlowAnswer(cmd, false, "EXCEPTION"); + _isOvs = true; + + Connection conn = getConnection(); + try { + Network nw = setupvSwitchNetwork(conn); + String bridge = nw.getBridge(conn); + + /*If VM is domainRouter, this will try to set flow and tag on its + * none guest network nic. don't worry, it will fail silently at host + * plugin side + */ + String result = callHostPlugin(conn, "ovsgre", "ovs_set_tag_and_flow", "bridge", bridge, + "vmName", cmd.getVmName(), "tag", cmd.getTag(), + "vlans", cmd.getVlans(), "seqno", cmd.getSeqNo()); + s_logger.debug("set flow for " + cmd.getVmName() + " " + result); + + if (result.equalsIgnoreCase("SUCCESS")) { + return new OvsSetTagAndFlowAnswer(cmd, true, result); + } else { + return new OvsSetTagAndFlowAnswer(cmd, false, result); + } + } catch (Exception e) { + e.printStackTrace(); + } + + return new OvsSetTagAndFlowAnswer(cmd, false, "EXCEPTION"); } - - + + private OvsCreateGreTunnelAnswer execute(OvsCreateGreTunnelCommand cmd) { - _isOvs = true; - - Connection conn = getConnection(); - String bridge = "unkonwn"; - try { - Network nw = setupvSwitchNetwork(conn); - bridge = nw.getBridge(conn); - - String result = callHostPlugin(conn, "ovsgre", "ovs_create_gre", "bridge", bridge, - "remoteIP", cmd.getRemoteIp(), "greKey", cmd.getKey(), "from", - Long.toString(cmd.getFrom()), "to", Long.toString(cmd.getTo())); - String[] res = result.split(":"); - if (res.length != 2 || (res.length == 2 && res[1].equalsIgnoreCase("[]"))) { - return new OvsCreateGreTunnelAnswer(cmd, false, result, - _host.ip, bridge); - } else { - return new OvsCreateGreTunnelAnswer(cmd, true, result, _host.ip, bridge, Integer.parseInt(res[1])); - } - } catch (Exception e) { - e.printStackTrace(); - } - - return new OvsCreateGreTunnelAnswer(cmd, false, "EXCEPTION", _host.ip, bridge); + _isOvs = true; + + Connection conn = getConnection(); + String bridge = "unkonwn"; + try { + Network nw = setupvSwitchNetwork(conn); + bridge = nw.getBridge(conn); + + String result = callHostPlugin(conn, "ovsgre", "ovs_create_gre", "bridge", bridge, + "remoteIP", cmd.getRemoteIp(), "greKey", cmd.getKey(), "from", + Long.toString(cmd.getFrom()), "to", Long.toString(cmd.getTo())); + String[] res = result.split(":"); + if (res.length != 2 || (res.length == 2 && res[1].equalsIgnoreCase("[]"))) { + return new OvsCreateGreTunnelAnswer(cmd, false, result, + _host.ip, bridge); + } else { + return new OvsCreateGreTunnelAnswer(cmd, true, result, _host.ip, bridge, Integer.parseInt(res[1])); + } + } catch (Exception e) { + e.printStackTrace(); + } + + return new OvsCreateGreTunnelAnswer(cmd, false, "EXCEPTION", _host.ip, bridge); } - + private Answer execute(SecurityEgressRulesCmd cmd) { Connection conn = getConnection(); if (s_logger.isTraceEnabled()) { @@ -4758,7 +4752,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.info("Host " + _host.ip + " cannot do bridge firewalling"); return new SecurityEgressRuleAnswer(cmd, false, "Host " + _host.ip + " cannot do bridge firewalling"); } - + String result = callHostPlugin(conn, "vmops", "network_rules", "vmName", cmd.getVmName(), "vmIP", cmd.getGuestIp(), @@ -4777,7 +4771,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new SecurityEgressRuleAnswer(cmd); } } - + private Answer execute(SecurityIngressRulesCmd cmd) { Connection conn = getConnection(); if (s_logger.isTraceEnabled()) { @@ -4787,10 +4781,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (!_canBridgeFirewall) { s_logger.warn("Host " + _host.ip + " cannot do bridge firewalling"); return new SecurityIngressRuleAnswer(cmd, false, - "Host " + _host.ip + " cannot do bridge firewalling", - SecurityIngressRuleAnswer.FailureReason.CANNOT_BRIDGE_FIREWALL); + "Host " + _host.ip + " cannot do bridge firewalling", + SecurityIngressRuleAnswer.FailureReason.CANNOT_BRIDGE_FIREWALL); } - + String result = callHostPlugin(conn, "vmops", "network_rules", "vmName", cmd.getVmName(), "vmIP", cmd.getGuestIp(), @@ -4851,13 +4845,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe cmd.setHypervisorVersion(hr.softwareVersion.get("product_version")); } if (_privateNetworkName != null) { - details.put("private.network.device", _privateNetworkName); + details.put("private.network.device", _privateNetworkName); } if (_publicNetworkName != null) { - details.put("public.network.device", _publicNetworkName); + details.put("public.network.device", _publicNetworkName); } if (_guestNetworkName != null) { - details.put("guest.network.device", _guestNetworkName); + details.put("guest.network.device", _guestNetworkName); } details.put("can_bridge_firewall", Boolean.toString(_canBridgeFirewall)); cmd.setHostDetails(details); @@ -4899,7 +4893,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (s_logger.isDebugEnabled()) { s_logger.debug("Total Ram: " + ram + " dom0 Ram: " + dom0Ram); } - + PIF pif = PIF.getByUuid(conn, _host.privatePif); PIF.Record pifr = pif.getRecord(conn); if (pifr.IP != null && pifr.IP.length() > 0) { @@ -4945,7 +4939,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe public CitrixResourceBase() { } - + @Override public boolean configure(String name, Map params) throws ConfigurationException { @@ -4970,7 +4964,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe _publicNetworkName = (String) params.get("public.network.device"); _guestNetworkName = (String)params.get("guest.network.device"); _instance = (String) params.get("instance.name"); - + _linkLocalPrivateNetworkName = (String) params.get("private.linkLocal.device"); if (_linkLocalPrivateNetworkName == null) { _linkLocalPrivateNetworkName = "cloud_link_local_network"; @@ -4980,13 +4974,13 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe _storageNetworkName2 = (String) params.get("storage.network.device2"); _heartbeatInterval = NumbersUtil.parseInt((String) params.get("xen.heartbeat.interval"), 60); - + String value = (String) params.get("wait"); _wait = NumbersUtil.parseInt(value, 600); - + value = (String) params.get("migratewait"); _migratewait = NumbersUtil.parseInt(value, 3600); - + if (_pod == null) { throw new ConfigurationException("Unable to get the pod"); } @@ -5006,7 +5000,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe if (_host.uuid == null) { throw new ConfigurationException("Unable to get the uuid"); } - + CheckXenHostInfo(); return true; @@ -5024,7 +5018,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe hostRec = host.getRecord(conn); Pool.Record poolRec = Pool.getAllRecords(conn).values().iterator().next(); _host.pool = poolRec.uuid; - + } catch (Exception e) { throw new ConfigurationException("Can not get host information from " + _host.ip); } @@ -5069,7 +5063,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe vdir.nameLabel = dskch.getName(); vdir.SR = poolSr; vdir.type = Types.VdiType.USER; - + vdir.virtualSize = dskch.getSize(); vdi = VDI.create(conn, vdir); } @@ -5079,7 +5073,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.debug("Succesfully created VDI for " + cmd + ". Uuid = " + vdir.uuid); VolumeTO vol = new VolumeTO(cmd.getVolumeId(), dskch.getType(), pool.getType(), pool.getUuid(), vdir.nameLabel, - pool.getPath(), vdir.uuid, vdir.virtualSize, null); + pool.getPath(), vdir.uuid, vdir.virtualSize, null); return new CreateAnswer(cmd, vol); } catch (Exception e) { s_logger.warn("Unable to create volume; Pool=" + pool + "; Disk: " + dskch, e); @@ -5162,7 +5156,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe SR sr = SR.create(conn, host, deviceConfig, new Long(0), uri.getHost() + uri.getPath(), "iso", "iso", "iso", shared, new HashMap()); sr.setNameLabel(conn, vmName + "-ISO"); sr.setNameDescription(conn, deviceConfig.get("location")); - + sr.scan(conn); return sr; } catch (XenAPIException e) { @@ -5255,7 +5249,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } if (target.equals(dc.get("target")) && targetiqn.equals(dc.get("targetIQN")) && lunid.equals(dc.get("lunid"))) { throw new CloudRuntimeException("There is a SR using the same configuration target:" + dc.get("target") + ", targetIQN:" - + dc.get("targetIQN") + ", lunid:" + dc.get("lunid") + " for pool " + pool.getUuid() + "on host:" + _host.uuid); + + dc.get("targetIQN") + ", lunid:" + dc.get("lunid") + " for pool " + pool.getUuid() + "on host:" + _host.uuid); } } deviceConfig.put("target", target); @@ -5308,7 +5302,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } if( pooluuid == null || pooluuid.length() != 36) { sr = SR.create(conn, host, deviceConfig, new Long(0), pool.getUuid(), poolId, type, "user", true, - smConfig); + smConfig); } else { sr = SR.introduce(conn, pooluuid, pool.getUuid(), poolId, type, "user", true, smConfig); @@ -5486,7 +5480,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } } - + protected AttachVolumeAnswer execute(final AttachVolumeCommand cmd) { Connection conn = getConnection(); boolean attach = cmd.getAttach(); @@ -5572,7 +5566,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe // Update the VDI's label to be "detached" vdi.setNameLabel(conn, "detached"); - + umount(conn, vdi); return new AttachVolumeAnswer(cmd); @@ -5590,7 +5584,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } protected void umount(Connection conn, VDI vdi) { - + } protected Answer execute(final AttachIsoCommand cmd) { @@ -5687,7 +5681,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe boolean IsISCSI(String type) { return SRType.LVMOHBA.equals(type) || SRType.LVMOISCSI.equals(type) || SRType.LVM.equals(type) ; } - + protected ManageSnapshotAnswer execute(final ManageSnapshotCommand cmd) { Connection conn = getConnection(); long snapshotId = cmd.getSnapshotId(); @@ -5713,7 +5707,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe // Create a snapshot VDI snapshot = volume.snapshot(conn, new HashMap()); - + if (snapshotName != null) { snapshot.setNameLabel(conn, snapshotName); } @@ -5728,16 +5722,16 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe String type = sr.getType(conn); Boolean isISCSI = IsISCSI(type); String snapshotParentUUID = getVhdParent(conn, srUUID, snapshotUUID, isISCSI); - + String preSnapshotParentUUID = getVhdParent(conn, srUUID, preSnapshotUUID, isISCSI); if( snapshotParentUUID != null && snapshotParentUUID.equals(preSnapshotParentUUID)) { // this is empty snapshot, remove it snapshot.destroy(conn); snapshotUUID = preSnapshotUUID; } - + } - + success = true; details = null; } else if (cmd.getCommandSwitch().equals(ManageSnapshotCommand.DESTROY_SNAPSHOT)) { @@ -5773,7 +5767,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe SR tmpltSR = null; boolean result = false; try { - + URI uri = new URI(secondaryStoragePoolURL); String secondaryStorageMountPath = uri.getHost() + ":" + uri.getPath(); String installPath = "template/tmpl/" + accountId + "/" + templateId; @@ -5820,9 +5814,9 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return new CreatePrivateTemplateAnswer(cmd, result, details); } - + protected Answer execute(final UpgradeSnapshotCommand cmd) { - + String secondaryStorageUrl = cmd.getSecondaryStorageUrl(); String backedUpSnapshotUuid = cmd.getSnapshotUuid(); Long volumeId = cmd.getVolumeId(); @@ -5830,7 +5824,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe Long templateId = cmd.getTemplateId(); Long tmpltAcountId = cmd.getTmpltAccountId(); String version = cmd.getVersion(); - + if ( !version.equals("2.1") ) { return new Answer(cmd, true, "success"); } @@ -5845,7 +5839,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } catch (Exception e) { String details = "upgrading snapshot " + backedUpSnapshotUuid + " failed due to " + e.toString(); s_logger.error(details, e); - + } return new Answer(cmd, false, "failure"); } @@ -5898,7 +5892,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return new CreatePrivateTemplateAnswer(cmd, result, details); } - + private boolean destroySnapshotOnPrimaryStorageExceptThis(Connection conn, String volumeUuid, String avoidSnapshotUuid){ try { VDI volume = getVDIbyUuid(conn, volumeUuid); @@ -5907,10 +5901,10 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } Set snapshots = volume.getSnapshots(conn); for( VDI snapshot : snapshots ) { - try { - if(! snapshot.getUuid(conn).equals(avoidSnapshotUuid)) { - snapshot.destroy(conn); - } + try { + if(! snapshot.getUuid(conn).equals(avoidSnapshotUuid)) { + snapshot.destroy(conn); + } } catch (Exception e) { String msg = "Destroying snapshot: " + snapshot+ " on primary storage failed due to " + e.toString(); s_logger.warn(msg, e); @@ -6043,7 +6037,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe String details = null; String volumeUUID = null; SR snapshotSR = null; - + if (secondaryStorageUrl == null) { details += " because the URL passed: " + secondaryStorageUrl + " is invalid."; return new CreateVolumeFromSnapshotAnswer(cmd, result, details, volumeUUID); @@ -6180,7 +6174,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe throw new CloudRuntimeException("Can not see storage pool: " + pool.getUuid() + " from on host:" + _host.uuid); } } - + protected Answer execute(final CheckConsoleProxyLoadCommand cmd) { return executeProxyLoadScan(cmd, cmd.getProxyVmId(), cmd.getProxyVmName(), cmd.getProxyManagementIp(), cmd.getProxyCmdPort()); @@ -6232,7 +6226,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new ConsoleProxyLoadAnswer(cmd, proxyVmId, proxyVmName, success, result); } - + protected boolean createSecondaryStorageFolder(Connection conn, String remoteMountPath, String newFolder) { String result = callHostPlugin(conn, "vmopsSnapshot", "create_secondary_storage_folder", "remoteMountPath", remoteMountPath, "newFolder", newFolder); return (result != null); @@ -6270,7 +6264,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return success; } - + protected String getVhdParent(Connection conn, String primaryStorageSRUuid, String snapshotUuid, Boolean isISCSI) { String parentUuid = callHostPlugin(conn, "vmopsSnapshot", "getVhdParent", "primaryStorageSRUuid", primaryStorageSRUuid, "snapshotUuid", snapshotUuid, "isISCSI", isISCSI.toString()); @@ -6343,7 +6337,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe public void setAgentControl(IAgentControl agentControl) { _agentControl = agentControl; } - + protected Answer execute(PoolEjectCommand cmd) { Connection conn = getConnection(); String hostuuid = cmd.getHostuuid(); @@ -6351,15 +6345,15 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe Map hostrs = Host.getAllRecords(conn); boolean found = false; for( Host.Record hr : hostrs.values() ) { - if( hr.uuid.equals(hostuuid)) { - found = true; - } + if( hr.uuid.equals(hostuuid)) { + found = true; + } } if( ! found) { s_logger.debug("host " + hostuuid + " has already been ejected from pool " + _host.pool); return new Answer(cmd); } - + Pool pool = Pool.getAll(conn).iterator().next(); Pool.Record poolr = pool.getRecord(conn); @@ -6368,7 +6362,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe s_logger.debug("This is last host to eject, so don't need to eject: " + hostuuid); return new Answer(cmd); } - + Host host = Host.getByUuid(conn, hostuuid); // remove all tags cloud stack add before eject Host.Record hr = host.getRecord(conn); @@ -6381,11 +6375,11 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } // eject from pool try { - Pool.eject(conn, host); - try { - Thread.sleep(10 * 1000); - } catch (InterruptedException e) { - } + Pool.eject(conn, host); + try { + Thread.sleep(10 * 1000); + } catch (InterruptedException e) { + } } catch (XenAPIException e) { String msg = "Unable to eject host " + _host.uuid + " due to " + e.toString(); s_logger.warn(msg); @@ -6402,7 +6396,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new Answer(cmd, false, msg); } } - + private Answer execute(CleanupNetworkRulesCmd cmd) { if (!_canBridgeFirewall) { return new Answer(cmd, true, null); @@ -6420,7 +6414,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe return new Answer(cmd, true, result); } - + /** * XsNic represents a network and the host's specific PIF. @@ -6434,26 +6428,26 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe public XsLocalNetwork(Network n) { this(n, null, null, null); } - + public XsLocalNetwork(Network n, Network.Record nr, PIF p, PIF.Record pr) { _n = n; _nr = nr; _p = p; _pr = pr; } - + public Network getNetwork() { return _n; } - + public Network.Record getNetworkRecord(Connection conn) throws XenAPIException, XmlRpcException { if (_nr == null) { _nr = _n.getRecord(conn); } - + return _nr; } - + public PIF getPif(Connection conn) throws XenAPIException, XmlRpcException { if (_p == null) { Network.Record nr = getNetworkRecord(conn); @@ -6471,7 +6465,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return _p; } - + public PIF.Record getPifRecord(Connection conn) throws XenAPIException, XmlRpcException { if (_pr == null) { PIF p = getPif(conn); @@ -6505,7 +6499,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe public String pool; public int speed; public int cpus; - + @Override public String toString() { return new StringBuilder("XS[").append(uuid).append("-").append(ip).append("]").toString(); @@ -6513,57 +6507,57 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } /*Override by subclass*/ - protected String getGuestOsType(String stdType, boolean bootFromCD) { - return stdType; - } + protected String getGuestOsType(String stdType, boolean bootFromCD) { + return stdType; + } - private Answer execute(NetworkRulesSystemVmCommand cmd) { - boolean success = true; - Connection conn = getConnection(); - if (cmd.getType() != VirtualMachine.Type.User) { - String result = callHostPlugin(conn, "vmops", "default_network_rules_systemvm", "vmName", cmd.getVmName()); - if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) { - success = false; - } - } + private Answer execute(NetworkRulesSystemVmCommand cmd) { + boolean success = true; + Connection conn = getConnection(); + if (cmd.getType() != VirtualMachine.Type.User) { + String result = callHostPlugin(conn, "vmops", "default_network_rules_systemvm", "vmName", cmd.getVmName()); + if (result == null || result.isEmpty() || !Boolean.parseBoolean(result)) { + success = false; + } + } - return new Answer(cmd, success, ""); - } - - protected SetFirewallRulesAnswer execute(SetFirewallRulesCommand cmd) { - String[] results = new String[cmd.getRules().length]; - String callResult; + return new Answer(cmd, success, ""); + } + + protected SetFirewallRulesAnswer execute(SetFirewallRulesCommand cmd) { + String[] results = new String[cmd.getRules().length]; + String callResult; Connection conn = getConnection(); String routerIp = cmd.getAccessDetail(NetworkElementCommand.ROUTER_IP); - if (routerIp == null) { - return new SetFirewallRulesAnswer(cmd, false, results); + if (routerIp == null) { + return new SetFirewallRulesAnswer(cmd, false, results); } - String[][] rules = cmd.generateFwRules(); - String args = ""; - args += routerIp + " -F "; - StringBuilder sb = new StringBuilder(); - String[] fwRules = rules[0]; - if (fwRules.length > 0) { - for (int i = 0; i < fwRules.length; i++) { - sb.append(fwRules[i]).append(','); - } - args += " -a " + sb.toString(); - } - - callResult = callHostPlugin(conn, "vmops", "setFirewallRule", "args", args); + String[][] rules = cmd.generateFwRules(); + String args = ""; + args += routerIp + " -F "; + StringBuilder sb = new StringBuilder(); + String[] fwRules = rules[0]; + if (fwRules.length > 0) { + for (int i = 0; i < fwRules.length; i++) { + sb.append(fwRules[i]).append(','); + } + args += " -a " + sb.toString(); + } + + callResult = callHostPlugin(conn, "vmops", "setFirewallRule", "args", args); + + if (callResult == null || callResult.isEmpty()) { + //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails + for (int i=0; i < results.length; i++) { + results[i] = "Failed"; + } + return new SetFirewallRulesAnswer(cmd, false, results); + } + return new SetFirewallRulesAnswer(cmd, true, results); + } - if (callResult == null || callResult.isEmpty()) { - //FIXME - in the future we have to process each rule separately; now we temporarily set every rule to be false if single rule fails - for (int i=0; i < results.length; i++) { - results[i] = "Failed"; - } - return new SetFirewallRulesAnswer(cmd, false, results); - } - return new SetFirewallRulesAnswer(cmd, true, results); - } - protected Answer execute(final ClusterSyncCommand cmd) { Connection conn = getConnection(); //check if this is master @@ -6598,7 +6592,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe cmd.incrStep(); return new ClusterSyncAnswer(cmd.getClusterId(), newStates, sync_type); } - + protected HashMap> fullClusterSync(Connection conn) { s_vms.clear(_cluster); @@ -6629,20 +6623,20 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } return s_vms.getClusterVmState(_cluster); } - + protected HashMap> deltaClusterSync(Connection conn) { HashMap> newStates; HashMap> oldStates = null; final HashMap> changes = new HashMap>(); - + newStates = getAllVms(conn); if (newStates == null) { s_logger.warn("Unable to get the vm states so no state sync at this point."); return null; } - + synchronized (s_vms) { oldStates = new HashMap>(s_vms.size(_cluster)); oldStates.putAll(s_vms.getClusterVmState(_cluster)); @@ -6653,7 +6647,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe State newState = entry.getValue().second(); String host_uuid = entry.getValue().first(); final Pair oldState = oldStates.remove(vm); - + //check if host is changed if (host_uuid != null && oldState != null){ if (!host_uuid.equals(oldState.first())){ @@ -6662,7 +6656,7 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe continue; } } - + if (newState == State.Stopped && oldState != null && oldState.second() != State.Stopping && oldState.second() != State.Stopped) { newState = getRealPowerState(conn, vm); } @@ -6737,6 +6731,6 @@ public abstract class CitrixResourceBase implements ServerResource, HypervisorRe } - + } diff --git a/server/src/com/cloud/hypervisor/HypervisorGuruBase.java b/server/src/com/cloud/hypervisor/HypervisorGuruBase.java index b4aa34ae4c2..229fa46fa4b 100644 --- a/server/src/com/cloud/hypervisor/HypervisorGuruBase.java +++ b/server/src/com/cloud/hypervisor/HypervisorGuruBase.java @@ -49,7 +49,7 @@ public abstract class HypervisorGuruBase extends AdapterBase implements Hypervis to.setBroadcastUri(profile.getBroadCastUri()); to.setIsolationuri(profile.getIsolationUri()); to.setNetworkRateMbps(profile.getNetworkRate()); - to.setTags(profile.getTags()); + to.setName(profile.getName()); return to; } @@ -78,7 +78,7 @@ public abstract class HypervisorGuruBase extends AdapterBase implements Hypervis } else { to.setArch("x86_64"); } - + to.setDetails(vm.getDetails()); return to; diff --git a/server/src/com/cloud/network/NetworkManager.java b/server/src/com/cloud/network/NetworkManager.java index d740cf328dc..b72463360e5 100644 --- a/server/src/com/cloud/network/NetworkManager.java +++ b/server/src/com/cloud/network/NetworkManager.java @@ -123,7 +123,7 @@ public interface NetworkManager extends NetworkService { void allocate(VirtualMachineProfile vm, List> networks) throws InsufficientCapacityException, ConcurrentOperationException; void prepare(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) throws InsufficientCapacityException, ConcurrentOperationException, - ResourceUnavailableException; + ResourceUnavailableException; void release(VirtualMachineProfile vmProfile, boolean forced); @@ -146,7 +146,7 @@ public interface NetworkManager extends NetworkService { List listPodVlans(long podId); Pair implementNetwork(long networkId, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, ResourceUnavailableException, - InsufficientCapacityException; + InsufficientCapacityException; List listNetworksUsedByVm(long vmId, boolean isSystem); @@ -171,7 +171,7 @@ public interface NetworkManager extends NetworkService { * @throws */ boolean associateIpAddressListToAccount(long userId, long accountId, long zoneId, Long vlanId, Network networkToAssociateWith) throws InsufficientCapacityException, ConcurrentOperationException, - ResourceUnavailableException; + ResourceUnavailableException; Nic getNicInNetwork(long vmId, long networkId); @@ -182,7 +182,7 @@ public interface NetworkManager extends NetworkService { Nic getDefaultNic(long vmId); List getPasswordResetElements(); - + boolean networkIsConfiguredForExternalNetworking(long zoneId, long networkId); Map getNetworkServiceCapabilities(long networkId, Service service); @@ -200,51 +200,51 @@ public interface NetworkManager extends NetworkService { List listNetworksForAccount(long accountId, long zoneId, Network.GuestType type, Boolean isDefault); IPAddressVO markIpAsUnavailable(long addrId); - + public String acquireGuestIpAddress(Network network, String requestedIp); String getGlobalGuestDomainSuffix(); - + String getStartIpAddress(long networkId); boolean applyStaticNats(List staticNats, boolean continueOnError) throws ResourceUnavailableException; - + String getIpInNetwork(long vmId, long networkId); String getIpInNetworkIncludingRemoved(long vmId, long networkId); - + Long getPodIdForVlan(long vlanDbId); - + List listNetworkOfferingsForUpgrade(long networkId); PhysicalNetwork translateZoneIdToPhysicalNetwork(long zoneId); boolean isSecurityGroupSupportedInNetwork(Network network); - + boolean isProviderSupportServiceInNetwork(long networkId, Service service, Provider provider); - + boolean isProviderEnabledInPhysicalNetwork(long physicalNetowrkId, String providerName); - List getNetworkTags(HypervisorType hType, Network network); + String getNetworkTag(HypervisorType hType, Network network); List getElementServices(Provider provider); boolean canElementEnableIndividualServices(Provider provider); - + PhysicalNetworkServiceProvider addDefaultVirtualRouterToPhysicalNetwork(long physicalNetworkId); - + boolean areServicesSupportedInNetwork(long networkId, Service... services); boolean isNetworkSystem(Network network); - boolean reallocate(VirtualMachineProfile vm, - DataCenterDeployment dest) throws InsufficientCapacityException, ConcurrentOperationException; + boolean reallocate(VirtualMachineProfile vm, + DataCenterDeployment dest) throws InsufficientCapacityException, ConcurrentOperationException; Map getNetworkOfferingServiceCapabilities(NetworkOffering offering, Service service); Long getPhysicalNetworkId(Network network); - boolean getAllowSubdomainAccessGlobal(); - - boolean isProviderForNetwork(Provider provider, long networkId); + boolean getAllowSubdomainAccessGlobal(); + + boolean isProviderForNetwork(Provider provider, long networkId); } diff --git a/server/src/com/cloud/network/NetworkManagerImpl.java b/server/src/com/cloud/network/NetworkManagerImpl.java index 420b7d75256..ed5410a96d2 100755 --- a/server/src/com/cloud/network/NetworkManagerImpl.java +++ b/server/src/com/cloud/network/NetworkManagerImpl.java @@ -281,7 +281,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Inject AgentManager _agentMgr; @Inject HostDao _hostDao; @Inject NetworkServiceMapDao _ntwkSrvcDao; - + private final HashMap _systemNetworks = new HashMap(5); ScheduledExecutorService _executor; @@ -303,7 +303,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag HashMap _lastNetworkIdsToFree = new HashMap(); - + private static HashMap> s_serviceToImplementedProvidersMap = new HashMap>(); private static HashMap s_providerToNetworkElementMap = new HashMap(); @@ -312,7 +312,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag NetworkElement element = _networkElements.get(elementName); return element; } - + @Override public List getElementServices(Provider provider){ NetworkElement element = getElementImplementingProvider(provider.getName()); @@ -321,7 +321,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } return new ArrayList(element.getCapabilities().keySet()); } - + @Override public boolean canElementEnableIndividualServices(Provider provider){ NetworkElement element = getElementImplementingProvider(provider.getName()); @@ -330,7 +330,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } return element.canEnableIndividualServices(); } - + @Override public PublicIp assignPublicIpAddress(long dcId, Long podId, Account owner, VlanType type, Long networkId, String requestedIp) throws InsufficientAddressCapacityException { return fetchNewPublicIp(dcId, podId, null, owner, type, networkId, false, true, requestedIp); @@ -338,7 +338,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @DB public PublicIp fetchNewPublicIp(long dcId, Long podId, Long vlanDbId, Account owner, VlanType vlanUse, Long networkId, boolean sourceNat, boolean assign, String requestedIp) - throws InsufficientAddressCapacityException { + throws InsufficientAddressCapacityException { StringBuilder errorMessage = new StringBuilder("Unable to get ip adress in "); Transaction txn = Transaction.currentTxn(); txn.start(); @@ -365,8 +365,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag errorMessage.append(", network id=" + networkId); } sc.setJoinParameters("vlan", "type", vlanUse); - - + + if (requestedIp != null) { sc.addAnd("address", SearchCriteria.Op.EQ, requestedIp); errorMessage.append(": requested ip " + requestedIp + " is not available"); @@ -402,19 +402,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (vlanUse != VlanType.DirectAttached) { addr.setAssociatedWithNetworkId(networkId); } - + _ipAddressDao.update(addr.getId(), addr); txn.commit(); - + if (vlanUse == VlanType.VirtualNetwork) { - _firewallMgr.addSystemFirewallRules(addr, owner); + _firewallMgr.addSystemFirewallRules(addr, owner); } - + long macAddress = NetUtils.createSequenceBasedMacAddress(addr.getMacAddress()); - - + + return new PublicIp(addr, _vlanDao.findById(addr.getVlanId()), macAddress); } @@ -435,7 +435,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag // Save usage event if (owner.getAccountId() != Account.ACCOUNT_ID_SYSTEM) { VlanVO vlan = _vlanDao.findById(addr.getVlanId()); - + String guestType = vlan.getVlanType().toString(); UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_ASSIGN, owner.getId(), addr.getDataCenterId(), addr.getId(), addr.getAddress().toString(), isSourceNat, guestType); @@ -464,7 +464,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag txn.start(); owner = _accountDao.acquireInLockTable(ownerId); - + if (owner == null) { throw new ConcurrentOperationException("Unable to lock account " + ownerId); } @@ -475,7 +475,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag IPAddressVO sourceNat = null; List addrs = listPublicIpAddressesInVirtualNetwork(ownerId, dcId, null, network.getId()); if (addrs.size() == 0) { - + // Check that the maximum number of public IPs for the given accountId will not be exceeded try { _resourceLimitMgr.checkResourceLimit(owner, ResourceType.public_ip); @@ -483,7 +483,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Failed to allocate resource of type " + ex.getResourceType() + " for account " + owner); throw new AccountLimitException("Maximum number of public IP addresses for account: " + owner.getAccountName() + " has been exceeded."); } - + if (s_logger.isDebugEnabled()) { s_logger.debug("assigning a new ip address in " + dcId + " to " + owner); } @@ -621,8 +621,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } return success; } - - + + @Override public List getIsolatedNetworksOwnedByAccountInZone(long zoneId, Account owner) { @@ -644,7 +644,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (ipOwner == null) { throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId + ", permission denied"); } - + _accountMgr.checkAccess(caller, null, ipOwner); DataCenterVO zone = null; @@ -653,9 +653,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (zone == null) { throw new InvalidParameterValueException("Can't find zone by id " + zoneId); } - + if (zone.getNetworkType() == NetworkType.Basic) { - throw new InvalidParameterValueException("Can't associate ip in basic zone"); + throw new InvalidParameterValueException("Can't associate ip in basic zone"); } if (Grouping.AllocationState.Disabled == zone.getAllocationState() && !_accountMgr.isRootAdmin(caller.getType())) { @@ -681,11 +681,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag boolean assign = false; //For basic zone, if there isn't a public network outside of the guest network, specify the vlan type to be direct attached if (zone.getNetworkType() == NetworkType.Basic) { - if (network.getTrafficType() == TrafficType.Guest){ - vlanType = VlanType.DirectAttached; - assign = true; - } - + if (network.getTrafficType() == TrafficType.Guest){ + vlanType = VlanType.DirectAttached; + assign = true; + } + } PublicIp ip = null; @@ -722,7 +722,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag sharedSourceNat = true; } } - + if (!sharedSourceNat) { // First IP address should be source nat when it's being associated with Guest Virtual network List addrs = listPublicIpAddressesInVirtualNetwork(ownerId, zoneId, true, networkId); @@ -871,7 +871,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag NetworkOfferingVO storageNetworkOffering = new NetworkOfferingVO(NetworkOfferingVO.SystemStorageNetwork, TrafficType.Storage); storageNetworkOffering = _networkOfferingDao.persistDefaultNetworkOffering(storageNetworkOffering); _systemNetworks.put(NetworkOfferingVO.SystemStorageNetwork, storageNetworkOffering); - + //populate providers Map> defaultSharedNetworkOfferingProviders = new HashMap>(); Set defaultProviders = new HashSet(); @@ -880,9 +880,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag defaultSharedNetworkOfferingProviders.put(Service.Dhcp, defaultProviders); defaultSharedNetworkOfferingProviders.put(Service.Dns, defaultProviders); defaultSharedNetworkOfferingProviders.put(Service.UserData, defaultProviders); - + Map> defaultIsolatedNetworkOfferingProviders = defaultSharedNetworkOfferingProviders; - + Map> defaultSharedSGEnabledNetworkOfferingProviders = new HashMap>(); defaultSharedSGEnabledNetworkOfferingProviders.put(Service.Dhcp, defaultProviders); defaultSharedSGEnabledNetworkOfferingProviders.put(Service.Dns, defaultProviders); @@ -890,7 +890,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Set sgProviders = new HashSet(); sgProviders.add(Provider.SecurityGroupProvider); defaultSharedSGEnabledNetworkOfferingProviders.put(Service.SecurityGroup, sgProviders); - + Map> defaultIsolatedSourceNatEnabledNetworkOfferingProviders = new HashMap>(); defaultProviders.clear(); defaultProviders.add(Network.Provider.VirtualRouter); @@ -904,11 +904,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.StaticNat, defaultProviders); defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.PortForwarding, defaultProviders); defaultIsolatedSourceNatEnabledNetworkOfferingProviders.put(Service.Vpn, defaultProviders); - + Transaction txn = Transaction.currentTxn(); txn.start(); //diff between offering #1 and #2 - securityGroup is enabled for the first, and disabled for the third - + NetworkOfferingVO offering = null; if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultSharedNetworkOfferingWithSGService) == null) { offering = _configMgr.createNetworkOffering(Account.ACCOUNT_ID_SYSTEM, NetworkOffering.DefaultSharedNetworkOfferingWithSGService, "Offering for Shared Security group enabled networks", TrafficType.Guest, null, null, true, Availability.Optional, null, defaultSharedNetworkOfferingProviders, true, Network.GuestType.Shared, false, null, null); @@ -921,19 +921,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag offering.setState(NetworkOffering.State.Enabled); _networkOfferingDao.update(offering.getId(), offering); } - + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService) == null) { offering = _configMgr.createNetworkOffering(Account.ACCOUNT_ID_SYSTEM,NetworkOffering.DefaultIsolatedNetworkOfferingWithSourceNatService, "Offering for Isolated networks with Source Nat service enabled", TrafficType.Guest, null, null, false, Availability.Required, null, defaultIsolatedSourceNatEnabledNetworkOfferingProviders, true, Network.GuestType.Isolated, false, null, null); offering.setState(NetworkOffering.State.Enabled); _networkOfferingDao.update(offering.getId(), offering); } - + if (_networkOfferingDao.findByUniqueName(NetworkOffering.DefaultIsolatedNetworkOffering) == null) { offering = _configMgr.createNetworkOffering(Account.ACCOUNT_ID_SYSTEM, NetworkOffering.DefaultIsolatedNetworkOffering, "Offering for Isolated networks with no Source Nat service", TrafficType.Guest, null, null, true, Availability.Optional, null, defaultIsolatedNetworkOfferingProviders, true, Network.GuestType.Isolated, false, null, null); offering.setState(NetworkOffering.State.Enabled); _networkOfferingDao.update(offering.getId(), offering); } - + txn.commit(); AccountsUsingNetworkSearch = _accountDao.createSearchBuilder(); @@ -982,11 +982,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag NicForTrafficTypeSearch.done(); _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Network-Scavenger")); - + _allowSubdomainNetworkAccess = Boolean.valueOf(_configs.get(Config.SubDomainNetworkAccess.key())); - + _agentMgr.registerForHostEvents(this, true, false, true); - + s_logger.info("Network Manager is configured."); return true; @@ -999,7 +999,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override public boolean start() { - + //populate s_serviceToImplementedProvidersMap & s_providerToNetworkElementMap with current _networkElements //Need to do this in start() since _networkElements are not completely configured until then. for (NetworkElement element : _networkElements) { @@ -1025,7 +1025,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } } } - + _executor.scheduleWithFixedDelay(new NetworkGarbageCollector(), _networkGcInterval, _networkGcInterval, TimeUnit.SECONDS); return true; } @@ -1069,7 +1069,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (locked == null) { throw new ConcurrentOperationException("Unable to acquire lock on " + owner); } - + try { if (predefined == null || (predefined.getCidr() == null && predefined.getBroadcastUri() == null && predefined.getBroadcastDomainType() != BroadcastDomainType.Vlan)) { List configs = _networksDao.listBy(owner.getId(), offering.getId(), plan.getDataCenterId()); @@ -1122,7 +1122,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (related == -1) { related = id; } - + NetworkVO vo = new NetworkVO(id, network, offering.getId(), guru.getName(), owner.getDomainId(), owner.getId(), related, name, displayText, isDefault, predefined.getNetworkDomain(), offering.getGuestType(), plan.getDataCenterId(), plan.getPhysicalNetworkId(), aclType); networks.add(_networksDao.persist(vo, vo.getGuestType() == Network.GuestType.Isolated, finalizeServicesAndProvidersForNetwork(offering, plan.getPhysicalNetworkId()))); @@ -1222,7 +1222,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag nics.add(vo); Integer networkRate = getNetworkRate(config.getId(), vm.getId()); - vm.addNic(new NicProfile(vo, network.first(), vo.getBroadcastUri(), vo.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network.first()), getNetworkTags(vm.getHypervisorType(), network.first()))); + vm.addNic(new NicProfile(vo, network.first(), vo.getBroadcastUri(), vo.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network.first()), getNetworkTag(vm.getHypervisorType(), network.first()))); } if (nics.size() != networks.size()) { @@ -1388,7 +1388,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throws ConcurrentOperationException, InsufficientAddressCapacityException, ResourceUnavailableException, InsufficientCapacityException { // If this is a 1) guest virtual network 2) network has sourceNat service 3) network offering does not support a Shared source NAT rule, // associate a source NAT IP (if one isn't already associated with the network) - + boolean sharedSourceNat = false; if (areServicesSupportedInNetwork(network.getId(), Service.SourceNat)) { Map sourceNatCapabilities = getNetworkServiceCapabilities(network.getId(), Service.SourceNat); @@ -1399,7 +1399,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } } } - + if (network.getGuestType() == Network.GuestType.Isolated && areServicesSupportedInNetwork(network.getId(), Service.SourceNat) && !sharedSourceNat) { List ips = _ipAddressDao.listByAssociatedNetwork(network.getId(), true); @@ -1409,21 +1409,21 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag assignSourceNatIpAddress(owner, network, context.getCaller().getId()); } } - + //get providers to implement List providersToImplement = getNetworkProviders(network.getId()); for (NetworkElement element : _networkElements) { - if (providersToImplement.contains(element.getProvider())) { - if (!isProviderEnabledInPhysicalNetwork(getPhysicalNetworkId(network), "VirtualRouter")) { - throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + " either doesn't exist or not enabled in physical network id=" + network.getPhysicalNetworkId()); + if (providersToImplement.contains(element.getProvider())) { + if (!isProviderEnabledInPhysicalNetwork(getPhysicalNetworkId(network), "VirtualRouter")) { + throw new CloudRuntimeException("Service provider " + element.getProvider().getName() + " either doesn't exist or not enabled in physical network id=" + network.getPhysicalNetworkId()); } - if (s_logger.isDebugEnabled()) { + if (s_logger.isDebugEnabled()) { s_logger.debug("Asking " + element.getName() + " to implemenet " + network); } if (!element.implement(network, offering, dest, context)) { - throw new CloudRuntimeException("Failed to implement provider " + element.getProvider().getName() + " for network " + network); + throw new CloudRuntimeException("Failed to implement provider " + element.getProvider().getName() + " for network " + network); } - } + } } // reapply all the firewall/staticNat/lb rules @@ -1435,8 +1435,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } protected void prepareElement(NetworkElement element, NetworkVO network, NicProfile profile, VirtualMachineProfile vmProfile, - DeployDestination dest, ReservationContext context) throws InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { + DeployDestination dest, ReservationContext context) throws InsufficientCapacityException, + ConcurrentOperationException, ResourceUnavailableException { element.prepare(network, profile, vmProfile, dest, context); if (vmProfile.getType() == Type.User && element.getProvider() != null) { if (areServicesSupportedInNetwork(network.getId(), Service.Dhcp) && @@ -1453,7 +1453,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } } } - + @DB protected void updateNic(NicVO nic, long networkId, int count) { Transaction txn = Transaction.currentTxn(); @@ -1503,7 +1503,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag URI isolationUri = nic.getIsolationUri(); - profile = new NicProfile(nic, network, broadcastUri, isolationUri, networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTags(vmProfile.getHypervisorType(), network)); + profile = new NicProfile(nic, network, broadcastUri, isolationUri, networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTag(vmProfile.getHypervisorType(), network)); guru.reserve(profile, network, vmProfile, dest, context); nic.setIp4Address(profile.getIp4Address()); nic.setAddressFormat(profile.getFormat()); @@ -1522,7 +1522,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag updateNic(nic, network.getId(), 1); } else { - profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTags(vmProfile.getHypervisorType(), network)); + profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTag(vmProfile.getHypervisorType(), network)); guru.updateNicProfile(profile, network); nic.setState(Nic.State.Reserved); updateNic(nic, network.getId(), 1); @@ -1534,7 +1534,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } prepareElement(element, network, profile, vmProfile, dest, context); } - + profile.setSecurityGroupEnabled(isSecurityGroupSupportedInNetwork(network)); guru.updateNicProfile(profile, network); vmProfile.addNic(profile); @@ -1549,7 +1549,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Integer networkRate = getNetworkRate(network.getId(), vm.getId()); NetworkGuru guru = _networkGurus.get(network.getGuruName()); - NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTags(vm.getHypervisorType(), network)); + NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTag(vm.getHypervisorType(), network)); guru.updateNicProfile(profile, network); vm.addNic(profile); } @@ -1566,7 +1566,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag NetworkGuru guru = _networkGurus.get(network.getGuruName()); nic.setState(Nic.State.Releasing); _nicDao.update(nic.getId(), nic); - NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, isSecurityGroupSupportedInNetwork(network), getNetworkTags(vmProfile.getHypervisorType(), network)); + NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, isSecurityGroupSupportedInNetwork(network), getNetworkTag(vmProfile.getHypervisorType(), network)); if (guru.release(profile, vmProfile, nic.getReservationId())) { applyProfileToNicForRelease(nic, profile); nic.setState(Nic.State.Allocated); @@ -1600,7 +1600,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Integer networkRate = getNetworkRate(network.getId(), vm.getId()); NetworkGuru guru = _networkGurus.get(network.getGuruName()); - NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTags(vm.getHypervisorType(), network)); + NicProfile profile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), networkRate, isSecurityGroupSupportedInNetwork(network), getNetworkTag(vm.getHypervisorType(), network)); guru.updateNicProfile(profile, network); profiles.add(profile); } @@ -1630,7 +1630,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (ipVO.getAllocatedToAccountId() != null) { _accountMgr.checkAccess(caller, null, ipVO); } - + Network associatedNetwork = getNetwork(ipVO.getAssociatedWithNetworkId()); if (areServicesSupportedInNetwork(associatedNetwork.getId(), Service.SourceNat)) { @@ -1695,16 +1695,16 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override public void cleanupNics(VirtualMachineProfile vm) { - if (s_logger.isDebugEnabled()) { + if (s_logger.isDebugEnabled()) { s_logger.debug("Cleaning network for vm: " + vm.getId()); } - + List nics = _nicDao.listByVmId(vm.getId()); for (NicVO nic : nics) { nic.setState(Nic.State.Deallocating); _nicDao.update(nic.getId(), nic); NetworkVO network = _networksDao.findById(nic.getNetworkId()); - NicProfile profile = new NicProfile(nic, network, null, null, null, isSecurityGroupSupportedInNetwork(network), getNetworkTags(vm.getHypervisorType(), network)); + NicProfile profile = new NicProfile(nic, network, null, null, null, isSecurityGroupSupportedInNetwork(network), getNetworkTag(vm.getHypervisorType(), network)); NetworkGuru guru = _networkGurus.get(network.getGuruName()); guru.deallocate(network, profile, vm); _nicDao.remove(nic.getId()); @@ -1747,7 +1747,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (networkOffering == null || networkOffering.isSystemOnly()) { throw new InvalidParameterValueException("Unable to find network offeirng by id " + networkOfferingId); } - //validate physical network and zone // Check if physical network exists PhysicalNetwork pNtwk = null; @@ -1757,9 +1756,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Unable to find physical network by id " + physicalNetworkId); } } - + if (zoneId == null) { - zoneId = pNtwk.getDataCenterId(); + zoneId = pNtwk.getDataCenterId(); } DataCenter zone = _dcDao.findById(zoneId); @@ -1770,47 +1769,46 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag //Only domain and account ACL types are supported in Acton. ACLType aclType = null; if (aclTypeStr != null) { - if (aclTypeStr.equalsIgnoreCase(ACLType.Account.toString())) { - aclType = ACLType.Account; - } else if (aclTypeStr.equalsIgnoreCase(ACLType.Domain.toString())){ - aclType = ACLType.Domain; - } else { - throw new InvalidParameterValueException("Incorrect aclType specified. Check the API documentation for supported types"); - } + if (aclTypeStr.equalsIgnoreCase(ACLType.Account.toString())) { + aclType = ACLType.Account; + } else if (aclTypeStr.equalsIgnoreCase(ACLType.Domain.toString())){ + aclType = ACLType.Domain; + } else { + throw new InvalidParameterValueException("Incorrect aclType specified. Check the API documentation for supported types"); + } } else if (zone.getNetworkType() == NetworkType.Advanced) { - aclType = ACLType.Account; + aclType = ACLType.Account; } else if (zone.getNetworkType() == NetworkType.Basic){ - aclType = ACLType.Domain; + aclType = ACLType.Domain; } // Check if the network is domain specific if (aclType == ACLType.Domain) { - //only Admin can create domain with aclType=Domain - if (!_accountMgr.isAdmin(caller.getType())) { - throw new PermissionDeniedException("Only admin can create networks with aclType=Domain"); - } - - //only shared networks can be Domain specific - if (networkOffering.getGuestType() != GuestType.Shared) { - throw new InvalidParameterValueException("Only " + GuestType.Shared + " networks can have aclType=" + ACLType.Domain); - } - - if (domainId != null) { - if (networkOffering.getTrafficType() != TrafficType.Guest || networkOffering.getGuestType() != Network.GuestType.Shared) { + //only Admin can create domain with aclType=Domain + if (!_accountMgr.isAdmin(caller.getType())) { + throw new PermissionDeniedException("Only admin can create networks with aclType=Domain"); + } + + //only shared networks can be Domain specific + if (networkOffering.getGuestType() != GuestType.Shared) { + throw new InvalidParameterValueException("Only " + GuestType.Shared + " networks can have aclType=" + ACLType.Domain); + } + + if (domainId != null) { + if (networkOffering.getTrafficType() != TrafficType.Guest || networkOffering.getGuestType() != Network.GuestType.Shared) { throw new InvalidParameterValueException("Domain level networks are supported just for traffic type " + TrafficType.Guest + " and guest type " + Network.GuestType.Shared); } - + DomainVO domain = _domainDao.findById(domainId); if (domain == null) { throw new InvalidParameterValueException("Unable to find domain by id " + domainId); } _accountMgr.checkAccess(caller, domain); - } - isDomainSpecific = true; + } + isDomainSpecific = true; } else if (subdomainAccess != null) { - throw new InvalidParameterValueException("Parameter subDomainAccess can be specified only with aclType=Domain"); + throw new InvalidParameterValueException("Parameter subDomainAccess can be specified only with aclType=Domain"); } - Account owner = null; if (cmd.getAccountName() != null && domainId != null) { owner = _accountMgr.finalizeOwner(caller, cmd.getAccountName(), domainId, cmd.getProjectId()); @@ -1878,15 +1876,14 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag Long sharedDomainId = null; if (isDomainSpecific) { - if (domainId != null) { - sharedDomainId = domainId; - } else { - sharedDomainId = _domainMgr.getDomain(Domain.ROOT_DOMAIN).getId(); - } + if (domainId != null) { + sharedDomainId = domainId; + } else { + sharedDomainId = _domainMgr.getDomain(Domain.ROOT_DOMAIN).getId(); + } } Network network = createGuestNetwork(networkOfferingId, name, displayText, isDefault, gateway, cidr, vlanId, networkDomain, owner, false, sharedDomainId, pNtwk, zoneId, aclType, subdomainAccess); - //Vlan is created in 2 cases - works in Advance zone only: //1) GuestType is Shared //2) GuestType is Isolated, but SourceNat service is disabled @@ -1904,87 +1901,87 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override @DB - public Network createGuestNetwork(long networkOfferingId, String name, String displayText, Boolean isDefault, String gateway, String cidr, String vlanId, String networkDomain, Account owner, + public Network createGuestNetwork(long networkOfferingId, String name, String displayText, Boolean isDefault, String gateway, String cidr, String vlanId, String networkDomain, Account owner, boolean isSecurityGroupEnabled, Long domainId, PhysicalNetwork pNtwk, long zoneId, ACLType aclType, Boolean subdomainAccess) throws ConcurrentOperationException, InsufficientCapacityException { - NetworkOfferingVO networkOffering = _networkOfferingDao.findById(networkOfferingId); - //this method supports only guest network creation - if (networkOffering.getTrafficType() != TrafficType.Guest) { - s_logger.warn("Only guest networks can be created using this method"); - return null; - } - - //Validate network offering - if (networkOffering.getState() != NetworkOffering.State.Enabled) { + NetworkOfferingVO networkOffering = _networkOfferingDao.findById(networkOfferingId); + //this method supports only guest network creation + if (networkOffering.getTrafficType() != TrafficType.Guest) { + s_logger.warn("Only guest networks can be created using this method"); + return null; + } + + //Validate network offering + if (networkOffering.getState() != NetworkOffering.State.Enabled) { throw new InvalidParameterValueException("Can't use network offering id=" + networkOfferingId + " as its state is not " + NetworkOffering.State.Enabled); } - - //Validate physical network + + //Validate physical network if (pNtwk.getState() != PhysicalNetwork.State.Enabled) { throw new InvalidParameterValueException("Physical network id " + pNtwk.getId() + " is in incorrect state: " + pNtwk.getState()); } - - //Validate zone - DataCenterVO zone = _dcDao.findById(zoneId); + + //Validate zone + DataCenterVO zone = _dcDao.findById(zoneId); if (zone.getNetworkType() == NetworkType.Basic) { - //Only one guest network is supported in Basic zone - List guestNetworks = _networksDao.listByZoneAndTrafficType(zone.getId(), TrafficType.Guest); - if (!guestNetworks.isEmpty()) { - throw new InvalidParameterValueException("Can't have more than one Guest network in zone with network type " + NetworkType.Basic); - } - - //if zone is basic, only Shared network offerings w/o source nat service are allowed - if (!(networkOffering.getGuestType() == GuestType.Shared && !areServicesSupportedByNetworkOffering(networkOffering.getId(), Service.SourceNat))) { - throw new InvalidParameterValueException("For zone of type " + NetworkType.Basic + " only offerings of guestType " + GuestType.Shared + " with disabled " + Service.SourceNat.getName() + " service are allowed"); - } - - //In Basic zone the network should have aclType=Domain, domainId=1, subdomainAccess=true - if (aclType == null || aclType != ACLType.Domain) { - throw new InvalidParameterValueException("Only AclType=Domain can be specified for network creation in Basic zone"); - } - - if (domainId == null || domainId != Domain.ROOT_DOMAIN) { - throw new InvalidParameterValueException("Guest network in Basic zone should be dedicated to ROOT domain"); - } - - if (subdomainAccess == null) { - subdomainAccess = true; - } else if (!subdomainAccess) { - throw new InvalidParameterValueException("Subdomain access should be set to true for the guest network in the Basic zone"); - } - - if (vlanId == null) { - vlanId = Vlan.UNTAGGED; - } else { - if (!vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) { - throw new InvalidParameterValueException("Only vlan " + Vlan.UNTAGGED + " can be created in the zone of type " + NetworkType.Basic); - } - } - + //Only one guest network is supported in Basic zone + List guestNetworks = _networksDao.listByZoneAndTrafficType(zone.getId(), TrafficType.Guest); + if (!guestNetworks.isEmpty()) { + throw new InvalidParameterValueException("Can't have more than one Guest network in zone with network type " + NetworkType.Basic); + } + + //if zone is basic, only Shared network offerings w/o source nat service are allowed + if (!(networkOffering.getGuestType() == GuestType.Shared && !areServicesSupportedByNetworkOffering(networkOffering.getId(), Service.SourceNat))) { + throw new InvalidParameterValueException("For zone of type " + NetworkType.Basic + " only offerings of guestType " + GuestType.Shared + " with disabled " + Service.SourceNat.getName() + " service are allowed"); + } + + //In Basic zone the network should have aclType=Domain, domainId=1, subdomainAccess=true + if (aclType == null || aclType != ACLType.Domain) { + throw new InvalidParameterValueException("Only AclType=Domain can be specified for network creation in Basic zone"); + } + + if (domainId == null || domainId != Domain.ROOT_DOMAIN) { + throw new InvalidParameterValueException("Guest network in Basic zone should be dedicated to ROOT domain"); + } + + if (subdomainAccess == null) { + subdomainAccess = true; + } else if (!subdomainAccess) { + throw new InvalidParameterValueException("Subdomain access should be set to true for the guest network in the Basic zone"); + } + + if (vlanId == null) { + vlanId = Vlan.UNTAGGED; + } else { + if (!vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) { + throw new InvalidParameterValueException("Only vlan " + Vlan.UNTAGGED + " can be created in the zone of type " + NetworkType.Basic); + } + } + } else if (zone.getNetworkType() == NetworkType.Advanced) { - if (zone.isSecurityGroupEnabled()) { + if (zone.isSecurityGroupEnabled()) { //Only Account specific Isolated network with sourceNat service disabled are allowed in security group enabled zone - boolean allowCreation = (networkOffering.getGuestType() == GuestType.Isolated && !areServicesSupportedByNetworkOffering(networkOffering.getId(), Service.SourceNat)); - if (!allowCreation) { - throw new InvalidParameterValueException("Only Account specific Isolated network with sourceNat service disabled are allowed in security group enabled zone"); - } + boolean allowCreation = (networkOffering.getGuestType() == GuestType.Isolated && !areServicesSupportedByNetworkOffering(networkOffering.getId(), Service.SourceNat)); + if (!allowCreation) { + throw new InvalidParameterValueException("Only Account specific Isolated network with sourceNat service disabled are allowed in security group enabled zone"); + } } } // allow isDefault to be set only for Shared network and Isolated networks with source nat disabled service boolean allowSettingDefault = (zone.getNetworkType() == NetworkType.Advanced && (networkOffering.getGuestType() == GuestType.Shared || (networkOffering.getGuestType() == GuestType.Isolated && !areServicesSupportedByNetworkOffering(networkOffering.getId(), Service.SourceNat)))); if (allowSettingDefault) { - if (isDefault == null) { + if (isDefault == null) { isDefault = false; } } else { - if (zone.getNetworkType() == NetworkType.Basic || isDefault == null) { + if (zone.getNetworkType() == NetworkType.Basic || isDefault == null) { isDefault = true; } else { - throw new InvalidParameterValueException("isDefault parameter can be passed in only for network creation of guestType Shared or Isolated with source nat service disabled; and only in Advance zone"); + throw new InvalidParameterValueException("isDefault parameter can be passed in only for network creation of guestType Shared or Isolated with source nat service disabled; and only in Advance zone"); } } - + // VlanId can be specified only when network offering supports it if (vlanId != null && !networkOffering.getSpecifyVlan()) { throw new InvalidParameterValueException("Can't specify vlan because network offering doesn't support it"); @@ -1998,7 +1995,6 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Network with vlan " + vlanId + " already exists in zone " + zoneId); } } - // If networkDomain is not specified, take it from the global configuration if (areServicesSupportedByNetworkOffering(networkOfferingId, Service.Dns)) { Map dnsCapabilities = getNetworkOfferingServiceCapabilities(_configMgr.getNetworkOffering(networkOfferingId), Service.Dns); @@ -2010,19 +2006,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } else { if (networkDomain == null) { //1) Get networkDomain from the corresponding account/domain/zone - if (aclType == null) { - networkDomain = getZoneNetworkDomain(zoneId); - } else if (aclType == ACLType.Domain) { + if (aclType == null) { + networkDomain = getZoneNetworkDomain(zoneId); + } else if (aclType == ACLType.Domain) { networkDomain = getDomainNetworkDomain(domainId, zoneId); } else if (aclType == ACLType.Account){ networkDomain = getAccountNetworkDomain(owner.getId(), zoneId); } - + //2) If null, generate networkDomain using domain suffix from the global config variables if (networkDomain == null) { networkDomain = "cs" + Long.toHexString(owner.getId()) + _networkDomain; } - + } else { // validate network domain if (!NetUtils.verifyDomainName(networkDomain)) { @@ -2040,7 +2036,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (cidr == null && cidrRequired) { throw new InvalidParameterValueException("StartIp/endIp/gateway/netmask are required when create network of type " + Network.GuestType.Shared + " and network of type " + GuestType.Isolated + " with service " + Service.SourceNat.getName() + " disabled"); } - + //No cidr can be specified in Basic zone if (zone.getNetworkType() == NetworkType.Basic && cidr != null) { throw new InvalidParameterValueException("StartIp/endIp/gateway/netmask can't be specified for zone of type " + NetworkType.Basic); @@ -2131,7 +2127,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (isSystem != null && isSystem && (accountName != null || domainId != null)) { throw new InvalidParameterValueException("System network belongs to system, account and domainId parameters can't be specified"); } - + if (domainId != null) { DomainVO domain = _domainDao.findById(domainId); if (domain == null) { @@ -2144,17 +2140,17 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (owner == null) { throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId); } - + _accountMgr.checkAccess(caller, null, owner); permittedAccounts.add(owner.getId()); } } - + if (!_accountMgr.isAdmin(caller.getType())) { permittedAccounts.add(caller.getId()); domainId = caller.getDomainId(); } - + //set project information if (projectId != null) { permittedAccounts.clear(); @@ -2169,7 +2165,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } else if (caller.getType() == Account.ACCOUNT_TYPE_NORMAL){ permittedAccounts.addAll(_projectMgr.listPermittedProjectAccounts(caller.getId())); } - + path = _domainDao.findById(caller.getDomainId()).getPath(); Filter searchFilter = new Filter(NetworkVO.class, "id", false, cmd.getStartIndex(), cmd.getPageSizeVal()); @@ -2187,7 +2183,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag zoneSearch.and("networkType", zoneSearch.entity().getNetworkType(), SearchCriteria.Op.EQ); sb.join("zoneSearch", zoneSearch, sb.entity().getDataCenterId(), zoneSearch.entity().getId(), JoinBuilder.JoinType.INNER); sb.and("removed", sb.entity().getRemoved(), Op.NULL); - + if (permittedAccounts.isEmpty()) { SearchBuilder domainSearch = _domainDao.createSearchBuilder(); domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE); @@ -2195,7 +2191,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } List networksToReturn = new ArrayList(); - + if (isSystem == null || !isSystem) { //Get domain level networks if (domainId != null) { @@ -2203,7 +2199,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } else if (permittedAccounts.isEmpty()){ networksToReturn.addAll(listAccountSpecificNetworksByDomainPath(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, isDefault, trafficType, physicalNetworkId, aclType), searchFilter, path)); } - + //get account specific networks if (!permittedAccounts.isEmpty()){ networksToReturn.addAll(listAccountSpecificNetworks(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, isDefault, trafficType, physicalNetworkId, aclType), searchFilter, permittedAccounts)); @@ -2211,7 +2207,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } else { networksToReturn = _networksDao.search(buildNetworkSearchCriteria(sb, keyword, id, isSystem, zoneId, guestIpType, isDefault, trafficType, physicalNetworkId, null), searchFilter); } - + if (supportedServicesStr != null && !supportedServicesStr.isEmpty() && !networksToReturn.isEmpty()) { List supportedNetworks = new ArrayList(); Service[] suppportedServices = new Service[supportedServicesStr.size()]; @@ -2225,13 +2221,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } i++; } - + for (NetworkVO network : networksToReturn) { if (areServicesSupportedInNetwork(network.getId(), suppportedServices)) { supportedNetworks.add(network); } } - + return supportedNetworks; } else { return networksToReturn; @@ -2274,37 +2270,37 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (aclType != null) { sc.addAnd("aclType", SearchCriteria.Op.EQ, aclType.toString()); } - + if (physicalNetworkId != null) { sc.addAnd("physicalNetworkId", SearchCriteria.Op.EQ, physicalNetworkId); } - + return sc; } private List listDomainLevelNetworks(SearchCriteria sc, Filter searchFilter, long domainId) { - List networkIds = new ArrayList(); - Set allowedDomains = _domainMgr.getDomainParentIds(domainId); - List maps = _networkDomainDao.listDomainNetworkMapByDomain(allowedDomains.toArray()); - - for (NetworkDomainVO map : maps) { - boolean subdomainAccess = (map.isSubdomainAccess() != null) ? map.isSubdomainAccess() : getAllowSubdomainAccessGlobal(); - if (map.getDomainId() == domainId || subdomainAccess) { - networkIds.add(map.getNetworkId()); - } - } - - if (!networkIds.isEmpty()) { - SearchCriteria domainSC = _networksDao.createSearchCriteria(); - domainSC.addAnd("id", SearchCriteria.Op.IN, networkIds.toArray()); - domainSC.addAnd("aclType", SearchCriteria.Op.EQ, ACLType.Domain.toString()); - + List networkIds = new ArrayList(); + Set allowedDomains = _domainMgr.getDomainParentIds(domainId); + List maps = _networkDomainDao.listDomainNetworkMapByDomain(allowedDomains.toArray()); + + for (NetworkDomainVO map : maps) { + boolean subdomainAccess = (map.isSubdomainAccess() != null) ? map.isSubdomainAccess() : getAllowSubdomainAccessGlobal(); + if (map.getDomainId() == domainId || subdomainAccess) { + networkIds.add(map.getNetworkId()); + } + } + + if (!networkIds.isEmpty()) { + SearchCriteria domainSC = _networksDao.createSearchCriteria(); + domainSC.addAnd("id", SearchCriteria.Op.IN, networkIds.toArray()); + domainSC.addAnd("aclType", SearchCriteria.Op.EQ, ACLType.Domain.toString()); + sc.addAnd("id", SearchCriteria.Op.SC, domainSC); return _networksDao.search(sc, searchFilter); - } else { - return new ArrayList(); - } + } else { + return new ArrayList(); + } } private List listAccountSpecificNetworks(SearchCriteria sc, Filter searchFilter, List permittedAccounts) { @@ -2312,7 +2308,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (!permittedAccounts.isEmpty()) { accountSC.addAnd("accountId", SearchCriteria.Op.IN, permittedAccounts.toArray()); } - + accountSC.addAnd("aclType", SearchCriteria.Op.EQ, ACLType.Account.toString()); sc.addAnd("id", SearchCriteria.Op.SC, accountSC); @@ -2320,13 +2316,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } private List listAccountSpecificNetworksByDomainPath(SearchCriteria sc, Filter searchFilter, String path) { - SearchCriteria accountSC = _networksDao.createSearchCriteria(); - accountSC.addAnd("aclType", SearchCriteria.Op.EQ, ACLType.Account.toString()); - + SearchCriteria accountSC = _networksDao.createSearchCriteria(); + accountSC.addAnd("aclType", SearchCriteria.Op.EQ, ACLType.Account.toString()); + if (path != null) { sc.setJoinParameters("domainSearch", "path", path + "%"); } - + return _networksDao.search(sc, searchFilter); } @@ -2415,24 +2411,24 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Failed to cleanup network id=" + network.getId() + " resources as a part of shutdownNetwork"); } } - + //2) Shutdown all the network elements //get providers to shutdown List providersToShutdown = getNetworkProviders(network.getId()); boolean success = true; for (NetworkElement element : _networkElements) { - if (providersToShutdown.contains(element.getProvider())) { - try { - if (!isProviderEnabledInPhysicalNetwork(getPhysicalNetworkId(network), "VirtualRouter")) { - s_logger.warn("Unable to complete shutdown of the network elements due to element: " + element.getName() + " either doesn't exist or not enabled in the physical network " + getPhysicalNetworkId(network)); - success = false; + if (providersToShutdown.contains(element.getProvider())) { + try { + if (!isProviderEnabledInPhysicalNetwork(getPhysicalNetworkId(network), "VirtualRouter")) { + s_logger.warn("Unable to complete shutdown of the network elements due to element: " + element.getName() + " either doesn't exist or not enabled in the physical network " + getPhysicalNetworkId(network)); + success = false; } if (s_logger.isDebugEnabled()) { s_logger.debug("Sending network shutdown to " + element.getName()); } if (!element.shutdown(network, context, cleanupElements)) { - s_logger.warn("Unable to complete shutdown of the network elements due to element: " + element.getName()); - success = false; + s_logger.warn("Unable to complete shutdown of the network elements due to element: " + element.getName()); + success = false; } } catch (ResourceUnavailableException e) { s_logger.warn("Unable to complete shutdown of the network elements due to element: " + element.getName(), e); @@ -2444,7 +2440,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Unable to complete shutdown of the network elements due to element: " + element.getName(), e); success = false; } - } + } } return success; } @@ -2496,29 +2492,29 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag //get providers to destroy List providersToDestroy = getNetworkProviders(network.getId()); for (NetworkElement element : _networkElements) { - if (providersToDestroy.contains(element.getProvider())) { - try { - if (!isProviderEnabledInPhysicalNetwork(getPhysicalNetworkId(network), "VirtualRouter")) { - s_logger.warn("Unable to complete destroy of the network elements due to element: " + element.getName() + " either doesn't exist or not enabled in the physical network " + getPhysicalNetworkId(network)); - success = false; + if (providersToDestroy.contains(element.getProvider())) { + try { + if (!isProviderEnabledInPhysicalNetwork(getPhysicalNetworkId(network), "VirtualRouter")) { + s_logger.warn("Unable to complete destroy of the network elements due to element: " + element.getName() + " either doesn't exist or not enabled in the physical network " + getPhysicalNetworkId(network)); + success = false; } - - if (s_logger.isDebugEnabled()) { - s_logger.debug("Sending destroy to " + element); - } - - element.destroy(network); - } catch (ResourceUnavailableException e) { - s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e); - success = false; - } catch (ConcurrentOperationException e) { - s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e); - success = false; - } catch (Exception e) { - s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e); - success = false; - } - } + + if (s_logger.isDebugEnabled()) { + s_logger.debug("Sending destroy to " + element); + } + + element.destroy(network); + } catch (ResourceUnavailableException e) { + s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e); + success = false; + } catch (ConcurrentOperationException e) { + s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e); + success = false; + } catch (Exception e) { + s_logger.warn("Unable to complete destroy of the network due to element: " + element.getName(), e); + success = false; + } + } } if (success) { @@ -2578,13 +2574,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (!(ne instanceof LoadBalancingServiceProvider)) { continue; } - handled = ((LoadBalancingServiceProvider)ne).applyLBRules(network, (List)rules); + handled = ((LoadBalancingServiceProvider)ne).applyLBRules(network, (List)rules); break; case PortForwarding: if (!(ne instanceof PortForwardingServiceProvider)) { continue; } - handled = ((PortForwardingServiceProvider)ne).applyPFRules(network, (List)rules); + handled = ((PortForwardingServiceProvider)ne).applyPFRules(network, (List)rules); break; case StaticNat: /* It's firewall rule for static nat, not static nat rule */ @@ -2725,33 +2721,33 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag NetworkVO network = _networksDao.findById(networkId); s_logger.debug("Restarting network " + networkId + "..."); - + //shutdown the network ReservationContext context = new ReservationContextImpl(null, null, callerUser, callerAccount); s_logger.debug("Shutting down the network id=" + networkId + " as a part of network restart"); - + if (!shutdownNetworkElementsAndResources(context, cleanup, network)) { s_logger.debug("Failed to shutdown the network elements and resources as a part of network restart: " + network.getState()); return false; } - + //implement the network elements and rules again DeployDestination dest = new DeployDestination(_dcDao.findById(network.getDataCenterId()), null, null, null); - + s_logger.debug("Implementing the network " + network + " elements and resources as a part of network restart"); NetworkOfferingVO offering = _networkOfferingDao.findById(network.getNetworkOfferingId()); - + try { implementNetworkElementsAndResources(dest, context, network, offering); } catch (Exception ex) { s_logger.warn("Failed to implement network " + network + " elements and resources as a part of network restart due to ", ex); return false; } - + return true; } - + //This method re-programs the rules/ips for existing network protected boolean reprogramNetworkRules(long networkId, Account caller, NetworkVO network) throws ResourceUnavailableException { boolean success = true; @@ -2760,7 +2756,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Failed to apply ip addresses as a part of network id" + networkId + " restart"); success = false; } - + // apply static nat if (!_rulesMgr.applyStaticNatsForNetwork(networkId, false, caller)) { s_logger.warn("Failed to apply static nats a part of network id" + networkId + " restart"); @@ -2811,7 +2807,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return _networksDao.getActiveNicsIn(networkId); } - + @Override public Map> getNetworkCapabilities(long networkId) { @@ -2839,48 +2835,48 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (!areServicesSupportedInNetwork(networkId, service)) { throw new UnsupportedServiceException("Service " + service.getName() + " is not supported in the network id=" + networkId); } - + Map serviceCapabilities = new HashMap(); //get the Provider for this Service for this offering String provider = _ntwkSrvcDao.getProviderForServiceInNetwork(networkId, service); - + NetworkElement element = getElementImplementingProvider(provider); if(element != null){ Map> elementCapabilities = element.getCapabilities();; - + if (elementCapabilities == null || elementCapabilities.get(service) == null) { throw new UnsupportedServiceException("Service " + service.getName() + " is not supported by the element=" + element.getName() + " implementing Provider=" + provider); } serviceCapabilities = elementCapabilities.get(service); } - + return serviceCapabilities; } - + @Override public Map getNetworkOfferingServiceCapabilities(NetworkOffering offering, Service service) { if (!areServicesSupportedByNetworkOffering(offering.getId(), service)) { throw new UnsupportedServiceException("Service " + service.getName() + " is not supported by the network offering " + offering); } - + Map serviceCapabilities = new HashMap(); //get the Provider for this Service for this offering String provider = _ntwkOfferingSrvcDao.getProviderForServiceForNetworkOffering(offering.getId(), service); - + //FIXME we return the capabilities of the first provider of the service - what if we have multiple providers for same Service? NetworkElement element = getElementImplementingProvider(provider); if(element != null){ Map> elementCapabilities = element.getCapabilities();; - + if (elementCapabilities == null || elementCapabilities.get(service) == null) { throw new UnsupportedServiceException("Service " + service.getName() + " is not supported by the element=" + element.getName() + " implementing Provider=" + provider); } serviceCapabilities = elementCapabilities.get(service); } - + return serviceCapabilities; } @@ -2944,7 +2940,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (nics != null) { for (Nic nic : nics) { NetworkVO network = _networksDao.findByIdIncludingRemoved(nic.getNetworkId()); - + if (isNetworkSystem(network) == isSystem) { networks.add(network); } @@ -2965,7 +2961,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag assert (guestNic != null && guestNic.getIp4Address() != null) : "Vm doesn't belong to network associated with ipAddress or ip4 address is null"; return guestNic.getIp4Address(); } - + @Override public String getIpInNetworkIncludingRemoved(long vmId, long networkId) { Nic guestNic = getNicInNetworkIncludingRemoved(vmId, networkId); @@ -2973,7 +2969,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return guestNic.getIp4Address(); } - + private Nic getNicInNetworkIncludingRemoved(long vmId, long networkId) { return _nicDao.findByInstanceIdAndNetworkIdIncludingRemoved(networkId, vmId); } @@ -3110,26 +3106,26 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } return elements; } - - + + @Override public boolean networkIsConfiguredForExternalNetworking(long zoneId, long networkId) { - boolean netscalerInNetwork = isProviderForNetwork(Network.Provider.Netscaler, networkId); - boolean juniperInNetwork = isProviderForNetwork(Network.Provider.JuniperSRX, networkId); - boolean f5InNetwork = isProviderForNetwork(Network.Provider.F5BigIp, networkId); - - if (netscalerInNetwork || juniperInNetwork || f5InNetwork) { - return true; - } else { - return false; - } + boolean netscalerInNetwork = isProviderForNetwork(Network.Provider.Netscaler, networkId); + boolean juniperInNetwork = isProviderForNetwork(Network.Provider.JuniperSRX, networkId); + boolean f5InNetwork = isProviderForNetwork(Network.Provider.F5BigIp, networkId); + + if (netscalerInNetwork || juniperInNetwork || f5InNetwork) { + return true; + } else { + return false; + } } @Override public boolean areServicesSupportedByNetworkOffering(long networkOfferingId, Service... services) { return (_ntwkOfferingSrvcDao.areServicesSupportedByNetworkOffering(networkOfferingId, services)); } - + @Override public boolean areServicesSupportedInNetwork(long networkId, Service... services) { return (_ntwkSrvcDao.areServicesSupportedInNetwork(networkId, services)); @@ -3137,7 +3133,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag private boolean cleanupIpResources(long ipId, long userId, Account caller) { boolean success = true; - + //Revoke all firewall rules for the ip try { s_logger.debug("Revoking all " + Purpose.Firewall + "rules as a part of public IP id=" + ipId + " release..."); @@ -3179,7 +3175,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Unable to destroy remote access vpn for ip id=" + ipId + " as a part of ip release", e); success = false; } - + return success; } @@ -3247,7 +3243,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag // Save usage event if (ip.getAccountId() != Account.ACCOUNT_ID_SYSTEM) { VlanVO vlan = _vlanDao.findById(ip.getVlanId()); - + String guestType = vlan.getVlanType().toString(); UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_NET_IP_RELEASE, ip.getAccountId(), ip.getDataCenterId(), addrId, ip.getAddress().addr(), isSourceNat, guestType); @@ -3282,7 +3278,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (domainId == networkDomainId.longValue()) { return true; } - + if (networkDomainMap.subdomainAccess) { Set parentDomains = _domainMgr.getDomainParentIds(domainId); @@ -3316,7 +3312,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } _accountMgr.checkAccess(callerAccount, null, network); - + // Don't allow to update system network NetworkOffering offering = _networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId()); if (offering.isSystemOnly()) { @@ -3330,25 +3326,25 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (displayText != null) { network.setDisplayText(displayText); } - + long oldNetworkOfferingId = network.getNetworkOfferingId(); if (networkOfferingId != null) { - + NetworkOfferingVO networkOffering = _networkOfferingDao.findById(networkOfferingId); if (networkOffering == null || networkOffering.isSystemOnly()) { throw new InvalidParameterValueException("Unable to find network offering by id " + networkOfferingId); } - + // Network offering upgrade is allowed for isolated networks only if (network.getGuestType() != GuestType.Isolated) { throw new InvalidParameterValueException("Network offering upgrade is allowed only for networks with guest type " + GuestType.Isolated); } - + //network offering should be in Enabled state if (networkOffering.getState() != NetworkOffering.State.Enabled) { throw new InvalidParameterValueException("Network offering " + networkOffering + " is not in " + NetworkOffering.State.Enabled + " state, can't upgrade to it"); } - + if (networkOfferingId != oldNetworkOfferingId) { //check if the network is upgradable if (!canUpgrade(oldNetworkOfferingId, networkOfferingId)) { @@ -3357,7 +3353,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag restartNetwork = true; } } - + //don't allow to modify network domain if the service is not supported if (domainSuffix != null) { // validate network domain @@ -3366,17 +3362,17 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag "Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters 'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\""); } - + //don't allow to update shared network if (offering.getGuestType() != GuestType.Isolated) { - throw new InvalidParameterValueException("networkDomain can be upgraded only for the network of type " + GuestType.Isolated); + throw new InvalidParameterValueException("networkDomain can be upgraded only for the network of type " + GuestType.Isolated); } - + long offeringId = oldNetworkOfferingId; if (networkOfferingId != null) { offeringId = networkOfferingId; } - + Map dnsCapabilities = getNetworkOfferingServiceCapabilities(_configMgr.getNetworkOffering(offeringId), Service.Dns); String isUpdateDnsSupported = dnsCapabilities.get(Capability.AllowDnsSuffixModification); if (isUpdateDnsSupported == null || !Boolean.valueOf(isUpdateDnsSupported)) { @@ -3387,42 +3383,42 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag //have to restart the network restartNetwork = true; } - + //1) Shutdown all the elements and cleanup all the rules ReservationContext context = new ReservationContextImpl(null, null, callerUser, callerAccount); if (restartNetwork) { s_logger.debug("Shutting down elements and resources for network id=" + networkId + " as a part of network update"); - + if (!shutdownNetworkElementsAndResources(context, true, network)) { s_logger.warn("Failed to shutdown the network elements and resources as a part of network restart: " + network.getState()); throw new CloudRuntimeException("Failed to shutdown the network elements and resources as a part of network restart: " + network.getState()); } } - + //2) Only after all the elements and rules are shutdown properly, update the network VO if (networkOfferingId != null) { - network.setNetworkOfferingId(networkOfferingId); - _networksDao.update(networkId, network, finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(networkOfferingId), network.getPhysicalNetworkId())); + network.setNetworkOfferingId(networkOfferingId); + _networksDao.update(networkId, network, finalizeServicesAndProvidersForNetwork(_configMgr.getNetworkOffering(networkOfferingId), network.getPhysicalNetworkId())); } else { _networksDao.update(networkId, network); } - + //get updated network network = _networksDao.findById(networkId); - + //3) Implement the elements and rules again if (restartNetwork) { - DeployDestination dest = new DeployDestination(_dcDao.findById(network.getDataCenterId()), null, null, null); - - s_logger.debug("Implementing the network " + network + " elements and resources as a part of network update"); - try { - implementNetworkElementsAndResources(dest, context, network, _networkOfferingDao.findById(network.getNetworkOfferingId())); - } catch (Exception ex) { - s_logger.warn("Failed to implement network " + network + " elements and resources as a part of network update due to ", ex); - throw new CloudRuntimeException("Failed to implement network " + network + " elements and resources as a part of network update"); - } + DeployDestination dest = new DeployDestination(_dcDao.findById(network.getDataCenterId()), null, null, null); + + s_logger.debug("Implementing the network " + network + " elements and resources as a part of network update"); + try { + implementNetworkElementsAndResources(dest, context, network, _networkOfferingDao.findById(network.getNetworkOfferingId())); + } catch (Exception ex) { + s_logger.warn("Failed to implement network " + network + " elements and resources as a part of network update due to ", ex); + throw new CloudRuntimeException("Failed to implement network " + network + " elements and resources as a part of network update"); + } } - + return getNetwork(network.getId()); } @@ -3454,7 +3450,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } Random _rand = new Random(System.currentTimeMillis()); - + @Override @DB public String acquireGuestIpAddress(Network network, String requestedIp) { @@ -3462,18 +3458,18 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag String[] cidr = network.getCidr().split("/"); Set allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1])); Set usedIps = new TreeSet(); - + if (requestedIp != null && requestedIp.equals(network.getGateway())) { s_logger.warn("Requested ip address " + requestedIp + " is used as a gateway address in network " + network); return null; } - + for (String ip : ips) { if (requestedIp != null && requestedIp.equals(ip)) { s_logger.warn("Requested ip address " + requestedIp + " is already in use in network " + network); return null; } - + usedIps.add(NetUtils.ip2Long(ip)); } if (usedIps.size() != 0) { @@ -3482,9 +3478,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (allPossibleIps.isEmpty()) { return null; } - + Long[] array = allPossibleIps.toArray(new Long[allPossibleIps.size()]); - + if (requestedIp != null) { //check that requested ip has the same cidr boolean isSameCidr = NetUtils.sameSubnetCIDR(requestedIp, NetUtils.long2Ip(array[0]), Integer.parseInt(cidr[1])); @@ -3495,66 +3491,66 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return requestedIp; } } - + String result; do { result = NetUtils.long2Ip(array[_rand.nextInt(array.length)]); } while (result.split("\\.")[3].equals("1")); return result; } - + private String getZoneNetworkDomain(long zoneId) { return _dcDao.findById(zoneId).getDomain(); } - + private String getDomainNetworkDomain(long domainId, long zoneId) { String networkDomain = _domainDao.findById(domainId).getNetworkDomain(); if (networkDomain == null) { return getZoneNetworkDomain(zoneId); } - + return networkDomain; } - + private String getAccountNetworkDomain(long accountId, long zoneId) { String networkDomain = _accountDao.findById(accountId).getNetworkDomain(); - + if (networkDomain == null) { //get domain level network domain return getDomainNetworkDomain(_accountDao.findById(accountId).getDomainId(), zoneId); } - + return networkDomain; } - + @Override public String getGlobalGuestDomainSuffix() { return _networkDomain; } - + @Override public String getStartIpAddress(long networkId) { List vlans = _vlanDao.listVlansByNetworkId(networkId); if (vlans.isEmpty()) { return null; } - + String startIP = vlans.get(0).getIpRange().split("-")[0]; - + for (VlanVO vlan : vlans) { String startIP1 = vlan.getIpRange().split("-")[0]; long startIPLong = NetUtils.ip2Long(startIP); long startIPLong1 = NetUtils.ip2Long(startIP1); - + if (startIPLong1 < startIPLong) { startIP = startIP1; } } - + return startIP; } - + @Override public boolean applyStaticNats(List staticNats, boolean continueOnError) throws ResourceUnavailableException { if (staticNats == null || staticNats.size() == 0) { @@ -3582,7 +3578,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return success; } - + @Override public Long getPodIdForVlan(long vlanDbId) { PodVlanMapVO podVlanMaps = _podVlanMapDao.listPodVlanMapsByVlan(vlanDbId); @@ -3592,37 +3588,37 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return podVlanMaps.getPodId(); } } - + @DB @Override public boolean reallocate(VirtualMachineProfile vm, DataCenterDeployment dest) throws InsufficientCapacityException, ConcurrentOperationException { - VMInstanceVO vmInstance = _vmDao.findById(vm.getId()); - DataCenterVO dc = _dcDao.findById(vmInstance.getDataCenterIdToDeployIn()); - if (dc.getNetworkType() == NetworkType.Basic) { - List nics = _nicDao.listByVmId(vmInstance.getId()); - NetworkVO network = _networksDao.findById(nics.get(0).getNetworkId()); - Pair profile = new Pair(network, null); - List> profiles = new ArrayList>(); - profiles.add(profile); - - Transaction txn = Transaction.currentTxn(); - txn.start(); - - try { - this.cleanupNics(vm); - this.allocate(vm, profiles); - } finally { - txn.commit(); - } - } - return true; + VMInstanceVO vmInstance = _vmDao.findById(vm.getId()); + DataCenterVO dc = _dcDao.findById(vmInstance.getDataCenterIdToDeployIn()); + if (dc.getNetworkType() == NetworkType.Basic) { + List nics = _nicDao.listByVmId(vmInstance.getId()); + NetworkVO network = _networksDao.findById(nics.get(0).getNetworkId()); + Pair profile = new Pair(network, null); + List> profiles = new ArrayList>(); + profiles.add(profile); + + Transaction txn = Transaction.currentTxn(); + txn.start(); + + try { + this.cleanupNics(vm); + this.allocate(vm, profiles); + } finally { + txn.commit(); + } + } + return true; } @Override public Map> listNetworkOfferingServices(long networkOfferingId) { Map> serviceProviderMap = new HashMap>(); List map = _ntwkOfferingSrvcDao.listByNetworkOfferingId(networkOfferingId); - + for (NetworkOfferingServiceMapVO instance : map) { String service = instance.getService(); Set providers; @@ -3634,36 +3630,36 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag providers.add(instance.getProvider()); serviceProviderMap.put(service, providers); } - + return serviceProviderMap; } - + @Override public boolean isProviderSupportServiceInNetwork(long networkId, Service service, Provider provider){ return _ntwkSrvcDao.canProviderSupportServiceInNetwork(networkId, service, provider); } - + protected boolean canUpgrade(long oldNetworkOfferingId, long newNetworkOfferingId) { NetworkOffering oldNetworkOffering = _networkOfferingDao.findByIdIncludingRemoved(oldNetworkOfferingId); NetworkOffering newNetworkOffering = _networkOfferingDao.findById(newNetworkOfferingId); - + //can upgrade only Isolated networks if (oldNetworkOffering.getGuestType() != GuestType.Isolated) { - throw new InvalidParameterValueException("NetworkOfferingId can be upgraded only for the network of type " + GuestType.Isolated); + throw new InvalidParameterValueException("NetworkOfferingId can be upgraded only for the network of type " + GuestType.Isolated); } - + //security group service should be the same if (areServicesSupportedByNetworkOffering(oldNetworkOfferingId, Service.SecurityGroup) != areServicesSupportedByNetworkOffering(newNetworkOfferingId, Service.SecurityGroup)) { s_logger.debug("Offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " have different securityGroupProperty, can't upgrade"); return false; } - + //Type of the network should be the same if (oldNetworkOffering.getGuestType() != newNetworkOffering.getGuestType()){ s_logger.debug("Network offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " are of different types, can't upgrade"); return false; } - + //tags should be the same if (newNetworkOffering.getTags() != null) { if (oldNetworkOffering.getTags() == null) { @@ -3675,38 +3671,38 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return false; } } - + //Traffic types should be the same if (oldNetworkOffering.getTrafficType() != newNetworkOffering.getTrafficType()) { s_logger.debug("Network offerings " + newNetworkOfferingId + " and " + oldNetworkOfferingId + " have different traffic types, can't upgrade"); return false; } - + return true; } - - + + protected boolean canUpgradeProviders(long oldNetworkOfferingId, long newNetworkOfferingId) { //list of services and providers should be the same Map> newServices = listNetworkOfferingServices(newNetworkOfferingId); Map> oldServices = listNetworkOfferingServices(oldNetworkOfferingId); - + if (newServices.size() < oldServices.size()) { s_logger.debug("Network offering downgrade is not allowed: number of supported services for the new offering " + newNetworkOfferingId + " is less than the old offering " + oldNetworkOfferingId); return false; } - + for (String service : oldServices.keySet()) { - + //1)check that all old services are present in the new network offering if (!newServices.containsKey(service)) { s_logger.debug("New service offering doesn't have " + service + " service present in the old service offering, downgrade is not allowed"); return false; } - + Set newProviders = newServices.get(service); Set oldProviders = oldServices.get(service); - + //2) Can upgrade only from internal provider to external provider. Any other combinations are not allowed for (String oldProvider : oldProviders) { if (newProviders.contains(oldProvider)) { @@ -3738,7 +3734,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (zone == null) { throw new InvalidParameterValueException("Please specify a valid zone."); } - + if(zone.getNetworkType() == NetworkType.Basic){ if(!_physicalNetworkDao.listByZone(zoneId).isEmpty()){ throw new CloudRuntimeException("Cannot add the physical network to basic zone id: "+zoneId+", there is a physical network already existing in this basic Zone"); @@ -3747,7 +3743,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (tags != null && tags.size() > 1) { throw new InvalidParameterException("Only one tag can be specified for a physical network at this time"); } - + if (isolationMethods != null && isolationMethods.size() > 1) { throw new InvalidParameterException("Only one isolationMethod can be specified for a physical network at this time"); } @@ -3755,13 +3751,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag int vnetStart = 0; int vnetEnd = 0; if (vnetRange != null) { - + //Verify zone type if (zone.getNetworkType() == NetworkType.Basic || (zone.getNetworkType() == NetworkType.Advanced && zone.isSecurityGroupEnabled())) { throw new InvalidParameterValueException("Can't add vnet range to the physical network in the zone that supports " + zone.getNetworkType() + " network, Security Group enabled: "+ zone.isSecurityGroupEnabled()); } - + String[] tokens = vnetRange.split("-"); try { vnetStart = Integer.parseInt(tokens[0]); @@ -3779,7 +3775,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Vnet range should be between 0-4096 and start range should be lesser than or equal to end range"); } } - + BroadcastDomainRange broadcastDomainRange = null; if (broadcastDomainRangeStr != null && !broadcastDomainRangeStr.isEmpty()) { try { @@ -3788,7 +3784,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Unable to resolve broadcastDomainRange '" + broadcastDomainRangeStr + "' to a supported value {Pod or Zone}"); } } - + if(broadcastDomainRange == null){ if(zone.getNetworkType() == NetworkType.Basic){ broadcastDomainRange = PhysicalNetwork.BroadcastDomainRange.POD; @@ -3796,7 +3792,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag broadcastDomainRange = PhysicalNetwork.BroadcastDomainRange.ZONE; } } - + Transaction txn = Transaction.currentTxn(); try { txn.start(); @@ -3839,13 +3835,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @DB @ActionEvent(eventType = EventTypes.EVENT_PHYSICAL_NETWORK_UPDATE, eventDescription = "updating physical network", async = true) public PhysicalNetwork updatePhysicalNetwork(Long id, String networkSpeed, List tags, String newVnetRangeString, String state) { - + // verify input parameters PhysicalNetworkVO network = _physicalNetworkDao.findById(id); if (network == null) { throw new InvalidParameterValueException("Physical Network id=" + id + "doesn't exist in the system"); } - + // if zone is of Basic type, don't allow to add vnet range DataCenter zone = _dcDao.findById(network.getDataCenterId()); if (zone == null) { @@ -3857,7 +3853,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Can't add vnet range to the physical network in the zone that supports " + zone.getNetworkType() + " network, Security Group enabled: "+ zone.isSecurityGroupEnabled()); } } - + if (tags != null && tags.size() > 1) { throw new InvalidParameterException("Unable to support more than one tag on network yet"); @@ -3871,15 +3867,15 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Unable to resolve state '" + state + "' to a supported value {Enabled or Disabled}"); } } - + if(state != null){ network.setState(networkState); } - + if (tags != null) { network.setTags(tags); } - + if(networkSpeed != null){ network.setSpeed(networkSpeed); } @@ -3887,7 +3883,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag // Vnet range can be extended only boolean replaceVnet = false; ArrayList> vnetsToAdd = new ArrayList>(2); - + if (newVnetRangeString != null) { Integer newStartVnet = 0; Integer newEndVnet = 0; @@ -3916,35 +3912,35 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (newStartVnet > newEndVnet) { throw new InvalidParameterValueException("Vnet range has to be between 0-4096 and start range should be lesser than or equal to stop range"); } - + if (physicalNetworkHasAllocatedVnets(network.getDataCenterId(), network.getId())) { String[] existingRange = network.getVnet().split("-"); int existingStartVnet = Integer.parseInt(existingRange[0]); int existingEndVnet = Integer.parseInt(existingRange[1]); - + //check if vnet is being extended if (!(newStartVnet.intValue() > existingStartVnet && newEndVnet.intValue() < existingEndVnet)) { throw new InvalidParameterValueException("Can's shrink existing vnet range as it the range has vnets allocated. Only extending existing vnet is supported"); } - + if (newStartVnet < existingStartVnet) { vnetsToAdd.add(new Pair(newStartVnet, existingStartVnet - 1)); } - + if (newEndVnet > existingEndVnet) { vnetsToAdd.add(new Pair(existingEndVnet + 1, newEndVnet)); } - + } else { vnetsToAdd.add(new Pair(newStartVnet, newEndVnet)); replaceVnet = true; } } - + if (newVnetRangeString != null) { network.setVnet(newVnetRangeString); } - + _physicalNetworkDao.update(id, network); @@ -3957,10 +3953,10 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.debug("Adding vnet range " + vnetToAdd.first() + "-" + vnetToAdd.second() + " for the physicalNetwork id= "+id +" and zone id=" + network.getDataCenterId() + " as a part of updatePhysicalNetwork call"); _dcDao.addVnet(network.getDataCenterId(), network.getId(), vnetToAdd.first(), vnetToAdd.second()); } - + return network; } - + private boolean physicalNetworkHasAllocatedVnets(long zoneId, long physicalNetworkId) { return !_dcDao.listAllocatedVnets(physicalNetworkId).isEmpty(); } @@ -3974,16 +3970,16 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (pNetwork == null) { throw new InvalidParameterValueException("Network id=" + physicalNetworkId + "doesn't exist in the system"); } - + checkIfPhysicalNetworkIsDeletable(physicalNetworkId); - - + + // delete vlans for this zone List vlans = _vlanDao.listVlansByPhysicalNetworkId(physicalNetworkId); for (VlanVO vlan : vlans) { _vlanDao.remove(vlan.getId()); } - + // Delete networks List networks = _networksDao.listByPhysicalNetwork(physicalNetworkId); if (networks != null && !networks.isEmpty()) { @@ -3991,19 +3987,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag _networksDao.remove(network.getId()); } } - + //delete vnets _dcDao.deleteVnet(physicalNetworkId); - + //delete service providers _pNSPDao.deleteProviders(physicalNetworkId); boolean success = _physicalNetworkDao.remove(physicalNetworkId); - - + + return success; } - + @DB private void checkIfPhysicalNetworkIsDeletable(Long physicalNetworkId) { List> tablesToCheck = new ArrayList>(); @@ -4031,8 +4027,8 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag publicIP.add(1, "physical_network_id"); publicIP.add(2, "there are public IP addresses allocated for this physical network"); tablesToCheck.add(publicIP); - - + + for (List table : tablesToCheck) { String tableName = table.get(0); @@ -4076,7 +4072,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override public List listNetworkServices(String providerName){ - + Provider provider = null; if(providerName != null){ provider = Network.Provider.getProvider(providerName); @@ -4084,7 +4080,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Invalid Network Service Provider=" + providerName); } } - + if(provider != null){ NetworkElement element = getElementImplementingProvider(providerName); if(element == null){ @@ -4095,7 +4091,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return Service.listAllServices(); } } - + @Override public List listSupportedNetworkServiceProviders(String serviceName){ Network.Service service = null; @@ -4105,9 +4101,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Invalid Network Service=" + serviceName); } } - + Set supportedProviders = new HashSet(); - + if(service != null){ supportedProviders.addAll(s_serviceToImplementedProvidersMap.get(service)); }else{ @@ -4115,10 +4111,10 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag supportedProviders.addAll(pList); } } - + return new ArrayList(supportedProviders); } - + @Override @DB @ActionEvent(eventType = EventTypes.EVENT_SERVICE_PROVIDER_CREATE, eventDescription = "Creating Physical Network ServiceProvider", create = true) @@ -4137,7 +4133,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Destination Physical Network id=" + destinationPhysicalNetworkId + "doesn't exist in the system"); } } - + if(providerName != null){ Provider provider = Network.Provider.getProvider(providerName); if(provider == null){ @@ -4151,14 +4147,14 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Unable to find the Network Element implementing the Service Provider '" + providerName + "'"); } List services = new ArrayList(); - + if(enabledServices != null){ if(!element.canEnableIndividualServices()){ if(enabledServices.size() != element.getCapabilities().keySet().size()){ throw new InvalidParameterValueException("Cannot enable subset of Services, Please specify the complete list of Services for this Service Provider '" + providerName + "'"); } } - + //validate Services boolean addGatewayService = false; for(String serviceName : enabledServices){ @@ -4166,25 +4162,25 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (service == null || service == Service.Gateway){ throw new InvalidParameterValueException("Invalid Network Service specified=" + serviceName); } else if (service == Service.SourceNat) { - addGatewayService = true; + addGatewayService = true; } - + //check if the service is provided by this Provider if(!element.getCapabilities().containsKey(service)){ throw new InvalidParameterValueException(providerName+" Provider cannot provide this Service specified=" + serviceName); } services.add(service); } - + if (addGatewayService) { - services.add(Service.Gateway); + services.add(Service.Gateway); } - + }else{ //enable all the default services supported by this element. services = new ArrayList(element.getCapabilities().keySet()); } - + Transaction txn = Transaction.currentTxn(); try { txn.start(); @@ -4192,7 +4188,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag PhysicalNetworkServiceProviderVO nsp = new PhysicalNetworkServiceProviderVO(physicalNetworkId, providerName); //set enabled services nsp.setEnabledServices(services); - + if(destinationPhysicalNetworkId != null){ nsp.setDestinationPhysicalNetworkId(destinationPhysicalNetworkId); } @@ -4204,7 +4200,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Exception: ", ex); throw new CloudRuntimeException("Fail to add a provider to physical network"); } - + } @Override @@ -4213,24 +4209,24 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (network == null) { throw new InvalidParameterValueException("Physical Network id=" + physicalNetworkId + "doesn't exist in the system"); } - + return _pNSPDao.listBy(physicalNetworkId); } @Override @ActionEvent(eventType = EventTypes.EVENT_SERVICE_PROVIDER_UPDATE, eventDescription = "Updating physical network ServiceProvider", async = true) public PhysicalNetworkServiceProvider updateNetworkServiceProvider(Long id, String stateStr, List enabledServices){ - + PhysicalNetworkServiceProviderVO provider = _pNSPDao.findById(id); if(provider == null){ throw new InvalidParameterValueException("Network Service Provider id=" + id + "doesn't exist in the system"); } - + NetworkElement element = getElementImplementingProvider(provider.getProviderName()); if(element == null){ throw new InvalidParameterValueException("Unable to find the Network Element implementing the Service Provider '" + provider.getProviderName() + "'"); } - + PhysicalNetworkServiceProvider.State state = null; if (stateStr != null && !stateStr.isEmpty()) { try { @@ -4239,40 +4235,40 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Unable to resolve state '" + stateStr + "' to a supported value {Enabled or Disabled}"); } } - + boolean update = false; if(state != null){ if(state == PhysicalNetworkServiceProvider.State.Shutdown){ throw new InvalidParameterValueException("Updating the provider state to 'Shutdown' is not supported"); } - + if(s_logger.isDebugEnabled()){ s_logger.debug("updating state of the service provider id=" + id + " on physical network: "+provider.getPhysicalNetworkId() + " to state: "+stateStr); } switch(state) { - case Enabled: - if(element != null && element.isReady(provider)){ - provider.setState(PhysicalNetworkServiceProvider.State.Enabled); - update = true; - }else{ - throw new CloudRuntimeException("Provider is not ready, cannot Enable the provider, please configure the provider first"); - } - break; - case Disabled: - //do we need to do anything for the provider instances before disabling? - provider.setState(PhysicalNetworkServiceProvider.State.Disabled); + case Enabled: + if(element != null && element.isReady(provider)){ + provider.setState(PhysicalNetworkServiceProvider.State.Enabled); update = true; - break; + }else{ + throw new CloudRuntimeException("Provider is not ready, cannot Enable the provider, please configure the provider first"); + } + break; + case Disabled: + //do we need to do anything for the provider instances before disabling? + provider.setState(PhysicalNetworkServiceProvider.State.Disabled); + update = true; + break; } } - + if(enabledServices != null){ //check if services can be turned of if(!element.canEnableIndividualServices()){ - throw new InvalidParameterValueException("Cannot update set of Services for this Service Provider '" + provider.getProviderName() + "'"); + throw new InvalidParameterValueException("Cannot update set of Services for this Service Provider '" + provider.getProviderName() + "'"); } - + //validate Services List services = new ArrayList(); for(String serviceName : enabledServices){ @@ -4286,7 +4282,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag provider.setEnabledServices(services); update = true; } - + if(update){ _pNSPDao.update(id, provider); } @@ -4297,17 +4293,17 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @ActionEvent(eventType = EventTypes.EVENT_SERVICE_PROVIDER_DELETE, eventDescription = "Deleting physical network ServiceProvider", async = true) public boolean deleteNetworkServiceProvider(Long id) throws ConcurrentOperationException, ResourceUnavailableException { PhysicalNetworkServiceProviderVO provider = _pNSPDao.findById(id); - + if(provider == null){ throw new InvalidParameterValueException("Network Service Provider id=" + id + "doesn't exist in the system"); } - + //check if there are networks using this provider List networks = _networksDao.listByPhysicalNetworkAndProvider(provider.getPhysicalNetworkId(), provider.getProviderName()); if(networks != null && !networks.isEmpty()){ throw new CloudRuntimeException("Provider is not deletable because there are active networks using this provider, please upgrade these networks to new network offerings"); } - + User callerUser = _accountMgr.getActiveUser(UserContext.current().getCallerUserId()); Account callerAccount = _accountMgr.getActiveAccountById(callerUser.getAccountId()); //shutdown the provider instances @@ -4319,14 +4315,14 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if(element == null){ throw new InvalidParameterValueException("Unable to find the Network Element implementing the Service Provider '" + provider.getProviderName() + "'"); } - + if(element != null && element.shutdownProviderInstances(provider, context)){ provider.setState(PhysicalNetworkServiceProvider.State.Shutdown); } - + return _pNSPDao.remove(id); } - + @Override public PhysicalNetwork getPhysicalNetwork(Long physicalNetworkId){ return _physicalNetworkDao.findById(physicalNetworkId); @@ -4342,26 +4338,26 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag public PhysicalNetworkServiceProvider getPhysicalNetworkServiceProvider(Long providerId) { return _pNSPDao.findById(providerId); } - + @Override @ActionEvent(eventType = EventTypes.EVENT_SERVICE_PROVIDER_CREATE, eventDescription = "Creating Physical Network ServiceProvider", async = true) public PhysicalNetworkServiceProvider getCreatedPhysicalNetworkServiceProvider(Long providerId) { return getPhysicalNetworkServiceProvider(providerId); } - + @Override public long findPhysicalNetworkId(long zoneId, String tag) { List pNtwks = _physicalNetworkDao.listByZone(zoneId); if (pNtwks.isEmpty()) { throw new InvalidParameterValueException("Unable to find physical network in zone id=" + zoneId); } - + if (pNtwks.size() > 1) { if (tag == null) { throw new InvalidParameterValueException("More than one physical networks exist in zone id=" + zoneId + " and no tags are specified in order to make a choice"); } - + Long pNtwkId = null; for (PhysicalNetwork pNtwk : pNtwks) { if (pNtwk.getTags().contains(tag)) { @@ -4373,48 +4369,48 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag if (pNtwkId == null) { throw new InvalidParameterValueException("Unable to find physical network which match the tags " + tag); } - + return pNtwkId; } else { return pNtwks.get(0).getId(); } } - + @Override public PhysicalNetwork translateZoneIdToPhysicalNetwork(long zoneId) { List pNtwks = _physicalNetworkDao.listByZone(zoneId); if (pNtwks.isEmpty()) { throw new InvalidParameterValueException("Unable to find physical network in zone id=" + zoneId); } - + if (pNtwks.size() > 1) { throw new InvalidParameterValueException("More than one physical networks exist in zone id=" + zoneId); } - + return pNtwks.get(0); } - + @Override public List listNetworkOfferingsForUpgrade(long networkId) { List offeringsToReturn = new ArrayList(); NetworkOffering originalOffering = _configMgr.getNetworkOffering(getNetwork(networkId).getNetworkOfferingId()); - + boolean securityGroupSupportedByOriginalOff = areServicesSupportedByNetworkOffering(originalOffering.getId(), Service.SecurityGroup); - + //security group supported property should be the same - + List offerings = _networkOfferingDao.getOfferingIdsToUpgradeFrom(originalOffering); - + for (Long offeringId : offerings) { if (areServicesSupportedByNetworkOffering(offeringId, Service.SecurityGroup) == securityGroupSupportedByOriginalOff) { offeringsToReturn.add(offeringId); } } - + return offeringsToReturn; } - - + + private boolean cleanupNetworkResources(long networkId, Account caller, long callerUserId) { boolean success = true; Network network = getNetwork(networkId); @@ -4441,7 +4437,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag success = false; s_logger.warn("Failed to cleanup LB rules as a part of network id=" + networkId + " cleanup"); } - + //revoke all firewall rules for the network try { if (_firewallMgr.revokeAllFirewallRulesForNetwork(networkId, callerUserId, caller)) { @@ -4474,23 +4470,23 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return success; } - - + + private boolean shutdownNetworkResources(long networkId, Account caller, long callerUserId) { //This method cleans up network rules on the backend w/o touching them in the DB boolean success = true; - + // Mark all PF rules as revoked and apply them on the backend (not in the DB) List pfRules = _portForwardingRulesDao.listByNetwork(networkId); if (s_logger.isDebugEnabled()) { s_logger.debug("Releasing " + pfRules.size() + " port forwarding rules for network id=" + networkId + " as a part of shutdownNetworkRules"); } - + for (PortForwardingRuleVO pfRule : pfRules) { s_logger.trace("Marking pf rule " + pfRule + " with Revoke state"); pfRule.setState(FirewallRule.State.Revoke); } - + try { if (!_firewallMgr.applyRules(pfRules, true, false)) { s_logger.warn("Failed to cleanup pf rules as a part of shutdownNetworkRules"); @@ -4500,14 +4496,14 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Failed to cleanup pf rules as a part of shutdownNetworkRules due to ", ex); success = false; } - + // Mark all static rules as revoked and apply them on the backend (not in the DB) List firewallStaticNatRules = _firewallDao.listByNetworkAndPurpose(networkId, Purpose.StaticNat); List staticNatRules = new ArrayList(); if (s_logger.isDebugEnabled()) { s_logger.debug("Releasing " + firewallStaticNatRules.size() + " static nat rules for network id=" + networkId + " as a part of shutdownNetworkRules"); } - + for (FirewallRuleVO firewallStaticNatRule : firewallStaticNatRules) { s_logger.trace("Marking static nat rule " + firewallStaticNatRule + " with Revoke state"); IpAddress ip = _ipAddressDao.findById(firewallStaticNatRule.getSourceIpAddressId()); @@ -4521,7 +4517,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag ruleVO.setState(FirewallRule.State.Revoke); staticNatRules.add(new StaticNatRuleImpl(ruleVO, dstIp)); } - + try { if (!_firewallMgr.applyRules(staticNatRules, true, false)) { s_logger.warn("Failed to cleanup static nat rules as a part of shutdownNetworkRules"); @@ -4531,7 +4527,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Failed to cleanup static nat rules as a part of shutdownNetworkRules due to ", ex); success = false; } - + // remove all LB rules for the network List lbs = _lbDao.listByNetworkId(networkId); List lbRules = new ArrayList(); @@ -4544,11 +4540,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.trace("Marking lb destination " + dst + " with Revoke state"); dst.setRevoked(true); } - + LoadBalancingRule loadBalancing = new LoadBalancingRule(lb, dstList); lbRules.add(loadBalancing); } - + try { if (!_firewallMgr.applyRules(lbRules, true, false)) { s_logger.warn("Failed to cleanup lb rules as a part of shutdownNetworkRules"); @@ -4558,18 +4554,18 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Failed to cleanup lb rules as a part of shutdownNetworkRules due to ", ex); success = false; } - + //revoke all firewall rules for the network w/o applying them on the DB List firewallRules = _firewallDao.listByNetworkAndPurpose(networkId, Purpose.Firewall); if (s_logger.isDebugEnabled()) { s_logger.debug("Releasing " + firewallRules.size() + " firewall rules for network id=" + networkId + " as a part of shutdownNetworkRules"); } - + for (FirewallRuleVO firewallRule : firewallRules) { s_logger.trace("Marking firewall rule " + firewallRule + " with Revoke state"); firewallRule.setState(FirewallRule.State.Revoke); } - + try { if (!_firewallMgr.applyRules(firewallRules, true, false)) { s_logger.warn("Failed to cleanup firewall rules as a part of shutdownNetworkRules"); @@ -4605,16 +4601,16 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return success; } - + @Override public boolean isSecurityGroupSupportedInNetwork(Network network) { Long physicalNetworkId = network.getPhysicalNetworkId(); - + //physical network id can be null in Guest Network in Basic zone, so locate the physical network if (physicalNetworkId == null) { physicalNetworkId = findPhysicalNetworkId(network.getDataCenterId(), null); } - + return isServiceEnabledInNetwork(physicalNetworkId, network.getId(), Service.SecurityGroup); } @@ -4638,19 +4634,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new InvalidParameterValueException("Unable to resolve trafficType '" + trafficTypeStr + "' to a supported value"); } } - + if(_pNTrafficTypeDao.isTrafficTypeSupported(physicalNetworkId, trafficType)){ throw new CloudRuntimeException("This physical network already supports the traffic type: "+trafficType); } //For Storage, Control, Management, Public check if the zone has any other physical network with this traffictype already present //If yes, we cant add these traffics to one more physical network in the zone. - + if(TrafficType.isSystemNetwork(trafficType) || TrafficType.Public.equals(trafficType)){ if(!_physicalNetworkDao.listByZoneAndTrafficType(network.getDataCenterId(), trafficType).isEmpty()){ throw new CloudRuntimeException("Fail to add the traffic type to physical network because Zone already has a physical network with this traffic type: "+trafficType); } } - + Transaction txn = Transaction.currentTxn(); try { txn.start(); @@ -4667,26 +4663,26 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag s_logger.warn("Exception: ", ex); throw new CloudRuntimeException("Fail to add a traffic type to physical network"); } - + } - + private String getDefaultXenNetworkLabel(TrafficType trafficType){ String xenLabel = null; switch(trafficType){ - case Public: xenLabel = "cloud-public"; - break; - case Guest: xenLabel = "cloud-guest"; - break; - case Storage: xenLabel = "cloud-storage"; - break; - case Management: xenLabel = "cloud-private"; - break; - case Control: xenLabel = "cloud_link_local_network"; - break; + case Public: xenLabel = "cloud-public"; + break; + case Guest: xenLabel = "cloud-guest"; + break; + case Storage: xenLabel = "cloud-storage"; + break; + case Management: xenLabel = "cloud-private"; + break; + case Control: xenLabel = "cloud_link_local_network"; + break; } return xenLabel; } - + @Override @ActionEvent(eventType = EventTypes.EVENT_TRAFFIC_TYPE_CREATE, eventDescription = "Creating Physical Network TrafficType", async = true) public PhysicalNetworkTrafficType getPhysicalNetworkTrafficType(Long id){ @@ -4696,13 +4692,13 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @Override @ActionEvent(eventType = EventTypes.EVENT_TRAFFIC_TYPE_UPDATE, eventDescription = "Updating physical network TrafficType", async = true) public PhysicalNetworkTrafficType updatePhysicalNetworkTrafficType(Long id, String xenLabel, String kvmLabel, String vmwareLabel) { - + PhysicalNetworkTrafficTypeVO trafficType = _pNTrafficTypeDao.findById(id); - + if(trafficType == null){ throw new InvalidParameterValueException("Traffic Type with id=" + id + "doesn't exist in the system"); } - + if(xenLabel != null){ trafficType.setXenNetworkLabel(xenLabel); } @@ -4713,7 +4709,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag trafficType.setVmwareNetworkLabel(vmwareLabel); } _pNTrafficTypeDao.update(id, trafficType); - + return trafficType; } @@ -4721,11 +4717,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag @ActionEvent(eventType = EventTypes.EVENT_TRAFFIC_TYPE_DELETE, eventDescription = "Deleting physical network TrafficType", async = true) public boolean deletePhysicalNetworkTrafficType(Long id) { PhysicalNetworkTrafficTypeVO trafficType = _pNTrafficTypeDao.findById(id); - + if(trafficType == null){ throw new InvalidParameterValueException("Traffic Type with id=" + id + "doesn't exist in the system"); } - + //check if there are any networks associated to this physical network with this traffic type if(TrafficType.Guest.equals(trafficType.getTrafficType())){ if(!_networksDao.listByPhysicalNetworkTrafficType(trafficType.getPhysicalNetworkId(), trafficType.getTrafficType()).isEmpty()){ @@ -4734,31 +4730,31 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } return _pNTrafficTypeDao.remove(id); } - + @Override public List listTrafficTypes(Long physicalNetworkId) { PhysicalNetworkVO network = _physicalNetworkDao.findById(physicalNetworkId); if (network == null) { throw new InvalidParameterValueException("Physical Network id=" + physicalNetworkId + "doesn't exist in the system"); } - + return _pNTrafficTypeDao.listBy(physicalNetworkId); } @Override public PhysicalNetwork getDefaultPhysicalNetworkByZoneAndTrafficType(long zoneId, TrafficType trafficType) { - + List networkList = _physicalNetworkDao.listByZoneAndTrafficType(zoneId, trafficType); - + if (networkList.isEmpty()) { throw new InvalidParameterValueException("Unable to find the default physical network with traffic=" + trafficType +" in zone id=" + zoneId); } - + if (networkList.size() > 1) { throw new InvalidParameterValueException("More than one physical networks exist in zone id=" + zoneId + " with traffic type="+trafficType); } - + return networkList.get(0); } @@ -4784,9 +4780,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } long hostId = host.getId(); StartupRoutingCommand startup = (StartupRoutingCommand)cmd; - + String dataCenter = startup.getDataCenter(); - + long dcId = -1; DataCenterVO dc = _dcDao.findByName(dataCenter); if (dc == null) { @@ -4801,10 +4797,10 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } dcId = dc.getId(); HypervisorType hypervisorType = startup.getHypervisorType(); - - + + List networkInfoList = new ArrayList(); - + //list all physicalnetworks in the zone & for each get the network names List physicalNtwkList = _physicalNetworkDao.listByZone(dcId); for(PhysicalNetworkVO pNtwk : physicalNtwkList){ @@ -4826,7 +4822,7 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } networkInfoList.add(info); } - + //send the names to the agent if(s_logger.isDebugEnabled()){ s_logger.debug("Sending CheckNetworkCommand to check the Network is setup correctly on Agent"); @@ -4834,16 +4830,16 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag CheckNetworkCommand nwCmd = new CheckNetworkCommand(networkInfoList); CheckNetworkAnswer answer = (CheckNetworkAnswer) _agentMgr.easySend(hostId, nwCmd); - + if (answer == null) { s_logger.warn("Unable to get an answer to the CheckNetworkCommand from agent:" +host.getId()); throw new ConnectionException(true, "Unable to get an answer to the CheckNetworkCommand from agent: "+host.getId()); } - + if (!answer.getResult()) { s_logger.warn("Unable to setup agent " + hostId + " due to " + ((answer != null)?answer.getDetails():"return null")); String msg = "Incorrect Network setup on agent, Reinitialize agent after network names are setup, details : " + answer.getDetails(); - _alertMgr.sendAlert(AlertManager.ALERT_TYPE_HOST, dcId, host.getPodId(), msg, msg); + _alertMgr.sendAlert(AlertManager.ALERT_TYPE_HOST, dcId, host.getPodId(), msg, msg); throw new ConnectionException(true, msg); }else{ if ( answer.needReconnect() ) { @@ -4882,46 +4878,41 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } return true; } - + @Override public boolean isProviderEnabledInPhysicalNetwork(long physicalNetowrkId, String providerName) { PhysicalNetworkServiceProviderVO ntwkSvcProvider = _pNSPDao.findByServiceProvider(physicalNetowrkId, providerName); if (ntwkSvcProvider == null) { - s_logger.warn("Unable to find provider " + providerName + " in physical network id=" + physicalNetowrkId); - return false; + s_logger.warn("Unable to find provider " + providerName + " in physical network id=" + physicalNetowrkId); + return false; } return isProviderEnabled(ntwkSvcProvider); } - + private boolean isServiceEnabledInNetwork(long physicalNetworkId, long networkId, Service service) { //check if the service is supported in the network if (!areServicesSupportedInNetwork(networkId, service)) { s_logger.debug("Service " + service.getName() + " is not supported in the network id=" + networkId); return false; } - + //get provider for the service and check if all of them are supported String provider = _ntwkSrvcDao.getProviderForServiceInNetwork(networkId, service); - + if (!isProviderEnabledInPhysicalNetwork(physicalNetworkId, provider)) { s_logger.debug("Provider " + provider + " is not enabled in physical network id=" + physicalNetworkId); return false; } - + return true; } - - + + @Override - public List getNetworkTags(HypervisorType hType, Network network) { + public String getNetworkTag(HypervisorType hType, Network network) { Long physicalNetworkId = network.getPhysicalNetworkId(); - - if (physicalNetworkId != null) { - String networkTag = _pNTrafficTypeDao.getNetworkTag(physicalNetworkId, network.getTrafficType(), hType); - if (networkTag != null) { - return new ArrayList(Collections.singletonList(networkTag)); - } - } else { + + if (physicalNetworkId == null) { List pNtwks = _physicalNetworkDao.listByZone(network.getDataCenterId()); if (pNtwks.size() == 1) { physicalNetworkId = pNtwks.get(0).getId(); @@ -4937,29 +4928,34 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } } - //in all other cases return empty list - return new ArrayList(); + if (physicalNetworkId == null) { + assert (false) : "Can't get the physical network"; + s_logger.warn("Can't get the physical network"); + return null; + } + + return _pNTrafficTypeDao.getNetworkTag(physicalNetworkId, network.getTrafficType(), hType); } - - + + @Override public NetworkVO getExclusiveGuestNetwork(long zoneId) { List networks = _networksDao.listBy(Account.ACCOUNT_ID_SYSTEM, zoneId, GuestType.Shared, TrafficType.Guest); if (networks == null || networks.isEmpty()) { throw new InvalidParameterValueException("Unable to find network with trafficType " + TrafficType.Guest + " and guestType " + GuestType.Shared + " in zone " + zoneId); } - + if (networks.size() > 1) { throw new InvalidParameterValueException("Found more than 1 network with trafficType " + TrafficType.Guest + " and guestType " + GuestType.Shared + " in zone " + zoneId); } - + return networks.get(0); } @Override public PhysicalNetworkServiceProvider addDefaultVirtualRouterToPhysicalNetwork(long physicalNetworkId) { - + PhysicalNetworkServiceProvider nsp = addProviderToPhysicalNetwork(physicalNetworkId, Network.Provider.VirtualRouter.getName(), null, null); //add instance of the provider VirtualRouterElement element = (VirtualRouterElement)getElementImplementingProvider(Network.Provider.VirtualRouter.getName()); @@ -4967,11 +4963,11 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag throw new CloudRuntimeException("Unable to find the Network Element implementing the VirtualRouter Provider"); } element.addElement(nsp.getId()); - + return nsp; } - + @Override public boolean isNetworkSystem(Network network) { NetworkOffering no = _networkOfferingDao.findByIdIncludingRemoved(network.getNetworkOfferingId()); @@ -4981,35 +4977,35 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag return false; } } - + protected Map finalizeServicesAndProvidersForNetwork(NetworkOffering offering, Long physicalNetworkId) { Map svcProviders = new HashMap(); List servicesMap = _ntwkOfferingSrvcDao.listByNetworkOfferingId(offering.getId()); - + boolean checkPhysicalNetwork = (physicalNetworkId != null) ? true : false; - + for (NetworkOfferingServiceMapVO serviceMap : servicesMap) { if (svcProviders.containsKey(serviceMap.getService())) { //FIXME - right now we pick up the first provider from the list, need to add more logic based on provider load, etc continue; } - + String service = serviceMap.getService(); String provider = serviceMap.getProvider(); - + //check that provider is supported if (checkPhysicalNetwork) { - if (!_pNSPDao.isServiceProviderEnabled(physicalNetworkId, provider, service)) { - throw new UnsupportedServiceException("Provider " + provider + " doesn't support service " + service + " in physical network id=" + physicalNetworkId); - } + if (!_pNSPDao.isServiceProviderEnabled(physicalNetworkId, provider, service)) { + throw new UnsupportedServiceException("Provider " + provider + " doesn't support service " + service + " in physical network id=" + physicalNetworkId); + } } - + svcProviders.put(service, provider); } - + return svcProviders; } - + @Override public Long getPhysicalNetworkId(Network network) { Long physicalNetworkId = network.getPhysicalNetworkId(); @@ -5018,30 +5014,30 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag } return physicalNetworkId; } - + @Override public boolean getAllowSubdomainAccessGlobal() { - return _allowSubdomainNetworkAccess; + return _allowSubdomainNetworkAccess; } - + private List getNetworkProviders(long networkId) { - List providerNames = _ntwkSrvcDao.getDistinctProviders(networkId); - List providers = new ArrayList(); - for (String providerName : providerNames) { - providers.add(Network.Provider.getProvider(providerName)); - } - - return providers; + List providerNames = _ntwkSrvcDao.getDistinctProviders(networkId); + List providers = new ArrayList(); + for (String providerName : providerNames) { + providers.add(Network.Provider.getProvider(providerName)); + } + + return providers; } - + @Override public boolean isProviderForNetwork(Provider provider, long networkId) { - if (_ntwkSrvcDao.isProviderForNetwork(networkId, provider) != null) { - return true; - } else { - return false; - } + if (_ntwkSrvcDao.isProviderForNetwork(networkId, provider) != null) { + return true; + } else { + return false; + } } - - + + } diff --git a/server/src/com/cloud/network/dao/PhysicalNetworkTrafficTypeDaoImpl.java b/server/src/com/cloud/network/dao/PhysicalNetworkTrafficTypeDaoImpl.java index f28564cd1a3..eaab3b9aa95 100644 --- a/server/src/com/cloud/network/dao/PhysicalNetworkTrafficTypeDaoImpl.java +++ b/server/src/com/cloud/network/dao/PhysicalNetworkTrafficTypeDaoImpl.java @@ -25,6 +25,7 @@ import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.network.Networks.TrafficType; import com.cloud.utils.db.DB; import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.GenericSearchBuilder; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; import com.cloud.utils.db.SearchCriteria.Op; @@ -32,33 +33,33 @@ import com.cloud.utils.db.SearchCriteria.Op; @Local(value=PhysicalNetworkTrafficTypeDao.class) @DB(txn=false) public class PhysicalNetworkTrafficTypeDaoImpl extends GenericDaoBase implements PhysicalNetworkTrafficTypeDao { final SearchBuilder physicalNetworkSearch; - final SearchBuilder kvmAllFieldsSearch; - final SearchBuilder xenAllFieldsSearch; - final SearchBuilder vmWareAllFieldsSearch; - + final GenericSearchBuilder kvmAllFieldsSearch; + final GenericSearchBuilder xenAllFieldsSearch; + final GenericSearchBuilder vmWareAllFieldsSearch; + protected PhysicalNetworkTrafficTypeDaoImpl() { super(); physicalNetworkSearch = createSearchBuilder(); physicalNetworkSearch.and("physicalNetworkId", physicalNetworkSearch.entity().getPhysicalNetworkId(), Op.EQ); physicalNetworkSearch.and("trafficType", physicalNetworkSearch.entity().getTrafficType(), Op.EQ); physicalNetworkSearch.done(); - - kvmAllFieldsSearch = createSearchBuilder(); + + kvmAllFieldsSearch = createSearchBuilder(String.class); kvmAllFieldsSearch.and("physicalNetworkId", kvmAllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ); kvmAllFieldsSearch.and("trafficType", kvmAllFieldsSearch.entity().getTrafficType(), Op.EQ); - kvmAllFieldsSearch.and("kvm_network_label", kvmAllFieldsSearch.entity().getKvmNetworkLabel(), Op.NNULL); + kvmAllFieldsSearch.selectField(kvmAllFieldsSearch.entity().getKvmNetworkLabel()); kvmAllFieldsSearch.done(); - - xenAllFieldsSearch = createSearchBuilder(); + + xenAllFieldsSearch = createSearchBuilder(String.class); xenAllFieldsSearch.and("physicalNetworkId", xenAllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ); xenAllFieldsSearch.and("trafficType", xenAllFieldsSearch.entity().getTrafficType(), Op.EQ); - xenAllFieldsSearch.and("xen_network_label", xenAllFieldsSearch.entity().getKvmNetworkLabel(), Op.NNULL); + xenAllFieldsSearch.selectField(xenAllFieldsSearch.entity().getXenNetworkLabel()); xenAllFieldsSearch.done(); - - vmWareAllFieldsSearch = createSearchBuilder(); + + vmWareAllFieldsSearch = createSearchBuilder(String.class); vmWareAllFieldsSearch.and("physicalNetworkId", vmWareAllFieldsSearch.entity().getPhysicalNetworkId(), Op.EQ); vmWareAllFieldsSearch.and("trafficType", vmWareAllFieldsSearch.entity().getTrafficType(), Op.EQ); - vmWareAllFieldsSearch.and("vmware_network_label", vmWareAllFieldsSearch.entity().getKvmNetworkLabel(), Op.NNULL); + vmWareAllFieldsSearch.selectField(vmWareAllFieldsSearch.entity().getKvmNetworkLabel()); vmWareAllFieldsSearch.done(); } @@ -68,7 +69,7 @@ public class PhysicalNetworkTrafficTypeDaoImpl extends GenericDaoBase sc = physicalNetworkSearch.create(); @@ -80,10 +81,10 @@ public class PhysicalNetworkTrafficTypeDaoImpl extends GenericDaoBase sc = null; + SearchCriteria sc = null; if (hType == HypervisorType.XenServer) { sc = xenAllFieldsSearch.create(); } else if (hType == HypervisorType.KVM) { @@ -91,25 +92,17 @@ public class PhysicalNetworkTrafficTypeDaoImpl extends GenericDaoBase tag = customSearch(sc, null); + + return tag.size() == 0 ? null : tag.get(0); } - + @Override public PhysicalNetworkTrafficTypeVO findBy(long physicalNetworkId, TrafficType trafficType){ SearchCriteria sc = physicalNetworkSearch.create(); diff --git a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java index aee91a0942d..8f0706d2ccb 100755 --- a/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java +++ b/server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java @@ -90,7 +90,6 @@ import com.cloud.dc.DataCenter.NetworkType; import com.cloud.dc.DataCenterVO; import com.cloud.dc.HostPodVO; import com.cloud.dc.Pod; -import com.cloud.dc.dao.AccountVlanMapDao; import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; import com.cloud.dc.dao.HostPodDao; @@ -344,13 +343,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian int _checkRouterInterval = 30; private ServiceOfferingVO _offering; private String _dnsBasicZoneUpdates = "all"; - + private boolean _disable_rp_filter = false; int _routerExtraPublicNics = 2; private int _usageAggregationRange = 1440; private String _usageTimeZone = "GMT"; private final long mgmtSrvrId = MacAddress.getMacAddress().toLong(); - + ScheduledExecutorService _executor; ScheduledExecutorService _checkExecutor; ScheduledExecutorService _networkStatsUpdateExecutor; @@ -387,9 +386,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (router == null) { return null; } - + _accountMgr.checkAccess(context.getCaller(), null, router); - + boolean result = _itMgr.expunge(router, user, _accountMgr.getAccount(router.getAccountId())); if (result) { @@ -421,7 +420,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (newServiceOffering == null) { throw new InvalidParameterValueException("Unable to find service offering with id " + serviceOfferingId); } - + // check if it is a system service offering, if yes return with error as it cannot be used for user vms if (!newServiceOffering.getSystemUse()) { throw new InvalidParameterValueException("Cannot upgrade router vm to a non system service offering " + serviceOfferingId); @@ -435,7 +434,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } ServiceOfferingVO currentServiceOffering = _serviceOfferingDao.findById(router.getServiceOfferingId()); - + // Check that the service offering being upgraded to has the same storage pool preference as the VM's current service // offering if (currentServiceOffering.getUseLocalStorage() != newServiceOffering.getUseLocalStorage()) { @@ -458,7 +457,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Unable save password, router doesn't exist in network " + network.getId()); throw new CloudRuntimeException("Unable to save password to router"); } - + UserVm userVm = profile.getVirtualMachine(); String password = (String) profile.getParameter(Param.VmPassword); String encodedPassword = PasswordGenerator.rot13(password); @@ -470,7 +469,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (dc.getNetworkType() == NetworkType.Basic && userVm.getPodIdToDeployIn().longValue() != router.getPodIdToDeployIn().longValue()) { sendPassword = false; } - + if (sendPassword) { Commands cmds = new Commands(OnError.Continue); SavePasswordCommand cmd = new SavePasswordCommand(encodedPassword, nic.getIp4Address(), userVm.getHostName()); @@ -503,7 +502,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian VirtualRouter virtualRouter = stop(router, forced, user, account); if(virtualRouter == null){ - throw new CloudRuntimeException("Failed to stop router with id " + routerId); + throw new CloudRuntimeException("Failed to stop router with id " + routerId); } return virtualRouter; } @@ -618,9 +617,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian _mgmt_host = configs.get("host"); _routerRamSize = NumbersUtil.parseInt(configs.get("router.ram.size"), DEFAULT_ROUTER_VM_RAMSIZE); _routerCpuMHz = NumbersUtil.parseInt(configs.get("router.cpu.mhz"), DEFAULT_ROUTER_CPU_MHZ); - + _routerExtraPublicNics = NumbersUtil.parseInt(_configDao.getValue(Config.RouterExtraPublicNics.key()), 2); - + String value = configs.get("start.retry"); _retry = NumbersUtil.parseInt(value, 2); @@ -631,12 +630,12 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (_instance == null) { _instance = "DEFAULT"; } - + String rpValue = configs.get("network.disable.rpfilter"); if (rpValue != null && rpValue.equalsIgnoreCase("true")) { _disable_rp_filter = true; } - + _dnsBasicZoneUpdates = String.valueOf(_configDao.getValue(Config.DnsBasicZoneUpdates.key())); s_logger.info("Router configurations: " + "ramsize=" + _routerRamSize); @@ -653,23 +652,23 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian _offering = new ServiceOfferingVO("System Offering For Software Router", 1, _routerRamSize, _routerCpuMHz, null, null, true, null, useLocalStorage, true, null, true, VirtualMachine.Type.DomainRouter, true); _offering.setUniqueName("Cloud.Com-SoftwareRouter"); _offering = _serviceOfferingDao.persistSystemServiceOffering(_offering); - + // this can sometimes happen, if DB is manually or programmatically manipulated if(_offering == null) { - String msg = "Data integrity problem : System Offering For Software router VM has been removed?"; - s_logger.error(msg); + String msg = "Data integrity problem : System Offering For Software router VM has been removed?"; + s_logger.error(msg); throw new ConfigurationException(msg); } _systemAcct = _accountService.getSystemAccount(); - + String aggregationRange = configs.get("usage.stats.job.aggregation.range"); _usageAggregationRange = NumbersUtil.parseInt(aggregationRange, 1440); _usageTimeZone = configs.get("usage.aggregation.timezone"); if(_usageTimeZone == null){ - _usageTimeZone = "GMT"; + _usageTimeZone = "GMT"; } - + _agentMgr.registerForHostEvents(this, true, false, false); s_logger.info("DomainRouterManager is configured."); @@ -684,43 +683,43 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public boolean start() { - if (_routerStatsInterval > 0){ - _executor.scheduleAtFixedRate(new NetworkUsageTask(), _routerStatsInterval, _routerStatsInterval, TimeUnit.SECONDS); - - //Schedule Network stats update task - TimeZone usageTimezone = TimeZone.getTimeZone(_usageTimeZone); - Calendar cal = Calendar.getInstance(usageTimezone); - cal.setTime(new Date()); - long endDate = 0; - int HOURLY_TIME = 60; - final int DAILY_TIME = 60 * 24; - if (_usageAggregationRange == DAILY_TIME) { - cal.roll(Calendar.DAY_OF_YEAR, false); - cal.set(Calendar.HOUR_OF_DAY, 0); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - cal.roll(Calendar.DAY_OF_YEAR, true); - cal.add(Calendar.MILLISECOND, -1); - endDate = cal.getTime().getTime(); - } else if (_usageAggregationRange == HOURLY_TIME) { - cal.roll(Calendar.HOUR_OF_DAY, false); - cal.set(Calendar.MINUTE, 0); - cal.set(Calendar.SECOND, 0); - cal.set(Calendar.MILLISECOND, 0); - cal.roll(Calendar.HOUR_OF_DAY, true); - cal.add(Calendar.MILLISECOND, -1); - endDate = cal.getTime().getTime(); - } else { - endDate = cal.getTime().getTime(); - } + if (_routerStatsInterval > 0){ + _executor.scheduleAtFixedRate(new NetworkUsageTask(), _routerStatsInterval, _routerStatsInterval, TimeUnit.SECONDS); + + //Schedule Network stats update task + TimeZone usageTimezone = TimeZone.getTimeZone(_usageTimeZone); + Calendar cal = Calendar.getInstance(usageTimezone); + cal.setTime(new Date()); + long endDate = 0; + int HOURLY_TIME = 60; + final int DAILY_TIME = 60 * 24; + if (_usageAggregationRange == DAILY_TIME) { + cal.roll(Calendar.DAY_OF_YEAR, false); + cal.set(Calendar.HOUR_OF_DAY, 0); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + cal.roll(Calendar.DAY_OF_YEAR, true); + cal.add(Calendar.MILLISECOND, -1); + endDate = cal.getTime().getTime(); + } else if (_usageAggregationRange == HOURLY_TIME) { + cal.roll(Calendar.HOUR_OF_DAY, false); + cal.set(Calendar.MINUTE, 0); + cal.set(Calendar.SECOND, 0); + cal.set(Calendar.MILLISECOND, 0); + cal.roll(Calendar.HOUR_OF_DAY, true); + cal.add(Calendar.MILLISECOND, -1); + endDate = cal.getTime().getTime(); + } else { + endDate = cal.getTime().getTime(); + } + + _networkStatsUpdateExecutor.scheduleAtFixedRate(new NetworkStatsUpdateTask(), (endDate - System.currentTimeMillis()), (_usageAggregationRange * 60 * 1000), TimeUnit.MILLISECONDS); + }else{ + s_logger.debug("router.stats.interval - " + _routerStatsInterval+ " so not scheduling the router stats thread"); + } + _checkExecutor.scheduleAtFixedRate(new CheckRouterTask(), _checkRouterInterval, _checkRouterInterval, TimeUnit.SECONDS); - _networkStatsUpdateExecutor.scheduleAtFixedRate(new NetworkStatsUpdateTask(), (endDate - System.currentTimeMillis()), (_usageAggregationRange * 60 * 1000), TimeUnit.MILLISECONDS); - }else{ - s_logger.debug("router.stats.interval - " + _routerStatsInterval+ " so not scheduling the router stats thread"); - } - _checkExecutor.scheduleAtFixedRate(new CheckRouterTask(), _checkRouterInterval, _checkRouterInterval, TimeUnit.SECONDS); - return true; } @@ -751,7 +750,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn()); cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString()); - + cmd.addVmData("userdata", "user-data", userData); cmd.addVmData("metadata", "service-offering", StringUtils.unicodeEscape(serviceOffering)); cmd.addVmData("metadata", "availability-zone", StringUtils.unicodeEscape(zoneName)); @@ -787,72 +786,72 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public void run() { - try{ + try{ final List routers = _routerDao.listByStateAndNetworkType(State.Running, GuestType.Isolated, mgmtSrvrId); - s_logger.debug("Found " + routers.size() + " running routers. "); + s_logger.debug("Found " + routers.size() + " running routers. "); - for (DomainRouterVO router : routers) { - String privateIP = router.getPrivateIpAddress(); - if (privateIP != null) { - final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName()); - UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(), router.getDataCenterIdToDeployIn(), router.getNetworkId(), null, router.getId(), router.getType().toString()); - NetworkUsageAnswer answer = null; - try { - answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd); - } catch (Exception e) { - s_logger.warn("Error while collecting network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId(), e); - continue; - } - if (answer != null) { - Transaction txn = Transaction.open(Transaction.CLOUD_DB); - try { - if ((answer.getBytesReceived() == 0) && (answer.getBytesSent() == 0)) { - s_logger.debug("Recieved and Sent bytes are both 0. Not updating user_statistics"); - continue; - } - txn.start(); - UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), router.getDataCenterIdToDeployIn(), router.getNetworkId(), null, router.getId(), router.getType().toString()); - if (stats == null) { - s_logger.warn("unable to find stats for account: " + router.getAccountId()); - continue; - } + for (DomainRouterVO router : routers) { + String privateIP = router.getPrivateIpAddress(); + if (privateIP != null) { + final NetworkUsageCommand usageCmd = new NetworkUsageCommand(privateIP, router.getHostName()); + UserStatisticsVO previousStats = _statsDao.findBy(router.getAccountId(), router.getDataCenterIdToDeployIn(), router.getNetworkId(), null, router.getId(), router.getType().toString()); + NetworkUsageAnswer answer = null; + try { + answer = (NetworkUsageAnswer) _agentMgr.easySend(router.getHostId(), usageCmd); + } catch (Exception e) { + s_logger.warn("Error while collecting network stats from router: "+router.getInstanceName()+" from host: "+router.getHostId(), e); + continue; + } + if (answer != null) { + Transaction txn = Transaction.open(Transaction.CLOUD_DB); + try { + if ((answer.getBytesReceived() == 0) && (answer.getBytesSent() == 0)) { + s_logger.debug("Recieved and Sent bytes are both 0. Not updating user_statistics"); + continue; + } + txn.start(); + UserStatisticsVO stats = _statsDao.lock(router.getAccountId(), router.getDataCenterIdToDeployIn(), router.getNetworkId(), null, router.getId(), router.getType().toString()); + if (stats == null) { + s_logger.warn("unable to find stats for account: " + router.getAccountId()); + continue; + } - if(previousStats != null - && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){ - s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. Ignoring current answer. Router: "+answer.getRouterName()+" Rcvd: " + answer.getBytesReceived()+ "Sent: " +answer.getBytesSent()); - continue; - } + if(previousStats != null + && ((previousStats.getCurrentBytesReceived() != stats.getCurrentBytesReceived()) || (previousStats.getCurrentBytesSent() != stats.getCurrentBytesSent()))){ + s_logger.debug("Router stats changed from the time NetworkUsageCommand was sent. Ignoring current answer. Router: "+answer.getRouterName()+" Rcvd: " + answer.getBytesReceived()+ "Sent: " +answer.getBytesSent()); + continue; + } - if (stats.getCurrentBytesReceived() > answer.getBytesReceived()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Received # of bytes that's less than the last one. Assuming something went wrong and persisting it. Router: "+answer.getRouterName()+" Reported: " + answer.getBytesReceived() - + " Stored: " + stats.getCurrentBytesReceived()); - } - stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); - } - stats.setCurrentBytesReceived(answer.getBytesReceived()); - if (stats.getCurrentBytesSent() > answer.getBytesSent()) { - if (s_logger.isDebugEnabled()) { - s_logger.debug("Received # of bytes that's less than the last one. Assuming something went wrong and persisting it. Router: "+answer.getRouterName()+" Reported: " + answer.getBytesSent() - + " Stored: " + stats.getCurrentBytesSent()); - } - stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); - } - stats.setCurrentBytesSent(answer.getBytesSent()); - _statsDao.update(stats.getId(), stats); - txn.commit(); - } catch (Exception e) { - txn.rollback(); - s_logger.warn("Unable to update user statistics for account: " + router.getAccountId() + " Rx: " + answer.getBytesReceived() + "; Tx: " + answer.getBytesSent()); - } finally { - txn.close(); - } - } - } - } - } catch (Exception e) { - s_logger.warn("Error while collecting network stats", e); - } + if (stats.getCurrentBytesReceived() > answer.getBytesReceived()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Received # of bytes that's less than the last one. Assuming something went wrong and persisting it. Router: "+answer.getRouterName()+" Reported: " + answer.getBytesReceived() + + " Stored: " + stats.getCurrentBytesReceived()); + } + stats.setNetBytesReceived(stats.getNetBytesReceived() + stats.getCurrentBytesReceived()); + } + stats.setCurrentBytesReceived(answer.getBytesReceived()); + if (stats.getCurrentBytesSent() > answer.getBytesSent()) { + if (s_logger.isDebugEnabled()) { + s_logger.debug("Received # of bytes that's less than the last one. Assuming something went wrong and persisting it. Router: "+answer.getRouterName()+" Reported: " + answer.getBytesSent() + + " Stored: " + stats.getCurrentBytesSent()); + } + stats.setNetBytesSent(stats.getNetBytesSent() + stats.getCurrentBytesSent()); + } + stats.setCurrentBytesSent(answer.getBytesSent()); + _statsDao.update(stats.getId(), stats); + txn.commit(); + } catch (Exception e) { + txn.rollback(); + s_logger.warn("Unable to update user statistics for account: " + router.getAccountId() + " Rx: " + answer.getBytesReceived() + "; Tx: " + answer.getBytesSent()); + } finally { + txn.close(); + } + } + } + } + } catch (Exception e) { + s_logger.warn("Error while collecting network stats", e); + } } } @@ -863,20 +862,20 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override public void run() { - try{ - if(_statsDao.updateAggStats()){ - s_logger.debug("Succussfully updated aggregate network stats"); - } else { - s_logger.debug("Failed to update aggregate network stats"); - } - } catch (Exception e){ - s_logger.debug("Failed to update aggregate network stats", e); - } + try{ + if(_statsDao.updateAggStats()){ + s_logger.debug("Succussfully updated aggregate network stats"); + } else { + s_logger.debug("Failed to update aggregate network stats"); + } + } catch (Exception e){ + s_logger.debug("Failed to update aggregate network stats", e); + } } - + } - + protected void updateRoutersRedundantState(List routers) { boolean updated = false; for (DomainRouterVO router : routers) { @@ -931,9 +930,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian RedundantState currState = router.getRedundantState(); if (prevState != currState) { String title = "Redundant virtual router " + router.getInstanceName() + - " just switch from " + prevState + " to " + currState; + " just switch from " + prevState + " to " + currState; String context = "Redundant virtual router (name: " + router.getHostName() + ", id: " + router.getId() + ") " + - " just switch from " + prevState + " to " + currState; + " just switch from " + prevState + " to " + currState; s_logger.info(context); if (currState == RedundantState.MASTER) { _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, @@ -977,12 +976,12 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } return priority; } - + protected class CheckRouterTask implements Runnable { public CheckRouterTask() { } - + /* * In order to make fail-over works well at any time, we have to ensure: * 1. Backup router's priority = Master's priority - DELTA + 1 @@ -1037,8 +1036,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DomainRouterVO dupRouter = networkRouterMaps.get(router.getNetworkId()); String title = "More than one redundant virtual router is in MASTER state! Router " + router.getHostName() + " and router " + dupRouter.getHostName(); String context = "Virtual router (name: " + router.getHostName() + ", id: " + router.getId() + " and router (name: " - + dupRouter.getHostName() + ", id: " + router.getId() + ") are both in MASTER state! If the problem persist, restart both of routers. "; - + + dupRouter.getHostName() + ", id: " + router.getId() + ") are both in MASTER state! If the problem persist, restart both of routers. "; + _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, router.getDataCenterIdToDeployIn(), router.getPodIdToDeployIn(), title, context); _alertMgr.sendAlert(AlertManager.ALERT_TYPE_DOMAIN_ROUTER, dupRouter.getDataCenterIdToDeployIn(), dupRouter.getPodIdToDeployIn(), title, context); } else { @@ -1047,13 +1046,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } } - + @Override public void run() { try { final List routers = _routerDao.listVirtualByHostId(null); s_logger.debug("Found " + routers.size() + " routers. "); - + updateRoutersRedundantState(routers); /* FIXME assumed the a pair of redundant routers managed by same mgmt server, @@ -1071,7 +1070,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } private final int DEFAULT_PRIORITY = 100; private final int DEFAULT_DELTA = 2; - + protected int getUpdatedPriority(Network guestNetwork, List routers, DomainRouterVO exclude) throws InsufficientVirtualNetworkCapcityException { int priority; if (routers.size() == 0) { @@ -1103,44 +1102,44 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } return priority; } - + /* * Ovm won't support any system. So we have to choose a partner cluster in the same pod to start domain router for us */ private HypervisorType getAClusterToStartDomainRouterForOvm(long podId) { List clusters = _clusterDao.listByPodId(podId); for (ClusterVO cv : clusters) { - if (cv.getHypervisorType() == HypervisorType.Ovm || cv.getHypervisorType() == HypervisorType.BareMetal) { - continue; - } - - List hosts = _resourceMgr.listAllHostsInCluster(cv.getId()); - if (hosts == null || hosts.isEmpty()) { - continue; - } - - for (HostVO h : hosts) { - if (h.getStatus() == Status.Up) { - s_logger.debug("Pick up host that has hypervisor type " + h.getHypervisorType() + " in cluster " + cv.getId() + " to start domain router for OVM"); - return h.getHypervisorType(); - } - } + if (cv.getHypervisorType() == HypervisorType.Ovm || cv.getHypervisorType() == HypervisorType.BareMetal) { + continue; + } + + List hosts = _resourceMgr.listAllHostsInCluster(cv.getId()); + if (hosts == null || hosts.isEmpty()) { + continue; + } + + for (HostVO h : hosts) { + if (h.getStatus() == Status.Up) { + s_logger.debug("Pick up host that has hypervisor type " + h.getHypervisorType() + " in cluster " + cv.getId() + " to start domain router for OVM"); + return h.getHypervisorType(); + } + } } - - String errMsg = "Cannot find an available cluster in Pod " - + podId - + " to start domain router for Ovm. \n Ovm won't support any system vm including domain router, please make sure you have a cluster with hypervisor type of any of xenserver/KVM/Vmware in the same pod with Ovm cluster. And there is at least one host in UP status in that cluster."; - throw new CloudRuntimeException(errMsg); + + String errMsg = "Cannot find an available cluster in Pod " + + podId + + " to start domain router for Ovm. \n Ovm won't support any system vm including domain router, please make sure you have a cluster with hypervisor type of any of xenserver/KVM/Vmware in the same pod with Ovm cluster. And there is at least one host in UP status in that cluster."; + throw new CloudRuntimeException(errMsg); } - + @DB protected List findOrCreateVirtualRouters(Network guestNetwork, DeployDestination dest, Account owner, boolean isRedundant) throws ConcurrentOperationException, InsufficientCapacityException { - + Network network = _networkDao.acquireInLockTable(guestNetwork.getId()); if (network == null) { throw new ConcurrentOperationException("Unable to lock network " + guestNetwork.getId()); } - + long dcId = dest.getDataCenter().getId(); DataCenterDeployment plan = new DataCenterDeployment(dcId); boolean isPodBased = (dest.getDataCenter().getNetworkType() == NetworkType.Basic || _networkMgr.areServicesSupportedInNetwork(guestNetwork.getId(), Service.SecurityGroup)) && guestNetwork.getTrafficType() == TrafficType.Guest; @@ -1157,7 +1156,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian routers = _routerDao.listByNetworkAndRole(guestNetwork.getId(), Role.VIRTUAL_ROUTER); } else { if (isPodBased) { - Long podId = dest.getPod().getId(); + Long podId = dest.getPod().getId(); routers = _routerDao.listByNetworkAndPodAndRole(guestNetwork.getId(), podId, Role.VIRTUAL_ROUTER); plan = new DataCenterDeployment(dcId, podId, null, null, null, null); } else { @@ -1165,7 +1164,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian plan = new DataCenterDeployment(dcId); } } - + try { int routerCount = 1; if (isRedundant) { @@ -1183,11 +1182,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (routers.size() >= routerCount) { return routers; } - + if (routers.size() >= 5) { s_logger.error("Too much redundant routers!"); } - + NicProfile defaultNic = new NicProfile(); //if source nat service is supported by the network, get the source nat ip address if (publicNetwork) { @@ -1202,7 +1201,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian defaultNic.setIsolationUri(IsolationType.Vlan.toUri(sourceNatIp.getVlanTag())); defaultNic.setDeviceId(2); } - + int count = routerCount - routers.size(); for (int i = 0; i < count; i++) { @@ -1253,12 +1252,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian networks.add(new Pair((NetworkVO) guestNetwork, gatewayNic)); networks.add(new Pair(controlConfig, null)); - + Long offering_id = _networkOfferingDao.findById(guestNetwork.getNetworkOfferingId()).getServiceOfferingId(); if (offering_id == null) { offering_id = _offering.getId(); } VirtualRouterProviderType type = VirtualRouterProviderType.VirtualRouter; + Long physicalNetworkId = _networkMgr.getPhysicalNetworkId(network); PhysicalNetworkServiceProvider provider = _physicalProviderDao.findByServiceProvider(physicalNetworkId, type.toString()); if (provider == null) { @@ -1269,7 +1269,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new CloudRuntimeException("Cannot find virtual router provider " + type.toString()+ " as service provider " + provider.getId()); } ServiceOfferingVO routerOffering = _serviceOfferingDao.findById(offering_id); - + //Router is the network element, we don't know the hypervisor type yet. //Try to allocate the domR twice using diff hypervisors, and when failed both times, throw the exception up List supportedHypervisors = _resourceMgr.getSupportedHypervisorTypes(dest.getDataCenter().getId()); @@ -1278,7 +1278,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian try { s_logger.debug("Allocating the domR with the hypervisor type " + hType); VMTemplateVO template = _templateDao.findRoutingTemplate(hType); - + if (template == null) { s_logger.debug(hType + " won't support system vm, skip it"); continue; @@ -1300,7 +1300,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } routers.add(router); - + // Creating stats entry for router UserStatisticsVO stats = _userStatsDao.findBy(owner.getId(), dcId, router.getNetworkId(), null, router.getId(), router.getType().toString()); if (stats == null) { @@ -1310,7 +1310,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian stats = new UserStatisticsVO(owner.getId(), dcId, null, router.getId(), router.getType().toString(), guestNetwork.getId()); _userStatsDao.persist(stats); } - + } } finally { if (network != null) { @@ -1321,11 +1321,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } private DomainRouterVO startVirtualRouter(DomainRouterVO router, User user, Account caller, Map params) throws StorageUnavailableException, InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { + ConcurrentOperationException, ResourceUnavailableException { if (router.getRole() == Role.VIRTUAL_ROUTER || !router.getIsRedundantRouter()) { return this.start(router, user, caller, params, null); } - + if (router.getState() == State.Running) { s_logger.debug("Redundant router " + router.getInstanceName() + " is already running!"); return router; @@ -1349,7 +1349,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } if (routerToBeAvoid == null) { - return this.start(router, user, caller, params, null); + return this.start(router, user, caller, params, null); } // We would try best to deploy the router to another place int retryIndex = 5; @@ -1367,7 +1367,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian avoids[3] = new ExcludeList(); avoids[3].addHost(routerToBeAvoid.getHostId()); avoids[4] = new ExcludeList(); - + for (int i = 0; i < retryIndex; i++) { if (s_logger.isTraceEnabled()) { s_logger.trace("Try to deploy redundant virtual router:" + router.getHostName() + ", for " + i + " time"); @@ -1384,10 +1384,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } return result; } - + @Override public List deployVirtualRouter(Network guestNetwork, DeployDestination dest, Account owner, Map params, boolean isRedundant) throws InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { + ConcurrentOperationException, ResourceUnavailableException { if (_networkMgr.isNetworkSystem(guestNetwork) || guestNetwork.getGuestType() == Network.GuestType.Shared) { owner = _accountMgr.getAccount(Account.ACCOUNT_ID_SYSTEM); } @@ -1402,11 +1402,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian List routers = findOrCreateVirtualRouters(guestNetwork, dest, owner, isRedundant); List runningRouters = null; - + if (routers != null) { runningRouters = new ArrayList(); } - + for (DomainRouterVO router : routers) { boolean skip = false; State state = router.getState(); @@ -1427,7 +1427,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } return runningRouters; } - + @Override public boolean finalizeVirtualMachineProfile(VirtualMachineProfile profile, DeployDestination dest, ReservationContext context) { @@ -1449,7 +1449,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian dhcpRange = NetUtils.getDhcpRange(cidr); } } - + String rpValue = _configDao.getValue(Config.NetworkRouterRpFilter.key()); if (rpValue != null && rpValue.equalsIgnoreCase("true")) { _disable_rp_filter = true; @@ -1457,7 +1457,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian { _disable_rp_filter = false; } - + boolean publicNetwork = false; if (_networkMgr.isProviderSupportServiceInNetwork(network.getId(), Service.SourceNat, Provider.VirtualRouter)) { publicNetwork = true; @@ -1530,11 +1530,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian buf.append(" mgmtcidr=").append(_mgmt_cidr); buf.append(" localgw=").append(dest.getPod().getGateway()); } - - + + if (dc.getNetworkType() == NetworkType.Basic) { - // ask domR to setup SSH on guest network - buf.append(" sshonguest=true"); + // ask domR to setup SSH on guest network + buf.append(" sshonguest=true"); } } @@ -1560,7 +1560,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (domain_suffix != null) { buf.append(" dnssearchorder=").append(domain_suffix); } - + if (!network.isDefault() && network.getGuestType() == Network.GuestType.Shared) { buf.append(" defaultroute=false"); @@ -1583,23 +1583,23 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (defaultDns2 != null) { buf.append(" dns2=").append(defaultDns2); } - + boolean useExtDns = !dnsProvided; /* For backward compatibility */ String use_external_dns = _configDao.getValue(Config.UseExternalDnsServers.key()); if (use_external_dns != null && use_external_dns.equals("true")) { useExtDns = true; } - + if (useExtDns) { buf.append(" useextdns=true"); } } if(profile.getHypervisorType() == HypervisorType.VMware) { - buf.append(" extra_pubnics=" + _routerExtraPublicNics); + buf.append(" extra_pubnics=" + _routerExtraPublicNics); } - + if (s_logger.isDebugEnabled()) { s_logger.debug("Boot Args for " + profile + ": " + buf.toString()); } @@ -1640,21 +1640,21 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn()); NicProfile controlNic = null; - + if(profile.getHypervisorType() == HypervisorType.VMware && dcVo.getNetworkType() == NetworkType.Basic) { - // TODO this is a ugly to test hypervisor type here - // for basic network mode, we will use the guest NIC for control NIC - for (NicProfile nic : profile.getNics()) { - if (nic.getTrafficType() == TrafficType.Guest && nic.getIp4Address() != null) { - controlNic = nic; - } - } + // TODO this is a ugly to test hypervisor type here + // for basic network mode, we will use the guest NIC for control NIC + for (NicProfile nic : profile.getNics()) { + if (nic.getTrafficType() == TrafficType.Guest && nic.getIp4Address() != null) { + controlNic = nic; + } + } } else { - for (NicProfile nic : profile.getNics()) { - if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { - controlNic = nic; - } - } + for (NicProfile nic : profile.getNics()) { + if (nic.getTrafficType() == TrafficType.Control && nic.getIp4Address() != null) { + controlNic = nic; + } + } } if (controlNic == null) { @@ -1672,13 +1672,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian // Network usage command to create iptables rules cmds.addCommand("networkUsage", new NetworkUsageCommand(controlNic.getIp4Address(), router.getHostName(), "create")); - + // restart network if restartNetwork = false is not specified in profile parameters boolean reprogramNetwork = true; if (profile.getParameter(Param.ReProgramNetwork) != null && (Boolean) profile.getParameter(Param.ReProgramNetwork) == false) { reprogramNetwork = false; } - + VirtualRouterProvider vrProvider = _vrProviderDao.findById(router.getElementId()); if (vrProvider == null) { throw new CloudRuntimeException("Cannot find related virtual router provider of router: " + router.getHostName()); @@ -1736,7 +1736,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian vpns.add(vpn); } } - + if (_networkMgr.isProviderSupportServiceInNetwork(router.getNetworkId(), Service.StaticNat, provider)) { if (ip.isOneToOneNat()) { String dstIp = _networkMgr.getIpInNetwork(ip.getAssociatedWithVmId(), networkId); @@ -1745,13 +1745,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } } } - + //Re-apply static nats s_logger.debug("Found " + staticNats.size() + " static nat(s) to apply as a part of domR " + router + " start."); if (!staticNats.isEmpty()) { createApplyStaticNatCommands(staticNats, router, cmds); } - + //Re-apply firewall rules s_logger.debug("Found " + staticNats.size() + " firewall rule(s) to apply as a part of domR " + router + " start."); if (!firewallRules.isEmpty()) { @@ -1819,7 +1819,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) { DomainRouterVO router = profile.getVirtualMachine(); boolean result = true; - + Answer answer = cmds.getAnswer("checkSsh"); if (answer != null && answer instanceof CheckSshAnswer) { CheckSshAnswer sshAnswer = (CheckSshAnswer) answer; @@ -1830,7 +1830,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } else { result = false; } - + answer = cmds.getAnswer("getDomRVersion"); if (answer != null && answer instanceof GetDomRVersionAnswer) { GetDomRVersionAnswer versionAnswer = (GetDomRVersionAnswer)answer; @@ -1867,7 +1867,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Failed to start remote access VPN: no router found for account and zone"); throw new ResourceUnavailableException("Failed to start remote access VPN: no router found for account and zone", DataCenter.class, network.getDataCenterId()); } - + for (VirtualRouter router : routers) { if (router.getState() != State.Running) { s_logger.warn("Failed to start remote access VPN: router not in right state " + router.getState()); @@ -1901,7 +1901,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } return true; } - + @Override public boolean deleteRemoteAccessVpn(Network network, RemoteAccessVpn vpn, List routers) throws ResourceUnavailableException { @@ -1909,7 +1909,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Failed to delete remote access VPN: no router found for account and zone"); throw new ResourceUnavailableException("Failed to delete remote access VPN", DataCenter.class, network.getDataCenterId()); } - + boolean result = true; for (VirtualRouter router : routers) { if (router.getState() == State.Running) { @@ -1923,7 +1923,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn()); removeVpnCmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString()); - + cmds.addCommand(removeVpnCmd); result = result && sendCommandsToRouter(router, cmds); @@ -1938,10 +1938,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian return result; } - + private DomainRouterVO start(DomainRouterVO router, User user, Account caller, Map params, DeploymentPlan planToDeploy) throws StorageUnavailableException, InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { + ConcurrentOperationException, ResourceUnavailableException { s_logger.debug("Starting router " + router); if (_itMgr.start(router, params, user, caller, planToDeploy) != null) { return _routerDao.findById(router.getId()); @@ -1970,7 +1970,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian List rets = new ArrayList(routers.size()); _userVmDao.loadDetails((UserVmVO) profile.getVirtualMachine()); - + DataCenter dc = dest.getDataCenter(); String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(profile.getServiceOfferingId()).getDisplayText(); String zoneName = _dcDao.findById(network.getDataCenterId()).getName(); @@ -1978,15 +1978,15 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian List connectedRouters = new ArrayList(); List disconnectedRouters = new ArrayList(); - + for (DomainRouterVO router : routers) { boolean sendDnsDhcpData = true; - + if (router.getState() != State.Running) { s_logger.warn("Unable to add virtual machine " + profile.getVirtualMachine() + " to the router " + router + " as the router is not in Running state"); continue; } - + if (router.isStopPending()) { if (_hostDao.findById(router.getHostId()).getStatus() == Status.Up) { throw new ResourceUnavailableException("Unable to process due to the stop pending router " + router.getInstanceName() + " haven't been stopped after it's host coming back!", @@ -1995,7 +1995,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Unable to add virtual machine " + profile.getVirtualMachine() + " to the router " + router + " as the router is to be stopped"); continue; } - + //for basic zone: //1) send vm data/password information only to the dhcp in the same pod //2) send dhcp/dns information to all routers in the cloudstack only when _dnsBasicZoneUpdates is set to "all" value @@ -2025,12 +2025,12 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_IP, routerControlIpAddress); dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, router.getGuestIpAddress()); dhcpCommand.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); - + dhcpCommand.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dc.getNetworkType().toString()); - + cmds.addCommand("dhcp", dhcpCommand); } - + if (cmds.size() > 0) { boolean podLevelException = false; //for user vm in Basic zone we should try to re-deploy vm in a diff pod if it fails to deploy in original pod; so throwing exception with Pod scope @@ -2059,10 +2059,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new ResourceUnavailableException("Unable to set dhcp entry for " + profile + " due to " + answer.getDetails(), DataCenter.class, router.getDataCenterIdToDeployIn()); } } - + rets.add(router); } - + String msg = "Unable to apply dhcp entry for new VM into network on disconnected router "; if (!connectedRouters.isEmpty()) { // These disconnected ones are out of sync now, stop them for synchronization @@ -2075,7 +2075,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } throw new ResourceUnavailableException(msg, VirtualRouter.class, disconnectedRouters.get(0).getId()); } - + return rets; } @@ -2085,7 +2085,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian List rets = new ArrayList(routers.size()); _userVmDao.loadDetails((UserVmVO) profile.getVirtualMachine()); - + DataCenter dc = dest.getDataCenter(); String serviceOffering = _serviceOfferingDao.findByIdIncludingRemoved(profile.getServiceOfferingId()).getDisplayText(); String zoneName = _dcDao.findById(network.getDataCenterId()).getName(); @@ -2093,15 +2093,15 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian List connectedRouters = new ArrayList(); List disconnectedRouters = new ArrayList(); - + for (DomainRouterVO router : routers) { boolean sendPasswordAndVmData = true; - + if (router.getState() != State.Running) { s_logger.warn("Unable to add virtual machine " + profile.getVirtualMachine() + " to the router " + router + " as the router is not in Running state"); continue; } - + if (router.isStopPending()) { if (_hostDao.findById(router.getHostId()).getStatus() == Status.Up) { throw new ResourceUnavailableException("Unable to process due to the stop pending router " + router.getInstanceName() + " haven't been stopped after it's host coming back!", @@ -2110,7 +2110,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.warn("Unable to add virtual machine " + profile.getVirtualMachine() + " to the router " + router + " as the router is to be stopped"); continue; } - + //for basic zone: //1) send vm data/password information only to the dhcp in the same pod //2) send dhcp/dns information to all routers in the cloudstack only when _dnsBasicZoneUpdates is set to "all" value @@ -2128,7 +2128,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian String password = (String) profile.getParameter(VirtualMachineProfile.Param.VmPassword); String userData = profile.getVirtualMachine().getUserData(); String sshPublicKey = profile.getVirtualMachine().getDetail("SSH.PublicKey"); - + // password should be set only on default network element if (password != null && network.isDefault()) { final String encodedPassword = PasswordGenerator.rot13(password); @@ -2137,11 +2137,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, router.getGuestIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dc.getNetworkType().toString()); - + cmds.addCommand("password", cmd); } - + cmds.addCommand( "vmdata", @@ -2186,10 +2186,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new ResourceUnavailableException("Unable to set VM data due to " + answer.getDetails(), DataCenter.class, router.getDataCenterIdToDeployIn()); } } - + rets.add(router); } - + String msg = "Unable to apply userdata for new VM into network on disconnected router "; if (!connectedRouters.isEmpty()) { // These disconnected ones are out of sync now, stop them for synchronization @@ -2202,7 +2202,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } throw new ResourceUnavailableException(msg, VirtualRouter.class, disconnectedRouters.get(0).getId()); } - + return rets; } @@ -2237,17 +2237,17 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian removeUsers.add(user); } } - - VpnUsersCfgCommand cmd = new VpnUsersCfgCommand(addUsers, removeUsers); - cmd.setAccessDetail(NetworkElementCommand.ACCOUNT_ID, String.valueOf(router.getAccountId())); - cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR, network.getCidr()); - cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); - cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, router.getGuestIpAddress()); - cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); - DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn()); - cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString()); - - cmds.addCommand(cmd); + + VpnUsersCfgCommand cmd = new VpnUsersCfgCommand(addUsers, removeUsers); + cmd.setAccessDetail(NetworkElementCommand.ACCOUNT_ID, String.valueOf(router.getAccountId())); + cmd.setAccessDetail(NetworkElementCommand.GUEST_NETWORK_CIDR, network.getCidr()); + cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); + cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, router.getGuestIpAddress()); + cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); + DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn()); + cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString()); + + cmds.addCommand(cmd); // Currently we receive just one answer from the agent. In the future we have to parse individual answers and set @@ -2255,7 +2255,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian boolean agentResult = sendCommandsToRouter(router, cmds); agentResults = agentResults && agentResult; } - + String[] result = new String[users.size()]; for (int i = 0; i < result.length; i++) { if (agentResults) { @@ -2264,7 +2264,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian result[i] = String.valueOf(agentResults); } } - + return result; } @@ -2284,9 +2284,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian @Override @ActionEvent(eventType = EventTypes.EVENT_ROUTER_START, eventDescription = "starting router Vm", async = true) public VirtualRouter startRouter(long id) throws ResourceUnavailableException, InsufficientCapacityException, ConcurrentOperationException{ - return startRouter(id, true); + return startRouter(id, true); } - + @Override public VirtualRouter startRouter(long routerId, boolean reprogramNetwork) throws ResourceUnavailableException, InsufficientCapacityException, ConcurrentOperationException { Account caller = UserContext.current().getCaller(); @@ -2328,9 +2328,9 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian params.put(Param.ReProgramNetwork, false); } VirtualRouter virtualRouter = startVirtualRouter(router, user, caller, params); - if(virtualRouter == null){ - throw new CloudRuntimeException("Failed to start router with id " + routerId); - } + if(virtualRouter == null){ + throw new CloudRuntimeException("Failed to start router with id " + routerId); + } return virtualRouter; } @@ -2381,13 +2381,13 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian IpAddressTO ip = new IpAddressTO(ipAddr.getAccountId(), ipAddr.getAddress().addr(), add, firstIP, sourceNat, vlanId, vlanGateway, vlanNetmask, vifMacAddress, vmGuestAddress, networkRate, ipAddr.isOneToOneNat()); ip.setTrafficType(network.getTrafficType()); - ip.setNetworkTags(_networkMgr.getNetworkTags(router.getHypervisorType(), network)); + ip.setNetworkName(_networkMgr.getNetworkTag(router.getHypervisorType(), network)); ipsToSend[i++] = ip; /* send the firstIP = true for the first Add, this is to create primary on interface*/ if (!firstIP || add) { firstIP = false; } - + } IpAssocCommand cmd = new IpAssocCommand(ipsToSend); @@ -2396,7 +2396,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); DataCenterVO dcVo = _dcDao.findById(router.getDataCenterIdToDeployIn()); cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString()); - + cmds.addCommand("IPAssocCommand", cmd); } } @@ -2458,10 +2458,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian lbs[i++] = lb; } String RouterPublicIp = null; - + if (router instanceof DomainRouterVO) { - DomainRouterVO domr = (DomainRouterVO)router; - RouterPublicIp = domr.getPublicIpAddress(); + DomainRouterVO domr = (DomainRouterVO)router; + RouterPublicIp = domr.getPublicIpAddress(); } LoadBalancerConfigCommand cmd = new LoadBalancerConfigCommand(lbs,RouterPublicIp, router.getGuestIpAddress(),router.getPrivateIpAddress()); @@ -2470,8 +2470,8 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmd.lbStatsUri = _configDao.getValue(Config.NetworkLBHaproxyStatsUri.key()); cmd.lbStatsAuth = _configDao.getValue(Config.NetworkLBHaproxyStatsAuth.key()); cmd.lbStatsPort = _configDao.getValue(Config.NetworkLBHaproxyStatsPort.key()); - - + + cmd.setAccessDetail(NetworkElementCommand.ROUTER_IP, router.getPrivateIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_GUEST_IP, router.getGuestIpAddress()); cmd.setAccessDetail(NetworkElementCommand.ROUTER_NAME, router.getInstanceName()); @@ -2520,7 +2520,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (dc.getNetworkType() == NetworkType.Basic && router.getPodIdToDeployIn().longValue() != vm.getPodIdToDeployIn().longValue()) { createVmData = false; } - + if (createVmData) { NicVO nic = _nicDao.findByInstanceIdAndNetworkId(networkId, vm.getId()); if (nic != null) { @@ -2584,7 +2584,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } return true; } - + protected void handleSingleWorkingRedundantRouter(List connectedRouters, List disconnectedRouters, String reason) throws ResourceUnavailableException { if (connectedRouters.isEmpty() || disconnectedRouters.isEmpty()) { @@ -2600,10 +2600,10 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian if (!disconnectedRouters.get(0).getIsRedundantRouter()) { throw new ResourceUnavailableException("Who is calling this with non-redundant router or non-domain router?", DataCenter.class, disconnectedRouters.get(0).getDataCenterIdToDeployIn()); } - + DomainRouterVO connectedRouter = (DomainRouterVO)connectedRouters.get(0); DomainRouterVO disconnectedRouter = (DomainRouterVO)disconnectedRouters.get(0); - + if (s_logger.isDebugEnabled()) { s_logger.debug("About to stop the router " + disconnectedRouter.getInstanceName() + " due to: " + reason); } @@ -2613,7 +2613,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian disconnectedRouter.getDataCenterIdToDeployIn(), disconnectedRouter.getPodIdToDeployIn(), title, context); disconnectedRouter.setStopPending(true); disconnectedRouter = this.persist(disconnectedRouter); - + int connRouterPR = getRealPriority(connectedRouter); int disconnRouterPR = getRealPriority(disconnectedRouter); if (connRouterPR < disconnRouterPR) { @@ -2647,7 +2647,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian String msg = "Unable to associate ip addresses on disconnected router "; for (VirtualRouter router : routers) { if (router.getState() == State.Running) { - + if (router.isStopPending()) { if (_hostDao.findById(router.getHostId()).getStatus() == Status.Up) { throw new ResourceUnavailableException("Unable to process due to the stop pending router " + router.getInstanceName() + " haven't been stopped after it's host coming back!", @@ -2717,7 +2717,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian s_logger.debug("Router " + router.getInstanceName() + " is stop pending, so not sending apply firewall rules commands to the backend"); continue; } - + if (rules != null && !rules.isEmpty()) { try { if (rules.get(0).getPurpose() == Purpose.LoadBalancing) { @@ -2751,7 +2751,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new ResourceUnavailableException("Unable to apply firewall rules, virtual router is not in the right state", VirtualRouter.class, router.getId()); } } - + if (!connectedRouters.isEmpty()) { // These disconnected ones are out of sync now, stop them for synchronization handleSingleWorkingRedundantRouter(connectedRouters, disconnectedRouters, msg); @@ -2763,7 +2763,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } throw new ResourceUnavailableException(msg, VirtualRouter.class, disconnectedRouters.get(0).getId()); } - + return true; } @@ -2797,7 +2797,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } return vrs; } - + private void createFirewallRulesCommands(List rules, VirtualRouter router, Commands cmds) { List rulesTO = null; if (rules != null) { @@ -2817,21 +2817,21 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian cmd.setAccessDetail(NetworkElementCommand.ZONE_NETWORK_TYPE, dcVo.getNetworkType().toString()); cmds.addCommand(cmd); } - - + + protected boolean applyFirewallRules(VirtualRouter router, List rules) throws ResourceUnavailableException { Commands cmds = new Commands(OnError.Continue); createFirewallRulesCommands(rules, router, cmds); // Send commands to router return sendCommandsToRouter(router, cmds); } - + @Override public String getDnsBasicZoneUpdate() { return _dnsBasicZoneUpdates; } - - + + @Override public boolean applyStaticNats(Network network, List rules, List routers) throws ResourceUnavailableException { if (routers == null || routers.isEmpty()) { @@ -2846,7 +2846,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian for (VirtualRouter router : routers) { if (router.getState() == State.Running) { s_logger.debug("Applying " + rules.size() + " static nat in network " + network); - + if (router.isStopPending()) { if (_hostDao.findById(router.getHostId()).getStatus() == Status.Up) { throw new ResourceUnavailableException("Unable to process due to the stop pending router " + router.getInstanceName() + " haven't been stopped after it's host coming back!", @@ -2875,7 +2875,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian throw new ResourceUnavailableException("Unable to apply static nat, virtual router is not in the right state", VirtualRouter.class, router.getId()); } } - + if (!connectedRouters.isEmpty()) { // These disconnected ones are out of sync now, stop them for synchronization handleSingleWorkingRedundantRouter(connectedRouters, disconnectedRouters, msg); @@ -2887,18 +2887,18 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian } throw new ResourceUnavailableException(msg, VirtualRouter.class, disconnectedRouters.get(0).getId()); } - + return true; } - - + + protected boolean applyStaticNat(VirtualRouter router, List rules) throws ResourceUnavailableException { Commands cmds = new Commands(OnError.Continue); createApplyStaticNatCommands(rules, router, cmds); // Send commands to router return sendCommandsToRouter(router, cmds); } - + private void createApplyStaticNatCommands(List rules, VirtualRouter router, Commands cmds) { List rulesTO = null; if (rules != null) { @@ -2978,7 +2978,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian public boolean processTimeout(long agentId, long seq) { return false; } - + @Override public long getDefaultVirtualRouterServiceOfferingId() { if (_offering != null) { diff --git a/server/src/com/cloud/vm/UserVmManagerImpl.java b/server/src/com/cloud/vm/UserVmManagerImpl.java index 5f93ca9055f..e7dd2b7509a 100755 --- a/server/src/com/cloud/vm/UserVmManagerImpl.java +++ b/server/src/com/cloud/vm/UserVmManagerImpl.java @@ -86,7 +86,6 @@ import com.cloud.dc.DataCenter; import com.cloud.dc.DataCenter.NetworkType; import com.cloud.dc.DataCenterVO; import com.cloud.dc.HostPodVO; -import com.cloud.dc.dao.AccountVlanMapDao; import com.cloud.dc.dao.ClusterDao; import com.cloud.dc.dao.DataCenterDao; import com.cloud.dc.dao.HostPodDao; @@ -393,7 +392,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if (userVm == null) { throw new InvalidParameterValueException("unable to find a virtual machine with id " + cmd.getId()); } - + VMTemplateVO template = _templateDao.findByIdIncludingRemoved(userVm.getTemplateId()); if (template == null || !template.getEnablePassword()) { throw new InvalidParameterValueException("Fail to reset password for the virtual machine, the template is not password enabled"); @@ -445,7 +444,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } Network defaultNetwork = _networkDao.findById(defaultNic.getNetworkId()); - NicProfile defaultNicProfile = new NicProfile(defaultNic, defaultNetwork, null, null, null, _networkMgr.isSecurityGroupSupportedInNetwork(defaultNetwork), _networkMgr.getNetworkTags(template.getHypervisorType(), defaultNetwork)); + NicProfile defaultNicProfile = new NicProfile(defaultNic, defaultNetwork, null, null, null, _networkMgr.isSecurityGroupSupportedInNetwork(defaultNetwork), _networkMgr.getNetworkTag(template.getHypervisorType(), defaultNetwork)); VirtualMachineProfile vmProfile = new VirtualMachineProfileImpl(vmInstance); vmProfile.setParameter(VirtualMachineProfile.Param.VmPassword, password); @@ -676,10 +675,10 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if (moveVolumeNeeded) { // Move the volume to a storage pool in the VM's zone, pod, or cluster try { - volume = _storageMgr.moveVolume(volume, vmRootVolumePool.getDataCenterId(), vmRootVolumePool.getPodId(), vmRootVolumePool.getClusterId(), dataDiskHyperType); - } catch (ConcurrentOperationException e) { - throw new CloudRuntimeException(e.toString()); - } + volume = _storageMgr.moveVolume(volume, vmRootVolumePool.getDataCenterId(), vmRootVolumePool.getPodId(), vmRootVolumePool.getClusterId(), dataDiskHyperType); + } catch (ConcurrentOperationException e) { + throw new CloudRuntimeException(e.toString()); + } } AsyncJobExecutor asyncExecutor = BaseAsyncJobExecutor.getCurrentExecutor(); @@ -1037,7 +1036,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if (vm == null) { throw new InvalidParameterValueException("unable to find a virtual machine with id " + vmId); } - + //check permissions _accountMgr.checkAccess(caller, null, vm); @@ -1129,10 +1128,10 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if (_instance == null) { _instance = "DEFAULT"; } - + String value = _configDao.getValue(Config.CreatePrivateTemplateFromVolumeWait.toString()); _createprivatetemplatefromvolumewait = NumbersUtil.parseInt(value, Integer.parseInt(Config.CreatePrivateTemplateFromVolumeWait.getDefaultValue())); - + value = _configDao.getValue(Config.CreatePrivateTemplateFromSnapshotWait.toString()); _createprivatetemplatefromsnapshotwait = NumbersUtil.parseInt(value, Integer.parseInt(Config.CreatePrivateTemplateFromSnapshotWait.getDefaultValue())); @@ -1234,7 +1233,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager //Remove vm from instance group removeInstanceFromInstanceGroup(vmId); - + //cleanup firewall rules if (_firewallMgr.revokeFirewallRulesForVm(vmId)) { s_logger.debug("Firewall rules are removed successfully as a part of vm id=" + vmId + " expunge"); @@ -1305,7 +1304,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if ((name == null) || (name.length() > 32)) { throw new InvalidParameterValueException("Template name cannot be null and should be less than 32 characters"); } - + if(cmd.getTemplateTag() != null){ if(!_accountService.isRootAdmin(caller.getType())){ throw new PermissionDeniedException("Parameter templatetag can only be specified by a Root Admin, permission denied"); @@ -1349,7 +1348,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } //check permissions _accountMgr.checkAccess(caller, null, volume); - + // If private template is created from Volume, check that the volume will not be active when the private template is // created if (!_storageMgr.volumeInactive(volume)) { @@ -1366,21 +1365,21 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager SnapshotVO snapshot = _snapshotDao.findById(snapshotId); volume = _volsDao.findById(snapshot.getVolumeId()); VolumeVO snapshotVolume = _volsDao.findByIdIncludingRemoved(snapshot.getVolumeId()); - + if (snapshot == null) { throw new InvalidParameterValueException("Failed to create private template record, unable to find snapshot " + snapshotId); } - + //check permissions _accountMgr.checkAccess(caller, null, snapshot); if (snapshot.getStatus() != Snapshot.Status.BackedUp) { throw new InvalidParameterValueException("Snapshot id=" + snapshotId + " is not in " + Snapshot.Status.BackedUp + " state yet and can't be used for template creation"); } - + // bug #11428. Operation not supported if vmware and snapshots parent volume = ROOT if(snapshot.getHypervisorType() == HypervisorType.VMware && snapshotVolume.getVolumeType() == Type.DATADISK){ - throw new UnsupportedServiceException("operation not supported, snapshot with id " + snapshotId + " is created from Data Disk"); + throw new UnsupportedServiceException("operation not supported, snapshot with id " + snapshotId + " is created from Data Disk"); } domainId = snapshot.getDomainId(); @@ -1606,7 +1605,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager String checkSum = getChecksum(secondaryStorageHost.getId(), answer.getPath()); Transaction txn = Transaction.currentTxn(); - + txn.start(); privateTemplate.setChecksum(checkSum); @@ -1855,7 +1854,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager String accountName = cmd.getAccountName(); String groupName = cmd.getGroupName(); Long projectId = cmd.getProjectId(); - + Account owner = _accountMgr.finalizeOwner(caller, accountName, domainId, projectId); long accountId = owner.getId(); @@ -1904,7 +1903,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if ((group == null) || (group.getRemoved() != null)) { throw new InvalidParameterValueException("unable to find a vm group with id " + groupId); } - + _accountMgr.checkAccess(caller, null, group); return deleteVmGroup(groupId); @@ -2025,7 +2024,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager @Override public UserVm createBasicSecurityGroupVirtualMachine(DataCenter zone, ServiceOffering serviceOffering, VirtualMachineTemplate template, List securityGroupIdList, Account owner, String hostName, String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair, Map requestedIps, String defaultIp, String keyboard) - throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException { + throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException { Account caller = UserContext.current().getCaller(); List networkList = new ArrayList(); @@ -2195,7 +2194,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager @Override public UserVm createAdvancedVirtualMachine(DataCenter zone, ServiceOffering serviceOffering, VirtualMachineTemplate template, List networkIdList, Account owner, String hostName, String displayName, Long diskOfferingId, Long diskSize, String group, HypervisorType hypervisor, String userData, String sshKeyPair, Map requestedIps, String defaultIp, String keyboard) - throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException { + throws InsufficientCapacityException, ConcurrentOperationException, ResourceUnavailableException, StorageUnavailableException, ResourceAllocationException { Account caller = UserContext.current().getCaller(); List networkList = new ArrayList(); @@ -2219,7 +2218,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if (defaultVirtualOffering.get(0).getAvailability() == Availability.Required) { // get Virtual netowrks List virtualNetworks = _networkMgr.listNetworksForAccount(owner.getId(), zone.getId(), Network.GuestType.Isolated, true); - + if (virtualNetworks.isEmpty()) { s_logger.debug("Creating default Virtual network for account " + owner + " as a part of deployVM process"); @@ -2280,7 +2279,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager throw new PermissionDeniedException("Shared network id=" + networkId + " is not available in domain id=" + owner.getDomainId()); } } - + //don't allow to use system networks NetworkOffering networkOffering = _configMgr.getNetworkOffering(network.getNetworkOfferingId()); if (networkOffering.isSystemOnly()) { @@ -2445,12 +2444,12 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } networks.add(new Pair(network, profile)); - + if (_networkMgr.isSecurityGroupSupportedInNetwork(network)) { securityGroupEnabled = true; } } - + if (securityGroupIdList != null && !securityGroupIdList.isEmpty() && !securityGroupEnabled) { throw new InvalidParameterValueException("Unable to deploy vm with security groups as SecurityGroup service is not enabled for the vm's network"); } @@ -2647,12 +2646,12 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } if (vm.getIsoId() != null) { - String isoPath = null; - - VirtualMachineTemplate template = _templateDao.findById(vm.getIsoId()); - if (template == null || template.getFormat() != ImageFormat.ISO) { - throw new CloudRuntimeException("Can not find ISO in vm_template table for id " + vm.getIsoId()); - } + String isoPath = null; + + VirtualMachineTemplate template = _templateDao.findById(vm.getIsoId()); + if (template == null || template.getFormat() != ImageFormat.ISO) { + throw new CloudRuntimeException("Can not find ISO in vm_template table for id " + vm.getIsoId()); + } Pair isoPathPair = _storageMgr.getAbsoluteIsoPath(template.getId(), vm.getDataCenterIdToDeployIn()); @@ -2709,7 +2708,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager @Override public boolean finalizeStart(VirtualMachineProfile profile, long hostId, Commands cmds, ReservationContext context) { UserVmVO vm = profile.getVirtualMachine(); - + Answer[] answersToCmds = cmds.getAnswers(); if(answersToCmds == null){ if(s_logger.isDebugEnabled()){ @@ -2886,7 +2885,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager s_logger.trace("Vm id=" + vmId + " is already destroyed"); return vm; } - + _accountMgr.checkAccess(caller, null, vm); User userCaller = _userDao.findById(userId); @@ -2976,7 +2975,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager domainId = null; } } - + //set project information if (projectId != null) { permittedAccounts.clear(); @@ -3229,10 +3228,10 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager public UserVm getUserVm(long vmId) { return _vmDao.findById(vmId); } - + @Override public VirtualMachine vmStorageMigration(Long vmId, StoragePool destPool) { - // access check - only root admin can migrate VM + // access check - only root admin can migrate VM Account caller = UserContext.current().getCaller(); if (caller.getType() != Account.ACCOUNT_TYPE_ADMIN) { if (s_logger.isDebugEnabled()) { @@ -3240,27 +3239,27 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } throw new PermissionDeniedException("No permission to migrate VM, Only Root Admin can migrate a VM!"); } - + VMInstanceVO vm = _vmInstanceDao.findById(vmId); if (vm == null) { throw new InvalidParameterValueException("Unable to find the VM by id=" + vmId); } - + if (vm.getState() != State.Stopped) { - throw new InvalidParameterValueException("VM is not Stopped, unable to migrate the vm " + vm); + throw new InvalidParameterValueException("VM is not Stopped, unable to migrate the vm " + vm); } - + if (vm.getType() != VirtualMachine.Type.User) { - throw new InvalidParameterValueException("can only do storage migration on user vm"); + throw new InvalidParameterValueException("can only do storage migration on user vm"); } - + HypervisorType destHypervisorType = _clusterDao.findById(destPool.getClusterId()).getHypervisorType(); if (vm.getHypervisorType() != destHypervisorType) { - throw new InvalidParameterValueException("hypervisor is not compatible: dest: " + destHypervisorType.toString() + ", vm: " + vm.getHypervisorType().toString()); + throw new InvalidParameterValueException("hypervisor is not compatible: dest: " + destHypervisorType.toString() + ", vm: " + vm.getHypervisorType().toString()); } VMInstanceVO migratedVm = _itMgr.storageMigration(vm, destPool); return migratedVm; - + } @Override @@ -3274,7 +3273,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } throw new PermissionDeniedException("No permission to migrate VM, Only Root Admin can migrate a VM!"); } - + VMInstanceVO vm = _vmInstanceDao.findById(vmId); if (vm == null) { throw new InvalidParameterValueException("Unable to find the VM by id=" + vmId); @@ -3307,7 +3306,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager long srcHostId = vm.getHostId(); Cluster cluster = _clusterDao.findById(destinationHost.getClusterId()); DeployDestination dest = new DeployDestination(dcVO, pod, cluster, destinationHost); - + //check max guest vm limit for the destinationHost HypervisorType hypervisorType = destinationHost.getHypervisorType(); String hypervisorVersion = destinationHost.getHypervisorVersion(); @@ -3336,7 +3335,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager if (newAccount == null || newAccount.getType() == Account.ACCOUNT_TYPE_PROJECT) { throw new InvalidParameterValueException("Invalid accountid=" + cmd.getAccountId() + " in domain " + oldAccount.getDomainId()); } - + //don't allow to move the vm from the project if (oldAccount.getType() == Account.ACCOUNT_TYPE_PROJECT) { throw new InvalidParameterValueException("Vm id=" + cmd.getVmId() + " belongs to the project and can't be moved"); @@ -3369,12 +3368,12 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager _accountMgr.checkAccess(newAccount, domain); DataCenterVO zone = _dcDao.findById(vm.getDataCenterIdToDeployIn()); - + //check is zone networking is advanced if (zone.getNetworkType() != NetworkType.Advanced) { throw new InvalidParameterValueException("Assing virtual machine to another account is only available for advanced networking " + vm); } - + VMInstanceVO vmoi = _itMgr.findByIdAndType(vm.getType(), vm.getId()); VirtualMachineProfileImpl vmOldProfile = new VirtualMachineProfileImpl(vmoi); @@ -3385,7 +3384,7 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString())); // update resource counts _resourceLimitMgr.decrementResourceCount(vm.getAccountId(), ResourceType.user_vm); - + // OWNERSHIP STEP 1: update the vm owner vm.setAccountId(newAccount.getAccountId()); _vmDao.persist(vm); @@ -3397,13 +3396,13 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager _volsDao.persist(volume); _resourceLimitMgr.incrementResourceCount(vm.getAccountId(), ResourceType.volume, new Long(volumes.size())); } - + _resourceLimitMgr.incrementResourceCount(vm.getAccountId(), ResourceType.user_vm); //generate usage evenst to account for this change _usageEventDao.persist(new UsageEventVO(EventTypes.EVENT_VM_CREATE, vm.getAccountId(), vm.getDataCenterIdToDeployIn(), vm.getId(), vm.getHostName(), vm.getServiceOfferingId(), vm.getTemplateId(), vm.getHypervisorType().toString())); - - + + // OS 3: update the network if (zone.getNetworkType() == NetworkType.Advanced) { @@ -3461,80 +3460,80 @@ public class UserVmManagerImpl implements UserVmManager, UserVmService, Manager } txn.commit(); - + return vm; } - @Override + @Override public UserVm restoreVM(RestoreVMCmd cmd) { // Input validation Account caller = UserContext.current().getCaller(); Long userId = UserContext.current().getCallerUserId(); UserVO user = _userDao.findById(userId); boolean needRestart = false; - + // if account is removed, return error if (caller != null && caller.getRemoved() != null) { throw new PermissionDeniedException("The account " + caller.getId() + " is removed"); } - long vmId = cmd.getVmId(); - UserVmVO vm = _vmDao.findById(vmId); - if (vm == null) { - throw new InvalidParameterValueException("Cann not find VM with ID " + vmId); - } - - if (vm.getState() != VirtualMachine.State.Running && vm.getState() != VirtualMachine.State.Stopped) { - throw new CloudRuntimeException("Vm " + vmId + " currently in " + vm.getState() + " state, restore vm can only execute when VM in Running or Stopped"); - } - - if (vm.getState() == VirtualMachine.State.Running) { - needRestart = true; - } - - List rootVols = _volsDao.findByInstance(vmId); - if (rootVols.isEmpty()) { - throw new InvalidParameterValueException("Can not find root volume for VM " + vmId); - } - - VolumeVO root = rootVols.get(0); - long templateId = root.getTemplateId(); - VMTemplateVO template = _templateDao.findById(templateId); - if (template == null) { - throw new InvalidParameterValueException("Cannot find template for volume " + root.getId() + " vm " + vmId); - } - - if (needRestart) { - try { - _itMgr.stop(vm, user, caller); - } catch (ResourceUnavailableException e) { - s_logger.debug("Stop vm " + vmId + " failed", e); - throw new CloudRuntimeException("Stop vm " + vmId + " failed"); - } - } - - /* allocate a new volume from original template*/ - VolumeVO newVol = _storageMgr.allocateDuplicateVolume(root, null); - _volsDao.attachVolume(newVol.getId(), vmId, newVol.getDeviceId()); - - /* Detach and destory the old root volume */ - try { - _volsDao.detachVolume(root.getId()); - _storageMgr.destroyVolume(root); - } catch (ConcurrentOperationException e) { - s_logger.debug("Unable to delete old root volume " + root.getId() + ", user may manually delete it", e); + long vmId = cmd.getVmId(); + UserVmVO vm = _vmDao.findById(vmId); + if (vm == null) { + throw new InvalidParameterValueException("Cann not find VM with ID " + vmId); } - - if (needRestart) { - try { - _itMgr.start(vm, null, user, caller); - } catch (Exception e) { - s_logger.debug("Unable to start VM " + vmId, e); - throw new CloudRuntimeException("Unable to start VM " + vmId + " " + e.getMessage()); - } - } - - s_logger.debug("Restore VM " + vmId + " with template " + root.getTemplateId() + " successfully"); - return vm; + + if (vm.getState() != VirtualMachine.State.Running && vm.getState() != VirtualMachine.State.Stopped) { + throw new CloudRuntimeException("Vm " + vmId + " currently in " + vm.getState() + " state, restore vm can only execute when VM in Running or Stopped"); + } + + if (vm.getState() == VirtualMachine.State.Running) { + needRestart = true; + } + + List rootVols = _volsDao.findByInstance(vmId); + if (rootVols.isEmpty()) { + throw new InvalidParameterValueException("Can not find root volume for VM " + vmId); + } + + VolumeVO root = rootVols.get(0); + long templateId = root.getTemplateId(); + VMTemplateVO template = _templateDao.findById(templateId); + if (template == null) { + throw new InvalidParameterValueException("Cannot find template for volume " + root.getId() + " vm " + vmId); + } + + if (needRestart) { + try { + _itMgr.stop(vm, user, caller); + } catch (ResourceUnavailableException e) { + s_logger.debug("Stop vm " + vmId + " failed", e); + throw new CloudRuntimeException("Stop vm " + vmId + " failed"); + } + } + + /* allocate a new volume from original template*/ + VolumeVO newVol = _storageMgr.allocateDuplicateVolume(root, null); + _volsDao.attachVolume(newVol.getId(), vmId, newVol.getDeviceId()); + + /* Detach and destory the old root volume */ + try { + _volsDao.detachVolume(root.getId()); + _storageMgr.destroyVolume(root); + } catch (ConcurrentOperationException e) { + s_logger.debug("Unable to delete old root volume " + root.getId() + ", user may manually delete it", e); + } + + if (needRestart) { + try { + _itMgr.start(vm, null, user, caller); + } catch (Exception e) { + s_logger.debug("Unable to start VM " + vmId, e); + throw new CloudRuntimeException("Unable to start VM " + vmId + " " + e.getMessage()); + } + } + + s_logger.debug("Restore VM " + vmId + " with template " + root.getTemplateId() + " successfully"); + return vm; } } diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index b38131668b3..6363fbf9f71 100755 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -226,7 +226,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene @Inject(adapter = HostAllocator.class) protected Adapters _hostAllocators; - + @Inject protected ResourceManager _resourceMgr; @@ -434,7 +434,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene _lockStateRetry = NumbersUtil.parseInt(params.get(Config.VmOpLockStateRetry.key()), 5); _operationTimeout = NumbersUtil.parseInt(params.get(Config.Wait.key()), 1800) * 2; _forceStop = Boolean.parseBoolean(params.get(Config.VmDestroyForcestop.key())); - + _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("Vm-Operations-Cleanup")); _nodeId = _clusterMgr.getManagementNodeId(); @@ -459,7 +459,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene @Override public T start(T vm, Map params, User caller, Account account, DeploymentPlan planToDeploy) throws InsufficientCapacityException, - ResourceUnavailableException { + ResourceUnavailableException { try { return advanceStart(vm, params, caller, account, planToDeploy); } catch (ConcurrentOperationException e) { @@ -585,7 +585,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene @Override public T advanceStart(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { + ConcurrentOperationException, ResourceUnavailableException { return advanceStart(vm, params, caller, account, null); } @@ -1179,68 +1179,68 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return true; } - + @Override public T storageMigration(T vm, StoragePool destPool) { - VirtualMachineGuru vmGuru = getVmGuru(vm); + VirtualMachineGuru vmGuru = getVmGuru(vm); - long vmId = vm.getId(); - vm = vmGuru.findById(vmId); + long vmId = vm.getId(); + vm = vmGuru.findById(vmId); - try { - stateTransitTo(vm, VirtualMachine.Event.StorageMigrationRequested, null); - } catch (NoTransitionException e) { - s_logger.debug("Unable to migrate vm: " + e.toString()); - throw new CloudRuntimeException("Unable to migrate vm: " + e.toString()); - } - - VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); - boolean migrationResult = false; - try { - migrationResult = _storageMgr.StorageMigration(profile, destPool); + try { + stateTransitTo(vm, VirtualMachine.Event.StorageMigrationRequested, null); + } catch (NoTransitionException e) { + s_logger.debug("Unable to migrate vm: " + e.toString()); + throw new CloudRuntimeException("Unable to migrate vm: " + e.toString()); + } - if (migrationResult) { - //if the vm is migrated to different pod in basic mode, need to reallocate ip - - if (vm.getPodIdToDeployIn() != destPool.getPodId()) { + VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); + boolean migrationResult = false; + try { + migrationResult = _storageMgr.StorageMigration(profile, destPool); + + if (migrationResult) { + //if the vm is migrated to different pod in basic mode, need to reallocate ip + + if (vm.getPodIdToDeployIn() != destPool.getPodId()) { DataCenterDeployment plan = new DataCenterDeployment(vm.getDataCenterIdToDeployIn(), destPool.getPodId(), null, null, null, null); - VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, null, null, null, null); - _networkMgr.reallocate(vmProfile, plan); - } - - //when start the vm next time, don;'t look at last_host_id, only choose the host based on volume/storage pool - vm.setLastHostId(null); - vm.setPodId(destPool.getPodId()); - } else { - s_logger.debug("Storage migration failed"); - } - } catch (ConcurrentOperationException e) { - s_logger.debug("Failed to migration: " + e.toString()); - throw new CloudRuntimeException("Failed to migration: " + e.toString()); - } catch (InsufficientVirtualNetworkCapcityException e) { - s_logger.debug("Failed to migration: " + e.toString()); - throw new CloudRuntimeException("Failed to migration: " + e.toString()); - } catch (InsufficientAddressCapacityException e) { - s_logger.debug("Failed to migration: " + e.toString()); - throw new CloudRuntimeException("Failed to migration: " + e.toString()); - } catch (InsufficientCapacityException e) { - s_logger.debug("Failed to migration: " + e.toString()); - throw new CloudRuntimeException("Failed to migration: " + e.toString()); - } finally { - try { - stateTransitTo(vm, VirtualMachine.Event.AgentReportStopped, null); - } catch (NoTransitionException e) { - s_logger.debug("Failed to change vm state: " + e.toString()); - throw new CloudRuntimeException("Failed to change vm state: " + e.toString()); - } - } - - return vm; + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, null, null, null, null); + _networkMgr.reallocate(vmProfile, plan); + } + + //when start the vm next time, don;'t look at last_host_id, only choose the host based on volume/storage pool + vm.setLastHostId(null); + vm.setPodId(destPool.getPodId()); + } else { + s_logger.debug("Storage migration failed"); + } + } catch (ConcurrentOperationException e) { + s_logger.debug("Failed to migration: " + e.toString()); + throw new CloudRuntimeException("Failed to migration: " + e.toString()); + } catch (InsufficientVirtualNetworkCapcityException e) { + s_logger.debug("Failed to migration: " + e.toString()); + throw new CloudRuntimeException("Failed to migration: " + e.toString()); + } catch (InsufficientAddressCapacityException e) { + s_logger.debug("Failed to migration: " + e.toString()); + throw new CloudRuntimeException("Failed to migration: " + e.toString()); + } catch (InsufficientCapacityException e) { + s_logger.debug("Failed to migration: " + e.toString()); + throw new CloudRuntimeException("Failed to migration: " + e.toString()); + } finally { + try { + stateTransitTo(vm, VirtualMachine.Event.AgentReportStopped, null); + } catch (NoTransitionException e) { + s_logger.debug("Failed to change vm state: " + e.toString()); + throw new CloudRuntimeException("Failed to change vm state: " + e.toString()); + } + } + + return vm; } @Override public T migrate(T vm, long srcHostId, DeployDestination dest) throws ResourceUnavailableException, ConcurrentOperationException, ManagementServerException, - VirtualMachineMigrationException { + VirtualMachineMigrationException { s_logger.info("Migrating " + vm + " to " + dest); long dstHostId = dest.getHost().getId(); @@ -1547,7 +1547,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene @Override public T advanceReboot(T vm, Map params, User caller, Account account) throws InsufficientCapacityException, - ConcurrentOperationException, ResourceUnavailableException { + ConcurrentOperationException, ResourceUnavailableException { T rebootedVm = null; DataCenter dc = _configMgr.getZone(vm.getDataCenterIdToDeployIn()); @@ -1588,59 +1588,59 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene public Command cleanup(String vmName) { return new StopCommand(vmName); } - + public Commands fullHostSync(final long hostId, StartupRoutingCommand startup) { - Commands commands = new Commands(OnError.Continue); - - Map infos = convertToInfos(startup); - - final List vms = _vmDao.listByHostId(hostId); - s_logger.debug("Found " + vms.size() + " VMs for host " + hostId); - for (VMInstanceVO vm : vms) { - AgentVmInfo info = infos.remove(vm.getId()); - VMInstanceVO castedVm = null; - if (info == null) { - info = new AgentVmInfo(vm.getInstanceName(), getVmGuru(vm), vm, State.Stopped); - castedVm = info.guru.findById(vm.getId()); - } else { - castedVm = info.vm; - } - - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(castedVm.getHypervisorType()); - - Command command = compareState(hostId, castedVm, info, true, hvGuru.trackVmHostChange()); - if (command != null) { - commands.addCommand(command); - } - } - - for (final AgentVmInfo left : infos.values()) { - boolean found = false; - for (VirtualMachineGuru vmGuru : _vmGurus.values()) { - VMInstanceVO vm = vmGuru.findByName(left.name); - if (vm != null) { - found = true; - HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); - if(hvGuru.trackVmHostChange()) { - Command command = compareState(hostId, vm, left, true, true); - if (command != null) { - commands.addCommand(command); - } - } else { - s_logger.warn("Stopping a VM, VM " + left.name + " migrate from Host " + vm.getHostId() + " to Host " + hostId ); - commands.addCommand(cleanup(left.name)); - } - break; - } - } - if ( ! found ) { - s_logger.warn("Stopping a VM that we have no record of: " + left.name); - commands.addCommand(cleanup(left.name)); - } - } - - return commands; - } + Commands commands = new Commands(OnError.Continue); + + Map infos = convertToInfos(startup); + + final List vms = _vmDao.listByHostId(hostId); + s_logger.debug("Found " + vms.size() + " VMs for host " + hostId); + for (VMInstanceVO vm : vms) { + AgentVmInfo info = infos.remove(vm.getId()); + VMInstanceVO castedVm = null; + if (info == null) { + info = new AgentVmInfo(vm.getInstanceName(), getVmGuru(vm), vm, State.Stopped); + castedVm = info.guru.findById(vm.getId()); + } else { + castedVm = info.vm; + } + + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(castedVm.getHypervisorType()); + + Command command = compareState(hostId, castedVm, info, true, hvGuru.trackVmHostChange()); + if (command != null) { + commands.addCommand(command); + } + } + + for (final AgentVmInfo left : infos.values()) { + boolean found = false; + for (VirtualMachineGuru vmGuru : _vmGurus.values()) { + VMInstanceVO vm = vmGuru.findByName(left.name); + if (vm != null) { + found = true; + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); + if(hvGuru.trackVmHostChange()) { + Command command = compareState(hostId, vm, left, true, true); + if (command != null) { + commands.addCommand(command); + } + } else { + s_logger.warn("Stopping a VM, VM " + left.name + " migrate from Host " + vm.getHostId() + " to Host " + hostId ); + commands.addCommand(cleanup(left.name)); + } + break; + } + } + if ( ! found ) { + s_logger.warn("Stopping a VM that we have no record of: " + left.name); + commands.addCommand(cleanup(left.name)); + } + } + + return commands; + } public Commands deltaHostSync(long hostId, Map newStates) { Map states = convertDeltaToInfos(newStates); @@ -1670,8 +1670,8 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return commands; } - - + + public Commands deltaSync(Map> newStates) { Map states = convertToInfos(newStates); Commands commands = new Commands(OnError.Continue); @@ -1684,7 +1684,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene String hostGuid = info.getHostUuid(); Host host = _resourceMgr.findHostByGuid(hostGuid); long hId = host.getId(); - + HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vm.getHypervisorType()); command = compareState(hId, vm, info, false, hvGuru.trackVmHostChange()); } else { @@ -1701,7 +1701,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return commands; } - + public Commands fullSync(final long clusterId, Map> newStates) { @@ -1769,7 +1769,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return map; } - + protected Map convertToInfos(StartupRoutingCommand cmd) { final Map states = cmd.getVmStates(); final HashMap map = new HashMap(); @@ -1777,7 +1777,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene return map; } Collection> vmGurus = _vmGurus.values(); - + for (Map.Entry entry : states.entrySet()) { for (VirtualMachineGuru vmGuru : vmGurus) { String name = entry.getKey(); @@ -1793,10 +1793,10 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene } } } - + return map; } - + protected Map convertDeltaToInfos(final Map states) { final HashMap map = new HashMap(); @@ -1847,7 +1847,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene if (s_logger.isDebugEnabled()) { s_logger.debug("VM " + serverName + ": cs state = " + serverState + " and realState = " + agentState); } - + if (agentState == State.Error) { agentState = State.Stopped; @@ -2024,13 +2024,13 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.debug("VM's " + vm + " state is starting on full sync so updating it to Running"); vm = vmGuru.findById(vm.getId()); // this should ensure vm has the most - // up to date info + // up to date info VirtualMachineProfile profile = new VirtualMachineProfileImpl(vm); List nics = _nicsDao.listByVmId(profile.getId()); for (NicVO nic : nics) { Network network = _networkMgr.getNetwork(nic.getNetworkId()); - NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkMgr.isSecurityGroupSupportedInNetwork(network), _networkMgr.getNetworkTags(profile.getHypervisorType(), network)); + NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null, _networkMgr.isSecurityGroupSupportedInNetwork(network), _networkMgr.getNetworkTag(profile.getHypervisorType(), network)); profile.addNic(nicProfile); } @@ -2101,24 +2101,24 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene @Override public boolean processCommands(long agentId, long seq, Command[] cmds) { - boolean processed = false; - for (Command cmd : cmds) { - if (cmd instanceof PingRoutingCommand) { - PingRoutingCommand ping = (PingRoutingCommand) cmd; - if (ping.getNewStates() != null && ping.getNewStates().size() > 0) { - Commands commands = deltaHostSync(agentId, ping.getNewStates()); - if (commands.size() > 0) { - try { - _agentMgr.send(agentId, commands, this); - } catch (final AgentUnavailableException e) { - s_logger.warn("Agent is now unavailable", e); - } - } - } - processed = true; - } - } - return processed; + boolean processed = false; + for (Command cmd : cmds) { + if (cmd instanceof PingRoutingCommand) { + PingRoutingCommand ping = (PingRoutingCommand) cmd; + if (ping.getNewStates() != null && ping.getNewStates().size() > 0) { + Commands commands = deltaHostSync(agentId, ping.getNewStates()); + if (commands.size() > 0) { + try { + _agentMgr.send(agentId, commands, this); + } catch (final AgentUnavailableException e) { + s_logger.warn("Agent is now unavailable", e); + } + } + } + processed = true; + } + } + return processed; } @Override @@ -2146,7 +2146,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene s_logger.debug("Not processing listener " + this + " as connect happens on rebalance process"); return; } - + Long clusterId = agent.getClusterId(); long agentId = agent.getId(); if (agent.getHypervisorType() == HypervisorType.XenServer) { // only fro Xen