mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
264 lines
10 KiB
Java
Executable File
264 lines
10 KiB
Java
Executable File
/**
|
|
* 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.guru;
|
|
|
|
import javax.ejb.Local;
|
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
import com.cloud.dc.DataCenter;
|
|
import com.cloud.dc.DataCenter.NetworkType;
|
|
import com.cloud.dc.Vlan.VlanType;
|
|
import com.cloud.dc.dao.DataCenterDao;
|
|
import com.cloud.dc.dao.VlanDao;
|
|
import com.cloud.deploy.DeployDestination;
|
|
import com.cloud.deploy.DeploymentPlan;
|
|
import com.cloud.exception.ConcurrentOperationException;
|
|
import com.cloud.exception.InsufficientAddressCapacityException;
|
|
import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
|
|
import com.cloud.exception.InvalidParameterValueException;
|
|
import com.cloud.network.IPAddressVO;
|
|
import com.cloud.network.Network;
|
|
import com.cloud.network.Network.Service;
|
|
import com.cloud.network.Network.State;
|
|
import com.cloud.network.NetworkManager;
|
|
import com.cloud.network.NetworkProfile;
|
|
import com.cloud.network.NetworkVO;
|
|
import com.cloud.network.Networks.AddressFormat;
|
|
import com.cloud.network.Networks.BroadcastDomainType;
|
|
import com.cloud.network.Networks.IsolationType;
|
|
import com.cloud.network.Networks.Mode;
|
|
import com.cloud.network.Networks.TrafficType;
|
|
import com.cloud.network.addr.PublicIp;
|
|
import com.cloud.network.dao.IPAddressDao;
|
|
import com.cloud.offering.NetworkOffering;
|
|
import com.cloud.offerings.dao.NetworkOfferingDao;
|
|
import com.cloud.user.Account;
|
|
import com.cloud.utils.component.AdapterBase;
|
|
import com.cloud.utils.component.Inject;
|
|
import com.cloud.utils.db.DB;
|
|
import com.cloud.utils.db.Transaction;
|
|
import com.cloud.utils.net.Ip4Address;
|
|
import com.cloud.vm.Nic.ReservationStrategy;
|
|
import com.cloud.vm.NicProfile;
|
|
import com.cloud.vm.ReservationContext;
|
|
import com.cloud.vm.VirtualMachine;
|
|
import com.cloud.vm.VirtualMachineProfile;
|
|
|
|
@Local(value = { NetworkGuru.class })
|
|
public class DirectNetworkGuru extends AdapterBase implements NetworkGuru {
|
|
private static final Logger s_logger = Logger.getLogger(DirectNetworkGuru.class);
|
|
|
|
@Inject
|
|
DataCenterDao _dcDao;
|
|
@Inject
|
|
VlanDao _vlanDao;
|
|
@Inject
|
|
NetworkManager _networkMgr;
|
|
@Inject
|
|
IPAddressDao _ipAddressDao;
|
|
@Inject
|
|
NetworkOfferingDao _networkOfferingDao;
|
|
|
|
private static final TrafficType[] _trafficTypes = {TrafficType.Guest};
|
|
|
|
@Override
|
|
public boolean isMyTrafficType(TrafficType type) {
|
|
for (TrafficType t : _trafficTypes) {
|
|
if (t == type) {
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
@Override
|
|
public TrafficType[] getSupportedTrafficType() {
|
|
return _trafficTypes;
|
|
}
|
|
|
|
protected boolean canHandle(NetworkOffering offering, DataCenter dc) {
|
|
// this guru handles only Guest networks in Advance zone with source nat service disabled
|
|
if (dc.getNetworkType() == NetworkType.Advanced && !_networkMgr.areServicesSupportedByNetworkOffering(offering.getId(), Service.SourceNat)&& isMyTrafficType(offering.getTrafficType())) {
|
|
return true;
|
|
} else {
|
|
s_logger.trace("We only take care of Guest Direct networks");
|
|
return false;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) {
|
|
DataCenter dc = _dcDao.findById(plan.getDataCenterId());
|
|
|
|
if (!canHandle(offering, dc)) {
|
|
return null;
|
|
}
|
|
|
|
State state = State.Allocated;
|
|
if (dc.getNetworkType() == NetworkType.Basic) {
|
|
state = State.Setup;
|
|
}
|
|
|
|
NetworkVO config = new NetworkVO(offering.getTrafficType(), Mode.Dhcp, BroadcastDomainType.Vlan, offering.getId(), state, plan.getDataCenterId(), plan.getPhysicalNetworkId());
|
|
|
|
if (userSpecified != null) {
|
|
if ((userSpecified.getCidr() == null && userSpecified.getGateway() != null) || (userSpecified.getCidr() != null && userSpecified.getGateway() == null)) {
|
|
throw new InvalidParameterValueException("cidr and gateway must be specified together.");
|
|
}
|
|
|
|
if (userSpecified.getCidr() != null) {
|
|
config.setCidr(userSpecified.getCidr());
|
|
config.setGateway(userSpecified.getGateway());
|
|
config.setSpecifiedCidr(true);
|
|
}
|
|
|
|
if (userSpecified.getBroadcastUri() != null) {
|
|
config.setBroadcastUri(userSpecified.getBroadcastUri());
|
|
config.setState(State.Setup);
|
|
}
|
|
|
|
if (userSpecified.getBroadcastDomainType() != null) {
|
|
config.setBroadcastDomainType(userSpecified.getBroadcastDomainType());
|
|
}
|
|
}
|
|
|
|
boolean isSecurityGroupEnabled = _networkMgr.areServicesSupportedByNetworkOffering(offering.getId(), Service.SecurityGroup);
|
|
if (isSecurityGroupEnabled) {
|
|
config.setName("SecurityGroupEnabledNetwork");
|
|
config.setDisplayText("SecurityGroupEnabledNetwork");
|
|
}
|
|
|
|
return config;
|
|
}
|
|
|
|
protected DirectNetworkGuru() {
|
|
super();
|
|
}
|
|
|
|
protected void getIp(NicProfile nic, DataCenter dc, VirtualMachineProfile<? extends VirtualMachine> vm, Network network, String requestedIp) throws InsufficientVirtualNetworkCapcityException,
|
|
InsufficientAddressCapacityException, ConcurrentOperationException {
|
|
if (nic.getIp4Address() == null) {
|
|
PublicIp ip = _networkMgr.assignPublicIpAddress(dc.getId(), null, vm.getOwner(), VlanType.DirectAttached, network.getId(), requestedIp, false);
|
|
nic.setIp4Address(ip.getAddress().toString());
|
|
nic.setGateway(ip.getGateway());
|
|
nic.setNetmask(ip.getNetmask());
|
|
nic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag()));
|
|
nic.setBroadcastType(BroadcastDomainType.Vlan);
|
|
nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag()));
|
|
nic.setFormat(AddressFormat.Ip4);
|
|
nic.setReservationId(String.valueOf(ip.getVlanTag()));
|
|
nic.setMacAddress(ip.getMacAddress());
|
|
}
|
|
|
|
nic.setDns1(dc.getDns1());
|
|
nic.setDns2(dc.getDns2());
|
|
}
|
|
|
|
@Override
|
|
public void updateNicProfile(NicProfile profile, Network network) {
|
|
DataCenter dc = _dcDao.findById(network.getDataCenterId());
|
|
if (profile != null) {
|
|
profile.setDns1(dc.getDns1());
|
|
profile.setDns2(dc.getDns2());
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException,
|
|
InsufficientAddressCapacityException, ConcurrentOperationException {
|
|
|
|
DataCenter dc = _dcDao.findById(network.getDataCenterId());
|
|
|
|
if (nic == null) {
|
|
nic = new NicProfile(ReservationStrategy.Create, null, null, null, null);
|
|
} else if (nic.getIp4Address() == null) {
|
|
nic.setStrategy(ReservationStrategy.Start);
|
|
} else {
|
|
nic.setStrategy(ReservationStrategy.Create);
|
|
}
|
|
|
|
getIp(nic, dc, vm, network, nic.getRequestedIp());
|
|
nic.setStrategy(ReservationStrategy.Create);
|
|
|
|
return nic;
|
|
}
|
|
|
|
@Override
|
|
public void reserve(NicProfile nic, Network network, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context)
|
|
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException {
|
|
if (nic.getIp4Address() == null) {
|
|
getIp(nic, dest.getDataCenter(), vm, network, null);
|
|
nic.setStrategy(ReservationStrategy.Create);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) throws InsufficientVirtualNetworkCapcityException {
|
|
return network;
|
|
}
|
|
|
|
@Override @DB
|
|
public void deallocate(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) {
|
|
if (s_logger.isDebugEnabled()) {
|
|
s_logger.debug("direct network deallocate network: networkId: " + nic.getNetworkId() + ", ip: " + nic.getIp4Address());
|
|
}
|
|
|
|
IPAddressVO ip = _ipAddressDao.findByIpAndSourceNetworkId(nic.getNetworkId(), nic.getIp4Address());
|
|
if (ip != null) {
|
|
Transaction txn = Transaction.currentTxn();
|
|
txn.start();
|
|
_networkMgr.markIpAsUnavailable(ip.getId());
|
|
_ipAddressDao.unassignIpAddress(ip.getId());
|
|
txn.commit();
|
|
}
|
|
nic.deallocate();
|
|
}
|
|
|
|
@Override
|
|
public void shutdown(NetworkProfile network, NetworkOffering offering) {
|
|
}
|
|
|
|
@Override
|
|
public boolean trash(Network network, NetworkOffering offering, Account owner) {
|
|
return true;
|
|
}
|
|
|
|
@Override
|
|
public void updateNetworkProfile(NetworkProfile networkProfile) {
|
|
DataCenter dc = _dcDao.findById(networkProfile.getDataCenterId());
|
|
networkProfile.setDns1(dc.getDns1());
|
|
networkProfile.setDns2(dc.getDns2());
|
|
}
|
|
|
|
@Override public Ip4Address acquireIp4Address(Network network, String requestedIp, String reservationId) throws InsufficientAddressCapacityException {
|
|
// TODO Auto-generated method stub
|
|
return null;
|
|
}
|
|
|
|
@Override public boolean releaseIp4Address(Network network, String reservationId) {
|
|
// TODO Auto-generated method stub
|
|
return false;
|
|
}
|
|
}
|