@@ -1590,65 +1590,86 @@ public Vlan createVlanAndPublicIpRange(CreateVlanIpRangeCmd cmd) throws Insuffic
15901590 throw new ServerApiException (BaseCmd .PARAM_ERROR , "Please specify a valid account." );
15911591 }
15921592 }
1593+
1594+ //Verify that network exists
1595+ NetworkVO network = null ;
1596+ if (networkId != null ) {
1597+ network = _networkDao .findById (networkId );
1598+ if (network == null ) {
1599+ throw new InvalidParameterValueException ("Unable to find network by id " + networkId );
1600+ } else {
1601+ zoneId = network .getDataCenterId ();
1602+ }
1603+ }
15931604
1594- //if Vlan is direct, don't allow to specify networkId
1595- if (forVirtualNetwork && networkId != null ) {
1596- throw new InvalidParameterValueException ("Can't specify networkId for Virtual network" );
1605+ //Verify that zone exists
1606+ DataCenterVO zone = _zoneDao .findById (zoneId );
1607+ if (zone == null ) {
1608+ throw new InvalidParameterValueException ("Unable to find zone by id " + zoneId );
15971609 }
15981610
1599- if (forVirtualNetwork && (vlanGateway == null || vlanNetmask == null || zoneId == null )) {
1600- throw new InvalidParameterValueException ("Gateway, netmask and zoneId have to be passed in for virtual network" );
1601- }
1611+ //If networkId is not specified, and vlan is Virtual or Direct Untagged, try to locate default networks
1612+ if (forVirtualNetwork ){
1613+ if (network == null ) {
1614+ //find default public network in the zone
1615+ networkId = _networkMgr .getSystemNetworkIdByZoneAndTrafficTypeAndGuestType (zoneId , TrafficType .Public , null );
1616+ } else if (network .getGuestType () != null || network .getTrafficType () != TrafficType .Public ){
1617+ throw new InvalidParameterValueException ("Can't find Public network by id=" + networkId );
1618+ }
1619+ } else {
1620+ if (network == null ) {
1621+ if (zone .getNetworkType () == DataCenter .NetworkType .Basic ) {
1622+ networkId = _networkMgr .getSystemNetworkIdByZoneAndTrafficTypeAndGuestType (zoneId , TrafficType .Public , GuestIpType .DirectPodBased );
1623+ } else {
1624+ throw new InvalidParameterValueException ("Nework id is required for Direct vlan creation " );
1625+ }
1626+ } else if (network .getGuestType () == null || network .getGuestType () == GuestIpType .Virtual ) {
1627+ throw new InvalidParameterValueException ("Can't create direct vlan for network id=" + networkId + " with GuestType: " + network .getGuestType ());
1628+ }
1629+ }
16021630
16031631 //if end ip is not specified, default it to startIp
16041632 if (endIP == null && startIP != null ) {
16051633 endIP = startIP ;
16061634 }
1607-
1608- //Verify that network is valid, and ip range matches network's cidr
1609- if (networkId != null ) {
1610- NetworkVO network = _networkDao .findById (networkId );
1611- if (network == null ) {
1612- throw new InvalidParameterValueException ("Unable to find network by id " + networkId );
1613- } else {
1614- //Check that network is of type Direct
1615- if (network .getGuestType () == GuestIpType .Virtual ) {
1616- throw new InvalidParameterValueException ("Can't create direct vlan for network with GuestType " + network .getGuestType ().toString ());
1617- }
1618-
1619- //check if startIp and endIp belong to network Cidr
1620- String networkCidr = network .getCidr ();
1621- String networkGateway = network .getGateway ();
1622-
1623- Long networkZoneId = network .getDataCenterId ();
1624- String [] splitResult = networkCidr .split ("\\ /" );
1625- long size = Long .valueOf (splitResult [1 ]);
1626- String networkNetmask = NetUtils .getCidrNetmask (size );
1627-
1628- //Check if ip addresses are in network range
1629- if (!NetUtils .sameSubnet (startIP , networkGateway , networkNetmask )) {
1630- throw new InvalidParameterValueException ("Start ip is not in network cidr: " + networkCidr );
1635+
1636+ if (forVirtualNetwork || zone .getNetworkType () == DataCenter .NetworkType .Basic ) {
1637+ if (vlanGateway == null || vlanNetmask == null || zoneId == null ) {
1638+ throw new InvalidParameterValueException ("Gateway, netmask and zoneId have to be passed in for virtual and direct untagged networks" );
1639+ }
1640+ } else {
1641+ //check if startIp and endIp belong to network Cidr
1642+ String networkCidr = network .getCidr ();
1643+ String networkGateway = network .getGateway ();
1644+
1645+ Long networkZoneId = network .getDataCenterId ();
1646+ String [] splitResult = networkCidr .split ("\\ /" );
1647+ long size = Long .valueOf (splitResult [1 ]);
1648+ String networkNetmask = NetUtils .getCidrNetmask (size );
1649+
1650+ //Check if ip addresses are in network range
1651+ if (!NetUtils .sameSubnet (startIP , networkGateway , networkNetmask )) {
1652+ throw new InvalidParameterValueException ("Start ip is not in network cidr: " + networkCidr );
1653+ }
1654+
1655+ if (endIP != null ) {
1656+ if (!NetUtils .sameSubnet (endIP , networkGateway , networkNetmask )) {
1657+ throw new InvalidParameterValueException ("End ip is not in network cidr: " + networkCidr );
16311658 }
1632-
1633- if (endIP != null ) {
1634- if (!NetUtils .sameSubnet (endIP , networkGateway , networkNetmask )) {
1635- throw new InvalidParameterValueException ("End ip is not in network cidr: " + networkCidr );
1636- }
1637- }
1638-
1639- //set gateway, netmask, zone from network object
1640- vlanGateway = networkGateway ;
1641- vlanNetmask = networkNetmask ;
1642- zoneId = networkZoneId ;
1643-
1644- //set vlanId if it's not null for the network
1645- URI uri = network .getBroadcastUri ();
1646- if (uri != null ) {
1647- String [] vlan = uri .toString ().split ("vlan:\\ /\\ /" );
1648- vlanId = vlan [1 ];
1649- }
1650- }
1651- }
1659+ }
1660+
1661+ //set gateway, netmask, zone from network object
1662+ vlanGateway = networkGateway ;
1663+ vlanNetmask = networkNetmask ;
1664+ zoneId = networkZoneId ;
1665+
1666+ //set vlanId if it's not null for the network
1667+ URI uri = network .getBroadcastUri ();
1668+ if (uri != null ) {
1669+ String [] vlan = uri .toString ().split ("vlan:\\ /\\ /" );
1670+ vlanId = vlan [1 ];
1671+ }
1672+ }
16521673
16531674 return createVlanAndPublicIpRange (userId , zoneId , podId , startIP , endIP , vlanGateway , vlanNetmask , forVirtualNetwork , vlanId , account , networkId );
16541675 }
0 commit comments