Search in sources :

Example 31 with DetailVO

use of com.cloud.host.DetailVO 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 DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class BigSwitchBcfElement method addBigSwitchBcfDevice.

@Override
@DB
public BigSwitchBcfDeviceVO addBigSwitchBcfDevice(AddBigSwitchBcfDeviceCmd cmd) {
    BigSwitchBcfDeviceVO newBcfDevice;
    bcfUtilsInit();
    ServerResource resource = new BigSwitchBcfResource();
    final String deviceName = BcfConstants.BIG_SWITCH_BCF.getName();
    NetworkDevice networkDevice = NetworkDevice.getNetworkDevice(deviceName);
    final Long physicalNetworkId = cmd.getPhysicalNetworkId();
    final String hostname = cmd.getHost();
    final String username = cmd.getUsername();
    final String password = cmd.getPassword();
    final Boolean nat = cmd.getNat();
    PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (physicalNetwork == null) {
        throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
    }
    long zoneId = physicalNetwork.getDataCenterId();
    final PhysicalNetworkServiceProviderVO ntwkSvcProvider = _physicalNetworkServiceProviderDao.findByServiceProvider(physicalNetwork.getId(), networkDevice.getNetworkServiceProvder());
    if (ntwkSvcProvider == null) {
        throw new CloudRuntimeException("Network Service Provider: " + networkDevice.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");
    }
    ntwkSvcProvider.setFirewallServiceProvided(true);
    ntwkSvcProvider.setGatewayServiceProvided(true);
    ntwkSvcProvider.setNetworkAclServiceProvided(true);
    ntwkSvcProvider.setSourcenatServiceProvided(true);
    ntwkSvcProvider.setStaticnatServiceProvided(true);
    if (_bigswitchBcfDao.listByPhysicalNetwork(physicalNetworkId).size() > 1) {
        throw new CloudRuntimeException("At most two BCF controllers can be configured");
    }
    DataCenterVO zone = _zoneDao.findById(physicalNetwork.getDataCenterId());
    String zoneName;
    if (zone != null) {
        zoneName = zone.getName();
    } else {
        zoneName = String.valueOf(zoneId);
    }
    Boolean natNow = _bcfUtils.isNatEnabled();
    if (!nat && natNow) {
        throw new CloudRuntimeException("NAT is enabled in existing controller. Enable NAT for new controller or remove existing controller first.");
    } else if (nat && !natNow) {
        throw new CloudRuntimeException("NAT is disabled in existing controller. Disable NAT for new controller or remove existing controller first.");
    }
    Map<String, String> params = new HashMap<String, String>();
    params.put("guid", UUID.randomUUID().toString());
    params.put("zoneId", zoneName);
    params.put("physicalNetworkId", String.valueOf(physicalNetwork.getId()));
    params.put("name", "BigSwitch Controller - " + cmd.getHost());
    params.put("hostname", cmd.getHost());
    params.put("username", username);
    params.put("password", password);
    params.put("nat", nat.toString());
    // FIXME What to do with multiple isolation types
    params.put("transportzoneisotype", physicalNetwork.getIsolationMethods().get(0).toLowerCase());
    Map<String, Object> hostdetails = new HashMap<String, Object>();
    hostdetails.putAll(params);
    try {
        resource.configure(cmd.getHost(), hostdetails);
        // store current topology in bcf resource
        TopologyData topo = _bcfUtils.getTopology(physicalNetwork.getId());
        ((BigSwitchBcfResource) resource).setTopology(topo);
        final Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.L2Networking, params);
        if (host != null) {
            newBcfDevice = Transaction.execute(new TransactionCallback<BigSwitchBcfDeviceVO>() {

                @Override
                public BigSwitchBcfDeviceVO doInTransaction(TransactionStatus status) {
                    BigSwitchBcfDeviceVO bigswitchBcfDevice = new BigSwitchBcfDeviceVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(), deviceName, hostname, username, password, nat, BigSwitchBcfApi.HASH_IGNORE);
                    _bigswitchBcfDao.persist(bigswitchBcfDevice);
                    DetailVO detail = new DetailVO(host.getId(), "bigswitchbcfdeviceid", String.valueOf(bigswitchBcfDevice.getId()));
                    _hostDetailsDao.persist(detail);
                    return bigswitchBcfDevice;
                }
            });
        } else {
            throw new CloudRuntimeException("Failed to add BigSwitch BCF Controller Device due to internal error.");
        }
    } catch (ConfigurationException e) {
        throw new CloudRuntimeException(e.getMessage());
    }
    // initial topology sync to newly added BCF controller
    HostVO bigswitchBcfHost = _hostDao.findById(newBcfDevice.getHostId());
    _bcfUtils.syncTopologyToBcfHost(bigswitchBcfHost, nat);
    return newBcfDevice;
}
Also used : DataCenterVO(com.cloud.dc.DataCenterVO) HashMap(java.util.HashMap) NetworkDevice(org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice) ServerResource(com.cloud.resource.ServerResource) TransactionStatus(com.cloud.utils.db.TransactionStatus) Host(com.cloud.host.Host) HostVO(com.cloud.host.HostVO) TransactionCallback(com.cloud.utils.db.TransactionCallback) BigSwitchBcfResource(com.cloud.network.resource.BigSwitchBcfResource) 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) TopologyData(com.cloud.network.bigswitch.TopologyData) BigSwitchBcfDeviceVO(com.cloud.network.BigSwitchBcfDeviceVO) DB(com.cloud.utils.db.DB)

