Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create DHCP relay config on the network VR allocation
  • Loading branch information
nvazquez committed Oct 6, 2023
commit eab65fb4b7534c3536436620c793a23170ea8af0
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,26 @@
// under the License.
package org.apache.cloudstack.agent.api;

import java.util.List;
import com.cloud.network.Network;
import com.cloud.vm.NicProfile;

public class CreateDhcpRelayCommand extends NsxCommand {
public class CreateNsxDhcpRelayCommand extends NsxCommand {

private String dhcpRelayName;
private List<String> serverAddresses;
private Network network;
private NicProfile nicProfile;

public CreateDhcpRelayCommand(String zoneName, Long zoneId, String accountName, Long accountId,
String dhcpRelayName, List<String> serverAddresses) {
public CreateNsxDhcpRelayCommand(String zoneName, Long zoneId, String accountName, Long accountId,
Network network, NicProfile nicProfile) {
super(zoneName, zoneId, accountName, accountId);
this.dhcpRelayName = dhcpRelayName;
this.serverAddresses = serverAddresses;
this.network = network;
this.nicProfile = nicProfile;
}

public String getDhcpRelayName() {
return dhcpRelayName;
public Network getNetwork() {
return network;
}

public List<String> getServerAddresses() {
return serverAddresses;
public NicProfile getNicProfile() {
return nicProfile;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@
import com.cloud.agent.api.StartupCommand;
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.host.Host;
import com.cloud.network.Network;
import com.cloud.resource.ServerResource;
import com.cloud.utils.exception.CloudRuntimeException;

import com.cloud.vm.NicProfile;
import com.vmware.nsx.model.TransportZone;
import com.vmware.nsx.model.TransportZoneListResult;
import com.vmware.nsx_policy.infra.DhcpRelayConfigs;
Expand All @@ -49,7 +51,7 @@
import com.vmware.vapi.std.errors.Error;
import org.apache.cloudstack.NsxAnswer;
import org.apache.cloudstack.StartupNsxCommand;
import org.apache.cloudstack.agent.api.CreateDhcpRelayCommand;
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayCommand;
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
import org.apache.cloudstack.agent.api.CreateNsxTier1GatewayCommand;
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
Expand Down Expand Up @@ -78,6 +80,7 @@ public class NsxResource implements ServerResource {
private static final Logger LOGGER = Logger.getLogger(NsxResource.class);
private static final String TIER_0_GATEWAY_PATH_PREFIX = "/infra/tier-0s/";
private static final String TIER_1_GATEWAY_PATH_PREFIX = "/infra/tier-1s/";
private static final String DHCP_RELAY_CONFIGS_PATH_PREFIX = "/infra/dhcp-relay-configs/";

private static final String Tier_1_LOCALE_SERVICE_ID = "default";
private static final String TIER_1_RESOURCE_TYPE = "Tier1";
Expand Down Expand Up @@ -131,8 +134,8 @@ public Answer executeRequest(Command cmd) {
return executeRequest((CreateNsxSegmentCommand) cmd);
} else if (cmd instanceof CreateNsxTier1GatewayCommand) {
return executeRequest((CreateNsxTier1GatewayCommand) cmd);
} else if (cmd instanceof CreateDhcpRelayCommand) {
return executeRequest((CreateDhcpRelayCommand) cmd);
} else if (cmd instanceof CreateNsxDhcpRelayCommand) {
return executeRequest((CreateNsxDhcpRelayCommand) cmd);
} else {
return Answer.createUnsupportedCommandAnswer(cmd);
}
Expand Down Expand Up @@ -240,23 +243,34 @@ public boolean configure(String name, Map<String, Object> params) throws Configu
return true;
}

private Answer executeRequest(CreateDhcpRelayCommand cmd) {
String dhcpRelayName = cmd.getDhcpRelayName();
List<String> addresses = cmd.getServerAddresses();
String msg = String.format("Creating DHCP relay with name %s for addresses %s", dhcpRelayName, addresses);
private String getDhcpRelayConfig(String zoneName, String accountName, Network network) {
return String.format("%s-%s-%s-Relay", zoneName, accountName, network.getName());
}

private Answer executeRequest(CreateNsxDhcpRelayCommand cmd) {
String zoneName = cmd.getZoneName();
String accountName = cmd.getAccountName();
Network network = cmd.getNetwork();
NicProfile nicProfile = cmd.getNicProfile();

String dhcpRelayConfigName = getDhcpRelayConfig(zoneName, accountName, network);
List<String> addresses = List.of(nicProfile.getIPv4Address());

String msg = String.format("Creating DHCP relay config with name %s for addresses %s on network %s",
dhcpRelayConfigName, nicProfile.getIPv4Address(), network.getName());
LOGGER.debug(msg);

try {
DhcpRelayConfigs service = (DhcpRelayConfigs) nsxService.apply(DhcpRelayConfigs.class);
DhcpRelayConfig config = new DhcpRelayConfig.Builder()
.setServerAddresses(addresses)
.setId(dhcpRelayName)
.setDisplayName(dhcpRelayName)
.setId(dhcpRelayConfigName)
.setDisplayName(dhcpRelayConfigName)
.build();
service.patch(dhcpRelayName, config);
service.patch(dhcpRelayConfigName, config);
} catch (Error error) {
ApiError ae = error.getData()._convertTo(ApiError.class);
msg = String.format("Error creating the DHCP relay with name %s: %s", dhcpRelayName, ae.getErrorMessage());
msg = String.format("Error creating the DHCP relay config with name %s: %s", dhcpRelayConfigName, ae.getErrorMessage());
LOGGER.error(msg);
return new NsxAnswer(cmd, new CloudRuntimeException(ae.getErrorMessage()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import com.cloud.agent.api.Answer;
import com.cloud.agent.api.Command;
import com.cloud.agent.api.StartupCommand;
import com.cloud.api.query.dao.DomainRouterJoinDao;
import com.cloud.api.query.vo.DomainRouterJoinVO;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deploy.DeployDestination;
Expand All @@ -52,7 +50,6 @@
import com.cloud.network.vpc.PrivateGateway;
import com.cloud.network.vpc.StaticRouteProfile;
import com.cloud.network.vpc.Vpc;
import com.cloud.network.vpc.dao.VpcDao;
import com.cloud.offering.NetworkOffering;
import com.cloud.resource.ResourceManager;
import com.cloud.resource.ResourceStateAdapter;
Expand All @@ -62,14 +59,11 @@
import com.cloud.user.AccountManager;
import com.cloud.utils.Pair;
import com.cloud.utils.component.AdapterBase;
import com.cloud.vm.DomainRouterVO;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachineProfile;
import com.cloud.vm.dao.DomainRouterDao;
import net.sf.ehcache.config.InvalidConfigurationException;
import org.apache.cloudstack.StartupNsxCommand;
import org.apache.cloudstack.agent.api.CreateDhcpRelayCommand;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -102,12 +96,6 @@ public class NsxElement extends AdapterBase implements DhcpServiceProvider, DnsS
PhysicalNetworkDao physicalNetworkDao;
@Inject
NetworkModel networkModel;
@Inject
private VpcDao vpcDao;
@Inject
private DomainRouterDao domainRouterDao;
@Inject
private DomainRouterJoinDao domainRouterJoinDao;

private static final Logger LOGGER = Logger.getLogger(NsxElement.class);

Expand Down Expand Up @@ -135,24 +123,6 @@ private static Map<Network.Service, Map<Network.Capability, String>> initCapabil
}
@Override
public boolean addDhcpEntry(Network network, NicProfile nic, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context) throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
Long vpcId = network.getVpcId();
Vpc vpc = vpcDao.getActiveVpcById(vpcId);
if (vpc == null) {
String msg = String.format("Cannot find the VPC with ID %s for network %s", vpcId, network.getName());
LOGGER.error(msg);
return false;
}
List<DomainRouterVO> routers = domainRouterDao.listByVpcId(vpcId);
if (org.apache.commons.collections.CollectionUtils.isEmpty(routers)) {
String msg = String.format("Cannot find a Virtual Router for the VPC %s", vpc.getName());
LOGGER.error(msg);
return false;
}
DomainRouterVO router = routers.get(0);
DomainRouterJoinVO nsxRouter = domainRouterJoinDao.findByRouterVpcAndNetworkId(router.getId(), vpcId, network.getId());
String nsxRouterIpAddress = nsxRouter.getIpAddress();
String dhcpRelayName = String.format("%s-%s-Relay", vpc.getName(), network.getName());
CreateDhcpRelayCommand cmd = new CreateDhcpRelayCommand();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static java.util.Objects.nonNull;

import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deploy.DeployDestination;
import com.cloud.deploy.DeploymentPlan;
Expand All @@ -43,12 +44,15 @@
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
import org.apache.cloudstack.NsxAnswer;
import org.apache.cloudstack.agent.api.CreateNsxDhcpRelayCommand;
import org.apache.cloudstack.agent.api.CreateNsxSegmentCommand;
import org.apache.log4j.Logger;

import javax.inject.Inject;
import java.util.Objects;

public class NsxGuestNetworkGuru extends GuestNetworkGuru implements NetworkMigrationResponder {
private static final Logger LOGGER = Logger.getLogger(NsxGuestNetworkGuru.class);
Expand Down Expand Up @@ -182,6 +186,36 @@ public Network implement(Network network, NetworkOffering offering, DeployDestin
return implemented;
}

@Override
public NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapacityException, InsufficientAddressCapacityException {
if (vm.getType() == VirtualMachine.Type.DomainRouter) {
// Create the DHCP relay config for the segment
Account account = accountDao.findById(network.getAccountId());
if (Objects.isNull(account)) {
String msg = String.format("Unable to find account with id: %s", network.getAccountId());
LOGGER.error(msg);
throw new CloudRuntimeException(msg);
}
long zoneId = network.getDataCenterId();
DataCenterVO zone = _dcDao.findById(zoneId);
if (Objects.isNull(zone)) {
String msg = String.format("Unable to find zone with id: %s", zoneId);
LOGGER.error(msg);
throw new CloudRuntimeException(msg);
}
CreateNsxDhcpRelayCommand command = new CreateNsxDhcpRelayCommand(zone.getName(), zone.getId(),
account.getAccountName(), network.getAccountId(),
network, nic);
NsxAnswer answer = nsxControllerUtils.sendNsxCommand(command, zone.getId());
if (!answer.getResult()) {
String msg = String.format("Error creating DHCP relay config for network %s and nic %s: %s", network.getName(), nic.getName(), answer.getDetails());
LOGGER.error(msg);
throw new CloudRuntimeException(msg);
}
}
return nic;
}

@Override
public void reserve(final NicProfile nic, final Network network, final VirtualMachineProfile vm,
final DeployDestination dest, final ReservationContext context)
Expand Down