use of org.apache.cloudstack.api.response.NetworkResponse in project cloudstack by apache.
the class ListNiciraNvpDeviceNetworksCmd method execute.
// ///////////////////////////////////////////////////
// ///////////// API Implementation///////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException {
try {
List<? extends Network> networks = niciraNvpElementService.listNiciraNvpDeviceNetworks(this);
ListResponse<NetworkResponse> response = new ListResponse<NetworkResponse>();
List<NetworkResponse> networkResponses = new ArrayList<NetworkResponse>();
if (networks != null && !networks.isEmpty()) {
for (Network network : networks) {
NetworkResponse networkResponse = _responseGenerator.createNetworkResponse(ResponseView.Full, network);
networkResponses.add(networkResponse);
}
}
response.setResponses(networkResponses);
response.setResponseName(getCommandName());
setResponseObject(response);
} catch (InvalidParameterValueException invalidParamExcp) {
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, invalidParamExcp.getMessage());
} catch (CloudRuntimeException runtimeExcp) {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, runtimeExcp.getMessage());
}
}
use of org.apache.cloudstack.api.response.NetworkResponse in project cloudstack by apache.
the class MigrateNetworkCmd method execute.
@Override
public void execute() {
User callerUser = _accountService.getActiveUser(CallContext.current().getCallingUserId());
Account callerAccount = _accountService.getActiveAccountById(callerUser.getAccountId());
Network network = _networkService.getNetwork(id);
if (network == null) {
throw new InvalidParameterValueException("Couldn't find network by id");
}
Network result = _networkService.migrateGuestNetwork(getId(), getNetworkOfferingId(), callerAccount, callerUser, getResume());
if (result != null) {
NetworkResponse response = _responseGenerator.createNetworkResponse(ResponseView.Restricted, result);
response.setResponseName(getCommandName());
setResponseObject(response);
} else {
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update network");
}
}
use of org.apache.cloudstack.api.response.NetworkResponse in project cloudstack by apache.
the class ApiResponseHelper method createNetworkResponse.
@Override
public NetworkResponse createNetworkResponse(ResponseView view, Network network) {
// need to get network profile in order to retrieve dns information from
// there
NetworkProfile profile = ApiDBUtils.getNetworkProfile(network.getId());
NetworkResponse response = new NetworkResponse();
response.setId(network.getUuid());
response.setName(network.getName());
response.setDisplaytext(network.getDisplayText());
if (network.getBroadcastDomainType() != null) {
response.setBroadcastDomainType(network.getBroadcastDomainType().toString());
}
if (network.getTrafficType() != null) {
response.setTrafficType(network.getTrafficType().name());
}
if (network.getGuestType() != null) {
response.setType(network.getGuestType().toString());
}
response.setGateway(network.getGateway());
// FIXME - either set netmask or cidr
response.setCidr(network.getCidr());
if (network.getNetworkCidr() != null) {
response.setNetworkCidr((network.getNetworkCidr()));
}
// network cidr
if (network.getNetworkCidr() != null) {
response.setNetmask(NetUtils.cidr2Netmask(network.getNetworkCidr()));
}
if (((network.getCidr()) != null) && (network.getNetworkCidr() == null)) {
response.setNetmask(NetUtils.cidr2Netmask(network.getCidr()));
}
response.setIp6Gateway(network.getIp6Gateway());
response.setIp6Cidr(network.getIp6Cidr());
// create response for reserved IP ranges that can be used for
// non-cloudstack purposes
String reservation = null;
if ((network.getCidr() != null) && (NetUtils.isNetworkAWithinNetworkB(network.getCidr(), network.getNetworkCidr()))) {
String[] guestVmCidrPair = network.getCidr().split("\\/");
String[] guestCidrPair = network.getNetworkCidr().split("\\/");
Long guestVmCidrSize = Long.valueOf(guestVmCidrPair[1]);
Long guestCidrSize = Long.valueOf(guestCidrPair[1]);
String[] guestVmIpRange = NetUtils.getIpRangeFromCidr(guestVmCidrPair[0], guestVmCidrSize);
String[] guestIpRange = NetUtils.getIpRangeFromCidr(guestCidrPair[0], guestCidrSize);
long startGuestIp = NetUtils.ip2Long(guestIpRange[0]);
long endGuestIp = NetUtils.ip2Long(guestIpRange[1]);
long startVmIp = NetUtils.ip2Long(guestVmIpRange[0]);
long endVmIp = NetUtils.ip2Long(guestVmIpRange[1]);
if (startVmIp == startGuestIp && endVmIp < endGuestIp - 1) {
reservation = (NetUtils.long2Ip(endVmIp + 1) + "-" + NetUtils.long2Ip(endGuestIp));
}
if (endVmIp == endGuestIp && startVmIp > startGuestIp + 1) {
reservation = (NetUtils.long2Ip(startGuestIp) + "-" + NetUtils.long2Ip(startVmIp - 1));
}
if (startVmIp > startGuestIp + 1 && endVmIp < endGuestIp - 1) {
reservation = (NetUtils.long2Ip(startGuestIp) + "-" + NetUtils.long2Ip(startVmIp - 1) + " , " + NetUtils.long2Ip(endVmIp + 1) + "-" + NetUtils.long2Ip(endGuestIp));
}
}
response.setReservedIpRange(reservation);
// return vlan information only to Root admin
if (network.getBroadcastUri() != null && view == ResponseView.Full) {
String broadcastUri = network.getBroadcastUri().toString();
response.setBroadcastUri(broadcastUri);
String vlan = "N/A";
switch(BroadcastDomainType.getSchemeValue(network.getBroadcastUri())) {
case Vlan:
case Vxlan:
vlan = BroadcastDomainType.getValue(network.getBroadcastUri());
break;
}
// return vlan information only to Root admin
response.setVlan(vlan);
}
// return network details only to Root admin
if (view == ResponseView.Full) {
Map<String, String> details = new HashMap<>();
for (NetworkDetailVO detail : networkDetailsDao.listDetails(network.getId())) {
details.put(detail.getName(), detail.getValue());
}
response.setDetails(details);
}
DataCenter zone = ApiDBUtils.findZoneById(network.getDataCenterId());
if (zone != null) {
response.setZoneId(zone.getUuid());
response.setZoneName(zone.getName());
}
if (network.getPhysicalNetworkId() != null) {
PhysicalNetworkVO pnet = ApiDBUtils.findPhysicalNetworkById(network.getPhysicalNetworkId());
response.setPhysicalNetworkId(pnet.getUuid());
}
// populate network offering information
NetworkOffering networkOffering = ApiDBUtils.findNetworkOfferingById(network.getNetworkOfferingId());
if (networkOffering != null) {
response.setNetworkOfferingId(networkOffering.getUuid());
response.setNetworkOfferingName(networkOffering.getName());
response.setNetworkOfferingDisplayText(networkOffering.getDisplayText());
response.setNetworkOfferingConserveMode(networkOffering.isConserveMode());
response.setIsSystem(networkOffering.isSystemOnly());
response.setNetworkOfferingAvailability(networkOffering.getAvailability().toString());
response.setIsPersistent(networkOffering.isPersistent());
if (Network.GuestType.Isolated.equals(network.getGuestType())) {
response.setEgressDefaultPolicy(networkOffering.isEgressDefaultPolicy());
}
}
if (network.getAclType() != null) {
response.setAclType(network.getAclType().toString());
}
response.setDisplayNetwork(network.getDisplayNetwork());
response.setState(network.getState().toString());
response.setRestartRequired(network.isRestartRequired());
NetworkVO nw = ApiDBUtils.findNetworkById(network.getRelated());
if (nw != null) {
response.setRelated(nw.getUuid());
}
response.setNetworkDomain(network.getNetworkDomain());
response.setDns1(profile.getDns1());
response.setDns2(profile.getDns2());
// populate capability
Map<Service, Map<Capability, String>> serviceCapabilitiesMap = ApiDBUtils.getNetworkCapabilities(network.getId(), network.getDataCenterId());
Map<Service, Set<Provider>> serviceProviderMap = ApiDBUtils.listNetworkOfferingServices(network.getNetworkOfferingId());
List<ServiceResponse> serviceResponses = new ArrayList<ServiceResponse>();
if (serviceCapabilitiesMap != null) {
for (Map.Entry<Service, Map<Capability, String>> entry : serviceCapabilitiesMap.entrySet()) {
Service service = entry.getKey();
ServiceResponse serviceResponse = new ServiceResponse();
// skip gateway service
if (service == Service.Gateway) {
continue;
}
serviceResponse.setName(service.getName());
// set list of capabilities for the service
List<CapabilityResponse> capabilityResponses = new ArrayList<>();
Map<Capability, String> serviceCapabilities = entry.getValue();
if (serviceCapabilities != null) {
for (Map.Entry<Capability, String> ser_cap_entries : serviceCapabilities.entrySet()) {
Capability capability = ser_cap_entries.getKey();
String capabilityValue = ser_cap_entries.getValue();
if (Service.Lb == service && capability.getName().equals(Capability.SupportedLBIsolation.getName())) {
capabilityValue = networkOffering.isDedicatedLB() ? "dedicated" : "shared";
}
Set<String> capabilitySet = new HashSet<>(Arrays.asList(Capability.SupportedLBIsolation.getName(), Capability.SupportedSourceNatTypes.getName(), Capability.RedundantRouter.getName()));
boolean canChoose = capabilitySet.contains(capability.getName());
createCapabilityResponse(capabilityResponses, capability.getName(), capabilityValue, canChoose, "capability");
}
}
if (Service.SourceNat == service) {
// overwrite
capabilityResponses = new ArrayList<>();
createCapabilityResponse(capabilityResponses, Capability.SupportedSourceNatTypes.getName(), networkOffering.isSharedSourceNat() ? "perzone" : "peraccount", true);
createCapabilityResponse(capabilityResponses, Capability.RedundantRouter.getName(), networkOffering.isRedundantRouter() ? "true" : "false", true);
} else if (service == Service.StaticNat) {
createCapabilityResponse(capabilityResponses, Capability.ElasticIp.getName(), networkOffering.isElasticIp() ? "true" : "false", false);
createCapabilityResponse(capabilityResponses, Capability.AssociatePublicIP.getName(), networkOffering.isAssociatePublicIP() ? "true" : "false", false);
} else if (Service.Lb == service) {
createCapabilityResponse(capabilityResponses, Capability.ElasticLb.getName(), networkOffering.isElasticLb() ? "true" : "false", false);
createCapabilityResponse(capabilityResponses, Capability.InlineMode.getName(), networkOffering.isInline() ? "true" : "false", false);
}
serviceResponse.setCapabilities(capabilityResponses);
List<ProviderResponse> providers = new ArrayList<>();
for (Provider provider : serviceProviderMap.get(service)) {
if (provider != null) {
ProviderResponse providerRsp = new ProviderResponse();
providerRsp.setName(provider.getName());
providers.add(providerRsp);
}
}
serviceResponse.setProviders(providers);
serviceResponse.setObjectName("service");
serviceResponses.add(serviceResponse);
}
}
response.setServices(serviceResponses);
if (network.getAclType() == null || network.getAclType() == ACLType.Account) {
populateOwner(response, network);
} else {
// get domain from network_domain table
Pair<Long, Boolean> domainNetworkDetails = ApiDBUtils.getDomainNetworkDetails(network.getId());
if (domainNetworkDetails.first() != null) {
Domain domain = ApiDBUtils.findDomainById(domainNetworkDetails.first());
if (domain != null) {
response.setDomainId(domain.getUuid());
}
}
response.setSubdomainAccess(domainNetworkDetails.second());
}
Long dedicatedDomainId = ApiDBUtils.getDedicatedNetworkDomain(network.getId());
if (dedicatedDomainId != null) {
Domain domain = ApiDBUtils.findDomainById(dedicatedDomainId);
if (domain != null) {
response.setDomainId(domain.getUuid());
response.setDomainName(domain.getName());
}
}
response.setSpecifyIpRanges(network.getSpecifyIpRanges());
if (network.getVpcId() != null) {
Vpc vpc = ApiDBUtils.findVpcById(network.getVpcId());
if (vpc != null) {
response.setVpcId(vpc.getUuid());
response.setVpcName(vpc.getName());
}
}
response.setCanUseForDeploy(ApiDBUtils.canUseForDeploy(network));
// set tag information
List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Network, network.getId());
List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
for (ResourceTag tag : tags) {
ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
}
response.setTags(tagResponses);
response.setHasAnnotation(annotationDao.hasAnnotations(network.getUuid(), AnnotationService.EntityType.NETWORK.name(), _accountMgr.isRootAdmin(CallContext.current().getCallingAccount().getId())));
if (network.getNetworkACLId() != null) {
NetworkACL acl = ApiDBUtils.findByNetworkACLId(network.getNetworkACLId());
if (acl != null) {
response.setAclId(acl.getUuid());
response.setAclName(acl.getName());
}
}
response.setStrechedL2Subnet(network.isStrechedL2Network());
if (network.isStrechedL2Network()) {
Set<String> networkSpannedZones = new HashSet<String>();
List<VMInstanceVO> vmInstances = new ArrayList<VMInstanceVO>();
vmInstances.addAll(ApiDBUtils.listUserVMsByNetworkId(network.getId()));
vmInstances.addAll(ApiDBUtils.listDomainRoutersByNetworkId(network.getId()));
for (VirtualMachine vm : vmInstances) {
DataCenter vmZone = ApiDBUtils.findZoneById(vm.getDataCenterId());
networkSpannedZones.add(vmZone.getUuid());
}
response.setNetworkSpannedZones(networkSpannedZones);
}
response.setExternalId(network.getExternalId());
response.setRedundantRouter(network.isRedundant());
response.setCreated(network.getCreated());
Long bytesReceived = 0L;
Long bytesSent = 0L;
SearchBuilder<UserStatisticsVO> sb = userStatsDao.createSearchBuilder();
sb.and("networkId", sb.entity().getNetworkId(), Op.EQ);
SearchCriteria<UserStatisticsVO> sc = sb.create();
sc.setParameters("networkId", network.getId());
for (UserStatisticsVO stat : userStatsDao.search(sc, null)) {
bytesReceived += stat.getNetBytesReceived() + stat.getCurrentBytesReceived();
bytesSent += stat.getNetBytesSent() + stat.getCurrentBytesSent();
}
response.setBytesReceived(bytesReceived);
response.setBytesSent(bytesSent);
response.setObjectName("network");
return response;
}
use of org.apache.cloudstack.api.response.NetworkResponse in project cloudstack by apache.
the class ApiResponseHelper method createVpcResponse.
@Override
public VpcResponse createVpcResponse(ResponseView view, Vpc vpc) {
VpcResponse response = new VpcResponse();
response.setId(vpc.getUuid());
response.setName(vpc.getName());
response.setDisplayText(vpc.getDisplayText());
response.setCreated(vpc.getCreated());
response.setState(vpc.getState().name());
VpcOffering voff = ApiDBUtils.findVpcOfferingById(vpc.getVpcOfferingId());
if (voff != null) {
response.setVpcOfferingId(voff.getUuid());
response.setVpcOfferingName(voff.getName());
}
response.setCidr(vpc.getCidr());
response.setRestartRequired(vpc.isRestartRequired());
response.setNetworkDomain(vpc.getNetworkDomain());
response.setForDisplay(vpc.isDisplay());
response.setUsesDistributedRouter(vpc.usesDistributedRouter());
response.setRedundantRouter(vpc.isRedundant());
response.setRegionLevelVpc(vpc.isRegionLevelVpc());
Map<Service, Set<Provider>> serviceProviderMap = ApiDBUtils.listVpcOffServices(vpc.getVpcOfferingId());
List<ServiceResponse> serviceResponses = new ArrayList<ServiceResponse>();
for (Map.Entry<Service, Set<Provider>> entry : serviceProviderMap.entrySet()) {
Service service = entry.getKey();
Set<Provider> serviceProviders = entry.getValue();
ServiceResponse svcRsp = new ServiceResponse();
// skip gateway service
if (service == Service.Gateway) {
continue;
}
svcRsp.setName(service.getName());
List<ProviderResponse> providers = new ArrayList<ProviderResponse>();
for (Provider provider : serviceProviders) {
if (provider != null) {
ProviderResponse providerRsp = new ProviderResponse();
providerRsp.setName(provider.getName());
providers.add(providerRsp);
}
}
svcRsp.setProviders(providers);
serviceResponses.add(svcRsp);
}
List<NetworkResponse> networkResponses = new ArrayList<NetworkResponse>();
List<? extends Network> networks = ApiDBUtils.listVpcNetworks(vpc.getId());
for (Network network : networks) {
NetworkResponse ntwkRsp = createNetworkResponse(view, network);
networkResponses.add(ntwkRsp);
}
DataCenter zone = ApiDBUtils.findZoneById(vpc.getZoneId());
if (zone != null) {
response.setZoneId(zone.getUuid());
response.setZoneName(zone.getName());
}
response.setNetworks(networkResponses);
response.setServices(serviceResponses);
populateOwner(response, vpc);
// set tag information
List<? extends ResourceTag> tags = ApiDBUtils.listByResourceTypeAndId(ResourceObjectType.Vpc, vpc.getId());
List<ResourceTagResponse> tagResponses = new ArrayList<ResourceTagResponse>();
for (ResourceTag tag : tags) {
ResourceTagResponse tagResponse = createResourceTagResponse(tag, true);
CollectionUtils.addIgnoreNull(tagResponses, tagResponse);
}
response.setTags(tagResponses);
response.setHasAnnotation(annotationDao.hasAnnotations(vpc.getUuid(), AnnotationService.EntityType.VPC.name(), _accountMgr.isRootAdmin(CallContext.current().getCallingAccount().getId())));
response.setObjectName("vpc");
return response;
}
use of org.apache.cloudstack.api.response.NetworkResponse in project cloudstack by apache.
the class ListNetworksCmdByAdmin method execute.
@Override
public void execute() {
Pair<List<? extends Network>, Integer> networks = _networkService.searchForNetworks(this);
ListResponse<NetworkResponse> response = new ListResponse<NetworkResponse>();
List<NetworkResponse> networkResponses = new ArrayList<NetworkResponse>();
for (Network network : networks.first()) {
NetworkResponse networkResponse = _responseGenerator.createNetworkResponse(ResponseView.Full, network);
networkResponses.add(networkResponse);
}
response.setResponses(networkResponses, networks.second());
response.setResponseName(getCommandName());
setResponseObject(response);
}
Aggregations