a bunch of other changes for network refactoring

This commit is contained in:
Alex Huang 2010-11-08 09:45:51 -08:00
parent 2be0cd6a95
commit aca8ef79cd
27 changed files with 379 additions and 394 deletions

View File

@ -22,12 +22,15 @@ import com.cloud.utils.fsm.StateMachine;
public interface NetworkConfiguration extends ControlledEntity {
enum Event {
ImplementNetwork,
DestroyNetwork;
DestroyNetwork,
OperationSucceeded,
OperationFailed;
}
enum State implements FiniteState<State, Event> {
Allocated("Indicates the network configuration is in allocated but not setup"),
Setup("Indicates the network configuration is setup"),
Implementing("Indicates the network configuration is being implemented"),
Implemented("Indicates the network configuration is in use"),
Destroying("Indicates the network configuration is being destroyed");
@ -64,13 +67,15 @@ public interface NetworkConfiguration extends ControlledEntity {
private static StateMachine<State, Event> s_fsm = new StateMachine<State, Event>();
static {
s_fsm.addTransition(State.Allocated, Event.ImplementNetwork, State.Implemented);
s_fsm.addTransition(State.Allocated, Event.ImplementNetwork, State.Implementing);
s_fsm.addTransition(State.Implementing, Event.OperationSucceeded, State.Implemented);
s_fsm.addTransition(State.Implementing, Event.OperationFailed, State.Destroying);
s_fsm.addTransition(State.Implemented, Event.DestroyNetwork, State.Destroying);
s_fsm.addTransition(State.Destroying, Event.OperationSucceeded, State.Allocated);
s_fsm.addTransition(State.Destroying, Event.OperationFailed, State.Implemented);
}
}
/**
* @return id of the network profile. Null means the network profile is not from the database.
*/

View File

@ -64,9 +64,9 @@ public interface NetworkGuru extends Adapter {
* @throws InsufficientVirtualNetworkCapcityException
* @throws InsufficientAddressCapacityException
*/
String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException;
void reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException;
boolean release(String uniqueId);
boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId);
void deallocate(NetworkConfiguration config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm);

View File

@ -117,9 +117,9 @@ listLoadBalancerRules=com.cloud.api.commands.ListLoadBalancerRulesCmd;15
listLoadBalancerRuleInstances=com.cloud.api.commands.ListLoadBalancerRuleInstancesCmd;15
#### router commands
startRouter=com.cloud.api.commands.StartRouterCmd;3
startRouter=com.cloud.api.commands.StartRouter2Cmd;3
rebootRouter=com.cloud.api.commands.RebootRouterCmd;3
stopRouter=com.cloud.api.commands.StopRouterCmd;3
stopRouter=com.cloud.api.commands.StopRouter2Cmd;3
changeServiceForRouter=com.cloud.api.commands.UpgradeRouterCmd;3
listRouters=com.cloud.api.commands.ListRoutersCmd;7

View File

@ -50,12 +50,23 @@ public class DataCenterIpAddressVO {
@Column(name="pod_id", updatable=false, nullable=false)
private long podId;
@Column(name="reservation_id")
String reservationId;
@Column(name="instance_id")
private Long instanceId;
protected DataCenterIpAddressVO() {
}
public String getReservationId() {
return reservationId;
}
public void setReservationId(String reservationId) {
this.reservationId = reservationId;
}
public DataCenterIpAddressVO(String ipAddress, long dataCenterId, long podId) {
this.ipAddress = ipAddress;
this.dataCenterId = dataCenterId;

View File

@ -53,6 +53,9 @@ public class DataCenterLinkLocalIpAddressVO {
@Column(name="instance_id")
private Long instanceId;
@Column(name="reservation_id")
private String reservationId;
protected DataCenterLinkLocalIpAddressVO() {
}
@ -81,6 +84,14 @@ public class DataCenterLinkLocalIpAddressVO {
public void setTakenAt(Date takenDate) {
this.takenAt = takenDate;
}
public void setReservationId(String reservationId) {
this.reservationId = reservationId;
}
public String getReservationId() {
return reservationId;
}
public String getIpAddress() {
return ipAddress;

View File

@ -32,7 +32,7 @@ public class PodVlanMapVO {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
private Long id;
private long id;
@Column(name="pod_id")
private long podId;
@ -46,10 +46,9 @@ public class PodVlanMapVO {
}
public PodVlanMapVO() {
}
public Long getId() {
public long getId() {
return id;
}

View File

@ -33,21 +33,21 @@ public interface DataCenterDao extends GenericDao<DataCenterVO, Long> {
*/
String[] getNextAvailableMacAddressPair(long id);
String[] getNextAvailableMacAddressPair(long id, long mask);
String allocatePrivateIpAddress(long id, long podId, long instanceId);
String allocateLinkLocalPrivateIpAddress(long id, long podId, long instanceId);
String allocatePrivateIpAddress(long id, long podId, long instanceId, String reservationId);
String allocateLinkLocalIpAddress(long id, long podId, long instanceId, String reservationId);
String allocateVnet(long dcId, long accountId);
void releaseVnet(String vnet, long dcId, long accountId);
void releasePrivateIpAddress(String ipAddress, long dcId, Long instanceId);
void releasePrivateIpAddress(long nicId);
void releaseLinkLocalPrivateIpAddress(String ipAddress, long dcId, Long instanceId);
void releaseLinkLocalPrivateIpAddress(long nicId);
void releasePrivateIpAddress(long nicId, String reservationId);
void releaseLinkLocalIpAddress(String ipAddress, long dcId, Long instanceId);
void releaseLinkLocalIpAddress(long nicId, String reservationId);
boolean deletePrivateIpAddressByPod(long podId);
boolean deleteLinkLocalPrivateIpAddressByPod(long podId);
boolean deleteLinkLocalIpAddressByPod(long podId);
void addPrivateIpAddress(long dcId,long podId, String start, String end);
void addLinkLocalPrivateIpAddress(long dcId,long podId, String start, String end);
void addLinkLocalIpAddress(long dcId,long podId, String start, String end);
List<DataCenterVnetVO> findVnet(long dcId, String vnet);

View File

@ -109,17 +109,17 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
}
@Override
public void releasePrivateIpAddress(long nicId) {
_ipAllocDao.releaseIpAddress(nicId);
public void releasePrivateIpAddress(long nicId, String reservationId) {
_ipAllocDao.releaseIpAddress(nicId, reservationId);
}
@Override
public void releaseLinkLocalPrivateIpAddress(long nicId) {
_LinkLocalIpAllocDao.releaseIpAddress(nicId);
public void releaseLinkLocalIpAddress(long nicId, String reservationId) {
_LinkLocalIpAllocDao.releaseIpAddress(nicId, reservationId);
}
@Override
public void releaseLinkLocalPrivateIpAddress(String ipAddress, long dcId, Long instanceId) {
public void releaseLinkLocalIpAddress(String ipAddress, long dcId, Long instanceId) {
_LinkLocalIpAllocDao.releaseIpAddress(ipAddress, dcId, instanceId);
}
@ -129,7 +129,7 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
}
@Override
public boolean deleteLinkLocalPrivateIpAddressByPod(long podId) {
public boolean deleteLinkLocalIpAddressByPod(long podId) {
return _LinkLocalIpAllocDao.deleteIpAddressByPod(podId);
}
@ -172,8 +172,8 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
}
@Override
public String allocatePrivateIpAddress(long dcId, long podId, long instanceId) {
DataCenterIpAddressVO vo = _ipAllocDao.takeIpAddress(dcId, podId, instanceId);
public String allocatePrivateIpAddress(long dcId, long podId, long instanceId, String reservationId) {
DataCenterIpAddressVO vo = _ipAllocDao.takeIpAddress(dcId, podId, instanceId, reservationId);
if (vo == null) {
return null;
}
@ -181,8 +181,8 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
}
@Override
public String allocateLinkLocalPrivateIpAddress(long dcId, long podId, long instanceId) {
DataCenterLinkLocalIpAddressVO vo = _LinkLocalIpAllocDao.takeIpAddress(dcId, podId, instanceId);
public String allocateLinkLocalIpAddress(long dcId, long podId, long instanceId, String reservationId) {
DataCenterLinkLocalIpAddressVO vo = _LinkLocalIpAllocDao.takeIpAddress(dcId, podId, instanceId, reservationId);
if (vo == null) {
return null;
}
@ -210,7 +210,7 @@ public class DataCenterDaoImpl extends GenericDaoBase<DataCenterVO, Long> implem
}
@Override
public void addLinkLocalPrivateIpAddress(long dcId,long podId, String start, String end) {
public void addLinkLocalIpAddress(long dcId,long podId, String start, String end) {
_LinkLocalIpAllocDao.addIpRange(dcId, podId, start, end);
}

View File

@ -23,6 +23,7 @@ import com.cloud.dc.DataCenterIpAddressVO;
import com.cloud.utils.db.GenericDao;
public interface DataCenterIpAddressDao extends GenericDao<DataCenterIpAddressVO, Long> {
boolean mark(long dcId, long podId, String ip);
List<DataCenterIpAddressVO> listByPodIdDcIdIpAddress(long podId, long dcId, String ipAddress);
int countIPs(long podId, long dcId, boolean onlyCountAllocated);

View File

@ -18,7 +18,7 @@
package com.cloud.dc.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
@ -27,67 +27,55 @@ import javax.ejb.Local;
import org.apache.log4j.Logger;
import com.cloud.dc.DataCenterIpAddressVO;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.NetUtils;
@Local(value={DataCenterIpAddressDao.class})
@Local(value={DataCenterIpAddressDao.class}) @DB(txn=false)
public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddressVO, Long> implements DataCenterIpAddressDao {
private static final Logger s_logger = Logger.getLogger(DataCenterIpAddressDaoImpl.class);
private static final String COUNT_ALL_PRIVATE_IPS = "SELECT count(*) from `cloud`.`op_dc_ip_address_alloc` where pod_id = ? AND data_center_id = ?";
private static final String COUNT_ALLOCATED_PRIVATE_IPS = "SELECT count(*) from `cloud`.`op_dc_ip_address_alloc` where pod_id = ? AND data_center_id = ? AND taken is not null";
private final SearchBuilder<DataCenterIpAddressVO> FreeIpSearch;
private final SearchBuilder<DataCenterIpAddressVO> IpDcSearch;
private final SearchBuilder<DataCenterIpAddressVO> PodDcSearch;
private final SearchBuilder<DataCenterIpAddressVO> PodDcIpSearch;
private final SearchBuilder<DataCenterIpAddressVO> FreePodDcIpSearch;
private final SearchBuilder<DataCenterIpAddressVO> AllFieldsSearch;
private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllIpCount;
private final GenericSearchBuilder<DataCenterIpAddressVO, Integer> AllAllocatedIpCount;
public DataCenterIpAddressVO takeIpAddress(long dcId, long podId, long instanceId) {
SearchCriteria<DataCenterIpAddressVO> sc = FreeIpSearch.create();
sc.setParameters("dc", dcId);
@DB
public DataCenterIpAddressVO takeIpAddress(long dcId, long podId, long instanceId, String reservationId) {
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
sc.setParameters("taken", (Date)null);
Transaction txn = Transaction.currentTxn();
try {
txn.start();
DataCenterIpAddressVO vo = lockOneRandomRow(sc, true);
if (vo == null) {
txn.rollback();
return vo;
}
vo.setTakenAt(new Date());
vo.setInstanceId(instanceId);
update(vo.getId(), vo);
txn.commit();
return vo;
} catch (Exception e) {
txn.rollback();
throw new CloudRuntimeException("Caught Exception ", e);
txn.start();
DataCenterIpAddressVO vo = lockOneRandomRow(sc, true);
if (vo == null) {
return null;
}
vo.setTakenAt(new Date());
vo.setInstanceId(instanceId);
vo.setReservationId(reservationId);
update(vo.getId(), vo);
txn.commit();
return vo;
}
@Override
public boolean deleteIpAddressByPod(long podId) {
Transaction txn = Transaction.currentTxn();
try {
String deleteSql = "DELETE FROM `cloud`.`op_dc_ip_address_alloc` WHERE `pod_id` = ?";
PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteSql);
stmt.setLong(1, podId);
return stmt.execute();
} catch(Exception e) {
throw new CloudRuntimeException("Caught Exception ", e);
}
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
return remove(sc) > 0;
}
@Override
public boolean mark(long dcId, long podId, String ip) {
SearchCriteria<DataCenterIpAddressVO> sc = FreePodDcIpSearch.create();
sc.setParameters("podId", podId);
sc.setParameters("dcId", dcId);
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
sc.setParameters("ipAddress", ip);
DataCenterIpAddressVO vo = createForUpdate();
@ -96,26 +84,27 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
return update(vo, sc) >= 1;
}
@DB
public void addIpRange(long dcId, long podId, String start, String end) {
Transaction txn = Transaction.currentTxn();
String insertSql = "INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)";
String insertSql = "INSERT INTO op_dc_ip_address_alloc (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)";
PreparedStatement stmt = null;
long startIP = NetUtils.ip2Long(start);
long endIP = NetUtils.ip2Long(end);
while (startIP <= endIP) {
try {
stmt = txn.prepareAutoCloseStatement(insertSql);
stmt.setString(1, NetUtils.long2Ip(startIP));
try {
txn.start();
stmt = txn.prepareAutoCloseStatement(insertSql);
while (startIP <= endIP) {
stmt.setString(1, NetUtils.long2Ip(startIP++));
stmt.setLong(2, dcId);
stmt.setLong(3, podId);
stmt.executeUpdate();
stmt.close();
} catch (Exception ex) {
s_logger.warn("Unable to persist " + NetUtils.long2Ip(startIP) + " due to " + ex.getMessage());
stmt.addBatch();
}
startIP++;
txn.commit();
} catch (SQLException ex) {
throw new CloudRuntimeException("Unable to persist ip address range ", ex);
}
}
@ -123,7 +112,7 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
if (s_logger.isDebugEnabled()) {
s_logger.debug("Releasing ip address: " + ipAddress + " data center " + dcId);
}
SearchCriteria<DataCenterIpAddressVO> sc = IpDcSearch.create();
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("ip", ipAddress);
sc.setParameters("dc", dcId);
sc.setParameters("instance", instanceId);
@ -132,86 +121,75 @@ public class DataCenterIpAddressDaoImpl extends GenericDaoBase<DataCenterIpAddre
vo.setTakenAt(null);
vo.setInstanceId(null);
vo.setReservationId(null);
update(vo, sc);
}
public void releaseIpAddress(long nicId) {
SearchCriteria<DataCenterIpAddressVO> sc = IpDcSearch.create();
public void releaseIpAddress(long nicId, String reservationId) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Releasing ip address for reservationId=" + reservationId + ", instance=" + nicId);
}
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("instance", nicId);
sc.setParameters("reservation", reservationId);
DataCenterIpAddressVO vo = createForUpdate();
vo.setTakenAt(null);
vo.setInstanceId(null);
vo.setReservationId(null);
update(vo, sc);
}
protected DataCenterIpAddressDaoImpl() {
super();
FreeIpSearch = createSearchBuilder();
FreeIpSearch.and("dc", FreeIpSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
FreeIpSearch.and("pod", FreeIpSearch.entity().getPodId(), SearchCriteria.Op.EQ);
FreeIpSearch.and("taken", FreeIpSearch.entity().getTakenAt(), SearchCriteria.Op.NULL);
FreeIpSearch.done();
IpDcSearch = createSearchBuilder();
IpDcSearch.and("ip", IpDcSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
IpDcSearch.and("dc", IpDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
IpDcSearch.and("instance", IpDcSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
IpDcSearch.done();
PodDcSearch = createSearchBuilder();
PodDcSearch.and("podId", PodDcSearch.entity().getPodId(), SearchCriteria.Op.EQ);
PodDcSearch.and("dataCenterId", PodDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
PodDcSearch.done();
PodDcIpSearch = createSearchBuilder();
PodDcIpSearch.and("dcId", PodDcIpSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
PodDcIpSearch.and("podId", PodDcIpSearch.entity().getPodId(), SearchCriteria.Op.EQ);
PodDcIpSearch.and("ipAddress", PodDcIpSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
PodDcIpSearch.done();
FreePodDcIpSearch = createSearchBuilder();
FreePodDcIpSearch.and("dcId", FreePodDcIpSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
FreePodDcIpSearch.and("podId", FreePodDcIpSearch.entity().getPodId(), SearchCriteria.Op.EQ);
FreePodDcIpSearch.and("ipAddress", FreePodDcIpSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
FreePodDcIpSearch.and("taken", FreePodDcIpSearch.entity().getTakenAt(), SearchCriteria.Op.EQ);
FreePodDcIpSearch.done();
}
public List<DataCenterIpAddressVO> listByPodIdDcId(long podId, long dcId) {
SearchCriteria<DataCenterIpAddressVO> sc = PodDcSearch.create();
sc.setParameters("podId", podId);
sc.setParameters("dataCenterId", dcId);
return listIncludingRemovedBy(sc);
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
return listBy(sc);
}
@Override
public List<DataCenterIpAddressVO> listByPodIdDcIdIpAddress(long podId, long dcId, String ipAddress) {
SearchCriteria<DataCenterIpAddressVO> sc = PodDcIpSearch.create();
sc.setParameters("dcId", dcId);
sc.setParameters("podId", podId);
SearchCriteria<DataCenterIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
sc.setParameters("ipAddress", ipAddress);
return listIncludingRemovedBy(sc);
return listBy(sc);
}
@Override
public int countIPs(long podId, long dcId, boolean onlyCountAllocated) {
Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
int ipCount = 0;
try {
String sql = "";
if (onlyCountAllocated) sql = COUNT_ALLOCATED_PRIVATE_IPS;
else sql = COUNT_ALL_PRIVATE_IPS;
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setLong(1, podId);
pstmt.setLong(2, dcId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) ipCount = rs.getInt(1);
} catch (Exception e) {
s_logger.warn("Exception searching for routers and proxies", e);
SearchCriteria<Integer> sc;
if (onlyCountAllocated) {
sc = AllAllocatedIpCount.create();
} else {
sc = AllIpCount.create();
}
return ipCount;
sc.setParameters("pod", podId);
List<Integer> count = customSearch(sc, null);
return count.get(0);
}
protected DataCenterIpAddressDaoImpl() {
super();
AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("ip", AllFieldsSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("pod", AllFieldsSearch.entity().getPodId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("ipAddress", AllFieldsSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("reservation", AllFieldsSearch.entity().getReservationId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("taken", AllFieldsSearch.entity().getTakenAt(), SearchCriteria.Op.EQ);
AllFieldsSearch.done();
AllIpCount = createSearchBuilder(Integer.class);
AllIpCount.select(null, Func.COUNT, AllIpCount.entity().getId());
AllIpCount.and("pod", AllIpCount.entity().getPodId(), SearchCriteria.Op.EQ);
AllIpCount.done();
AllAllocatedIpCount = createSearchBuilder(Integer.class);
AllAllocatedIpCount.select(null, Func.COUNT, AllIpCount.entity().getId());
AllAllocatedIpCount.and("pod", AllAllocatedIpCount.entity().getPodId(), SearchCriteria.Op.EQ);
AllAllocatedIpCount.and("removed", AllAllocatedIpCount.entity().getTakenAt(), SearchCriteria.Op.NNULL);
AllAllocatedIpCount.done();
}
}

View File

@ -18,114 +18,93 @@
package com.cloud.dc.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import com.cloud.dc.DataCenterLinkLocalIpAddressVO;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.GenericDao;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.NetUtils;
@Local(value={DataCenterLinkLocalIpAddressDaoImpl.class})
@Local(value={DataCenterLinkLocalIpAddressDaoImpl.class}) @DB(txn=false)
public class DataCenterLinkLocalIpAddressDaoImpl extends GenericDaoBase<DataCenterLinkLocalIpAddressVO, Long> implements GenericDao<DataCenterLinkLocalIpAddressVO, Long> {
private static final Logger s_logger = Logger.getLogger(DataCenterLinkLocalIpAddressDaoImpl.class);
private static final String COUNT_ALL_PRIVATE_IPS = "SELECT count(*) from `cloud`.`op_dc_link_local_ip_address_alloc` where pod_id = ? AND data_center_id = ?";
private static final String COUNT_ALLOCATED_PRIVATE_IPS = "SELECT count(*) from `cloud`.`op_dc_link_local_ip_address_alloc` where pod_id = ? AND data_center_id = ? AND taken is not null";
private final SearchBuilder<DataCenterLinkLocalIpAddressVO> FreeIpSearch;
private final SearchBuilder<DataCenterLinkLocalIpAddressVO> IpDcSearch;
private final SearchBuilder<DataCenterLinkLocalIpAddressVO> PodDcSearch;
private final SearchBuilder<DataCenterLinkLocalIpAddressVO> PodDcIpSearch;
private final SearchBuilder<DataCenterLinkLocalIpAddressVO> FreePodDcIpSearch;
private final SearchBuilder<DataCenterLinkLocalIpAddressVO> AllFieldsSearch;
private final GenericSearchBuilder<DataCenterLinkLocalIpAddressVO, Integer> AllIpCount;
private final GenericSearchBuilder<DataCenterLinkLocalIpAddressVO, Integer> AllAllocatedIpCount;
public DataCenterLinkLocalIpAddressVO takeIpAddress(long dcId, long podId, long instanceId) {
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = FreeIpSearch.create();
sc.setParameters("dc", dcId);
@DB
public DataCenterLinkLocalIpAddressVO takeIpAddress(long dcId, long podId, long instanceId, String reservationId) {
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
sc.setParameters("ipAddr", NetUtils.getLinkLocalGateway()); /*explicitly removing the gateway*/
sc.setParameters("taken", (Date)null);
Transaction txn = Transaction.currentTxn();
try {
txn.start();
DataCenterLinkLocalIpAddressVO vo = lockOneRandomRow(sc, true);
if (vo == null) {
txn.rollback();
return vo;
}
vo.setTakenAt(new Date());
vo.setInstanceId(instanceId);
update(vo.getId(), vo);
txn.commit();
return vo;
} catch (Exception e) {
txn.rollback();
throw new CloudRuntimeException("Caught Exception ", e);
txn.start();
DataCenterLinkLocalIpAddressVO vo = lockOneRandomRow(sc, true);
if (vo == null) {
return null;
}
vo.setTakenAt(new Date());
vo.setInstanceId(instanceId);
vo.setReservationId(reservationId);
update(vo.getId(), vo);
txn.commit();
return vo;
}
public boolean deleteIpAddressByPod(long podId) {
Transaction txn = Transaction.currentTxn();
try {
String deleteSql = "DELETE FROM `cloud`.`op_dc_link_local_ip_address_alloc` WHERE `pod_id` = ?";
PreparedStatement stmt = txn.prepareAutoCloseStatement(deleteSql);
stmt.setLong(1, podId);
return stmt.execute();
} catch(Exception e) {
throw new CloudRuntimeException("Caught Exception ", e);
}
}
public boolean mark(long dcId, long podId, String ip) {
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = FreePodDcIpSearch.create();
sc.setParameters("podId", podId);
sc.setParameters("dcId", dcId);
sc.setParameters("ipAddress", ip);
DataCenterLinkLocalIpAddressVO vo = createForUpdate();
vo.setTakenAt(new Date());
return update(vo, sc) >= 1;
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
return remove(sc) > 0;
}
@DB
public void addIpRange(long dcId, long podId, String start, String end) {
Transaction txn = Transaction.currentTxn();
String insertSql = "INSERT INTO `cloud`.`op_dc_link_local_ip_address_alloc` (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)";
PreparedStatement stmt = null;
long startIP = NetUtils.ip2Long(start);
long endIP = NetUtils.ip2Long(end);
while (startIP <= endIP) {
try {
stmt = txn.prepareAutoCloseStatement(insertSql);
stmt.setString(1, NetUtils.long2Ip(startIP));
stmt.setLong(2, dcId);
stmt.setLong(3, podId);
stmt.executeUpdate();
stmt.close();
} catch (Exception ex) {
s_logger.warn("Unable to persist " + NetUtils.long2Ip(startIP) + " due to " + ex.getMessage());
Transaction txn = Transaction.currentTxn();
try {
txn.start();
stmt = txn.prepareAutoCloseStatement(insertSql);
while (startIP <= endIP) {
stmt.setString(1, NetUtils.long2Ip(startIP++));
stmt.setLong(2, dcId);
stmt.setLong(3, podId);
stmt.addBatch();
}
startIP++;
txn.commit();
} catch (SQLException e) {
throw new CloudRuntimeException("Unable to insert", e);
}
}
public void releaseIpAddress(String ipAddress, long dcId, Long instanceId) {
public void releaseIpAddress(String ipAddress, long dcId, long instanceId) {
if (s_logger.isDebugEnabled()) {
s_logger.debug("Releasing ip address: " + ipAddress + " data center " + dcId);
}
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = IpDcSearch.create();
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("ip", ipAddress);
sc.setParameters("dc", dcId);
sc.setParameters("instance", instanceId);
@ -134,88 +113,74 @@ public class DataCenterLinkLocalIpAddressDaoImpl extends GenericDaoBase<DataCent
vo.setTakenAt(null);
vo.setInstanceId(null);
vo.setReservationId(null);
update(vo, sc);
}
public void releaseIpAddress(long nicId) {
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = IpDcSearch.create();
public void releaseIpAddress(long nicId, String reservationId) {
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("instance", nicId);
sc.setParameters("reservation", reservationId);
DataCenterLinkLocalIpAddressVO vo = createForUpdate();
vo.setTakenAt(null);
vo.setInstanceId(null);
vo.setReservationId(null);
update(vo, sc);
}
protected DataCenterLinkLocalIpAddressDaoImpl() {
super();
FreeIpSearch = createSearchBuilder();
FreeIpSearch.and("dc", FreeIpSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
FreeIpSearch.and("pod", FreeIpSearch.entity().getPodId(), SearchCriteria.Op.EQ);
FreeIpSearch.and("ipAddr", FreeIpSearch.entity().getIpAddress(), SearchCriteria.Op.NEQ);
FreeIpSearch.and("taken", FreeIpSearch.entity().getTakenAt(), SearchCriteria.Op.NULL);
FreeIpSearch.done();
IpDcSearch = createSearchBuilder();
IpDcSearch.and("ip", IpDcSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
IpDcSearch.and("dc", IpDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
IpDcSearch.and("instance", IpDcSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
IpDcSearch.done();
PodDcSearch = createSearchBuilder();
PodDcSearch.and("podId", PodDcSearch.entity().getPodId(), SearchCriteria.Op.EQ);
PodDcSearch.and("dataCenterId", PodDcSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
PodDcSearch.done();
PodDcIpSearch = createSearchBuilder();
PodDcIpSearch.and("dcId", PodDcIpSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
PodDcIpSearch.and("podId", PodDcIpSearch.entity().getPodId(), SearchCriteria.Op.EQ);
PodDcIpSearch.and("ipAddress", PodDcIpSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
PodDcIpSearch.done();
FreePodDcIpSearch = createSearchBuilder();
FreePodDcIpSearch.and("dcId", FreePodDcIpSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
FreePodDcIpSearch.and("podId", FreePodDcIpSearch.entity().getPodId(), SearchCriteria.Op.EQ);
FreePodDcIpSearch.and("ipAddress", FreePodDcIpSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
FreePodDcIpSearch.and("taken", FreePodDcIpSearch.entity().getTakenAt(), SearchCriteria.Op.EQ);
FreePodDcIpSearch.done();
}
public List<DataCenterLinkLocalIpAddressVO> listByPodIdDcId(long podId, long dcId) {
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = PodDcSearch.create();
sc.setParameters("podId", podId);
sc.setParameters("dataCenterId", dcId);
return listIncludingRemovedBy(sc);
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("pod", podId);
return listBy(sc);
}
public List<DataCenterLinkLocalIpAddressVO> listByPodIdDcIdIpAddress(long podId, long dcId, String ipAddress) {
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = PodDcIpSearch.create();
sc.setParameters("dcId", dcId);
sc.setParameters("podId", podId);
sc.setParameters("ipAddress", ipAddress);
return listIncludingRemovedBy(sc);
}
public int countIPs(long podId, long dcId, boolean onlyCountAllocated) {
Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
int ipCount = 0;
try {
String sql = "";
if (onlyCountAllocated) sql = COUNT_ALLOCATED_PRIVATE_IPS;
else sql = COUNT_ALL_PRIVATE_IPS;
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setLong(1, podId);
pstmt.setLong(2, dcId);
ResultSet rs = pstmt.executeQuery();
if (rs.next()) ipCount = rs.getInt(1);
} catch (Exception e) {
s_logger.warn("Exception searching for routers and proxies", e);
SearchCriteria<Integer> sc;
if (onlyCountAllocated) {
sc = AllAllocatedIpCount.create();
} else {
sc = AllIpCount.create();
}
return ipCount;
sc.setParameters("pod", podId);
List<Integer> count = customSearch(sc, null);
return count.get(0);
}
protected DataCenterLinkLocalIpAddressDaoImpl() {
super();
AllFieldsSearch = createSearchBuilder();
AllFieldsSearch.and("ip", AllFieldsSearch.entity().getIpAddress(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("dc", AllFieldsSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("pod", AllFieldsSearch.entity().getPodId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("instance", AllFieldsSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("reservation", AllFieldsSearch.entity().getReservationId(), SearchCriteria.Op.EQ);
AllFieldsSearch.and("taken", AllFieldsSearch.entity().getTakenAt(), SearchCriteria.Op.EQ);
AllFieldsSearch.done();
AllIpCount = createSearchBuilder(Integer.class);
AllIpCount.select(null, Func.COUNT, AllIpCount.entity().getId());
AllIpCount.and("pod", AllIpCount.entity().getPodId(), SearchCriteria.Op.EQ);
AllIpCount.done();
AllAllocatedIpCount = createSearchBuilder(Integer.class);
AllAllocatedIpCount.select(null, Func.COUNT, AllIpCount.entity().getId());
AllAllocatedIpCount.and("pod", AllAllocatedIpCount.entity().getPodId(), SearchCriteria.Op.EQ);
AllAllocatedIpCount.and("removed", AllAllocatedIpCount.entity().getTakenAt(), SearchCriteria.Op.NNULL);
AllAllocatedIpCount.done();
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
super.configure(name, params);
SearchCriteria<DataCenterLinkLocalIpAddressVO> sc = AllFieldsSearch.create();
sc.setParameters("ip", NetUtils.getLinkLocalGateway());
remove(sc);
return true;
}
}

View File

@ -1,17 +1,17 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
* This software is licensed under the GNU General License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* it under the terms of the GNU General License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* GNU General License for more details.
*
* You should have received a copy of the GNU General Public License
* You should have received a copy of the GNU General License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
@ -21,35 +21,34 @@ package com.cloud.dc.dao;
import java.util.List;
import com.cloud.dc.Vlan;
import com.cloud.dc.VlanVO;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.dc.VlanVO;
import com.cloud.utils.Pair;
import com.cloud.utils.db.GenericDao;
public interface VlanDao extends GenericDao<VlanVO, Long> {
public VlanVO findByZoneAndVlanId(long zoneId, String vlanId);
VlanVO findByZoneAndVlanId(long zoneId, String vlanId);
public List<VlanVO> findByZone(long zoneId);
List<VlanVO> listByZone(long zoneId);
public List<VlanVO> listByZoneAndType(long zoneId, Vlan.VlanType vlanType);
List<VlanVO> listByZoneAndType(long zoneId, Vlan.VlanType vlanType);
public List<VlanVO> listVlansForPod(long podId);
List<VlanVO> listVlansForPod(long podId);
public List<VlanVO> listVlansForPodByType(long podId, Vlan.VlanType vlanType);
List<VlanVO> listVlansForPodByType(long podId, Vlan.VlanType vlanType);
public void addToPod(long podId, long vlanDbId);
void addToPod(long podId, long vlanDbId);
public Pair<String, VlanVO> assignIpAddress(long zoneId, long accountId, long domainId, VlanType vlanType, boolean sourceNat);
Pair<String, VlanVO> assignIpAddress(long zoneId, long accountId, long domainId, VlanType vlanType, boolean sourceNat);
List<VlanVO> listVlansForAccountByType(Long zoneId, long accountId, VlanType vlanType);
public Pair<String, VlanVO> assignPodDirectAttachIpAddress(long zoneId, long podId, long accountId, long domainId);
Pair<String, VlanVO> assignPodDirectAttachIpAddress(long zoneId, long podId, long accountId, long domainId);
boolean zoneHasDirectAttachUntaggedVlans(long zoneId);
List<VlanVO> listZoneWideVlans(long zoneId, VlanType vlanType, String vlanId);
List<VlanVO> searchForZoneWideVlans(long dcId, String vlanType,String vlanId);
}

View File

@ -31,8 +31,8 @@ import javax.naming.ConfigurationException;
import com.cloud.dc.AccountVlanMapVO;
import com.cloud.dc.PodVlanMapVO;
import com.cloud.dc.Vlan;
import com.cloud.dc.VlanVO;
import com.cloud.dc.Vlan.VlanType;
import com.cloud.dc.VlanVO;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.utils.Pair;
import com.cloud.utils.component.ComponentLocator;
@ -69,10 +69,10 @@ public class VlanDaoImpl extends GenericDaoBase<VlanVO, Long> implements VlanDao
}
@Override
public List<VlanVO> findByZone(long zoneId) {
public List<VlanVO> listByZone(long zoneId) {
SearchCriteria<VlanVO> sc = ZoneSearch.create();
sc.setParameters("zoneId", zoneId);
return listIncludingRemovedBy(sc);
return listBy(sc);
}
public VlanDaoImpl() {
@ -106,7 +106,7 @@ public class VlanDaoImpl extends GenericDaoBase<VlanVO, Long> implements VlanDao
SearchCriteria<VlanVO> sc = ZoneTypeSearch.create();
sc.setParameters("zoneId", zoneId);
sc.setParameters("vlanType", vlanType);
return listIncludingRemovedBy(sc);
return listBy(sc);
}
@Override

View File

@ -623,7 +623,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura
String[] linkLocalIpRanges = getLinkLocalIPRange();
if (linkLocalIpRanges != null) {
_zoneDao.addLinkLocalPrivateIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]);
_zoneDao.addLinkLocalIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]);
}
txn.commit();
@ -1495,7 +1495,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura
// 1. Another VLAN in the same zone has a different tag but the same subnet as the new VLAN
// 2. Another VLAN in the same zone that has the same tag and subnet as the new VLAN has IPs that overlap with the IPs being added
// 3. Another VLAN in the same zone that has the same tag and subnet as the new VLAN has a different gateway than the new VLAN
List<VlanVO> vlans = _vlanDao.findByZone(zone.getId());
List<VlanVO> vlans = _vlanDao.listByZone(zone.getId());
for (VlanVO vlan : vlans) {
String otherVlanGateway = vlan.getVlanGateway();
String otherVlanSubnet = NetUtils.getSubNet(vlan.getVlanGateway(), vlan.getVlanNetmask());

View File

@ -662,7 +662,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, VirtualMach
}
proxy.setPrivateIpAddress(privateIpAddress);
String guestIpAddress = _dcDao.allocateLinkLocalPrivateIpAddress(proxy.getDataCenterId(), routingHost.getPodId(), proxy.getId());
String guestIpAddress = _dcDao.allocateLinkLocalIpAddress(proxy.getDataCenterId(), routingHost.getPodId(), proxy.getId(), null);
proxy.setGuestIpAddress(guestIpAddress);
_consoleProxyDao.updateIf(proxy, Event.OperationRetry, routingHost.getId());
@ -745,7 +745,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, VirtualMach
proxy.setPrivateIpAddress(null);
freePrivateIpAddress(privateIpAddress, proxy.getDataCenterId(), proxy.getId());
proxy.setGuestIpAddress(null);
_dcDao.releaseLinkLocalPrivateIpAddress(guestIpAddress, proxy.getDataCenterId(), proxy.getId());
_dcDao.releaseLinkLocalIpAddress(guestIpAddress, proxy.getDataCenterId(), proxy.getId());
_storageMgr.unshare(proxy, vols, routingHost);
} while (--retry > 0
&& (routingHost = (HostVO) _agentMgr
@ -1163,7 +1163,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, VirtualMach
if (_IpAllocator != null && _IpAllocator.exteralIpAddressAllocatorEnabled()) {
return _IpAllocator.getPrivateIpAddress(macAddr, dcId, podId).ipaddr;
} else {
return _dcDao.allocatePrivateIpAddress(dcId, podId, proxyId);
return _dcDao.allocatePrivateIpAddress(dcId, podId, proxyId, null);
}
}
@ -1804,7 +1804,7 @@ public class ConsoleProxyManagerImpl implements ConsoleProxyManager, VirtualMach
String guestIpAddress = proxy.getGuestIpAddress();
if (guestIpAddress != null) {
proxy.setGuestIpAddress(null);
_dcDao.releaseLinkLocalPrivateIpAddress(guestIpAddress, proxy.getDataCenterId(), proxy.getId());
_dcDao.releaseLinkLocalIpAddress(guestIpAddress, proxy.getDataCenterId(), proxy.getId());
}
if (!_consoleProxyDao.updateIf(proxy, ev, null)) {

View File

@ -179,7 +179,7 @@ public class Db20to21MigrationUtil {
/*local link ip address starts from 169.254.0.2 - 169.254.(nums)*/
String[] ipRanges = NetUtils.getLinkLocalIPRange(nums);
_dcDao.addLinkLocalPrivateIpAddress(zoneId, pod.getId(), ipRanges[0], ipRanges[1]);
_dcDao.addLinkLocalIpAddress(zoneId, pod.getId(), ipRanges[0], ipRanges[1]);
}
}
}
@ -568,7 +568,7 @@ public class Db20to21MigrationUtil {
proxy.setState(State.Stopping);
}
String guestIpAddress = _dcDao.allocateLinkLocalPrivateIpAddress(proxy.getDataCenterId(), proxy.getPodId(), proxy.getId());
String guestIpAddress = _dcDao.allocateLinkLocalIpAddress(proxy.getDataCenterId(), proxy.getPodId(), proxy.getId(), null);
proxy.setGuestIpAddress(guestIpAddress);
proxy.setGuestNetmask("255.255.0.0");
@ -595,7 +595,7 @@ public class Db20to21MigrationUtil {
secStorageVm.setState(State.Stopping);
}
String guestIpAddress = _dcDao.allocateLinkLocalPrivateIpAddress(secStorageVm.getDataCenterId(), secStorageVm.getPodId(), secStorageVm.getId());
String guestIpAddress = _dcDao.allocateLinkLocalIpAddress(secStorageVm.getDataCenterId(), secStorageVm.getPodId(), secStorageVm.getId(), null);
secStorageVm.setGuestIpAddress(guestIpAddress);
secStorageVm.setGuestNetmask("255.255.0.0");

View File

@ -328,8 +328,8 @@ public class NetworkConfigurationVO implements NetworkConfiguration {
@Override
public String toString() {
StringBuilder buf = new StringBuilder("NtwkCfg:");
buf.append(id).append("-").append(trafficType.toString()).append("-").append(networkOfferingId);
StringBuilder buf = new StringBuilder("NtwkCfg[");
buf.append(id).append("|").append(trafficType.toString()).append("|").append(networkOfferingId).append("]");
return buf.toString();
}
}

View File

@ -356,4 +356,6 @@ public interface NetworkManager {
VpnUserVO addVpnUser(AddVpnUserCmd cmd) throws ConcurrentOperationException;
boolean removeVpnUser(RemoveVpnUserCmd cmd) throws ConcurrentOperationException;
String getNextAvailableMacAddressInNetwork(long networkConfigurationId);
}

View File

@ -372,9 +372,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
@Override @DB
public String assignSourceNatIpAddress(Account account, DataCenter dc) throws InsufficientAddressCapacityException {
final long dcId = dc.getId();
final long accountId = account.getId();
String sourceNat = null;
final long accountId = account.getId();
Transaction txn = Transaction.currentTxn();
try {
@ -2043,13 +2043,12 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
URI isolationUri = nic.getIsolationUri();
profile = new NicProfile(nic, config, broadcastUri, isolationUri);
String reservationId = concierge.reserve(profile, config, vmProfile, dest, context);
concierge.reserve(profile, config, vmProfile, dest, context);
nic.setIp4Address(profile.getIp4Address());
nic.setIp6Address(profile.getIp6Address());
nic.setMacAddress(profile.getMacAddress());
nic.setIsolationUri(profile.getIsolationUri());
nic.setBroadcastUri(profile.getBroadCastUri());
nic.setReservationId(reservationId);
nic.setReserver(concierge.getName());
nic.setState(Resource.State.Reserved);
nic.setNetmask(profile.getNetmask());
@ -2077,26 +2076,15 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
NetworkGuru concierge = _networkGurus.get(config.getGuruName());
nic.setState(Resource.State.Releasing);
_nicDao.update(nic.getId(), nic);
concierge.release(nic.getReservationId());
NicProfile profile = new NicProfile(nic, config, null, null);
if (!concierge.release(profile, vmProfile, nic.getReservationId())) {
nic.setState(Resource.State.Allocated);
_nicDao.update(nic.getId(), nic);
}
}
}
}
public void release(long vmId) {
List<NicVO> nics = _nicDao.listBy(vmId);
for (NicVO nic : nics) {
nic.setState(Resource.State.Releasing);
_nicDao.update(nic.getId(), nic);
NetworkGuru concierge = _networkGurus.get(nic.getReserver());
if (!concierge.release(nic.getReservationId())) {
s_logger.warn("Unable to release " + nic + " using " + concierge.getName());
}
nic.setState(Resource.State.Allocated);
_nicDao.update(nic.getId(), nic);
}
}
@Override
public List<NicVO> getNics(VMInstanceVO vm) {
return _nicDao.listBy(vm.getId());
@ -2931,5 +2919,9 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
public List<NetworkOfferingVO> listNetworkOfferings() {
return _networkOfferingDao.listNonSystemNetworkOfferings();
}
@Override
public String getNextAvailableMacAddressInNetwork(long networkConfigurationId) {
return _networkConfigDao.getNextAvailableMacAddress(networkConfigurationId);
}
}

View File

@ -60,30 +60,6 @@ public class ControlNetworkGuru extends AdapterBase implements NetworkGuru {
super();
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
super.configure(name, params);
ComponentLocator locator = ComponentLocator.getCurrentLocator();
ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
Map<String, String> dbParams = configDao.getConfiguration(params);
_cidr = dbParams.get(Config.ControlCidr);
if (_cidr == null) {
_cidr = "169.254.0.0/16";
}
_gateway = dbParams.get(Config.ControlGateway);
if (_gateway == null) {
_gateway = "169.254.0.1";
}
s_logger.info("Control network setup: cidr=" + _cidr + "; gateway = " + _gateway);
return true;
}
@Override
public NicProfile allocate(NetworkConfiguration config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
@ -103,29 +79,65 @@ public class ControlNetworkGuru extends AdapterBase implements NetworkGuru {
}
@Override
public String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException,
public void reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
String ip = _dcDao.allocateLinkLocalPrivateIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), vm.getId());
assert nic.getTrafficType() == TrafficType.Control;
String ip = _dcDao.allocateLinkLocalIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), vm.getId(), context.getReservationId());
nic.setIp4Address(ip);
nic.setMacAddress(NetUtils.long2Mac(NetUtils.ip2Long(ip) | (14l << 40)));
nic.setNetmask("255.255.0.0");
nic.setFormat(AddressFormat.Ip4);
return Long.toString(nic.getId());
nic.setGateway(NetUtils.getLinkLocalGateway());
}
@Override
public boolean release(String uniqueId) {
_dcDao.releaseLinkLocalPrivateIpAddress(Long.parseLong(uniqueId));
public boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) {
assert nic.getTrafficType() == TrafficType.Control;
_dcDao.releaseLinkLocalIpAddress(nic.getId(), reservationId);
nic.setIp4Address(null);
nic.setMacAddress(null);
nic.setNetmask(null);
nic.setFormat(null);
nic.setGateway(null);
return true;
}
@Override
public NetworkConfiguration implement(NetworkConfiguration config, NetworkOffering offering, DeployDestination destination, ReservationContext context) {
assert config.getTrafficType() == TrafficType.Control : "Why are you sending this configuration to me " + config;
return config;
}
@Override
public void destroy(NetworkConfiguration config, NetworkOffering offering) {
assert false : "Destroying a link local...Either you're out of your mind or something has changed.";
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
super.configure(name, params);
ComponentLocator locator = ComponentLocator.getCurrentLocator();
ConfigurationDao configDao = locator.getDao(ConfigurationDao.class);
Map<String, String> dbParams = configDao.getConfiguration(params);
_cidr = dbParams.get(Config.ControlCidr);
if (_cidr == null) {
_cidr = "169.254.0.0/16";
}
_gateway = dbParams.get(Config.ControlGateway);
if (_gateway == null) {
_gateway = NetUtils.getLinkLocalGateway();
}
s_logger.info("Control network setup: cidr=" + _cidr + "; gateway = " + _gateway);
return true;
}
}

View File

@ -1,4 +1,18 @@
/**
* Copyright (C) 2010 Cloud.com, Inc. All rights reserved.
*
* This software is licensed under the GNU General Public License v3 or later.
*
* It is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package com.cloud.network.configuration;
@ -24,7 +38,7 @@ import com.cloud.network.Network.TrafficType;
import com.cloud.network.NetworkConfiguration;
import com.cloud.network.NetworkConfiguration.State;
import com.cloud.network.NetworkConfigurationVO;
import com.cloud.network.dao.NetworkConfigurationDao;
import com.cloud.network.NetworkManager;
import com.cloud.offering.NetworkOffering;
import com.cloud.offering.NetworkOffering.GuestIpType;
import com.cloud.resource.Resource.ReservationStrategy;
@ -41,7 +55,7 @@ import com.cloud.vm.dao.NicDao;
@Local(value=NetworkGuru.class)
public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
@Inject protected NetworkConfigurationDao _networkConfigDao;
@Inject protected NetworkManager _networkMgr;
@Inject protected DataCenterDao _dcDao;
@Inject protected VlanDao _vlanDao;
@Inject protected NicDao _nicDao;
@ -158,7 +172,7 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
}
if (nic.getMacAddress() == null) {
nic.setMacAddress(_networkConfigDao.getNextAvailableMacAddress(config.getId()));
nic.setMacAddress(_networkMgr.getNextAvailableMacAddressInNetwork(config.getId()));
if (nic.getMacAddress() == null) {
throw new InsufficientAddressCapacityException("Unable to allocate more mac addresses");
}
@ -187,7 +201,7 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
}
@Override
public String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException,
public void reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
assert (nic.getReservationStrategy() == ReservationStrategy.Start) : "What can I do for nics that are not allocated at start? ";
@ -198,11 +212,12 @@ public class GuestNetworkGuru extends AdapterBase implements NetworkGuru {
nic.setNetmask(NetUtils.cidr2Netmask(config.getCidr()));
nic.setDns1(config.getDns1());
nic.setDns2(config.getDns2());
return "ABCD";
}
@Override
public boolean release(String uniqueId) {
public boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) {
nic.setBroadcastUri(null);
nic.setIsolationUri(null);
return true;
}

View File

@ -7,7 +7,6 @@ import javax.ejb.Local;
import org.apache.log4j.Logger;
import com.cloud.dc.DataCenter;
import com.cloud.dc.Pod;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deploy.DeployDestination;
@ -20,6 +19,7 @@ import com.cloud.network.Network.Mode;
import com.cloud.network.Network.TrafficType;
import com.cloud.network.NetworkConfiguration;
import com.cloud.network.NetworkConfigurationVO;
import com.cloud.network.NetworkManager;
import com.cloud.offering.NetworkOffering;
import com.cloud.resource.Resource.ReservationStrategy;
import com.cloud.user.Account;
@ -35,6 +35,7 @@ import com.cloud.vm.VirtualMachineProfile;
public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru {
private static final Logger s_logger = Logger.getLogger(PodBasedNetworkGuru.class);
@Inject DataCenterDao _dcDao;
@Inject NetworkManager _networkMgr;
@Override
public NetworkConfiguration design(NetworkOffering offering, DeploymentPlan plan, NetworkConfiguration userSpecified, Account owner) {
@ -61,9 +62,7 @@ public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru {
public NicProfile allocate(NetworkConfiguration config, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
TrafficType trafficType = config.getTrafficType();
if (trafficType != TrafficType.Storage && trafficType != TrafficType.Management) {
return null;
}
assert (trafficType == TrafficType.Storage || trafficType == TrafficType.Management) : "Well, I can't take care of this config now can I? " + config;
if (nic != null) {
nic.setStrategy(ReservationStrategy.Start);
@ -71,35 +70,33 @@ public class PodBasedNetworkGuru extends AdapterBase implements NetworkGuru {
nic = new NicProfile(ReservationStrategy.Start, null, null, null, null);
}
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId());
nic.setMacAddress(mac);
return nic;
}
@Override
public String reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException,
public void reserve(NicProfile nic, NetworkConfiguration config, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
DataCenter dc = dest.getDataCenter();
Pod pod = dest.getPod();
String ip = _dcDao.allocatePrivateIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), nic.getId());
String ip = _dcDao.allocatePrivateIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), nic.getId(), context.getReservationId());
if (ip == null) {
throw new InsufficientAddressCapacityException("Unable to get a management ip address");
}
String[] macs = _dcDao.getNextAvailableMacAddressPair(dc.getId());
nic.setIp4Address(ip);
nic.setGateway(pod.getGateway());
nic.setMacAddress(macs[0]);
nic.setFormat(AddressFormat.Ip4);
String netmask = NetUtils.getCidrSubNet(pod.getCidrAddress(), pod.getCidrSize());
nic.setNetmask(netmask);
return Long.toString(nic.getId());
}
@Override
public boolean release(String uniqueId) {
_dcDao.releasePrivateIpAddress(Long.parseLong(uniqueId));
public boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) {
_dcDao.releasePrivateIpAddress(nic.getId(), reservationId);
return true;
}

View File

@ -23,7 +23,8 @@ import com.cloud.network.Network.Mode;
import com.cloud.network.Network.TrafficType;
import com.cloud.network.NetworkConfiguration;
import com.cloud.network.NetworkConfigurationVO;
import com.cloud.network.dao.NetworkConfigurationDao;
import com.cloud.network.NetworkManager;
import com.cloud.network.dao.IPAddressDao;
import com.cloud.offering.NetworkOffering;
import com.cloud.resource.Resource.ReservationStrategy;
import com.cloud.user.Account;
@ -41,7 +42,8 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru {
@Inject DataCenterDao _dcDao;
@Inject VlanDao _vlanDao;
@Inject NetworkConfigurationDao _networkConfigDao;
@Inject NetworkManager _networkMgr;
@Inject IPAddressDao _ipAddressDao;
@Override
public NetworkConfiguration design(NetworkOffering offering, DeploymentPlan plan, NetworkConfiguration config, Account owner) {
@ -89,7 +91,7 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru {
nic.setStrategy(ReservationStrategy.Create);
}
String mac = _networkConfigDao.getNextAvailableMacAddress(config.getId());
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId());
if (mac == null) {
throw new InsufficientAddressCapacityException("Not enough mac addresses");
}
@ -102,21 +104,16 @@ public class PublicNetworkGuru extends AdapterBase implements NetworkGuru {
}
@Override
public String reserve(NicProfile nic, NetworkConfiguration configuration, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException {
if (nic.getReservationId() != null) {
return nic.getReservationId();
}
public void reserve(NicProfile nic, NetworkConfiguration configuration, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException {
if (nic.getIp4Address() == null) {
getIp(nic, dest.getDataCenter(), vm);
}
return nic.getReservationId();
}
@Override
public boolean release(String uniqueId) {
return _vlanDao.releaseFromLockTable(Long.parseLong(uniqueId));
public boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) {
_ipAddressDao.unassignIpAddress(reservationId);
return true;
}
@Override

View File

@ -914,10 +914,10 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute
String privateNetMask = null;
if(_defaultHypervisorType == null || !_defaultHypervisorType.equalsIgnoreCase(Hypervisor.HypervisorType.VmWare.toString())) {
privateIpAddress = _dcDao.allocateLinkLocalPrivateIpAddress(router.getDataCenterId(), routingHost.getPodId(), router.getId());
privateIpAddress = _dcDao.allocateLinkLocalIpAddress(router.getDataCenterId(), routingHost.getPodId(), router.getId(), null);
privateNetMask = NetUtils.getLinkLocalNetMask();
} else {
privateIpAddress = _dcDao.allocatePrivateIpAddress(router.getDataCenterId(), routingHost.getPodId(), router.getId());
privateIpAddress = _dcDao.allocatePrivateIpAddress(router.getDataCenterId(), routingHost.getPodId(), router.getId(), null);
privateNetMask = NetUtils.getCidrNetmask(pod.getCidrSize());
}
@ -1023,7 +1023,7 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute
router.setPrivateIpAddress(null);
if(_defaultHypervisorType == null || !_defaultHypervisorType.equalsIgnoreCase(Hypervisor.HypervisorType.VmWare.toString()))
_dcDao.releaseLinkLocalPrivateIpAddress(privateIpAddress, router.getDataCenterId(), router.getId());
_dcDao.releaseLinkLocalIpAddress(privateIpAddress, router.getDataCenterId(), router.getId());
else
_dcDao.releasePrivateIpAddress(privateIpAddress, router.getDataCenterId(), router.getId());
@ -1571,7 +1571,7 @@ public class DomainRouterManagerImpl implements DomainRouterManager, DomainRoute
if (privateIpAddress != null) {
if(_defaultHypervisorType == null || !_defaultHypervisorType.equalsIgnoreCase(Hypervisor.HypervisorType.VmWare.toString()))
_dcDao.releaseLinkLocalPrivateIpAddress(privateIpAddress, router.getDataCenterId(), router.getId());
_dcDao.releaseLinkLocalIpAddress(privateIpAddress, router.getDataCenterId(), router.getId());
else
_dcDao.releasePrivateIpAddress(privateIpAddress, router.getDataCenterId(), router.getId());
}

View File

@ -625,7 +625,7 @@ public class ConfigurationServerImpl implements ConfigurationServer {
if (linkLocalIpRanges == null) {
throw new InvalidParameterValueException("The linkLocalIp.nums: " + nums + "may be wrong, should be 1~16");
} else {
_zoneDao.addLinkLocalPrivateIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]);
_zoneDao.addLinkLocalIpAddress(zoneId, pod.getId(), linkLocalIpRanges[0], linkLocalIpRanges[1]);
}
txn.commit();

View File

@ -363,7 +363,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V
}
secStorageVm.setPrivateIpAddress(privateIpAddress);
String guestIpAddress = _dcDao.allocateLinkLocalPrivateIpAddress(secStorageVm.getDataCenterId(), routingHost.getPodId(), secStorageVm.getId());
String guestIpAddress = _dcDao.allocateLinkLocalIpAddress(secStorageVm.getDataCenterId(), routingHost.getPodId(), secStorageVm.getId(), null);
secStorageVm.setGuestIpAddress(guestIpAddress);
_secStorageVmDao.updateIf(secStorageVm, Event.OperationRetry, routingHost.getId());
secStorageVm = _secStorageVmDao.findById(secStorageVm.getId());
@ -452,7 +452,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V
freePrivateIpAddress(privateIpAddress, secStorageVm
.getDataCenterId(), secStorageVm.getId());
secStorageVm.setGuestIpAddress(null);
_dcDao.releaseLinkLocalPrivateIpAddress(guestIpAddress, secStorageVm.getDataCenterId(), secStorageVm.getId());
_dcDao.releaseLinkLocalIpAddress(guestIpAddress, secStorageVm.getDataCenterId(), secStorageVm.getId());
_storageMgr.unshare(secStorageVm, vols, routingHost);
} while (--retry > 0 && (routingHost = (HostVO) _agentMgr.findHost(
Host.Type.Routing, dc, pod, sp, _serviceOffering, _template,
@ -859,7 +859,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V
if (_IpAllocator != null && _IpAllocator.exteralIpAddressAllocatorEnabled()) {
return _IpAllocator.getPrivateIpAddress(macAddr, dcId, podId).ipaddr;
} else {
return _dcDao.allocatePrivateIpAddress(dcId, podId, proxyId);
return _dcDao.allocatePrivateIpAddress(dcId, podId, proxyId, null);
}
}
@ -1447,7 +1447,7 @@ public class SecondaryStorageManagerImpl implements SecondaryStorageVmManager, V
String guestIpAddress = secStorageVm.getGuestIpAddress();
if (guestIpAddress != null) {
secStorageVm.setGuestIpAddress(null);
_dcDao.releaseLinkLocalPrivateIpAddress(guestIpAddress, secStorageVm.getDataCenterId(), secStorageVm.getId());
_dcDao.releaseLinkLocalIpAddress(guestIpAddress, secStorageVm.getDataCenterId(), secStorageVm.getId());
}
if (!_secStorageVmDao.updateIf(secStorageVm, ev, null)) {
s_logger.debug("Unable to update the secondary storage vm");

View File

@ -403,6 +403,7 @@ CREATE TABLE `cloud`.`op_dc_link_local_ip_address_alloc` (
`data_center_id` bigint unsigned NOT NULL COMMENT 'data center it belongs to',
`pod_id` bigint unsigned NOT NULL COMMENT 'pod it belongs to',
`instance_id` bigint unsigned NULL COMMENT 'instance id',
`reservation_id` char(40) NULL COMMENT 'reservation id used to reserve this network',
`taken` datetime COMMENT 'Date taken',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;