Пример #1
0
  private List<SspClient> fetchSspClients(
      Long physicalNetworkId, Long dataCenterId, boolean enabled_only) {
    ArrayList<SspClient> clients = new ArrayList<SspClient>();

    boolean provider_found = false;
    PhysicalNetworkServiceProviderVO provider =
        _physicalNetworkServiceProviderDao.findByServiceProvider(physicalNetworkId, s_SSP_NAME);
    if (enabled_only) {
      if (provider != null && provider.getState() == State.Enabled) {
        provider_found = true;
      }
    } else {
      provider_found = true;
    }

    if (physicalNetworkId != null && provider_found) {
      SspCredentialVO credential = _sspCredentialDao.findByZone(dataCenterId);
      List<HostVO> hosts =
          _resourceMgr.listAllHostsInOneZoneByType(Host.Type.L2Networking, dataCenterId);
      for (HostVO host : hosts) {
        assert (credential != null);
        _hostDao.loadDetails(host);
        if ("v1Api".equals(host.getDetail("sspHost"))) {
          clients.add(
              new SspClient(
                  host.getDetail("url"), credential.getUsername(), credential.getPassword()));
        }
      }
    }
    if (clients.size() == 0) {
      String global_apiUrl = _configDao.getValueAndInitIfNotExist("ssp.url", "Network", null);
      String global_username =
          _configDao.getValueAndInitIfNotExist("ssp.username", "Network", null);
      String global_password =
          _configDao.getValueAndInitIfNotExist("ssp.password", "Network", null);
      if (global_apiUrl != null && global_username != null && global_password != null) {
        clients.add(new SspClient(global_apiUrl, global_username, global_password));
      }
    }
    return clients;
  }
Пример #2
0
  @Override
  public Host addSspHost(AddSspCmd cmd) {
    SspClient client = new SspClient(cmd.getUrl(), cmd.getUsername(), cmd.getPassword());
    if (!client.login()) {
      throw new CloudRuntimeException("Ssp login failed.");
    }

    long zoneId = cmd.getZoneId();
    SspCredentialVO credential = _sspCredentialDao.findByZone(zoneId);
    if (credential == null) {
      if (cmd.getUsername() == null || cmd.getPassword() == null) {
        throw new InvalidParameterValueException("Initial credential required for zone: " + zoneId);
      }
      credential = new SspCredentialVO();
      credential.setZoneId(zoneId);
      credential.setUsername(cmd.getUsername());
      credential.setPassword(cmd.getPassword());
      _sspCredentialDao.persist(credential);
    } else {
      if (cmd.getUsername() != null || cmd.getPassword() != null) {
        s_logger.warn("Tenant credential already configured for zone:" + zoneId);
      }
    }

    String tenantUuid = _sspTenantDao.findUuidByZone(zoneId);
    if (tenantUuid == null) {
      if (cmd.getTenantUuid() == null) {
        throw new InvalidParameterValueException(
            "Initial tenant uuid required for zone: " + zoneId);
      }
      SspTenantVO tenant = new SspTenantVO();
      tenant.setZoneId(zoneId);
      tenant.setUuid(cmd.getTenantUuid());
      _sspTenantDao.persist(tenant);
    } else {
      if (cmd.getTenantUuid() != null) {
        s_logger.warn("Tenant uuid already configured for zone:" + zoneId);
      }
    }

    String normalizedUrl = null;
    String hostname = null;
    try {
      URL url = new URL(cmd.getUrl());
      normalizedUrl = url.toString();
      hostname = url.getHost();
    } catch (MalformedURLException e1) {
      throw new CloudRuntimeException("Invalid url " + cmd.getUrl());
    }

    List<HostVO> hosts = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.L2Networking, zoneId);
    for (HostVO host : hosts) {
      assert (credential != null);
      _hostDao.loadDetails(host);
      if ("v1Api".equals(host.getDetail("sspHost"))) {
        if (normalizedUrl.equals(host.getDetail("url"))) {
          s_logger.warn("Ssp host already registered " + normalizedUrl);
          return host;
        }
      }
    }
    // SspHost HostVO will be created per zone and url.
    HostVO host = new HostVO(UUID.randomUUID().toString());
    host.setDataCenterId(zoneId);
    host.setType(Host.Type.L2Networking);
    host.setPrivateIpAddress(hostname); // db schema not null. It may be a name, not IP address.
    //        host.setPrivateMacAddress(""); // db schema nullable
    //        host.setPrivateNetmask(""); // db schema nullable
    host.setVersion("1"); // strange db schema not null
    host.setName(cmd.getName());

    host.setDetails(new HashMap<String, String>());
    host.setDetail("sspHost", "v1Api");
    host.setDetail("url", normalizedUrl);
    return _hostDao.persist(host);
  }