Example 33 with DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class FirstFitAllocator method getHostGuestOSCategory.

protected String getHostGuestOSCategory(Host host) {
    DetailVO hostDetail = _hostDetailsDao.findDetail(host.getId(), "guest.os.category.id");
    if (hostDetail != null) {
        String guestOSCategoryIdString = hostDetail.getValue();
        long guestOSCategoryId;
        try {
            guestOSCategoryId = Long.parseLong(guestOSCategoryIdString);
        } catch (Exception e) {
            return null;
        }
        GuestOSCategoryVO guestOSCategory = _guestOSCategoryDao.findById(guestOSCategoryId);
        if (guestOSCategory != null) {
            return guestOSCategory.getName();
        } else {
            return null;
        }
    } else {
        return null;
    }
}
Also used : UserVmDetailVO(com.cloud.vm.UserVmDetailVO) DetailVO(com.cloud.host.DetailVO) GuestOSCategoryVO(com.cloud.storage.GuestOSCategoryVO) ConfigurationException(javax.naming.ConfigurationException)

Example 34 with DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class ManagementServerImpl method updateHostPassword.

@Override
@DB
public boolean updateHostPassword(final UpdateHostPasswordCmd cmd) {
    if (cmd.getHostId() == null) {
        throw new InvalidParameterValueException("You should provide an host id.");
    }
    final HostVO host = _hostDao.findById(cmd.getHostId());
    if (host.getHypervisorType() == HypervisorType.XenServer) {
        throw new InvalidParameterValueException("Single host update is not supported by XenServer hypervisors. Please try again informing the Cluster ID.");
    }
    if (!supportedHypervisors.contains(host.getHypervisorType())) {
        throw new InvalidParameterValueException("This operation is not supported for this hypervisor type");
    }
    String userNameWithoutSpaces = StringUtils.deleteWhitespace(cmd.getUsername());
    if (StringUtils.isBlank(userNameWithoutSpaces)) {
        throw new InvalidParameterValueException("Username should be non empty string");
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            if (s_logger.isDebugEnabled()) {
                s_logger.debug("Changing password for host name = " + host.getName());
            }
            // update password for this host
            final DetailVO nv = _detailsDao.findDetail(host.getId(), ApiConstants.USERNAME);
            if (nv == null) {
                final DetailVO nvu = new DetailVO(host.getId(), ApiConstants.USERNAME, userNameWithoutSpaces);
                _detailsDao.persist(nvu);
                final DetailVO nvp = new DetailVO(host.getId(), ApiConstants.PASSWORD, DBEncryptionUtil.encrypt(cmd.getPassword()));
                _detailsDao.persist(nvp);
            } else if (nv.getValue().equals(userNameWithoutSpaces)) {
                final DetailVO nvp = _detailsDao.findDetail(host.getId(), ApiConstants.PASSWORD);
                nvp.setValue(DBEncryptionUtil.encrypt(cmd.getPassword()));
                _detailsDao.persist(nvp);
            } else {
                // rollback to maintain consistency
                throw new InvalidParameterValueException("The username is not same for the hosts..");
            }
        }
    });
    return true;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DetailVO(com.cloud.host.DetailVO) UserVmDetailVO(com.cloud.vm.UserVmDetailVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) HostVO(com.cloud.host.HostVO) DB(com.cloud.utils.db.DB)

