Skip to content

Commit 88afdb3

Browse files
committed
Made NetworkId parameter required for Vlan, modified DB schema and corresponding Managers' methods. For Virtual and Direct untagged vlan networkId is got from default system Public/Public-DirectPodBased networks accordingly
1 parent 5903e91 commit 88afdb3

13 files changed

Lines changed: 176 additions & 133 deletions

File tree

api/src/com/cloud/dc/Vlan.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,8 @@ public enum VlanType {
3636

3737
public long getDataCenterId();
3838

39-
public void setIpRange(String description);
40-
4139
public String getIpRange();
4240

43-
public void setVlanType(VlanType ipRange);
44-
4541
public VlanType getVlanType();
4642

4743
public Long getNetworkId();

client/tomcatconf/components.xml.in

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@
9999
<dao name="DomainDao" class="com.cloud.domain.dao.DomainDaoImpl" singleton="false"/>
100100
<dao name="NetworkOfferingDao" class="com.cloud.offerings.dao.NetworkOfferingDaoImpl" singleton="false"/>
101101
<dao name="DataCenterDao" class="com.cloud.dc.dao.DataCenterDaoImpl" singleton="false"/>
102-
<dao name="NetworkDao" class="com.cloud.network.dao.NetworkDaoImpl" singleton="false"/>
102+
<dao name="NetworkDao" class="com.cloud.network.dao.NetworkDaoImpl" singleton="false"/>
103+
<dao name="IpAddressDao" class="com.cloud.network.dao.IPAddressDaoImpl" singleton="false"/>
104+
<dao name="VlanDao" class="com.cloud.dc.dao.VlanDaoImpl" singleton="false"/>
103105
</configuration-server>
104106
</components.xml>

server/src/com/cloud/api/ApiDBUtils.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@
3232
import com.cloud.network.Network;
3333
import com.cloud.network.Network.Capability;
3434
import com.cloud.network.Network.Service;
35+
import com.cloud.network.Networks.TrafficType;
3536
import com.cloud.network.NetworkManager;
3637
import com.cloud.network.NetworkRuleConfigVO;
3738
import com.cloud.network.NetworkVO;
38-
import com.cloud.network.Networks.TrafficType;
3939
import com.cloud.network.dao.IPAddressDao;
4040
import com.cloud.network.dao.LoadBalancerDao;
4141
import com.cloud.network.dao.NetworkDao;
@@ -517,25 +517,7 @@ public static Map<Service, Map<Capability, String>> getZoneCapabilities(long zon
517517
}
518518

519519
public static long getPublicNetworkIdByZone(long zoneId) {
520-
//find system public network offering
521-
Long networkOfferingId = null;
522-
List<NetworkOfferingVO> offerings = _networkOfferingDao.listSystemNetworkOfferings();
523-
for (NetworkOfferingVO offering: offerings) {
524-
if (offering.getGuestIpType() == null && offering.getTrafficType() == TrafficType.Public) {
525-
networkOfferingId = offering.getId();
526-
break;
527-
}
528-
}
529-
530-
if (networkOfferingId == null) {
531-
throw new InvalidParameterValueException("Unable to find system Public network offering");
532-
}
533-
534-
List<NetworkVO> networks = _networkDao.listBy(Account.ACCOUNT_ID_SYSTEM, networkOfferingId, zoneId);
535-
if (networks == null) {
536-
throw new InvalidParameterValueException("Unable to find public network in zone " + zoneId);
537-
}
538-
return networks.get(0).getId();
520+
return _networkMgr.getSystemNetworkIdByZoneAndTrafficTypeAndGuestType(zoneId, TrafficType.Public, null);
539521
}
540522

541523
public static Long getVlanNetworkId(long vlanId) {

server/src/com/cloud/configuration/ConfigurationManagerImpl.java

Lines changed: 71 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

server/src/com/cloud/dc/VlanVO.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ public String getVlanTag() {
8383
public String getVlanGateway() {
8484
return vlanGateway;
8585
}
86-
87-
public void setVlanGateway(String vlanGateway) {
88-
this.vlanGateway = vlanGateway;
89-
}
9086

9187
public String getVlanNetmask() {
9288
return vlanNetmask;
@@ -96,23 +92,19 @@ public long getDataCenterId() {
9692
return dataCenterId;
9793
}
9894

99-
public void setIpRange(String ipRange) {
100-
this.ipRange = ipRange;
101-
}
102-
10395
public String getIpRange() {
10496
return ipRange;
10597
}
10698

107-
public void setVlanType(VlanType vlanType) {
108-
this.vlanType = vlanType;
109-
}
110-
11199
public VlanType getVlanType() {
112100
return vlanType;
113101
}
114102

115103
public Long getNetworkId() {
116104
return networkId;
117105
}
106+
107+
public void setNetworkId(Long networkId) {
108+
this.networkId = networkId;
109+
}
118110
}

server/src/com/cloud/dc/dao/VlanDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public interface VlanDao extends GenericDao<VlanVO, Long> {
3131

3232
List<VlanVO> listByZone(long zoneId);
3333

34+
List<VlanVO> listByType(Vlan.VlanType vlanType);
35+
3436
List<VlanVO> listByZoneAndType(long zoneId, Vlan.VlanType vlanType);
3537

3638
List<VlanVO> listVlansForPod(long podId);

server/src/com/cloud/dc/dao/VlanDaoImpl.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ public List<VlanVO> listByZoneAndType(long zoneId, VlanType vlanType) {
112112
sc.setParameters("vlanType", vlanType);
113113
return listBy(sc);
114114
}
115+
116+
117+
@Override
118+
public List<VlanVO> listByType(VlanType vlanType) {
119+
SearchCriteria<VlanVO> sc = ZoneTypeSearch.create();
120+
sc.setParameters("vlanType", vlanType);
121+
return listBy(sc);
122+
}
115123

116124
@Override
117125
public List<VlanVO> listVlansForPod(long podId) {

server/src/com/cloud/network/NetworkManager.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@
2929
import com.cloud.exception.ResourceUnavailableException;
3030
import com.cloud.network.Network.Capability;
3131
import com.cloud.network.Network.Service;
32+
import com.cloud.network.Networks.TrafficType;
3233
import com.cloud.network.addr.PublicIp;
3334
import com.cloud.network.rules.FirewallRule;
35+
import com.cloud.offering.NetworkOffering.GuestIpType;
3436
import com.cloud.offerings.NetworkOfferingVO;
3537
import com.cloud.service.ServiceOfferingVO;
3638
import com.cloud.user.Account;
@@ -128,4 +130,7 @@ public interface NetworkManager extends NetworkService {
128130
boolean applyRules(List<? extends FirewallRule> rules, boolean continueOnError) throws ResourceUnavailableException;
129131

130132
Map<Service, Map<Capability, String>> getZoneCapabilities(long zoneId);
133+
134+
long getSystemNetworkIdByZoneAndTrafficTypeAndGuestType(long zoneId, TrafficType trafficType, GuestIpType guestType);
135+
131136
}

server/src/com/cloud/network/NetworkManagerImpl.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,4 +2205,27 @@ public Map<Service, Map<Capability, String>> getZoneCapabilities(long zoneId) {
22052205
return networkCapabilities;
22062206
}
22072207

2208+
@Override
2209+
public long getSystemNetworkIdByZoneAndTrafficTypeAndGuestType(long zoneId, TrafficType trafficType, GuestIpType guestType) {
2210+
//find system public network offering
2211+
Long networkOfferingId = null;
2212+
List<NetworkOfferingVO> offerings = _networkOfferingDao.listSystemNetworkOfferings();
2213+
for (NetworkOfferingVO offering: offerings) {
2214+
if (offering.getTrafficType() == trafficType && offering.getGuestIpType() == guestType) {
2215+
networkOfferingId = offering.getId();
2216+
break;
2217+
}
2218+
}
2219+
2220+
if (networkOfferingId == null) {
2221+
throw new InvalidParameterValueException("Unable to find system network offering with traffic type " + trafficType + " and guestIpType " + guestType);
2222+
}
2223+
2224+
List<NetworkVO> networks = _networksDao.listBy(Account.ACCOUNT_ID_SYSTEM, networkOfferingId, zoneId);
2225+
if (networks == null) {
2226+
throw new InvalidParameterValueException("Unable to find network with traffic type " + trafficType + " in zone " + zoneId);
2227+
}
2228+
return networks.get(0).getId();
2229+
}
2230+
22082231
}

0 commit comments

Comments
 (0)