Search in sources :

Example 31 with PhysicalNetworkServiceProviderVO

use of com.cloud.network.dao.PhysicalNetworkServiceProviderVO in project cloudstack by apache.

the class ExternalLoadBalancerDeviceManagerImpl method addExternalLoadBalancer.

@Override
@DB
public ExternalLoadBalancerDeviceVO addExternalLoadBalancer(long physicalNetworkId, String url, String username, String password, final String deviceName, ServerResource resource, final boolean gslbProvider, final boolean exclusiveGslbProivider, final String gslbSitePublicIp, final String gslbSitePrivateIp) {
    PhysicalNetworkVO pNetwork = null;
    final NetworkDevice ntwkDevice = NetworkDevice.getNetworkDevice(deviceName);
    long zoneId;
    if ((ntwkDevice == null) || (url == null) || (username == null) || (resource == null) || (password == null)) {
        throw new InvalidParameterValueException("Atleast one of the required parameters (url, username, password," + " server resource, zone id/physical network id) is not specified or a valid parameter.");
    }
    pNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (pNetwork == null) {
        throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
    }
    zoneId = pNetwork.getDataCenterId();
    PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(pNetwork.getId(), ntwkDevice.getNetworkServiceProvder());
    ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(pNetwork.getId(), ntwkDevice.getNetworkServiceProvder());
    if (ntwkSvcProvider == null) {
        throw new CloudRuntimeException("Network Service Provider: " + ntwkDevice.getNetworkServiceProvder() + " is not enabled in the physical network: " + physicalNetworkId + "to add this device");
    } else if (ntwkSvcProvider.getState() == PhysicalNetworkServiceProvider.State.Shutdown) {
        throw new CloudRuntimeException("Network Service Provider: " + ntwkSvcProvider.getProviderName() + " is in shutdown state in the physical network: " + physicalNetworkId + "to add this device");
    }
    if (gslbProvider) {
        ExternalLoadBalancerDeviceVO zoneGslbProvider = _externalLoadBalancerDeviceDao.findGslbServiceProvider(physicalNetworkId, ntwkDevice.getNetworkServiceProvder());
        if (zoneGslbProvider != null) {
            throw new CloudRuntimeException("There is a GSLB service provider configured in the zone alredy.");
        }
    }
    URI uri;
    try {
        uri = new URI(url);
    } catch (Exception e) {
        s_logger.debug(e);
        throw new InvalidParameterValueException(e.getMessage());
    }
    String ipAddress = uri.getHost();
    Map hostDetails = new HashMap<String, String>();
    String hostName = getExternalLoadBalancerResourceGuid(pNetwork.getId(), deviceName, ipAddress);
    hostDetails.put("name", hostName);
    hostDetails.put("guid", UUID.randomUUID().toString());
    hostDetails.put("zoneId", String.valueOf(pNetwork.getDataCenterId()));
    hostDetails.put("ip", ipAddress);
    hostDetails.put("physicalNetworkId", String.valueOf(pNetwork.getId()));
    hostDetails.put("username", username);
    hostDetails.put("password", password);
    hostDetails.put("deviceName", deviceName);
    // leave parameter validation to be part server resource configure
    Map<String, String> configParams = new HashMap<String, String>();
    UrlUtil.parseQueryParameters(uri.getQuery(), false, configParams);
    hostDetails.putAll(configParams);
    try {
        resource.configure(hostName, hostDetails);
        final Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalLoadBalancer, hostDetails);
        if (host != null) {
            final boolean dedicatedUse = (configParams.get(ApiConstants.LOAD_BALANCER_DEVICE_DEDICATED) != null) ? Boolean.parseBoolean(configParams.get(ApiConstants.LOAD_BALANCER_DEVICE_DEDICATED)) : false;
            long capacity = NumbersUtil.parseLong(configParams.get(ApiConstants.LOAD_BALANCER_DEVICE_CAPACITY), 0);
            if (capacity == 0) {
                capacity = _defaultLbCapacity;
            }
            final long capacityFinal = capacity;
            final PhysicalNetworkVO pNetworkFinal = pNetwork;
            return Transaction.execute(new TransactionCallback<ExternalLoadBalancerDeviceVO>() {

                @Override
                public ExternalLoadBalancerDeviceVO doInTransaction(TransactionStatus status) {
                    ExternalLoadBalancerDeviceVO lbDeviceVO = new ExternalLoadBalancerDeviceVO(host.getId(), pNetworkFinal.getId(), ntwkDevice.getNetworkServiceProvder(), deviceName, capacityFinal, dedicatedUse, gslbProvider);
                    if (gslbProvider) {
                        lbDeviceVO.setGslbSitePublicIP(gslbSitePublicIp);
                        lbDeviceVO.setGslbSitePrivateIP(gslbSitePrivateIp);
                        lbDeviceVO.setExclusiveGslbProvider(exclusiveGslbProivider);
                    }
                    _externalLoadBalancerDeviceDao.persist(lbDeviceVO);
                    DetailVO hostDetail = new DetailVO(host.getId(), ApiConstants.LOAD_BALANCER_DEVICE_ID, String.valueOf(lbDeviceVO.getId()));
                    _hostDetailDao.persist(hostDetail);
                    return lbDeviceVO;
                }
            });
        } else {
            throw new CloudRuntimeException("Failed to add load balancer device due to internal error.");
        }
    } catch (ConfigurationException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) NetworkDevice(org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice) TransactionStatus(com.cloud.utils.db.TransactionStatus) Host(com.cloud.host.Host) URI(java.net.URI) TransactionCallbackWithException(com.cloud.utils.db.TransactionCallbackWithException) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) URISyntaxException(java.net.URISyntaxException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) ExternalLoadBalancerDeviceVO(com.cloud.network.dao.ExternalLoadBalancerDeviceVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DetailVO(com.cloud.host.DetailVO) ConfigurationException(javax.naming.ConfigurationException) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) Map(java.util.Map) HashMap(java.util.HashMap) DB(com.cloud.utils.db.DB)

