Search in sources :

Example 1 with NuageVspResource

use of com.cloud.network.resource.NuageVspResource in project cloudstack by apache.

the class NuageVspManagerImpl method addNuageVspDevice.

@Override
public NuageVspDeviceVO addNuageVspDevice(AddNuageVspDeviceCmd cmd) {
    final NuageVspResource resource = new NuageVspResource();
    final String deviceName = Network.Provider.NuageVsp.getName();
    ExternalNetworkDeviceManager.NetworkDevice networkDevice = ExternalNetworkDeviceManager.NetworkDevice.getNetworkDevice(deviceName);
    final Long physicalNetworkId = cmd.getPhysicalNetworkId();
    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");
    }
    if (_nuageVspDao.listByPhysicalNetwork(physicalNetworkId).size() != 0) {
        throw new CloudRuntimeException("A NuageVsp device is already configured on this physical network");
    }
    // While the default VSD port is 8443, clustering via HAProxy will go over port 443 (CLOUD-58)
    int port = cmd.getPort() > 0 ? cmd.getPort() : 443;
    try {
        String apiVersion = null;
        String cmsUserPasswordBase64 = NuageVspUtil.encodePassword(cmd.getPassword());
        NuageVspResourceConfiguration resourceConfiguration = new NuageVspResourceConfiguration().guid(UUID.randomUUID().toString()).zoneId(String.valueOf(physicalNetwork.getDataCenterId())).hostName(cmd.getHostName()).cmsUser(cmd.getUserName()).cmsUserPassword(cmsUserPasswordBase64).port(String.valueOf(port)).apiVersion(NuageVspApiVersion.CURRENT.toString()).retryCount(NuageVspConstants.DEFAULT_API_RETRY_COUNT.toString()).retryInterval(NuageVspConstants.DEFAULT_API_RETRY_INTERVAL.toString()).apiRelativePath("/nuage");
        VspHost vspHost = resourceConfiguration.buildVspHost();
        NuageVspPluginClientLoader clientLoader = NuageVspPluginClientLoader.getClientLoader(vspHost);
        VspApiDefaults apiDefaults = clientLoader.getNuageVspManagerClient().getApiDefaults();
        if (StringUtils.isNotBlank(cmd.getApiVersion())) {
            if (!clientLoader.getNuageVspManagerClient().isSupportedApiVersion(cmd.getApiVersion())) {
                throw new CloudRuntimeException("Unsupported API version : " + cmd.getApiVersion());
            }
            apiVersion = cmd.getApiVersion();
        } else {
            boolean apiVersionFound = false;
            Map<NuageVspApiVersion, NuageVspApiVersion.Status> supportedVersions = clientLoader.getNuageVspManagerClient().getSupportedVersions();
            for (NuageVspApiVersion selectedVersion : supportedVersions.keySet()) {
                if (supportedVersions.get(selectedVersion) == NuageVspApiVersion.Status.CURRENT) {
                    apiVersion = selectedVersion.toString();
                    apiVersionFound = true;
                    break;
                }
            }
            if (!apiVersionFound) {
                throw new CloudRuntimeException("No supported API version found!");
            }
        }
        String retryCount = String.valueOf(MoreObjects.firstNonNull(cmd.getApiRetryCount(), apiDefaults.getRetryCount()));
        String retryInterval = String.valueOf(MoreObjects.firstNonNull(cmd.getApiRetryInterval(), apiDefaults.getRetryInterval()));
        resourceConfiguration.apiVersion(apiVersion).apiRelativePath("/nuage/api/" + apiVersion).retryCount(retryCount).retryInterval(retryInterval);
        Map<String, String> hostDetails = resourceConfiguration.build();
        resource.configure("Nuage VSD - " + cmd.getHostName(), Maps.<String, Object>newHashMap(hostDetails));
        Host host = _resourceMgr.addHost(zoneId, resource, Host.Type.L2Networking, hostDetails);
        if (host == null) {
            throw new CloudRuntimeException("Failed to add Nuage Vsp Device due to internal error.");
        }
        NuageVspDeviceVO nuageVspDevice = new NuageVspDeviceVO(host.getId(), physicalNetworkId, ntwkSvcProvider.getProviderName(), deviceName);
        _nuageVspDao.persist(nuageVspDevice);
        DetailVO detail = new DetailVO(host.getId(), "nuagevspdeviceid", String.valueOf(nuageVspDevice.getId()));
        _hostDetailsDao.persist(detail);
        NuageVspDeviceVO matchingNuageVspDevice = findMatchingNuageVspDevice(nuageVspDevice);
        String cmsId;
        if (matchingNuageVspDevice != null) {
            cmsId = findNuageVspCmsIdForDeviceOrHost(matchingNuageVspDevice.getId(), matchingNuageVspDevice.getHostId());
        } else {
            SyncNuageVspCmsIdCommand syncCmd = new SyncNuageVspCmsIdCommand(SyncType.REGISTER, null);
            SyncNuageVspCmsIdAnswer answer = (SyncNuageVspCmsIdAnswer) _agentMgr.easySend(nuageVspDevice.getHostId(), syncCmd);
            if (answer != null && answer.getSuccess()) {
                cmsId = answer.getNuageVspCmsId();
            } else {
                throw new CloudRuntimeException("Failed to register CMS ID");
            }
        }
        host = findNuageVspHost(nuageVspDevice.getHostId());
        registerNewNuageVspDevice(host.getId(), cmsId);
        resourceConfiguration.nuageVspCmsId(cmsId);
        resource.configure(cmd.getHostName(), Maps.<String, Object>newHashMap(resourceConfiguration.build()));
        if (matchingNuageVspDevice == null) {
            auditDomainsOnVsp((HostVO) host, true);
        }
        return nuageVspDevice;
    } catch (ConfigurationException e) {
        s_logger.error("Failed to configure Nuage VSD resource " + cmd.getHostName(), e);
        throw new CloudRuntimeException("Failed to configure Nuage VSD resource " + cmd.getHostName(), e);
    } catch (ExecutionException ee) {
        s_logger.error("Failed to add Nuage VSP device " + cmd.getHostName(), ee);
        throw new CloudRuntimeException("Failed to add Nuage VSP device " + cmd.getHostName(), ee);
    }
}
Also used : NuageVspResourceConfiguration(com.cloud.network.resource.NuageVspResourceConfiguration) VspApiDefaults(net.nuage.vsp.acs.client.api.model.VspApiDefaults) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NuageVspApiVersion(net.nuage.vsp.acs.client.common.NuageVspApiVersion) SyncNuageVspCmsIdAnswer(com.cloud.agent.api.sync.SyncNuageVspCmsIdAnswer) VspHost(net.nuage.vsp.acs.client.api.model.VspHost) ExecutionException(java.util.concurrent.ExecutionException) TransactionStatus(com.cloud.utils.db.TransactionStatus) Status(com.cloud.host.Status) NuageVspDeviceVO(com.cloud.network.NuageVspDeviceVO) ExternalNetworkDeviceManager(org.apache.cloudstack.network.ExternalNetworkDeviceManager) VspHost(net.nuage.vsp.acs.client.api.model.VspHost) Host(com.cloud.host.Host) NuageVspResource(com.cloud.network.resource.NuageVspResource) DetailVO(com.cloud.host.DetailVO) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) SyncNuageVspCmsIdCommand(com.cloud.agent.api.sync.SyncNuageVspCmsIdCommand) NuageVspPluginClientLoader(net.nuage.vsp.acs.client.api.NuageVspPluginClientLoader)

