@@ -4407,14 +4407,36 @@ public Vlan createVlanAndPublicIpRange(final CreateVlanIpRangeCmd cmd) throws In
44074407 String endIP = cmd .getEndIp ();
44084408 final String newVlanGateway = cmd .getGateway ();
44094409 final String newVlanNetmask = cmd .getNetmask ();
4410+ Long networkId = cmd .getNetworkID ();
4411+ Long physicalNetworkId = cmd .getPhysicalNetworkId ();
4412+
4413+ // Verify that network exists
4414+ Network network = null ;
4415+ if (networkId != null ) {
4416+ network = _networkDao .findById (networkId );
4417+ if (network == null ) {
4418+ throw new InvalidParameterValueException ("Unable to find network by id " + networkId );
4419+ } else {
4420+ zoneId = network .getDataCenterId ();
4421+ physicalNetworkId = network .getPhysicalNetworkId ();
4422+ }
4423+ }
4424+
44104425 String vlanId = cmd .getVlan ();
4426+ if ((StringUtils .isBlank (vlanId ))) {
4427+ vlanId = Vlan .UNTAGGED ;
4428+ if (network != null & network .getTrafficType () == TrafficType .Guest ) {
4429+ boolean connectivityWithoutVlan = isConnectivityWithoutVlan (network );
4430+ vlanId = getNetworkVlanId (network , connectivityWithoutVlan );
4431+ }
4432+ }
4433+
4434+
44114435 // TODO decide if we should be forgiving or demand a valid and complete URI
44124436 if (!(vlanId == null || "" .equals (vlanId ) || vlanId .startsWith (BroadcastDomainType .Vlan .scheme ()))) {
44134437 vlanId = BroadcastDomainType .Vlan .toUri (vlanId ).toString ();
44144438 }
44154439 final Boolean forVirtualNetwork = cmd .isForVirtualNetwork ();
4416- Long networkId = cmd .getNetworkID ();
4417- Long physicalNetworkId = cmd .getPhysicalNetworkId ();
44184440 final String accountName = cmd .getAccountName ();
44194441 final Long projectId = cmd .getProjectId ();
44204442 final Long domainId = cmd .getDomainId ();
@@ -4487,18 +4509,6 @@ public Vlan createVlanAndPublicIpRange(final CreateVlanIpRangeCmd cmd) throws In
44874509 }
44884510 }
44894511
4490- // Verify that network exists
4491- Network network = null ;
4492- if (networkId != null ) {
4493- network = _networkDao .findById (networkId );
4494- if (network == null ) {
4495- throw new InvalidParameterValueException ("Unable to find network by id " + networkId );
4496- } else {
4497- zoneId = network .getDataCenterId ();
4498- physicalNetworkId = network .getPhysicalNetworkId ();
4499- }
4500- }
4501-
45024512 // Verify that zone exists
45034513 final DataCenterVO zone = _zoneDao .findById (zoneId );
45044514 if (zone == null ) {
@@ -4847,28 +4857,8 @@ public Vlan createVlanAndPublicIpRange(final long zoneId, final long networkId,
48474857 // same as network's vlan
48484858 // 2) if vlan is missing, default it to the guest network's vlan
48494859 if (network .getTrafficType () == TrafficType .Guest ) {
4850- String networkVlanId = null ;
4851- boolean connectivityWithoutVlan = false ;
4852- if (_networkModel .areServicesSupportedInNetwork (network .getId (), Service .Connectivity )) {
4853- Map <Capability , String > connectivityCapabilities = _networkModel .getNetworkServiceCapabilities (network .getId (), Service .Connectivity );
4854- connectivityWithoutVlan = MapUtils .isNotEmpty (connectivityCapabilities ) && connectivityCapabilities .containsKey (Capability .NoVlan );
4855- }
4856-
4857- final URI uri = network .getBroadcastUri ();
4858- if (connectivityWithoutVlan ) {
4859- networkVlanId = network .getBroadcastDomainType ().toUri (network .getUuid ()).toString ();
4860- } else if (uri != null ) {
4861- // Do not search for the VLAN tag when the network doesn't support VLAN
4862- if (uri .toString ().startsWith ("vlan" )) {
4863- final String [] vlan = uri .toString ().split ("vlan:\\ /\\ /" );
4864- networkVlanId = vlan [1 ];
4865- // For pvlan
4866- if (network .getBroadcastDomainType () != BroadcastDomainType .Vlan ) {
4867- networkVlanId = networkVlanId .split ("-" )[0 ];
4868- }
4869- }
4870- }
4871-
4860+ boolean connectivityWithoutVlan = isConnectivityWithoutVlan (network );
4861+ String networkVlanId = getNetworkVlanId (network , connectivityWithoutVlan );
48724862 if (vlanId != null && !connectivityWithoutVlan ) {
48734863 // if vlan is specified, throw an error if it's not equal to
48744864 // network's vlanId
@@ -5000,6 +4990,36 @@ public Vlan createVlanAndPublicIpRange(final long zoneId, final long networkId,
50004990 return vlan ;
50014991 }
50024992
4993+ private boolean isConnectivityWithoutVlan (Network network ) {
4994+ boolean connectivityWithoutVlan = false ;
4995+ if (_networkModel .areServicesSupportedInNetwork (network .getId (), Service .Connectivity )) {
4996+ Map <Capability , String > connectivityCapabilities = _networkModel .getNetworkServiceCapabilities (network .getId (), Service .Connectivity );
4997+ connectivityWithoutVlan = MapUtils .isNotEmpty (connectivityCapabilities ) && connectivityCapabilities .containsKey (Capability .NoVlan );
4998+ }
4999+ return connectivityWithoutVlan ;
5000+ }
5001+
5002+ private String getNetworkVlanId (Network network , boolean connectivityWithoutVlan ) {
5003+ String networkVlanId = null ;
5004+ if (connectivityWithoutVlan ) {
5005+ return network .getBroadcastDomainType ().toUri (network .getUuid ()).toString ();
5006+ }
5007+
5008+ final URI uri = network .getBroadcastUri ();
5009+ if (uri != null ) {
5010+ // Do not search for the VLAN tag when the network doesn't support VLAN
5011+ if (uri .toString ().startsWith ("vlan" )) {
5012+ final String [] vlan = uri .toString ().split ("vlan:\\ /\\ /" );
5013+ networkVlanId = vlan [1 ];
5014+ // For pvlan
5015+ if (network .getBroadcastDomainType () != BroadcastDomainType .Vlan ) {
5016+ networkVlanId = networkVlanId .split ("-" )[0 ];
5017+ }
5018+ }
5019+ }
5020+ return networkVlanId ;
5021+ }
5022+
50035023 private void checkZoneVlanIpOverlap (DataCenterVO zone , Network network , String newCidr , String vlanId , String vlanGateway , String vlanNetmask , String startIP , String endIP ) {
50045024 // Throw an exception if this subnet overlaps with subnet on other VLAN,
50055025 // if this is ip range extension, gateway, network mask should be same and ip range should not overlap
0 commit comments