/*
     * If there is a VTEP configured with a given tunnel zone, it should
     * not be possible to delete the tunnel zone.
     */
    @Test
    public void testDeleteFailsIfVTEPUsingTunnelZone() {
      DtoApplication app = topology.getApplication();
      URI tunnelZonesUri = app.getTunnelZones();
      DtoTunnelZone tunnelZone = new DtoTunnelZone();
      tunnelZone.setName("tz1");
      tunnelZone.setType(TunnelZoneType.VTEP);
      tunnelZone =
          dtoResource.postAndVerifyCreated(
              tunnelZonesUri,
              MidonetMediaTypes.APPLICATION_TUNNEL_ZONE_JSON(),
              tunnelZone,
              DtoTunnelZone.class);
      DtoVtep vtep = new DtoVtep();
      vtep.setManagementIp("192.168.1.2");
      vtep.setManagementPort(6632);
      vtep.setTunnelZoneId(tunnelZone.getId());
      dtoResource.postAndVerifyCreated(
          app.getVteps(), APPLICATION_VTEP_JSON_V2(), vtep, DtoVtep.class);

      // now try to delete the tunnel zone
      dtoResource.deleteAndVerifyError(
          tunnelZone.getUri(),
          MidonetMediaTypes.APPLICATION_TUNNEL_ZONE_JSON(),
          ClientResponse.Status.CONFLICT.getStatusCode());
    }
 private void bindHostToTunnelZone(UUID hostId) {
   DtoTunnelZone tz = hostTopology.getGreTunnelZone("tz1");
   Assert.assertNotNull(tz);
   // Map a tunnel zone to a host
   DtoTunnelZoneHost mapping = new DtoTunnelZoneHost();
   mapping.setHostId(hostId);
   // Now set the ip address and the create should succeed.
   mapping.setIpAddress("192.168.100.2");
   dtoResource.postAndVerifyCreated(
       tz.getHosts(), APPLICATION_TUNNEL_ZONE_HOST_JSON(), mapping, DtoTunnelZoneHost.class);
 }
    @Before
    public void setUp()
        throws StateAccessException, InterruptedException, KeeperException, SerializationException {

      WebResource resource = resource();
      dtoResource = new DtoWebResource(resource);

      DtoHost host1 = new DtoHost();
      host1.setName("host1");

      DtoBridge bridge1 = new DtoBridge();
      bridge1.setName("bridge1-name");
      bridge1.setTenantId("tenant1-id");

      DtoRouter router1 = new DtoRouter();
      router1.setName("router1-name");
      router1.setTenantId("tenant1-id");

      DtoBridgePort bridgePort1 = new DtoBridgePort();
      DtoBridgePort bridgePort2 = new DtoBridgePort();

      DtoTunnelZone tunnelZone1 = new DtoTunnelZone();
      tunnelZone1.setName("tz1-name");

      topology =
          new Topology.Builder(dtoResource)
              .create("router1", router1)
              .create("bridge1", bridge1)
              .create("bridge1", "bridgePort1", bridgePort1)
              .create("bridge1", "bridgePort2", bridgePort2)
              .build();

      hostTopology =
          new HostTopology.Builder(dtoResource)
              .create(host1Id, host1)
              .create("tz1", tunnelZone1)
              .build();

      URI baseUri = resource().getURI();
      api = new MidonetApi(baseUri.toString());
      api.enableLogging();
    }
    @Test
    public void testCrud() throws Exception {

      DtoApplication app = topology.getApplication();
      URI tunnelZonesUri = app.getTunnelZones();

      // Get tunnel zones and verify there is none
      DtoTunnelZone tunnelZone = new DtoTunnelZone();
      tunnelZone.setName("tz1-name");
      tunnelZone =
          dtoResource.postAndVerifyCreated(
              tunnelZonesUri, APPLICATION_TUNNEL_ZONE_JSON(), tunnelZone, DtoTunnelZone.class);
      Assert.assertNotNull(tunnelZone.getId());
      Assert.assertEquals("tz1-name", tunnelZone.getName());

      // Do not allow duplicates
      DtoTunnelZone tunnelZone2 = new DtoTunnelZone();
      tunnelZone2.setName("tz1-name");
      DtoError error =
          dtoResource.postAndVerifyError(
              tunnelZonesUri, APPLICATION_TUNNEL_ZONE_JSON(), tunnelZone2, CONFLICT);
      assertErrorMatches(error, UNIQUE_TUNNEL_ZONE_NAME_TYPE);

      // There should only be one
      DtoTunnelZone[] tunnelZones =
          dtoResource.getAndVerifyOk(
              tunnelZonesUri, APPLICATION_TUNNEL_ZONE_COLLECTION_JSON(), DtoTunnelZone[].class);
      Assert.assertEquals(1, tunnelZones.length);

      // Update tunnel zone name
      tunnelZone.setName("tz1-name-updated");
      tunnelZone =
          dtoResource.putAndVerifyNoContent(
              tunnelZone.getUri(), APPLICATION_TUNNEL_ZONE_JSON(), tunnelZone, DtoTunnelZone.class);
      Assert.assertEquals("tz1-name-updated", tunnelZone.getName());

      // List and make sure that there is one
      tunnelZones =
          dtoResource.getAndVerifyOk(
              tunnelZonesUri, APPLICATION_TUNNEL_ZONE_COLLECTION_JSON(), DtoTunnelZone[].class);
      Assert.assertEquals(1, tunnelZones.length);

      // Get the tunnel zone building the URI by hand.
      DtoTunnelZone tZone =
          dtoResource.getAndVerifyOk(
              UriBuilder.fromUri(tunnelZonesUri).path(tunnelZone.getId().toString()).build(),
              APPLICATION_TUNNEL_ZONE_JSON(),
              DtoTunnelZone.class);
      Assert.assertEquals(tunnelZone.getType(), tZone.getType());
      Assert.assertEquals(tunnelZone.getName(), tZone.getName());

      // Getting a non-existent zone returns a 404.
      dtoResource.getAndVerifyNotFound(
          UriBuilder.fromUri(tunnelZonesUri).path(UUID.randomUUID().toString()).build(),
          APPLICATION_TUNNEL_ZONE_JSON());

      // Delete it
      dtoResource.deleteAndVerifyNoContent(tunnelZone.getUri(), APPLICATION_TUNNEL_ZONE_JSON());

      // list and make sure it's gone
      tunnelZones =
          dtoResource.getAndVerifyOk(
              tunnelZonesUri, APPLICATION_TUNNEL_ZONE_COLLECTION_JSON(), DtoTunnelZone[].class);
      Assert.assertEquals(0, tunnelZones.length);
    }