Example 2 with NuageVspResource

use of com.cloud.network.resource.NuageVspResource in project cloudstack by apache.

the class NuageVspManagerImpl method updateNuageVspDevice.

@Override
public NuageVspDeviceVO updateNuageVspDevice(UpdateNuageVspDeviceCmd command) {
    NuageVspResource resource = new NuageVspResource();
    final String deviceName = Network.Provider.NuageVsp.getName();
    ExternalNetworkDeviceManager.NetworkDevice networkDevice = ExternalNetworkDeviceManager.NetworkDevice.getNetworkDevice(deviceName);
    final Long physicalNetworkId = command.getPhysicalNetworkId();
    PhysicalNetworkVO physicalNetwork = _physicalNetworkDao.findById(physicalNetworkId);
    if (physicalNetwork == null) {
        throw new InvalidParameterValueException("Could not find phyical network with ID: " + physicalNetworkId);
    }
    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");
    }
    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");
    }
    HostVO nuageVspHost = null;
    NuageVspDeviceVO nuageVspDevice = null;
    List<NuageVspDeviceVO> nuageVspDevices = _nuageVspDao.listByPhysicalNetwork(physicalNetworkId);
    if (nuageVspDevices == null || nuageVspDevices.isEmpty()) {
        throw new CloudRuntimeException("Nuage VSD is not configured on physical network " + physicalNetworkId);
    } else {
        nuageVspDevice = nuageVspDevices.iterator().next();
        nuageVspHost = _hostDao.findById(nuageVspDevice.getHostId());
        _hostDao.loadDetails(nuageVspHost);
    }
    boolean resourceConfigurationChanged = false;
    NuageVspResourceConfiguration resourceConfiguration = NuageVspResourceConfiguration.fromConfiguration(nuageVspHost.getDetails());
    if (!Strings.isNullOrEmpty(command.getHostName()) && !command.getHostName().equals(resourceConfiguration.hostName())) {
        resourceConfiguration.hostName(command.getHostName());
        resourceConfigurationChanged = true;
    }
    if (!Strings.isNullOrEmpty(command.getUserName()) && !command.getUserName().equals(resourceConfiguration.cmsUser())) {
        resourceConfiguration.cmsUser(command.getUserName());
        resourceConfigurationChanged = true;
    }
    if (!Strings.isNullOrEmpty(command.getPassword())) {
        String encodedNewPassword = NuageVspUtil.encodePassword(command.getPassword());
        if (!encodedNewPassword.equals(resourceConfiguration.cmsUserPassword())) {
            resourceConfiguration.cmsUserPassword(encodedNewPassword);
            resourceConfigurationChanged = true;
        }
    }
    if (command.getPort() != null && command.getPort() != Integer.parseInt(resourceConfiguration.port())) {
        resourceConfiguration.port(String.valueOf(command.getPort()));
        resourceConfigurationChanged = true;
    }
    String apiVersion = MoreObjects.firstNonNull(command.getApiVersion(), resourceConfiguration.apiVersion());
    NuageVspApiVersion apiVersionObj = NuageVspApiVersion.fromString(apiVersion);
    NuageVspApiVersion apiVersionCurrent = null;
    try {
        apiVersionCurrent = resourceConfiguration.getApiVersion();
    } catch (ConfigurationException e) {
        throw new CloudRuntimeException("Current version is not configured correctly");
    }
    if (command.getApiVersion() != null) {
        if (apiVersionObj.compareTo(apiVersionCurrent) < 0) {
            throw new CloudRuntimeException("Downgrading is not supported");
        }
        GetApiDefaultsCommand apiDefaultsCmd = new GetApiDefaultsCommand();
        GetApiDefaultsAnswer apiDefaultsAnswer = (GetApiDefaultsAnswer) _agentMgr.easySend(nuageVspHost.getId(), apiDefaultsCmd);
        SupportedApiVersionCommand supportedApiVersionCmd = new SupportedApiVersionCommand(apiVersion);
        Answer supportedApiVersionAnswer = _agentMgr.easySend(nuageVspHost.getId(), supportedApiVersionCmd);
        if (!supportedApiVersionAnswer.getResult()) {
            throw new CloudRuntimeException("Incorrect API version: Nuage plugin only supports " + apiDefaultsAnswer.getApiDefaults().getVersion());
        }
        String apiRelativePath = "/nuage/api/" + apiVersion;
        if (!apiRelativePath.equals(resourceConfiguration.apiRelativePath())) {
            resourceConfiguration.apiVersion(apiVersion);
            resourceConfiguration.apiRelativePath(apiRelativePath);
            resourceConfigurationChanged = true;
        }
    }
    if (command.getApiRetryCount() != null && resourceConfiguration.retryCount() != null) {
        final int retryCount = Integer.parseInt(resourceConfiguration.retryCount());
        if (command.getApiRetryCount() != retryCount) {
            resourceConfiguration.retryCount(String.valueOf(command.getApiRetryCount()));
            resourceConfigurationChanged = true;
        }
    }
    if (command.getApiRetryInterval() != null && resourceConfiguration.retryInterval() != null) {
        final int apiRetryInterval = Integer.parseInt(resourceConfiguration.retryInterval());
        if (command.getApiRetryInterval() != apiRetryInterval) {
            resourceConfiguration.retryInterval(String.valueOf(command.getApiRetryInterval()));
            resourceConfigurationChanged = true;
        }
    }
    if (!resourceConfigurationChanged) {
        if (s_logger.isDebugEnabled()) {
            s_logger.debug("No change in the NuageVsp device parameters. None of the NuageVsp device parameters are modified");
        }
        return nuageVspDevice;
    }
    Map<String, String> config = resourceConfiguration.build();
    try {
        resource.validate(config);
        UpdateNuageVspDeviceCommand cmd = new UpdateNuageVspDeviceCommand(resourceConfiguration);
        Answer answer = _agentMgr.easySend(nuageVspHost.getId(), cmd);
        if (answer == null || !answer.getResult()) {
            s_logger.error("UpdateNuageVspDeviceCommand failed");
            if ((null != answer) && (null != answer.getDetails())) {
                throw new CloudRuntimeException(answer.getDetails());
            }
        }
        _hostDetailsDao.persist(nuageVspDevice.getHostId(), config);
    } catch (ConfigurationException e) {
        throw new CloudRuntimeException("Failed to update Nuage VSP device " + nuageVspDevice.getId() + " with parameters " + resourceConfiguration, e);
    }
    return nuageVspDevice;
}
Also used : NuageVspDeviceVO(com.cloud.network.NuageVspDeviceVO) NuageVspResourceConfiguration(com.cloud.network.resource.NuageVspResourceConfiguration) ExternalNetworkDeviceManager(org.apache.cloudstack.network.ExternalNetworkDeviceManager) GetApiDefaultsCommand(com.cloud.agent.api.manager.GetApiDefaultsCommand) SupportedApiVersionCommand(com.cloud.agent.api.manager.SupportedApiVersionCommand) UpdateNuageVspDeviceCommand(com.cloud.agent.api.manager.UpdateNuageVspDeviceCommand) HostVO(com.cloud.host.HostVO) NuageVspResource(com.cloud.network.resource.NuageVspResource) GetApiDefaultsAnswer(com.cloud.agent.api.manager.GetApiDefaultsAnswer) AgentControlAnswer(com.cloud.agent.api.AgentControlAnswer) Answer(com.cloud.agent.api.Answer) GetApiDefaultsAnswer(com.cloud.agent.api.manager.GetApiDefaultsAnswer) SyncNuageVspCmsIdAnswer(com.cloud.agent.api.sync.SyncNuageVspCmsIdAnswer) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ConfigurationException(javax.naming.ConfigurationException) PhysicalNetworkServiceProviderVO(com.cloud.network.dao.PhysicalNetworkServiceProviderVO) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) PhysicalNetworkVO(com.cloud.network.dao.PhysicalNetworkVO) NuageVspApiVersion(net.nuage.vsp.acs.client.common.NuageVspApiVersion)

