use of com.cloud.network.IpAddress in project cloudstack by apache.
the class NiciraNvpElement method applyStaticNats.
/**
* From interface StaticNatServiceProvider
*/
@Override
public boolean applyStaticNats(Network network, List<? extends StaticNat> rules) throws ResourceUnavailableException {
if (!canHandle(network, Service.StaticNat)) {
return false;
}
List<NiciraNvpDeviceVO> devices = niciraNvpDao.listByPhysicalNetwork(network.getPhysicalNetworkId());
if (devices.isEmpty()) {
s_logger.error("No NiciraNvp Controller on physical network " + network.getPhysicalNetworkId());
return false;
}
NiciraNvpDeviceVO niciraNvpDevice = devices.get(0);
HostVO niciraNvpHost = hostDao.findById(niciraNvpDevice.getHostId());
NiciraNvpRouterMappingVO routermapping = niciraNvpRouterMappingDao.findByNetworkId(network.getId());
if (routermapping == null) {
s_logger.error("No logical router uuid found for network " + network.getDisplayText());
return false;
}
List<StaticNatRuleTO> staticNatRules = new ArrayList<StaticNatRuleTO>();
for (StaticNat rule : rules) {
IpAddress sourceIp = networkModel.getIp(rule.getSourceIpAddressId());
// Force the nat rule into the StaticNatRuleTO, no use making a new TO object
// we only need the source and destination ip. Unfortunately no mention if a rule
// is new.
StaticNatRuleTO ruleTO = new StaticNatRuleTO(1, sourceIp.getAddress().addr(), MIN_PORT, MAX_PORT, rule.getDestIpAddress(), MIN_PORT, MAX_PORT, "any", rule.isForRevoke(), false);
staticNatRules.add(ruleTO);
}
ConfigureStaticNatRulesOnLogicalRouterCommand cmd = new ConfigureStaticNatRulesOnLogicalRouterCommand(routermapping.getLogicalRouterUuid(), staticNatRules);
ConfigureStaticNatRulesOnLogicalRouterAnswer answer = (ConfigureStaticNatRulesOnLogicalRouterAnswer) agentMgr.easySend(niciraNvpHost.getId(), cmd);
return answer.getResult();
}
use of com.cloud.network.IpAddress in project cloudstack by apache.
the class ApiResponseHelper method createSystemVmResponse.
@Override
public SystemVmResponse createSystemVmResponse(VirtualMachine vm) {
SystemVmResponse vmResponse = new SystemVmResponse();
if (vm.getType() == Type.SecondaryStorageVm || vm.getType() == Type.ConsoleProxy || vm.getType() == Type.DomainRouter) {
// SystemVm vm = (SystemVm) systemVM;
vmResponse.setId(vm.getUuid());
// vmResponse.setObjectId(vm.getId());
vmResponse.setSystemVmType(vm.getType().toString().toLowerCase());
vmResponse.setName(vm.getHostName());
if (vm.getPodIdToDeployIn() != null) {
HostPodVO pod = ApiDBUtils.findPodById(vm.getPodIdToDeployIn());
if (pod != null) {
vmResponse.setPodId(pod.getUuid());
}
}
VMTemplateVO template = ApiDBUtils.findTemplateById(vm.getTemplateId());
if (template != null) {
vmResponse.setTemplateId(template.getUuid());
}
vmResponse.setCreated(vm.getCreated());
if (vm.getHostId() != null) {
Host host = ApiDBUtils.findHostById(vm.getHostId());
if (host != null) {
vmResponse.setHostId(host.getUuid());
vmResponse.setHostName(host.getName());
vmResponse.setHypervisor(host.getHypervisorType().toString());
}
}
if (vm.getState() != null) {
vmResponse.setState(vm.getState().toString());
}
// for console proxies, add the active sessions
if (vm.getType() == Type.ConsoleProxy) {
ConsoleProxyVO proxy = ApiDBUtils.findConsoleProxy(vm.getId());
// proxy can be already destroyed
if (proxy != null) {
vmResponse.setActiveViewerSessions(proxy.getActiveSession());
}
}
DataCenter zone = ApiDBUtils.findZoneById(vm.getDataCenterId());
if (zone != null) {
vmResponse.setZoneId(zone.getUuid());
vmResponse.setZoneName(zone.getName());
vmResponse.setDns1(zone.getDns1());
vmResponse.setDns2(zone.getDns2());
}
List<NicProfile> nicProfiles = ApiDBUtils.getNics(vm);
for (NicProfile singleNicProfile : nicProfiles) {
Network network = ApiDBUtils.findNetworkById(singleNicProfile.getNetworkId());
if (network != null) {
if (network.getTrafficType() == TrafficType.Management) {
vmResponse.setPrivateIp(singleNicProfile.getIPv4Address());
vmResponse.setPrivateMacAddress(singleNicProfile.getMacAddress());
vmResponse.setPrivateNetmask(singleNicProfile.getIPv4Netmask());
} else if (network.getTrafficType() == TrafficType.Control) {
vmResponse.setLinkLocalIp(singleNicProfile.getIPv4Address());
vmResponse.setLinkLocalMacAddress(singleNicProfile.getMacAddress());
vmResponse.setLinkLocalNetmask(singleNicProfile.getIPv4Netmask());
} else if (network.getTrafficType() == TrafficType.Public) {
vmResponse.setPublicIp(singleNicProfile.getIPv4Address());
vmResponse.setPublicMacAddress(singleNicProfile.getMacAddress());
vmResponse.setPublicNetmask(singleNicProfile.getIPv4Netmask());
vmResponse.setGateway(singleNicProfile.getIPv4Gateway());
} else if (network.getTrafficType() == TrafficType.Guest) {
/*
* In basic zone, public ip has TrafficType.Guest in case EIP service is not enabled.
* When EIP service is enabled in the basic zone, system VM by default get the public
* IP allocated for EIP. So return the guest/public IP accordingly.
* */
NetworkOffering networkOffering = ApiDBUtils.findNetworkOfferingById(network.getNetworkOfferingId());
if (networkOffering.getElasticIp()) {
IpAddress ip = ApiDBUtils.findIpByAssociatedVmId(vm.getId());
if (ip != null) {
Vlan vlan = ApiDBUtils.findVlanById(ip.getVlanId());
vmResponse.setPublicIp(ip.getAddress().addr());
vmResponse.setPublicNetmask(vlan.getVlanNetmask());
vmResponse.setGateway(vlan.getVlanGateway());
}
} else {
vmResponse.setPublicIp(singleNicProfile.getIPv4Address());
vmResponse.setPublicMacAddress(singleNicProfile.getMacAddress());
vmResponse.setPublicNetmask(singleNicProfile.getIPv4Netmask());
vmResponse.setGateway(singleNicProfile.getIPv4Gateway());
}
}
}
}
}
vmResponse.setObjectName("systemvm");
return vmResponse;
}
use of com.cloud.network.IpAddress in project cloudstack by apache.
the class ApiResponseHelper method createFirewallResponse.
@Override
public FirewallResponse createFirewallResponse(FirewallRule fwRule) {
FirewallResponse response = new FirewallResponse();
response.setId(fwRule.getUuid());
response.setProtocol(fwRule.getProtocol());
if (fwRule.getSourcePortStart() != null) {
response.setStartPort(fwRule.getSourcePortStart());
}
if (fwRule.getSourcePortEnd() != null) {
response.setEndPort(fwRule.getSourcePortEnd());
}
List<String> cidrs = ApiDBUtils.findFirewallSourceCidrs(fwRule.getId());
response.setCidrList(StringUtils.join(cidrs, ","));
if (fwRule.getTrafficType() == FirewallRule.TrafficType.Ingress) {
IpAddress ip = ApiDBUtils.findIpAddressById(fwRule.getSourceIpAddressId());
response.setPublicIpAddressId(ip.getUuid());
response.setPublicIpAddress(ip.getAddress().addr());
}
Network network = ApiDBUtils.findNetworkById(fwRule.getNetworkId());
response.setNetworkId(network.getUuid());
FirewallRule.State state = fwRule.getState();
String stateToSet = state.toString();
if (state.equals(FirewallRule.State.Revoke)) {
stateToSet = "Deleting";
}
response.setIcmpCode(fwRule.getIcmpCode());
response.setIcmpType(fwRule.getIcmpType());
response.setForDisplay(fwRule.isDisplay());
// set tag information
List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.FirewallRule, fwRule.getId());
List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
for (ResourceTag tag : tags) {
ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
}
response.setTags(tagResponses);
response.setState(stateToSet);
response.setObjectName("firewallrule");
return response;
}
use of com.cloud.network.IpAddress in project cloudstack by apache.
the class LoadBalancingRulesManagerImpl method createPublicLoadBalancerRule.
@Override
@ActionEvent(eventType = EventTypes.EVENT_LOAD_BALANCER_CREATE, eventDescription = "creating load balancer")
public LoadBalancer createPublicLoadBalancerRule(String xId, String name, String description, int srcPortStart, int srcPortEnd, int defPortStart, int defPortEnd, Long ipAddrId, String protocol, String algorithm, long networkId, long lbOwnerId, boolean openFirewall, String lbProtocol, Boolean forDisplay) throws NetworkRuleConflictException, InsufficientAddressCapacityException {
Account lbOwner = _accountMgr.getAccount(lbOwnerId);
if (srcPortStart != srcPortEnd) {
throw new InvalidParameterValueException("Port ranges are not supported by the load balancer");
}
IPAddressVO ipVO = null;
if (ipAddrId != null) {
ipVO = _ipAddressDao.findById(ipAddrId);
}
Network network = _networkModel.getNetwork(networkId);
// FIXME: breaking the dependency on ELB manager. This breaks
// functionality of ELB using virtual router
// Bug CS-15411 opened to document this
// LoadBalancer result = _elbMgr.handleCreateLoadBalancerRule(lb,
// lbOwner, lb.getNetworkId());
LoadBalancer result = null;
if (result == null) {
IpAddress systemIp = null;
NetworkOffering off = _entityMgr.findById(NetworkOffering.class, network.getNetworkOfferingId());
if (off.getElasticLb() && ipVO == null && network.getVpcId() == null) {
systemIp = _ipAddrMgr.assignSystemIp(networkId, lbOwner, true, false);
if (systemIp != null) {
ipVO = _ipAddressDao.findById(systemIp.getId());
}
}
// Validate ip address
if (ipVO == null) {
throw new InvalidParameterValueException("Unable to create load balance rule; can't find/allocate source IP");
} else if (ipVO.isOneToOneNat()) {
throw new NetworkRuleConflictException("Can't do load balance on ip address: " + ipVO.getAddress());
}
boolean performedIpAssoc = false;
try {
if (ipVO.getAssociatedWithNetworkId() == null) {
boolean assignToVpcNtwk = network.getVpcId() != null && ipVO.getVpcId() != null && ipVO.getVpcId().longValue() == network.getVpcId();
if (assignToVpcNtwk) {
// set networkId just for verification purposes
_networkModel.checkIpForService(ipVO, Service.Lb, networkId);
s_logger.debug("The ip is not associated with the VPC network id=" + networkId + " so assigning");
ipVO = _ipAddrMgr.associateIPToGuestNetwork(ipAddrId, networkId, false);
performedIpAssoc = true;
}
} else {
_networkModel.checkIpForService(ipVO, Service.Lb, null);
}
if (ipVO.getAssociatedWithNetworkId() == null) {
throw new InvalidParameterValueException("Ip address " + ipVO + " is not assigned to the network " + network);
}
result = createPublicLoadBalancer(xId, name, description, srcPortStart, defPortStart, ipVO.getId(), protocol, algorithm, openFirewall, CallContext.current(), lbProtocol, forDisplay);
} catch (Exception ex) {
s_logger.warn("Failed to create load balancer due to ", ex);
if (ex instanceof NetworkRuleConflictException) {
throw (NetworkRuleConflictException) ex;
}
if (ex instanceof InvalidParameterValueException) {
throw (InvalidParameterValueException) ex;
}
} finally {
if (result == null && systemIp != null) {
s_logger.debug("Releasing system IP address " + systemIp + " as corresponding lb rule failed to create");
_ipAddrMgr.handleSystemIpRelease(systemIp);
}
// release ip address if ipassoc was perfored
if (performedIpAssoc) {
ipVO = _ipAddressDao.findById(ipVO.getId());
_vpcMgr.unassignIPFromVpcNetwork(ipVO.getId(), networkId);
}
}
}
if (result == null) {
throw new CloudRuntimeException("Failed to create load balancer rule: " + name);
}
return result;
}
use of com.cloud.network.IpAddress in project cloudstack by apache.
the class EnableStaticNatCmd method getNetworkId.
public long getNetworkId() {
IpAddress ip = _entityMgr.findById(IpAddress.class, getIpAddressId());
Long ntwkId = null;
if (ip.getAssociatedWithNetworkId() != null) {
ntwkId = ip.getAssociatedWithNetworkId();
} else {
ntwkId = networkId;
}
// in case of portable public IP, network ID passed takes precedence
if (ip.isPortable() && networkId != null) {
ntwkId = networkId;
}
if (ntwkId == null) {
throw new InvalidParameterValueException("Unable to enable static NAT for the ipAddress id=" + ipAddressId + " as IP is not associated with any network and no networkId is passed in");
}
return ntwkId;
}
Aggregations