Some build fixes and code refactoring for redundant router

This commit is contained in:
Sheng Yang 2011-06-06 14:07:32 -07:00
parent 5db86e6c20
commit 0c92bf5b79
5 changed files with 33 additions and 65 deletions

View File

@ -203,4 +203,6 @@ public interface NetworkManager extends NetworkService {
List<NetworkVO> listNetworksForAccount(long accountId, long zoneId, GuestIpType guestType, Boolean isDefault);
IPAddressVO markIpAsUnavailable(long addrId);
public String acquireGuestIpAddress(Network network);
}

View File

@ -27,6 +27,9 @@ import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -2900,5 +2903,29 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
}
}
Random _rand = new Random(System.currentTimeMillis());
@DB
public String acquireGuestIpAddress(Network network) {
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
String[] cidr = network.getCidr().split("/");
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]));
Set<Long> usedIps = new TreeSet<Long>();
for (String ip : ips) {
usedIps.add(NetUtils.ip2Long(ip));
}
if (usedIps.size() != 0) {
allPossibleIps.removeAll(usedIps);
}
if (allPossibleIps.isEmpty()) {
return null;
}
Long[] array = allPossibleIps.toArray(new Long[allPossibleIps.size()]);
String result;
do {
result = NetUtils.long2Ip(array[_rand.nextInt(array.length)]);
} while (result.split("\\.")[3].equals("1"));
return result;
}
}

View File

@ -17,11 +17,6 @@
*/
package com.cloud.network.guru;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.TreeSet;
import javax.ejb.Local;
import org.apache.log4j.Logger;
@ -54,7 +49,6 @@ import com.cloud.user.Account;
import com.cloud.user.UserContext;
import com.cloud.utils.component.AdapterBase;
import com.cloud.utils.component.Inject;
import com.cloud.utils.db.DB;
import com.cloud.utils.net.NetUtils;
import com.cloud.vm.Nic.ReservationStrategy;
import com.cloud.vm.NicProfile;
@ -79,7 +73,6 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
String _defaultGateway;
String _defaultCidr;
Random _rand = new Random(System.currentTimeMillis());
protected GuestNetworkGuru() {
super();
@ -191,7 +184,7 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
nic.setIsolationUri(network.getBroadcastUri());
nic.setGateway(network.getGateway());
String guestIp = acquireGuestIpAddress(network);
String guestIp = _networkMgr.acquireGuestIpAddress(network);
if (guestIp == null) {
throw new InsufficientVirtualNetworkCapcityException("Unable to acquire guest IP address for network " + network, DataCenter.class, dc.getId());
}
@ -225,30 +218,6 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
}
}
@DB
protected String acquireGuestIpAddress(Network network) {
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
String[] cidr = network.getCidr().split("/");
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]));
Set<Long> usedIps = new TreeSet<Long>();
for (String ip : ips) {
usedIps.add(NetUtils.ip2Long(ip));
}
if (usedIps.size() != 0) {
allPossibleIps.removeAll(usedIps);
}
if (allPossibleIps.isEmpty()) {
return null;
}
Long[] array = allPossibleIps.toArray(new Long[allPossibleIps.size()]);
String result;
String[] splits;
do {
result = NetUtils.long2Ip(array[_rand.nextInt(array.length)]);
} while (result.split("\\.")[3].equals("1"));
return result;
}
@Override
public void reserve(NicProfile nic, Network network, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context)
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException {

View File

@ -17,21 +17,12 @@
*/
package com.cloud.network.router;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeSet;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
@ -753,27 +744,6 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
return ((accountType == Account.ACCOUNT_TYPE_ADMIN) || (accountType == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) || (accountType == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN) || (accountType == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN));
}
Random _rand = new Random(System.currentTimeMillis());
@DB /*FIXME: Duplicate function*/
private String acquireGuestIpAddress(Network network) {
List<String> ips = _nicDao.listIpAddressInNetwork(network.getId());
String[] cidr = network.getCidr().split("/");
Set<Long> allPossibleIps = NetUtils.getAllIpsFromCidr(cidr[0], Integer.parseInt(cidr[1]));
Set<Long> usedIps = new TreeSet<Long>();
for (String ip : ips) {
usedIps.add(NetUtils.ip2Long(ip));
}
if (usedIps.size() != 0) {
allPossibleIps.removeAll(usedIps);
}
if (allPossibleIps.isEmpty()) {
return null;
}
Long[] array = allPossibleIps.toArray(new Long[allPossibleIps.size()]);
return NetUtils.long2Ip(array[_rand.nextInt(array.length)]);
}
@Override
@DB
public List<DomainRouterVO> deployVirtualRouter(Network guestNetwork, DeployDestination dest, Account owner, Map<Param, Object> params) throws InsufficientCapacityException,
@ -841,7 +811,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
NicProfile gatewayNic = new NicProfile();
/* For redundant router */
if (offering.isRedundantRouterEnabled()) {
gatewayNic.setIp4Address(acquireGuestIpAddress(guestNetwork));
gatewayNic.setIp4Address(_networkMgr.acquireGuestIpAddress(guestNetwork));
gatewayNic.setMacAddress(_networkMgr.getNextAvailableMacAddressInNetwork(guestNetwork.getId()));
} else {
gatewayNic.setIp4Address(guestNetwork.getGateway());

View File

@ -645,7 +645,7 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Listene
ExcludeList avoids = new ExcludeList();
if (vm.getType().equals(VirtualMachine.Type.DomainRouter)) {
List<DomainRouterVO> routers = _routerDao.findBy(vm.getAccountId(), vm.getDataCenterId());
List<DomainRouterVO> routers = _routerDao.findBy(vm.getAccountId(), vm.getDataCenterIdToDeployIn());
for (DomainRouterVO router : routers) {
if (router.hostId != null) {
avoids.addHost(router.hostId);