Aggregations

SyncNuageVspCmsIdAnswer (com.cloud.agent.api.sync.SyncNuageVspCmsIdAnswer)2 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 NuageVspDeviceVO (com.cloud.network.NuageVspDeviceVO)2 PhysicalNetworkServiceProviderVO (com.cloud.network.dao.PhysicalNetworkServiceProviderVO)2 PhysicalNetworkVO (com.cloud.network.dao.PhysicalNetworkVO)2 NuageVspResource (com.cloud.network.resource.NuageVspResource)2 NuageVspResourceConfiguration (com.cloud.network.resource.NuageVspResourceConfiguration)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 ConfigurationException (javax.naming.ConfigurationException)2 NuageVspApiVersion (net.nuage.vsp.acs.client.common.NuageVspApiVersion)2 ExternalNetworkDeviceManager (org.apache.cloudstack.network.ExternalNetworkDeviceManager)2 AgentControlAnswer (com.cloud.agent.api.AgentControlAnswer)1 Answer (com.cloud.agent.api.Answer)1 GetApiDefaultsAnswer (com.cloud.agent.api.manager.GetApiDefaultsAnswer)1 GetApiDefaultsCommand (com.cloud.agent.api.manager.GetApiDefaultsCommand)1 SupportedApiVersionCommand (com.cloud.agent.api.manager.SupportedApiVersionCommand)1 UpdateNuageVspDeviceCommand (com.cloud.agent.api.manager.UpdateNuageVspDeviceCommand)1 SyncNuageVspCmsIdCommand (com.cloud.agent.api.sync.SyncNuageVspCmsIdCommand)1 DetailVO (com.cloud.host.DetailVO)1 Host (com.cloud.host.Host)1