Example 35 with DetailVO

use of com.cloud.host.DetailVO in project cloudstack by apache.

the class ManagementServerImpl method updateHostsInCluster.

private boolean updateHostsInCluster(final UpdateHostPasswordCmd command) {
    // get all the hosts in this cluster
    final List<HostVO> hosts = _resourceMgr.listAllHostsInCluster(command.getClusterId());
    String userNameWithoutSpaces = StringUtils.deleteWhitespace(command.getUsername());
    if (StringUtils.isBlank(userNameWithoutSpaces)) {
        throw new InvalidParameterValueException("Username should be non empty string");
    }
    Transaction.execute(new TransactionCallbackNoReturn() {

        @Override
        public void doInTransactionWithoutResult(final TransactionStatus status) {
            for (final HostVO h : hosts) {
                if (s_logger.isDebugEnabled()) {
                    s_logger.debug("Changing password for host name = " + h.getName());
                }
                // update password for this host
                final DetailVO nv = _detailsDao.findDetail(h.getId(), ApiConstants.USERNAME);
                if (nv == null) {
                    final DetailVO nvu = new DetailVO(h.getId(), ApiConstants.USERNAME, userNameWithoutSpaces);
                    _detailsDao.persist(nvu);
                    final DetailVO nvp = new DetailVO(h.getId(), ApiConstants.PASSWORD, DBEncryptionUtil.encrypt(command.getPassword()));
                    _detailsDao.persist(nvp);
                } else if (nv.getValue().equals(userNameWithoutSpaces)) {
                    final DetailVO nvp = _detailsDao.findDetail(h.getId(), ApiConstants.PASSWORD);
                    nvp.setValue(DBEncryptionUtil.encrypt(command.getPassword()));
                    _detailsDao.persist(nvp);
                } else {
                    // rollback to maintain consistency
                    throw new InvalidParameterValueException("The username is not same for all hosts, please modify passwords for individual hosts.");
                }
            }
        }
    });
    return true;
}
Also used : InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) DetailVO(com.cloud.host.DetailVO) UserVmDetailVO(com.cloud.vm.UserVmDetailVO) TransactionStatus(com.cloud.utils.db.TransactionStatus) TransactionCallbackNoReturn(com.cloud.utils.db.TransactionCallbackNoReturn) HostVO(com.cloud.host.HostVO)

Aggregations

DetailVO (com.cloud.host.DetailVO)38 HostVO (com.cloud.host.HostVO)15 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)13 TransactionStatus (com.cloud.utils.db.TransactionStatus)13 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)13 ConfigurationException (javax.naming.ConfigurationException)13 HashMap (java.util.HashMap)12 Host (com.cloud.host.Host)9 DB (com.cloud.utils.db.DB)9 PhysicalNetworkServiceProviderVO (com.cloud.network.dao.PhysicalNetworkServiceProviderVO)8 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)8 StoragePoolHostVO (com.cloud.storage.StoragePoolHostVO)7 NetworkDevice (org.apache.cloudstack.network.ExternalNetworkDeviceManager.NetworkDevice)6 InsufficientCapacityException (com.cloud.exception.InsufficientCapacityException)5 InsufficientNetworkCapacityException (com.cloud.exception.InsufficientNetworkCapacityException)5 ResourceUnavailableException (com.cloud.exception.ResourceUnavailableException)5 ServerResource (com.cloud.resource.ServerResource)5 UnableDeleteHostException (com.cloud.resource.UnableDeleteHostException)5 GuestOSCategoryVO (com.cloud.storage.GuestOSCategoryVO)4 TransactionCallbackNoReturn (com.cloud.utils.db.TransactionCallbackNoReturn)4