Example 32 with PhysicalNetworkServiceProviderVO

use of com.cloud.network.dao.PhysicalNetworkServiceProviderVO in project cloudstack by apache.

the class ExternalFirewallDeviceManagerImpl method addExternalFirewall.

@Override
@DB
public ExternalFirewallDeviceVO addExternalFirewall(long physicalNetworkId, String url, String username, String password, final String deviceName, ServerResource resource) {
    String guid;
    PhysicalNetworkVO pNetwork = null;
    NetworkDevice ntwkDevice = NetworkDevice.getNetworkDevice(deviceName);
    long zoneId;
    if ((ntwkDevice == null) || (url == null) || (username == null) || (resource == null) || (password == null)) {
        throw new InvalidParameterValueException("Atleast one of the required parameters (url, username, password," + " server resource, zone id/physical network id) is not specified or a valid parameter.");
    }
    pNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (pNetwork == null) {
        throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
    }
    zoneId = pNetwork.getDataCenterId();
    final PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(pNetwork.getId(), ntwkDevice.getNetworkServiceProvder());
    if (ntwkSvcProvider == null) {
        throw new CloudRuntimeException("Network Service Provider: " + ntwkDevice.getNetworkServiceProvder() + " is not enabled in the physical network: " + physicalNetworkId + "to add this device");
    } else if (ntwkSvcProvider.getState() == PhysicalNetworkServiceProvider.State.Shutdown) {
        throw new CloudRuntimeException("Network Service Provider: " + ntwkSvcProvider.getProviderName() + " is not added or in shutdown state in the physical network: " + physicalNetworkId + "to add this device");
    }
    URI uri;
    try {
        uri = new URI(url);
    } catch (Exception e) {
        s_logger.debug(e);
        throw new InvalidParameterValueException(e.getMessage());
    }
    String ipAddress = uri.getHost();
    Map hostDetails = new HashMap<String, String>();
    guid = getExternalNetworkResourceGuid(pNetwork.getId(), deviceName, ipAddress);
    hostDetails.put("name", guid);
    hostDetails.put("guid", guid);
    hostDetails.put("zoneId", String.valueOf(pNetwork.getDataCenterId()));
    hostDetails.put("ip", ipAddress);
    hostDetails.put("physicalNetworkId", String.valueOf(pNetwork.getId()));
    hostDetails.put("username", username);
    hostDetails.put("password", password);
    hostDetails.put("deviceName", deviceName);
    final Map<String, String> configParams = new HashMap<String, String>();
    UrlUtil.parseQueryParameters(uri.getQuery(), false, configParams);
    hostDetails.putAll(configParams);
    // let the server resource to do parameters validation
    try {
        resource.configure(guid, hostDetails);
    } catch (ConfigurationException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    final Host externalFirewall = _resourceMgr.addHost(zoneId, resource, Host.Type.ExternalFirewall, hostDetails);
    if (externalFirewall != null) {
        final PhysicalNetworkVO pNetworkFinal = pNetwork;
        return Transaction.execute(new TransactionCallback<ExternalFirewallDeviceVO>() {

            @Override
            public ExternalFirewallDeviceVO doInTransaction(TransactionStatus status) {
                boolean dedicatedUse = (configParams.get(ApiConstants.FIREWALL_DEVICE_DEDICATED) != null) ? Boolean.parseBoolean(configParams.get(ApiConstants.FIREWALL_DEVICE_DEDICATED)) : false;
                long capacity = NumbersUtil.parseLong(configParams.get(ApiConstants.FIREWALL_DEVICE_CAPACITY), 0);
                if (capacity == 0) {
                    capacity = _defaultFwCapacity;
                }
                ExternalFirewallDeviceVO fwDevice = new ExternalFirewallDeviceVO(externalFirewall.getId(), pNetworkFinal.getId(), ntwkSvcProvider.getProviderName(), deviceName, capacity, dedicatedUse);
                _externalFirewallDeviceDao.persist(fwDevice);
                DetailVO hostDetail = new DetailVO(externalFirewall.getId(), ApiConstants.FIREWALL_DEVICE_ID, String.valueOf(fwDevice.getId()));
                _hostDetailDao.persist(hostDetail);
                return fwDevice;
            }
        });
    } else {
        return null;
    }
}
Also used : ExternalFirewallDeviceVO(com.cloud.network.dao.ExternalFirewallDeviceVO) HashMap(java.util.HashMap) NetworkDevice(org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice) TransactionStatus(com.cloud.utils.db.TransactionStatus) Host(com.cloud.host.Host) URI(java.net.URI) InsufficientCapacityException(com.cloud.exception.InsufficientCapacityException) ResourceUnavailableException(com.cloud.exception.ResourceUnavailableException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) InsufficientNetworkCapacityException(com.cloud.exception.InsufficientNetworkCapacityException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) UnableDeleteHostException(com.cloud.resource.UnableDeleteHostException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) DetailVO(com.cloud.host.DetailVO) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) Map(java.util.Map) HashMap(java.util.HashMap) DB(com.cloud.utils.db.DB)

