@Override
 public HostVO createHostVOForDirectConnectAgent(
     HostVO host,
     StartupCommand[] startup,
     ServerResource resource,
     Map<String, String> details,
     List<String> hostTags) {
   if (!(startup[0] instanceof StartupExternalFirewallCommand)) {
     return null;
   }
   host.setType(Host.Type.ExternalFirewall);
   return host;
 }
 @Override
 public HostVO createHostVOForDirectConnectAgent(
     final HostVO host,
     final StartupCommand[] startup,
     final ServerResource resource,
     final Map<String, String> details,
     final List<String> hostTags) {
   if (!(startup[0] instanceof StartupOvsCommand)) {
     return null;
   }
   host.setType(Host.Type.L2Networking);
   return host;
 }
  @Override
  public HostVO createHostVOForDirectConnectAgent(
      HostVO host,
      StartupCommand[] startup,
      ServerResource resource,
      Map<String, String> details,
      List<String> hostTags) {
    // for detecting SSVM dispatch
    StartupCommand firstCmd = startup[0];
    if (!(firstCmd instanceof StartupSecondaryStorageCommand)) {
      return null;
    }

    host.setType(com.cloud.host.Host.Type.SecondaryStorageVM);
    return host;
  }
  @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);
  }
  @Test(priority = -1)
  public void setUp() {
    ComponentContext.initComponentsLifeCycle();

    host = hostDao.findByGuid(this.getHostGuid());
    if (host != null) {
      dcId = host.getDataCenterId();
      clusterId = host.getClusterId();
      podId = host.getPodId();
      return;
    }
    // create data center
    DataCenterVO dc =
        new DataCenterVO(
            UUID.randomUUID().toString(),
            "test",
            "8.8.8.8",
            null,
            "10.0.0.1",
            null,
            "10.0.0.1/24",
            null,
            null,
            NetworkType.Basic,
            null,
            null,
            true,
            true,
            null,
            null);
    dc = dcDao.persist(dc);
    dcId = dc.getId();
    // create pod

    HostPodVO pod =
        new HostPodVO(
            UUID.randomUUID().toString(),
            dc.getId(),
            this.getHostGateway(),
            this.getHostCidr(),
            8,
            "test");
    pod = podDao.persist(pod);
    podId = pod.getId();
    // create xen cluster
    ClusterVO cluster = new ClusterVO(dc.getId(), pod.getId(), "devcloud cluster");
    cluster.setHypervisorType(HypervisorType.XenServer.toString());
    cluster.setClusterType(ClusterType.CloudManaged);
    cluster.setManagedState(ManagedState.Managed);
    cluster = clusterDao.persist(cluster);
    clusterId = cluster.getId();
    // create xen host

    host = new HostVO(this.getHostGuid());
    host.setName("devcloud xen host");
    host.setType(Host.Type.Routing);
    host.setPrivateIpAddress(this.getHostIp());
    host.setDataCenterId(dc.getId());
    host.setVersion("6.0.1");
    host.setAvailable(true);
    host.setSetup(true);
    host.setPodId(podId);
    host.setLastPinged(0);
    host.setResourceState(ResourceState.Enabled);
    host.setHypervisorType(HypervisorType.XenServer);
    host.setClusterId(cluster.getId());

    host = hostDao.persist(host);

    imageStore = new ImageStoreVO();
    imageStore.setName("test");
    imageStore.setDataCenterId(dcId);
    imageStore.setProviderName("CloudStack ImageStore Provider");
    imageStore.setRole(DataStoreRole.Image);
    imageStore.setUrl(this.getSecondaryStorage());
    imageStore.setUuid(UUID.randomUUID().toString());
    imageStore = imageStoreDao.persist(imageStore);
  }
  @Test(priority = -1)
  public void setUp() {
    ComponentContext.initComponentsLifeCycle();

    host = hostDao.findByGuid(this.getHostGuid());
    if (host != null) {
      dcId = host.getDataCenterId();
      clusterId = host.getClusterId();
      podId = host.getPodId();
      imageStore = this.imageStoreDao.findByName(imageStoreName);
    } else {
      // create data center
      DataCenterVO dc =
          new DataCenterVO(
              UUID.randomUUID().toString(),
              "test",
              "8.8.8.8",
              null,
              "10.0.0.1",
              null,
              "10.0.0.1/24",
              null,
              null,
              NetworkType.Basic,
              null,
              null,
              true,
              true,
              null,
              null);
      dc = dcDao.persist(dc);
      dcId = dc.getId();
      // create pod

      HostPodVO pod =
          new HostPodVO(
              UUID.randomUUID().toString(),
              dc.getId(),
              this.getHostGateway(),
              this.getHostCidr(),
              8,
              "test");
      pod = podDao.persist(pod);
      podId = pod.getId();
      // create xen cluster
      ClusterVO cluster = new ClusterVO(dc.getId(), pod.getId(), "devcloud cluster");
      cluster.setHypervisorType(HypervisorType.VMware.toString());
      cluster.setClusterType(ClusterType.ExternalManaged);
      cluster.setManagedState(ManagedState.Managed);
      cluster = clusterDao.persist(cluster);
      clusterId = cluster.getId();

      // setup vcenter
      ClusterDetailsVO clusterDetailVO = new ClusterDetailsVO(cluster.getId(), "url", null);
      this.clusterDetailsDao.persist(clusterDetailVO);
      clusterDetailVO = new ClusterDetailsVO(cluster.getId(), "username", null);
      this.clusterDetailsDao.persist(clusterDetailVO);
      clusterDetailVO = new ClusterDetailsVO(cluster.getId(), "password", null);
      this.clusterDetailsDao.persist(clusterDetailVO);
      // create xen host

      host = new HostVO(this.getHostGuid());
      host.setName("devcloud vmware host");
      host.setType(Host.Type.Routing);
      host.setPrivateIpAddress(this.getHostIp());
      host.setDataCenterId(dc.getId());
      host.setVersion("6.0.1");
      host.setAvailable(true);
      host.setSetup(true);
      host.setPodId(podId);
      host.setLastPinged(0);
      host.setResourceState(ResourceState.Enabled);
      host.setHypervisorType(HypervisorType.VMware);
      host.setClusterId(cluster.getId());

      host = hostDao.persist(host);

      imageStore = new ImageStoreVO();
      imageStore.setName(imageStoreName);
      imageStore.setDataCenterId(dcId);
      imageStore.setProviderName("CloudStack ImageStore Provider");
      imageStore.setRole(DataStoreRole.Image);
      imageStore.setUrl(this.getSecondaryStorage());
      imageStore.setUuid(UUID.randomUUID().toString());
      imageStore.setProtocol("nfs");
      imageStore = imageStoreDao.persist(imageStore);
    }

    image = new VMTemplateVO();
    image.setTemplateType(TemplateType.USER);
    image.setUrl(this.getTemplateUrl());
    image.setUniqueName(UUID.randomUUID().toString());
    image.setName(UUID.randomUUID().toString());
    image.setPublicTemplate(true);
    image.setFeatured(true);
    image.setRequiresHvm(true);
    image.setBits(64);
    image.setFormat(Storage.ImageFormat.VHD);
    image.setEnablePassword(true);
    image.setEnableSshKey(true);
    image.setGuestOSId(1);
    image.setBootable(true);
    image.setPrepopulate(true);
    image.setCrossZones(true);
    image.setExtractable(true);

    image = imageDataDao.persist(image);

    /*
     * TemplateDataStoreVO templateStore = new TemplateDataStoreVO();
     *
     * templateStore.setDataStoreId(imageStore.getId());
     * templateStore.setDownloadPercent(100);
     * templateStore.setDownloadState(Status.DOWNLOADED);
     * templateStore.setDownloadUrl(imageStore.getUrl());
     * templateStore.setInstallPath(this.getImageInstallPath());
     * templateStore.setTemplateId(image.getId());
     * templateStoreDao.persist(templateStore);
     */

    DataStore store = this.dataStoreMgr.getDataStore(imageStore.getId(), DataStoreRole.Image);
    TemplateInfo template = templateFactory.getTemplate(image.getId(), DataStoreRole.Image);
    DataObject templateOnStore = store.create(template);
    TemplateObjectTO to = new TemplateObjectTO();
    to.setPath(this.getImageInstallPath());
    CopyCmdAnswer answer = new CopyCmdAnswer(to);
    templateOnStore.processEvent(Event.CreateOnlyRequested);
    templateOnStore.processEvent(Event.OperationSuccessed, answer);
  }