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;
}
use of com.cloud.dc.VlanVO in project cloudstack by apache.
the class ManagementServerImpl method searchForVlans.
@Override
public Pair<List<? extends Vlan>, Integer> searchForVlans(final ListVlanIpRangesCmd cmd) {
// If an account name and domain ID are specified, look up the account
final String accountName = cmd.getAccountName();
final Long domainId = cmd.getDomainId();
Long accountId = null;
final Long networkId = cmd.getNetworkId();
final Boolean forVirtual = cmd.getForVirtualNetwork();
String vlanType = null;
final Long projectId = cmd.getProjectId();
final Long physicalNetworkId = cmd.getPhysicalNetworkId();
if (accountName != null && domainId != null) {
if (projectId != null) {
throw new InvalidParameterValueException("Account and projectId can't be specified together");
}
final Account account = _accountDao.findActiveAccount(accountName, domainId);
if (account == null) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find account " + accountName + " in specified domain");
// Since we don't have a DomainVO object here, we directly set
// tablename to "domain".
final DomainVO domain = ApiDBUtils.findDomainById(domainId);
String domainUuid = domainId.toString();
if (domain != null) {
domainUuid = domain.getUuid();
}
ex.addProxyObject(domainUuid, "domainId");
throw ex;
} else {
accountId = account.getId();
}
}
if (forVirtual != null) {
if (forVirtual) {
vlanType = VlanType.VirtualNetwork.toString();
} else {
vlanType = VlanType.DirectAttached.toString();
}
}
// set project information
if (projectId != null) {
final Project project = _projectMgr.getProject(projectId);
if (project == null) {
final InvalidParameterValueException ex = new InvalidParameterValueException("Unable to find project by id " + projectId);
ex.addProxyObject(projectId.toString(), "projectId");
throw ex;
}
accountId = project.getProjectAccountId();
}
final Filter searchFilter = new Filter(VlanVO.class, "id", true, cmd.getStartIndex(), cmd.getPageSizeVal());
final Object id = cmd.getId();
final Object vlan = cmd.getVlan();
final Object dataCenterId = cmd.getZoneId();
final Object podId = cmd.getPodId();
final Object keyword = cmd.getKeyword();
final SearchBuilder<VlanVO> sb = _vlanDao.createSearchBuilder();
sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ);
sb.and("vlan", sb.entity().getVlanTag(), SearchCriteria.Op.EQ);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
sb.and("vlan", sb.entity().getVlanTag(), SearchCriteria.Op.EQ);
sb.and("networkId", sb.entity().getNetworkId(), SearchCriteria.Op.EQ);
sb.and("vlanType", sb.entity().getVlanType(), SearchCriteria.Op.EQ);
sb.and("physicalNetworkId", sb.entity().getPhysicalNetworkId(), SearchCriteria.Op.EQ);
if (accountId != null) {
final SearchBuilder<AccountVlanMapVO> accountVlanMapSearch = _accountVlanMapDao.createSearchBuilder();
accountVlanMapSearch.and("accountId", accountVlanMapSearch.entity().getAccountId(), SearchCriteria.Op.EQ);
sb.join("accountVlanMapSearch", accountVlanMapSearch, sb.entity().getId(), accountVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.INNER);
}
if (podId != null) {
final SearchBuilder<PodVlanMapVO> podVlanMapSearch = _podVlanMapDao.createSearchBuilder();
podVlanMapSearch.and("podId", podVlanMapSearch.entity().getPodId(), SearchCriteria.Op.EQ);
sb.join("podVlanMapSearch", podVlanMapSearch, sb.entity().getId(), podVlanMapSearch.entity().getVlanDbId(), JoinBuilder.JoinType.INNER);
}
final SearchCriteria<VlanVO> sc = sb.create();
if (keyword != null) {
final SearchCriteria<VlanVO> ssc = _vlanDao.createSearchCriteria();
ssc.addOr("vlanTag", SearchCriteria.Op.LIKE, "%" + keyword + "%");
ssc.addOr("ipRange", SearchCriteria.Op.LIKE, "%" + keyword + "%");
sc.addAnd("vlanTag", SearchCriteria.Op.SC, ssc);
} else {
if (id != null) {
sc.setParameters("id", id);
}
if (vlan != null) {
sc.setParameters("vlan", vlan);
}
if (dataCenterId != null) {
sc.setParameters("dataCenterId", dataCenterId);
}
if (networkId != null) {
sc.setParameters("networkId", networkId);
}
if (accountId != null) {
sc.setJoinParameters("accountVlanMapSearch", "accountId", accountId);
}
if (podId != null) {
sc.setJoinParameters("podVlanMapSearch", "podId", podId);
}
if (vlanType != null) {
sc.setParameters("vlanType", vlanType);
}
if (physicalNetworkId != null) {
sc.setParameters("physicalNetworkId", physicalNetworkId);
}
}
final Pair<List<VlanVO>, Integer> result = _vlanDao.searchAndCount(sc, searchFilter);
return new Pair<List<? extends Vlan>, Integer>(result.first(), result.second());
}
Aggregations