use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class NetworkServiceImpl method releaseIpAddressInternal.
@DB
private boolean releaseIpAddressInternal(long ipAddressId) throws InsufficientAddressCapacityException {
Long userId = CallContext.current().getCallingUserId();
Account caller = CallContext.current().getCallingAccount();
// Verify input parameters
IPAddressVO ipVO = _ipAddressDao.findById(ipAddressId);
if (ipVO == null) {
throw new InvalidParameterValueException("Unable to find ip address by id");
}
if (ipVO.getAllocatedTime() == null) {
s_logger.debug("Ip Address id= " + ipAddressId + " is not allocated, so do nothing.");
return true;
}
// verify permissions
if (ipVO.getAllocatedToAccountId() != null) {
_accountMgr.checkAccess(caller, null, true, ipVO);
}
if (ipVO.isSourceNat()) {
throw new IllegalArgumentException("ip address is used for source nat purposes and can not be disassociated.");
}
VlanVO vlan = _vlanDao.findById(ipVO.getVlanId());
if (!vlan.getVlanType().equals(VlanType.VirtualNetwork)) {
throw new IllegalArgumentException("only ip addresses that belong to a virtual network may be disassociated.");
}
// don't allow releasing system ip address
if (ipVO.getSystem()) {
InvalidParameterValueException ex = new InvalidParameterValueException("Can't release system IP address with specified id");
ex.addProxyObject(ipVO.getUuid(), "systemIpAddrId");
throw ex;
}
boolean success = _ipAddrMgr.disassociatePublicIpAddress(ipAddressId, userId, caller);
if (success) {
Long networkId = ipVO.getAssociatedWithNetworkId();
if (networkId != null) {
Network guestNetwork = getNetwork(networkId);
NetworkOffering offering = _entityMgr.findById(NetworkOffering.class, guestNetwork.getNetworkOfferingId());
Long vmId = ipVO.getAssociatedWithVmId();
if (offering.getElasticIp() && vmId != null) {
_rulesMgr.getSystemIpAndEnableStaticNatForVm(_userVmDao.findById(vmId), true);
return true;
}
}
} else {
s_logger.warn("Failed to release public ip address id=" + ipAddressId);
}
return success;
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class VlanDaoImpl method assignPodDirectAttachIpAddress.
public Pair<String, VlanVO> assignPodDirectAttachIpAddress(long zoneId, long podId, long accountId, long domainId) {
SearchCriteria<VlanVO> sc = ZoneTypePodSearch.create();
sc.setParameters("zoneId", zoneId);
sc.setParameters("vlanType", VlanType.DirectAttached);
sc.setJoinParameters("vlan", "podId", podId);
VlanVO vlan = findOneIncludingRemovedBy(sc);
if (vlan == null) {
return null;
}
return null;
// String ipAddress = _ipAddressDao.assignIpAddress(accountId, domainId, vlan.getId(), false).getAddress();
// if (ipAddress == null) {
// return null;
// }
// return new Pair<String, VlanVO>(ipAddress, vlan);
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class VlanDaoImpl method findNextVlan.
private VlanVO findNextVlan(long zoneId, Vlan.VlanType vlanType) {
List<VlanVO> allVlans = listByZoneAndType(zoneId, vlanType);
List<VlanVO> emptyVlans = new ArrayList<VlanVO>();
List<VlanVO> fullVlans = new ArrayList<VlanVO>();
// Try to find a VLAN that is partially allocated
for (VlanVO vlan : allVlans) {
long vlanDbId = vlan.getId();
int countOfAllocatedIps = _ipAddressDao.countIPs(zoneId, vlanDbId, true);
int countOfAllIps = _ipAddressDao.countIPs(zoneId, vlanDbId, false);
if ((countOfAllocatedIps > 0) && (countOfAllocatedIps < countOfAllIps)) {
return vlan;
} else if (countOfAllocatedIps == 0) {
emptyVlans.add(vlan);
} else if (countOfAllocatedIps == countOfAllIps) {
fullVlans.add(vlan);
}
}
if (emptyVlans.isEmpty()) {
return null;
}
// Try to find an empty VLAN with the same tag/subnet as a VLAN that is full
for (VlanVO fullVlan : fullVlans) {
for (VlanVO emptyVlan : emptyVlans) {
if (fullVlan.getVlanTag().equals(emptyVlan.getVlanTag()) && fullVlan.getVlanGateway().equals(emptyVlan.getVlanGateway()) && fullVlan.getVlanNetmask().equals(emptyVlan.getVlanNetmask())) {
return emptyVlan;
}
}
}
// Return a random empty VLAN
return emptyVlans.get(0);
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class BigSwitchBcfUtils method getTopology.
public TopologyData getTopology(long physicalNetworkId) {
List<NetworkVO> networks;
List<NicVO> nics;
networks = _networkDao.listByPhysicalNetworkTrafficType(physicalNetworkId, TrafficType.Guest);
TopologyData topo = new TopologyData();
// handle external network first, only if NAT service is enabled
if (networks != null) {
if (!(networks.isEmpty()) && isNatEnabled()) {
// get public net info - needed to set up source nat gateway
NetworkVO pubNet = getPublicNetwork(physicalNetworkId);
// locate subnet info
SearchCriteria<VlanVO> sc = _vlanDao.createSearchCriteria();
sc.setParameters("network_id", pubNet.getId());
VlanVO vlanVO = _vlanDao.findOneBy(sc);
// add tenant external network external
TopologyData.Network network = topo.new Network();
network.setId("external");
network.setName("external");
network.setTenantId("external");
network.setTenantName("external");
String pubVlan = null;
try {
pubVlan = BroadcastDomainType.getValue(vlanVO.getVlanTag());
if (StringUtils.isNumeric(pubVlan)) {
network.setVlan(Integer.valueOf(pubVlan));
} else {
// untagged
pubVlan = "0";
}
} catch (URISyntaxException e) {
e.printStackTrace();
}
topo.addNetwork(network);
}
}
// routerMap used internally for multiple updates to same tenant's router
// add back to topo.routers after loop
HashMap<String, RouterData> routerMap = new HashMap<String, RouterData>();
for (NetworkVO netVO : networks) {
TopologyData.Network network = topo.new Network();
network.setId(netVO.getUuid());
network.setName(netVO.getName());
Integer vlan = null;
if (netVO.getBroadcastUri() != null) {
String vlanStr = BroadcastDomainType.getValue(netVO.getBroadcastUri());
if (StringUtils.isNumeric(vlanStr)) {
vlan = Integer.valueOf(vlanStr);
} else {
// untagged
vlan = 0;
}
}
network.setVlan(vlan);
network.setState(netVO.getState().name());
nics = _nicDao.listByNetworkId(netVO.getId());
List<Port> ports = new ArrayList<Port>();
String tenantId = null;
String tenantName = null;
// if VPC network, assign BCF tenant id with vpc uuid
Vpc vpc = null;
if (netVO.getVpcId() != null) {
vpc = _vpcDao.acquireInLockTable(netVO.getVpcId());
}
if (vpc != null) {
tenantId = vpc.getUuid();
tenantName = vpc.getName();
} else {
tenantId = netVO.getUuid();
tenantName = netVO.getName();
}
for (NicVO nic : nics) {
NetworkData netData = new NetworkData();
TopologyData.Port p = topo.new Port();
p.setAttachmentInfo(netData.new AttachmentInfo(nic.getUuid(), nic.getMacAddress()));
VMInstanceVO vm = _vmDao.findById(nic.getInstanceId());
HostVO host = _hostDao.findById(vm.getHostId());
// if host not found, ignore this nic
if (host == null) {
continue;
}
String hostname = host.getName();
long zoneId = netVO.getDataCenterId();
String vmwareVswitchLabel = _networkModel.getDefaultGuestTrafficLabel(zoneId, HypervisorType.VMware);
String[] labelArray = null;
String vswitchName = null;
if (vmwareVswitchLabel != null) {
labelArray = vmwareVswitchLabel.split(",");
vswitchName = labelArray[0];
}
// hypervisor type:
// kvm: ivs port name
// vmware: specific portgroup naming convention
String pgName = "";
if (host.getHypervisorType() == HypervisorType.KVM) {
pgName = hostname;
} else if (host.getHypervisorType() == HypervisorType.VMware) {
pgName = hostname + "-" + vswitchName;
}
p.setHostId(pgName);
p.setSegmentInfo(netData.new SegmentInfo(BroadcastDomainType.Vlan.name(), vlan));
p.setOwner(BigSwitchBcfApi.getCloudstackInstanceId());
List<AttachmentData.Attachment.IpAddress> ipList = new ArrayList<AttachmentData.Attachment.IpAddress>();
ipList.add(new AttachmentData().getAttachment().new IpAddress(nic.getIPv4Address()));
p.setIpAddresses(ipList);
p.setId(nic.getUuid());
p.setMac(nic.getMacAddress());
netData.getNetwork().setId(network.getId());
netData.getNetwork().setName(network.getName());
netData.getNetwork().setTenantId(tenantId);
netData.getNetwork().setTenantName(tenantName);
netData.getNetwork().setState(netVO.getState().name());
p.setNetwork(netData.getNetwork());
ports.add(p);
}
network.setTenantId(tenantId);
network.setTenantName(tenantName);
network.setPorts(ports);
topo.addNetwork(network);
// add router for network
RouterData routerData;
if (tenantId != null) {
if (!routerMap.containsKey(tenantId)) {
routerData = new RouterData(tenantId);
routerMap.put(tenantId, routerData);
} else {
routerData = routerMap.get(tenantId);
}
routerData.getRouter().getAcls().addAll(listACLbyNetwork(netVO));
if (vpc != null) {
routerData.getRouter().addExternalGateway(getPublicIpByVpc(vpc));
} else {
routerData.getRouter().addExternalGateway(getPublicIpByNetwork(netVO));
}
RouterInterfaceData intf = new RouterInterfaceData(tenantId, netVO.getGateway(), netVO.getCidr(), netVO.getUuid(), netVO.getName());
routerData.getRouter().addInterface(intf);
}
}
for (RouterData rd : routerMap.values()) {
topo.addRouter(rd.getRouter());
}
return topo;
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class BigSwitchBcfGuestNetworkGuru method implement.
@Override
public Network implement(Network network, NetworkOffering offering, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapacityException {
assert (network.getState() == State.Implementing) : "Why are we implementing " + network;
bcfUtilsInit();
long dcId = dest.getDataCenter().getId();
long physicalNetworkId = _networkModel.findPhysicalNetworkId(dcId, offering.getTags(), offering.getTrafficType());
NetworkVO implemented = new NetworkVO(network.getTrafficType(), network.getMode(), network.getBroadcastDomainType(), network.getNetworkOfferingId(), State.Allocated, network.getDataCenterId(), physicalNetworkId, false);
if (network.getGateway() != null) {
implemented.setGateway(network.getGateway());
}
if (network.getCidr() != null) {
implemented.setCidr(network.getCidr());
}
String vnetId = "";
if (network.getBroadcastUri() == null) {
vnetId = _dcDao.allocateVnet(dcId, physicalNetworkId, network.getAccountId(), context.getReservationId(), UseSystemGuestVlans.valueIn(network.getAccountId()));
if (vnetId == null) {
throw new InsufficientVirtualNetworkCapacityException("Unable to allocate vnet as a " + "part of network " + network + " implement ", DataCenter.class, dcId);
}
implemented.setBroadcastUri(BroadcastDomainType.Vlan.toUri(vnetId));
ActionEventUtils.onCompletedActionEvent(CallContext.current().getCallingUserId(), network.getAccountId(), EventVO.LEVEL_INFO, EventTypes.EVENT_ZONE_VLAN_ASSIGN, "Assigned Zone Vlan: " + vnetId + " Network Id: " + network.getId(), 0);
} else {
implemented.setBroadcastUri(network.getBroadcastUri());
}
// Name is either the given name or the uuid
String name = network.getName();
if (name == null || name.isEmpty()) {
name = ((NetworkVO) network).getUuid();
}
if (name.length() > 64) {
// max length 64
name = name.substring(0, 63);
}
// update fields in network object
NetworkVO networkObject = (NetworkVO) network;
// determine whether this is VPC network or stand-alone network
Vpc vpc = null;
if (network.getVpcId() != null) {
vpc = _vpcDao.acquireInLockTable(network.getVpcId());
}
// use uuid of networkVO as network id in BSN
String networkId = networkObject.getUuid();
String tenantId;
String tenantName;
if (vpc != null) {
tenantId = vpc.getUuid();
tenantName = vpc.getName();
_vpcDao.releaseFromLockTable(vpc.getId());
} else {
// use network in CS as tenant in BSN
// for non-VPC networks, use network name and id as tenant name and id
tenantId = networkId;
tenantName = name;
}
// store tenantId in networkObject for NetworkOrchestrator implementNetwork use
networkObject.setNetworkDomain(tenantId);
// store tenant Id in implemented object for future actions (e.g., shutdown)
implemented.setNetworkDomain(tenantId);
String vlanStr = BroadcastDomainType.getValue(implemented.getBroadcastUri());
// get public net info - needed to set up source nat gateway
NetworkVO pubNet = _bcfUtils.getPublicNetwork(physicalNetworkId);
// locate subnet info
SearchCriteria<VlanVO> sc = _vlanDao.createSearchCriteria();
sc.setParameters("network_id", pubNet.getId());
VlanVO pubVlanVO = _vlanDao.findOneBy(sc);
String pubVlanStr = pubVlanVO.getVlanTag();
Integer vlan;
if (StringUtils.isNumeric(vlanStr)) {
vlan = Integer.valueOf(vlanStr);
} else {
vlan = 0;
}
CreateBcfSegmentCommand cmd1 = new CreateBcfSegmentCommand(tenantId, tenantName, networkId, name, vlan);
_bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd1, networkObject);
if (_bcfUtils.isNatEnabled()) {
Integer pvlan;
if (StringUtils.isNumeric(pubVlanStr)) {
pvlan = Integer.valueOf(pubVlanStr);
} else {
pvlan = 0;
}
CreateBcfSegmentCommand cmd2 = new CreateBcfSegmentCommand("external", "external", "external", "external", pvlan);
_bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd2, networkObject);
CreateBcfRouterCommand cmd3 = new CreateBcfRouterCommand(tenantId);
_bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd3, network);
CreateBcfRouterInterfaceCommand cmd5 = new CreateBcfRouterInterfaceCommand(tenantId, network.getUuid(), network.getCidr(), network.getGateway(), network.getName());
_bcfUtils.sendBcfCommandWithNetworkSyncCheck(cmd5, network);
}
return implemented;
}
Aggregations