@@ -4407,14 +4407,35 @@ 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+
44114434 // TODO decide if we should be forgiving or demand a valid and complete URI
44124435 if (!(vlanId == null || "" .equals (vlanId ) || vlanId .startsWith (BroadcastDomainType .Vlan .scheme ()))) {
44134436 vlanId = BroadcastDomainType .Vlan .toUri (vlanId ).toString ();
44144437 }
44154438 final Boolean forVirtualNetwork = cmd .isForVirtualNetwork ();
4416- Long networkId = cmd .getNetworkID ();
4417- Long physicalNetworkId = cmd .getPhysicalNetworkId ();
44184439 final String accountName = cmd .getAccountName ();
44194440 final Long projectId = cmd .getProjectId ();
44204441 final Long domainId = cmd .getDomainId ();
@@ -4487,18 +4508,6 @@ public Vlan createVlanAndPublicIpRange(final CreateVlanIpRangeCmd cmd) throws In
44874508 }
44884509 }
44894510
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-
45024511 // Verify that zone exists
45034512 final DataCenterVO zone = _zoneDao .findById (zoneId );
45044513 if (zone == null ) {
@@ -4847,28 +4856,8 @@ public Vlan createVlanAndPublicIpRange(final long zoneId, final long networkId,
48474856 // same as network's vlan
48484857 // 2) if vlan is missing, default it to the guest network's vlan
48494858 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-
4859+ boolean connectivityWithoutVlan = isConnectivityWithoutVlan (network );
4860+ String networkVlanId = getNetworkVlanId (network , connectivityWithoutVlan );
48724861 if (vlanId != null && !connectivityWithoutVlan ) {
48734862 // if vlan is specified, throw an error if it's not equal to
48744863 // network's vlanId
@@ -5000,6 +4989,36 @@ public Vlan createVlanAndPublicIpRange(final long zoneId, final long networkId,
50004989 return vlan ;
50014990 }
50024991
4992+ private boolean isConnectivityWithoutVlan (Network network ) {
4993+ boolean connectivityWithoutVlan = false ;
4994+ if (_networkModel .areServicesSupportedInNetwork (network .getId (), Service .Connectivity )) {
4995+ Map <Capability , String > connectivityCapabilities = _networkModel .getNetworkServiceCapabilities (network .getId (), Service .Connectivity );
4996+ connectivityWithoutVlan = MapUtils .isNotEmpty (connectivityCapabilities ) && connectivityCapabilities .containsKey (Capability .NoVlan );
4997+ }
4998+ return connectivityWithoutVlan ;
4999+ }
5000+
5001+ private String getNetworkVlanId (Network network , boolean connectivityWithoutVlan ) {
5002+ String networkVlanId = null ;
5003+ if (connectivityWithoutVlan ) {
5004+ return network .getBroadcastDomainType ().toUri (network .getUuid ()).toString ();
5005+ }
5006+
5007+ final URI uri = network .getBroadcastUri ();
5008+ if (uri != null ) {
5009+ // Do not search for the VLAN tag when the network doesn't support VLAN
5010+ if (uri .toString ().startsWith ("vlan" )) {
5011+ final String [] vlan = uri .toString ().split ("vlan:\\ /\\ /" );
5012+ networkVlanId = vlan [1 ];
5013+ // For pvlan
5014+ if (network .getBroadcastDomainType () != BroadcastDomainType .Vlan ) {
5015+ networkVlanId = networkVlanId .split ("-" )[0 ];
5016+ }
5017+ }
5018+ }
5019+ return networkVlanId ;
5020+ }
5021+
50035022 private void checkZoneVlanIpOverlap (DataCenterVO zone , Network network , String newCidr , String vlanId , String vlanGateway , String vlanNetmask , String startIP , String endIP ) {
50045023 // Throw an exception if this subnet overlaps with subnet on other VLAN,
50055024 // if this is ip range extension, gateway, network mask should be same and ip range should not overlap
0 commit comments