Example 33 with PhysicalNetworkServiceProviderVO

use of com.cloud.network.dao.PhysicalNetworkServiceProviderVO in project cloudstack by apache.

the class ExternalFirewallDeviceManagerImpl method listExternalFirewalls.

@Override
public List<Host> listExternalFirewalls(long physicalNetworkId, String deviceName) {
    List<Host> firewallHosts = new ArrayList<Host>();
    NetworkDevice fwNetworkDevice = NetworkDevice.getNetworkDevice(deviceName);
    PhysicalNetworkVO pNetwork = null;
    pNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (pNetwork == null) {
        throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
    }
    if ((pNetwork == null) || (fwNetworkDevice == null)) {
        throw new InvalidParameterValueException("Atleast one of ther required parameter physical networkId, device name is missing or invalid.");
    }
    PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(pNetwork.getId(), fwNetworkDevice.getNetworkServiceProvder());
    if (ntwkSvcProvider == null) {
        return null;
    }
    List<ExternalFirewallDeviceVO> fwDevices = _externalFirewallDeviceDao.listByPhysicalNetworkAndProvider(physicalNetworkId, ntwkSvcProvider.getProviderName());
    for (ExternalFirewallDeviceVO fwDevice : fwDevices) {
        firewallHosts.add(_hostDao.findById(fwDevice.getHostId()));
    }
    return firewallHosts;
}
Also used : ExternalFirewallDeviceVO(com.cloud.network.dao.ExternalFirewallDeviceVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) NetworkDevice(org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice) ArrayList(java.util.ArrayList) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) Host(com.cloud.host.Host)

Aggregations

PhysicalNetworkServiceProviderVO (com.cloud.network.dao.PhysicalNetworkServiceProviderVO)33 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)19 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)15 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)15 Host (com.cloud.host.Host)13 ConfigurationException (javax.naming.ConfigurationException)11 DB (com.cloud.utils.db.DB)10 HashMap (java.util.HashMap)10 TransactionStatus (com.cloud.utils.db.TransactionStatus)9 NetworkDevice (org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice)9 HostVO (com.cloud.host.HostVO)8 ServerResource (com.cloud.resource.ServerResource)8 ArrayList (java.util.ArrayList)8 DetailVO (com.cloud.host.DetailVO)7 Test (org.junit.Test)6 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)5 URI (java.net.URI)5 Map (java.util.Map)5 ActionEvent (com.cloud.event.ActionEvent)4 DataCenterVO (com.cloud.dc.DataCenterVO)3