Example usage for java.net HttpURLConnection HTTP_NOT_FOUND

List of usage examples for java.net HttpURLConnection HTTP_NOT_FOUND

Introduction

In this page you can find the example usage for java.net HttpURLConnection HTTP_NOT_FOUND.

Prototype

int HTTP_NOT_FOUND

To view the source code for java.net HttpURLConnection HTTP_NOT_FOUND.

Click Source Link

Document

HTTP Status-Code 404: Not Found.

Usage

From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java

/**
 * Test case for VBridgeInterface APIs.//from w  ww. ja  va  2 s  . c  o  m
 *
 * This method called by {@code testVBridge}.
 * This calls {@code testPortMappingAPI}.
 *
 * @param tname1    A tenant name.
 *                  Specified tenant is necessary to be configured
 *                  before method is called.
 * @param bname1    A vBridge name.
 *                  Specified vBridge is necessary to be configured
 *                  before method is called.
 * @param bname2    A vBridge name.
 *                  This vBridge is also necessary to be configured
 *                  before method is called.
 * @throws JSONException  An error occurred.
 */
private void testVBridgeInterfaceAPI(String tname1, String bname1, String bname2) throws JSONException {
    LOG.info("Starting vBridge Intergace JAX-RS client.");
    String url = VTN_BASE_URL;
    StringBuilder baseURL = new StringBuilder();
    baseURL.append(url);
    baseURL.append("default/vtns/");
    baseURL.append(tname1);
    baseURL.append("/vbridges/");

    StringBuilder baseURL2 = new StringBuilder();
    baseURL2.append(url);
    baseURL2.append("default/vtns/");
    baseURL2.append("testVtn_2");
    baseURL2.append("/vbridges/");

    String ifname = "testInterface0";
    String ifname1 = "testInterface";
    String ifname2 = "test_Interface2";
    String ifname3 = "testInterface3";
    String ifname4 = "4";
    String ifname5 = "testInterface5testinterface5tes";
    String ifname32 = "testInterface5testinterface5test";

    String bnameDummy = "bname_dummy";
    String tnameDummy = "tenant_dummy";

    String desc1 = "testDescription1";
    String desc2 = "t";
    String desc3 = String.format("%01000d", 1);

    // Test GET vBridge Interfaces in default container, expecting no
    // results
    String result = getJsonResult(baseURL + bname1 + "/interfaces");
    assertResponse(HTTP_OK);
    JSONTokener jt = new JSONTokener(result);
    JSONObject json = new JSONObject(jt);
    JSONArray vbridgeifArray = json.getJSONArray("interface");
    Assert.assertEquals(0, vbridgeifArray.length());

    // Test POST vBridge Interface1 expecting 404
    String requestBody = "{}";
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname1, HTTP_POST, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test POST vbridge interface expecting 201
    // setting vbridge Interface1
    requestBody = "{}";
    String requestUri = baseURL + bname1 + "/interfaces/" + ifname1;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // Test POST vbridge interface expecting 409
    // setting vbridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1, HTTP_POST, requestBody);
    assertResponse(HTTP_CONFLICT);

    // Test GET vbridge interface expecitng 404
    // setting dummy vtn
    result = getJsonResult(url + "default/vtns/" + tnameDummy + "/vbridges/" + bname1 + "/interfaces");
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge
    result = getJsonResult(baseURL + bnameDummy + "/interfaces");
    assertResponse(HTTP_NOT_FOUND);

    // Test GET vBridge Interfaces in default container, expecting one
    // result
    result = getJsonResult(baseURL + bname1 + "/interfaces");
    assertResponse(HTTP_OK);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vbridgeifArray = json.getJSONArray("interface");
    Assert.assertEquals(1, vbridgeifArray.length());

    // Test POST vbridge interface expecting 200
    // setting vbridge Interface2
    requestBody = "{\"description\":\"" + desc1 + "\", \"enabled\":true}";
    requestUri = baseURL + bname1 + "/interfaces/" + ifname2;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // setting vbridge Interface3
    requestBody = "{\"description\":\"" + desc2 + "\", \"enabled\":true}";
    requestUri = baseURL + bname2 + "/interfaces/" + ifname3;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    testPortMappingAPI(tname1, bname1, bname2, ifname2, ifname3);

    // Test POST vBridge Interface2, for other tenant
    requestBody = "{\"description\":\"" + desc1 + "\", \"enabled\":true}";
    requestUri = baseURL2 + bname2 + "/interfaces/" + ifname2;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // Test GET all vBridge Interfaces in default container
    result = getJsonResult(baseURL + bname1 + "/interfaces");
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vbridgeifArray = json.getJSONArray("interface");
    JSONObject vbridgeif;

    assertResponse(HTTP_OK);
    Assert.assertEquals(2, vbridgeifArray.length());

    for (int i = 0; i < vbridgeifArray.length(); i++) {
        vbridgeif = vbridgeifArray.getJSONObject(i);
        if (vbridgeif.getString("name").equals(ifname1)) {
            Assert.assertFalse(vbridgeif.has("description"));

            try {
                Assert.assertEquals("true", vbridgeif.getString("enabled"));
            } catch (JSONException expected) {
                assertThat(expected.getMessage(), is("JSONObject[\"enabled\"] not found."));
            }
            Assert.assertEquals("-1", vbridgeif.getString("state"));
            Assert.assertEquals("-1", vbridgeif.getString("entityState"));
        } else if (vbridgeif.getString("name").equals(ifname2)) {
            Assert.assertEquals(desc1, vbridgeif.getString("description"));
            Assert.assertEquals("true", vbridgeif.getString("enabled"));
            Assert.assertEquals("0", vbridgeif.getString("state"));
            Assert.assertEquals("-1", vbridgeif.getString("entityState"));
        } else if (vbridgeif.getString("name").equals(ifname3)) {
            Assert.assertEquals(desc2, vbridgeif.getString("description"));
            Assert.assertEquals("true", vbridgeif.getString("enabled"));
            Assert.assertEquals("-1", vbridgeif.getString("state"));
            Assert.assertEquals("-1", vbridgeif.getString("entityState"));
        } else {
            // Unexpected vBridge Interface name
            Assert.assertTrue(false);
        }
    }

    // Test POST vBridge Interface4
    requestBody = "{\"enabled\":false}";
    requestUri = baseURL + bname1 + "/interfaces/" + ifname4;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // Test GET vBridge Interface4
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname4);
    assertResponse(HTTP_OK);

    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    Assert.assertFalse(json.has("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test POST vBridge Interface5
    requestBody = "{\"description\":\"" + desc3 + "\"}";
    requestUri = baseURL + bname1 + "/interfaces/" + ifname5;

    // Ensure that query parameters are eliminated from Location.
    result = getJsonResult(requestUri + "?param1=1&param2=2", HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    Assert.assertEquals(requestUri, httpLocation);

    // Test POST vBridge Interface expecting 400, setting invalid value
    // for ageInterval
    requestBody = "{\"description\":\"" + desc2 + "\", \"enabled\":enabled}";
    requestUri = baseURL + bname1 + "/interfaces/" + ifname;
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST vBridge Interface expecting 404, setting dummy tenant
    requestBody = "{}";
    result = getJsonResult(url + "default/vtns/" + tnameDummy + "/vbridges/" + bname1 + "/interfaces/" + ifname,
            HTTP_POST, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test POST vBridge Interface expecting 404, setting vbridge that
    // don't exist
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname, HTTP_POST, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test POST vBridge Interface expecting 405, setting "" to vbridgeIF
    // name
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + "", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_METHOD);

    // Test POST vBridge Interface expecting 400, specifying too long
    // interface name.
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname32, HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST vBridge Interface expecting 400, specifying invalid
    // interface name which starts with "_".
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + "_ifname", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST vBridge Interface expecting 400, specifying invalid
    // interface name which includes "@".
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + "if@name", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test GET vBridge Interface expecting 404, setting vtn that don't exist
    result = getJsonResult(
            url + "default/vtns/" + tnameDummy + "/vbridges/" + bname1 + "/interfaces/" + ifname1);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET vBridge Interface expecting 404, setting vbridge that don't
    // exits
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname1);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET vBridge Interface expecting 404, setting vbridgeIF that
    // don't exits
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(ifname2, json.getString("name"));
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));
    Assert.assertEquals("0", json.getString("state"));
    Assert.assertEquals("-1", json.getString("entityState"));

    // Test PUT vBridge interface1
    requestBody = "{\"description\":\"" + desc1 + "\", \"enabled\":\"true\"}";
    String queryParameter = new QueryParameter("all", "true").getString();
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));

    // Test PUT vBridge interface1, setting description
    // (queryparameter is true)
    requestBody = "{\"description\":\"" + desc2 + "\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc2, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));

    // Test PUT vBridge interface1, setting enabled (queryparameter is true)
    requestBody = "{\"enabled\":\"false\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertFalse(json.has("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test PUT vBridge interface1, setting description and enabled
    requestBody = "{\"description\":\"" + desc3 + "\", \"enabled\":\"false\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface1
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc3, json.getString("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test PUT vBridge interface1, setting {}
    requestBody = "{}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test PUT vBridge Interface2 expecting not change
    queryParameter = new QueryParameter("all", "false").getString();
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));

    // Test PUT vBridge Interface2, setting enabled
    requestBody = "{\"enabled\":false}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test PUT vBridge Interface2, setting enabled
    requestBody = "{\"description\":\"" + desc3 + "\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc3, json.getString("description"));
    Assert.assertEquals("false", json.getString("enabled"));

    // Test PUT vBridge Interface2, setting enabled
    requestBody = "{\"description\":\"" + desc1 + "\", \"enabled\":\"true\"}";
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2 + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET vBridge Interface2
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname2);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);

    assertResponse(HTTP_OK);
    Assert.assertEquals(desc1, json.getString("description"));
    Assert.assertEquals("true", json.getString("enabled"));

    // Test PUT vBridge Interface expecting 404
    // setting dummy vtn
    result = getJsonResult(url + "default/vtns/" + tnameDummy + "/vbridges/" + bname1 + "/interfaces/" + ifname1
            + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge
    result = getJsonResult(baseURL + bnameDummy + "/interfaces/" + ifname1 + queryParameter, HTTP_PUT,
            requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // setting  dummy vbridgeIF
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname + queryParameter, HTTP_PUT, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET all vBridge Interfaces in default container
    result = getJsonResult(baseURL + bname1 + "/interfaces");
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vbridgeifArray = json.getJSONArray("interface");

    assertResponse(HTTP_OK);
    Assert.assertEquals(4, vbridgeifArray.length());

    // Test DELETE expecting 200
    // delete vBridge Interface2 on other tenant
    result = getJsonResult(baseURL2 + bname2 + "/interfaces/" + ifname2, HTTP_DELETE);
    assertResponse(HTTP_OK);

    // specify not supported Content-Type
    requestBody = "{}";
    result = getJsonResult(baseURL + bname2 + "/interfaces/" + ifname2, HTTP_POST, requestBody, "text/plain");
    assertResponse(HTTP_UNSUPPORTED_TYPE);

    requestBody = "{}";
    result = getJsonResult(baseURL + bname2 + "/interfaces/" + ifname3 + queryParameter, HTTP_PUT, requestBody,
            "text/plain");
    assertResponse(HTTP_UNSUPPORTED_TYPE);

    // Authentication failure.
    checkVBridgeInterfaceError(GlobalConstants.DEFAULT.toString(), tname1, bname1, bname2, ifname3, ifname2,
            false, HttpURLConnection.HTTP_UNAUTHORIZED);

    // Invalid container name.
    String[] invalid = { "bad_container", "version" };
    for (String inv : invalid) {
        checkVBridgeInterfaceError(inv, tname1, bname1, bname2, ifname3, ifname2, true,
                HttpURLConnection.HTTP_NOT_FOUND);
        checkVBridgeInterfaceError(inv, tname1, bname1, bname2, ifname3, ifname2, false,
                HttpURLConnection.HTTP_UNAUTHORIZED);
    }

    // delete  vBridge Interface3
    result = getJsonResult(baseURL + bname2 + "/interfaces/" + ifname3, HTTP_DELETE);
    assertResponse(HTTP_OK);

    // delete vBridge Interface4
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname4, HTTP_DELETE);
    assertResponse(HTTP_OK);

    // delete vBridge Interface5
    result = getJsonResult(baseURL + bname1 + "/interfaces/" + ifname5, HTTP_DELETE);
    assertResponse(HTTP_OK);
}

From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java

/**
 * Test case for Port Mapping APIs.//from  ww  w  .j av a2 s  .c om
 *
 * This method is called by {@code testVBridgeInterfaceAPI.}
 *
 * @param tname     A tenant name.
 *                  Specified tenant is necessary to be configured
 *                  before method is called.
 * @param bname     A vBridge name.
 *                  Specified vBridge is necessary to be configured
 *                  before method is called.
 * @param ifname    A vBridgeInterface name.
 *                  Specified vBridgeInterface is necessary to be configured
 *                  before method is called.
 * @param ifname2   A vBridgeInterface name.
 *                  This interface is also necessary to be configured
 *                  before method is called.
 * @throws JSONException  An error occurred.
 */
private void testPortMappingAPI(String tname, String bname, String bname2, String ifname, String ifname2)
        throws JSONException {
    LOG.info("Starting Port Mapping JAX-RS client.");
    String url = VTN_BASE_URL;
    StringBuilder baseURL = new StringBuilder();
    baseURL.append(url);
    baseURL.append("default/vtns/");
    baseURL.append(tname);
    baseURL.append("/vbridges/");
    baseURL.append(bname);
    baseURL.append("/interfaces/");

    String vlan0 = "0";
    String vlan1 = "100";
    String vlan2 = "4095";
    String vlanOver = "4096";
    String vlanNegative = "-10";

    String pname = "testPortname";

    String ifnameDummy = "ifname_dummy";
    String tenantDummy = "tenant_dummy";
    String bnameDummy = "bname_dummy";

    String nodeid = "00:00:00:00:00:00:00:01";
    String nodeType = "OF";
    String portnum = "1";

    String test = "ERROR_TEST";

    // Tset GET PortMapping expecting 204
    // Test GET PortMapping in default container, expecting no results
    String result = getJsonResult(baseURL + ifname + "/portmap");
    assertResponse(HTTP_NO_CONTENT);

    // Test PUT PortMapping expecting 400
    // setting invalid value to requestBody
    String requestBody = "{\"description\":\", \"enabled\":\"true\"}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test PUT PortMapping expecting 400, specifying too large VLAN ID.
    requestBody = "{\"vlan\":" + vlanOver + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying a negative VLAN ID.
    requestBody = "{\"vlan\":" + vlanNegative + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying invalid node type.
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + test + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Without specifying node element.
    requestBody = "{\"vlan\":" + vlan1 + ", \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType
            + "\", \"id\":\"" + portnum + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Without specifying port element.
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying incomplete port element.
    //   - "id" is specified without "type".
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"id\":\"" + portnum + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying incomplete port element.
    //   - "name" and "id" are specified without "type",
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"type\":\"" + nodeType + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying invalid port element.
    //   - Invalid port type with specifying "name".
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + "" + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying invalid node which does not contain node type.
    requestBody = "{\"vlan\":" + vlan0 + ", \"node\":{\"id\":\"" + nodeid + "\"}, \"port\":{\"name\":\"" + pname
            + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum + "\"}}";
    result = getJsonResult(
            url + "default/vtns/" + tname + "/vbridges/" + bname2 + "/interfaces/" + ifname2 + "/portmap/",
            HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying invalid node which does not contain node ID.
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\"}, \"port\":{\"name\":\""
            + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum + "\"}}";
    result = getJsonResult(
            url + "default/vtns/" + tname + "/vbridges/" + bname2 + "/interfaces/" + ifname2 + "/portmap/",
            HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying invalid port which does not contain port type.
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"id\":\"" + portnum + "\"}}";
    result = getJsonResult(
            url + "default/vtns/" + tname + "/vbridges/" + bname2 + "/interfaces/" + ifname2 + "/portmap/",
            HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying invalid port which does not contain port ID.
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\"}}";
    result = getJsonResult(
            url + "default/vtns/" + tname + "/vbridges/" + bname2 + "/interfaces/" + ifname2 + "/portmap/",
            HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test PUT PortMapping 400
    // setting invalid value to node id
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + test
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test PUT PortMappint expecting 404
    // setting dummy vtn
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(
            url + "default/vtns/" + tenantDummy + "/vbridges/" + bname + "/interfaces/" + ifname + "/portmap/",
            HTTP_PUT, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vBridge
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(url + "default/vtns/" + tenantDummy + "/vbridges/" + bnameDummy + "/interfaces/"
            + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vBridge interface
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(baseURL + ifnameDummy + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // specfiy not supported Content-Type
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody, "text/plain");
    assertResponse(HTTP_UNSUPPORTED_TYPE);

    // Authentication failure.
    checkPortMapError(GlobalConstants.DEFAULT.toString(), tname, bname, ifname, false,
            HttpURLConnection.HTTP_UNAUTHORIZED);

    // Invalid container name.
    String[] invalid = { "bad_container", "version" };
    for (String inv : invalid) {
        checkPortMapError(inv, tname, bname, ifname, true, HttpURLConnection.HTTP_NOT_FOUND);
        checkPortMapError(inv, tname, bname, ifname, false, HttpURLConnection.HTTP_UNAUTHORIZED);
    }

    // Test PUT PortMapping expecting 200
    // Test PUT PortMapping
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // setting port element without port id and  port type
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test PUT PortMapping, change vlan value
    requestBody = "{\"vlan\":" + vlan2 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(baseURL + ifname + "/portmap/", HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test PUT PortMapping expecting 409
    // Test PUT PortMapping for other vbridge
    requestBody = "{\"vlan\":" + vlan2 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"name\":\"" + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum
            + "\"}}";
    result = getJsonResult(
            url + "default/vtns/" + tname + "/vbridges/" + bname2 + "/interfaces/" + ifname2 + "/portmap/",
            HTTP_PUT, requestBody);

    // when physical port doesn't exist, conflict mapping succeeds.
    assertResponse(HTTP_OK);

    // Test PUT PortMapping expecting 200
    // Test PUT PortMapping for other vbridge and except port name
    requestBody = "{\"vlan\":" + vlan1 + ", \"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid
            + "\"}, \"port\":{\"type\":\"" + nodeType + "\", \"id\":\"" + portnum + "\"}}";
    result = getJsonResult(
            url + "default/vtns/" + tname + "/vbridges/" + bname2 + "/interfaces/" + ifname2 + "/portmap/",
            HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test PUT PortMapping, except vlan
    requestBody = "{\"node\":{\"type\":\"" + nodeType + "\", \"id\":\"" + nodeid + "\"}, \"port\":{\"name\":\""
            + pname + "\", \"type\":\"" + nodeType + "\", \"id\":\"" + portnum + "\"}}";
    result = getJsonResult(
            url + "default/vtns/" + tname + "/vbridges/" + bname2 + "/interfaces/" + ifname2 + "/portmap/",
            HTTP_PUT, requestBody);
    assertResponse(HTTP_OK);

    // Test GET PortMapping from bname2
    result = getJsonResult(
            url + "default/vtns/" + tname + "/vbridges/" + bname2 + "/interfaces/" + ifname2 + "/portmap");
    assertResponse(HTTP_OK);
    JSONTokener jt = new JSONTokener(result);
    JSONObject json = new JSONObject(jt);

    Assert.assertEquals(vlan0, json.getString("vlan"));

    // Test GET PortMapping expecting 404
    // setting dummy vtn
    result = getJsonResult(
            url + "default/vtns/" + tenantDummy + "/vbridges/" + bname + "/interfaces/" + ifname + "/portmap");
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge
    result = getJsonResult(url + "default/vtns/" + tenantDummy + "/vbridges/" + bnameDummy + "/interfaces/"
            + ifname + "/portmap");
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge interface
    result = getJsonResult(baseURL + ifnameDummy + "/portmap");
    assertResponse(HTTP_NOT_FOUND);

    // Test GET PortMapping expecting 200
    // Test GET PortMapping
    result = getJsonResult(baseURL + ifname + "/portmap");
    assertResponse(HTTP_OK);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    JSONObject nodeinfo = json.getJSONObject("node");
    JSONObject portinfo = json.getJSONObject("port");

    Assert.assertEquals(vlan2, json.getString("vlan"));
    Assert.assertEquals(nodeType, nodeinfo.getString("type"));
    Assert.assertEquals(nodeid, nodeinfo.getString("id"));
    Assert.assertEquals(pname, portinfo.getString("name"));
    Assert.assertEquals(nodeType, portinfo.getString("type"));
    Assert.assertEquals(portnum, portinfo.getString("id"));

    if (!portinfo.getString("type").equals(nodeType)) {
        JSONObject mapinfo = json.getJSONObject("mapped");
        Assert.assertEquals(nodeType, mapinfo.getString("type"));
        Assert.assertEquals(portnum, mapinfo.getString("id"));
    }
}

From source file:org.openecomp.sdnc.sli.aai.AAIService.java

@Override
protected boolean deleteRelationshipList(URL httpReqUrl, String json_text) throws AAIServiceException {
    if (httpReqUrl == null) {
        throw new NullPointerException();
    }/*from w  w  w .  java  2  s  . com*/

    boolean response = false;
    InputStream inputStream = null;

    try {
        HttpURLConnection con = getConfiguredConnection(httpReqUrl, HttpMethod.DELETE);

        //            SSLSocketFactory sockFact = CTX.getSocketFactory();
        //            con.setSSLSocketFactory( sockFact );
        OutputStreamWriter osw = new OutputStreamWriter(con.getOutputStream());
        osw.write(json_text);
        osw.flush();
        osw.close();

        LOGwriteFirstTrace("DELETE", httpReqUrl.toString());
        LOGwriteDateTrace("data", json_text);

        // Check for errors
        int responseCode = con.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            inputStream = con.getInputStream();
        } else {
            inputStream = con.getErrorStream();
        }

        // Process the response
        LOG.debug("HttpURLConnection result:" + responseCode);
        if (inputStream == null)
            inputStream = new ByteArrayInputStream("".getBytes(StandardCharsets.UTF_8));
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        String line = null;

        ObjectMapper mapper = getObjectMapper();

        if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
            StringBuilder stringBuilder = new StringBuilder();

            while ((line = reader.readLine()) != null) {
                stringBuilder.append(line);
            }
            LOGwriteEndingTrace(responseCode, "SUCCESS", stringBuilder.toString());
            response = true;
        } else if (responseCode == HttpURLConnection.HTTP_NOT_FOUND) {
            LOGwriteEndingTrace(responseCode, "HTTP_NOT_FOUND", "Entry does not exist.");
            response = false;
        } else {
            ErrorResponse errorresponse = mapper.readValue(reader, ErrorResponse.class);
            LOGwriteEndingTrace(responseCode, "FAILURE", mapper.writeValueAsString(errorresponse));
            throw new AAIServiceException(responseCode, errorresponse);
        }

    } catch (AAIServiceException aaiexc) {
        throw aaiexc;
    } catch (Exception exc) {
        LOG.warn("deleteRelationshipList", exc);
        throw new AAIServiceException(exc);
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            } catch (Exception exc) {

            }
        }
    }
    return response;
}

From source file:jenkins.model.Jenkins.java

/**
 * @since 1.509.1/*w w  w. j a v a 2  s.co m*/
 */
public static void _doScript(StaplerRequest req, StaplerResponse rsp, RequestDispatcher view,
        VirtualChannel channel, ACL acl) throws IOException, ServletException {
    // ability to run arbitrary script is dangerous
    acl.checkPermission(RUN_SCRIPTS);

    String text = req.getParameter("script");
    if (text != null) {
        if (!"POST".equals(req.getMethod())) {
            throw HttpResponses.error(HttpURLConnection.HTTP_BAD_METHOD, "requires POST");
        }

        if (channel == null) {
            throw HttpResponses.error(HttpURLConnection.HTTP_NOT_FOUND, "Node is offline");
        }

        try {
            req.setAttribute("output", RemotingDiagnostics.executeGroovy(text, channel));
        } catch (InterruptedException e) {
            throw new ServletException(e);
        }
    }

    view.forward(req, rsp);
}

From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java

/**
 * Test case for VLAN Mapping APIs./* w ww. java  2  s  . c om*/
 *
 * <p>
 *   This method is called by {@link #testVBridgeAPI(String, String)}.
 * </p>
 *
 * @param tname     A tenant name.
 *                  Specified tenant is necessary to be configured
 *                  before method is called.
 * @param bname     A vBridge name.
 *                  Specified vBridge is necessary to be configured
 *                  before method is called.
 * @throws JSONException  An error occurred.
 */
private void testVLANMappingAPI(String tname, String bname) throws JSONException {
    LOG.info("Starting VLAN Mapping JAX-RS client.");
    String url = VTN_BASE_URL;
    StringBuilder baseURL = new StringBuilder();
    baseURL.append(url);
    baseURL.append("default/vtns/");
    baseURL.append(tname);
    baseURL.append("/vbridges/");

    String nodeid1 = "00:00:00:00:00:00:00:01";
    String nodeid2 = "00:00:00:00:00:00:00:02";

    String vlan0 = "0";
    String vlan1 = "1000";
    String vlan2 = "4095";
    String vlan3 = "2000";
    String vlan = "10";

    String vlanNegative = "-100";
    String vlanOver = "4096";

    String nodeType = "OF";

    String tnameDummy = "tnameDummy";
    String bnameDummy = "bname_dummy";

    String bname2 = "vbridge_Name2";

    // Test GET VLAN Mapping
    String result = getJsonResult(baseURL + bname + "/vlanmaps");
    assertResponse(HTTP_OK);
    JSONTokener jt = new JSONTokener(result);
    JSONObject json = new JSONObject(jt);
    JSONArray vLANMapArray = json.getJSONArray("vlanmap");
    Assert.assertEquals(0, vLANMapArray.length());

    // Test GET VLAN Mapping expecting 404, setting dummy vtn
    String searchByConf = "/vlanmapsearch/byconf";
    String badTenant = url + "default/vtns/" + tnameDummy + "/vbridges/" + bname;
    result = getJsonResult(badTenant + "/vlanmaps");
    assertResponse(HTTP_NOT_FOUND);
    result = getJsonResult(badTenant + searchByConf);
    assertResponse(HTTP_NOT_FOUND);
    result = getJsonResult(badTenant + searchByConf + "?vlan=1");
    assertResponse(HTTP_NOT_FOUND);

    // Test GET VLAN Mapping expecting 404, setting dummy vbridge
    result = getJsonResult(baseURL + bnameDummy + "/vlanmaps");
    assertResponse(HTTP_NOT_FOUND);
    result = getJsonResult(baseURL + bnameDummy + searchByConf);
    assertResponse(HTTP_NOT_FOUND);
    result = getJsonResult(baseURL + bnameDummy + searchByConf + "?vlan=1");
    assertResponse(HTTP_NOT_FOUND);

    // Specifying malformed VLAN ID and node.
    String searchUrl = baseURL + bname + searchByConf;
    result = getJsonResult(searchUrl + "?vlan=0x123");
    assertResponse(HTTP_BAD_REQUEST);
    result = getJsonResult(searchUrl + "?node=InvalidNode");
    assertResponse(HTTP_BAD_REQUEST);

    // Specifying VLAN ID and node which don't exist.
    result = getJsonResult(searchUrl + "?vlan=0");
    assertResponse(HTTP_NOT_FOUND);
    result = getJsonResult(searchUrl + "?node=OF|12345");
    assertResponse(HTTP_NOT_FOUND);
    result = getJsonResult(searchUrl + "?vlan=0&node=OF|1000");
    assertResponse(HTTP_NOT_FOUND);

    // Test POST VLAN Mapping expecting 404, setting dummy vbridge
    String requestBody = "{\"vlan\":\"" + vlan1 + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\""
            + nodeid1 + "\"}}";
    result = getJsonResult(url + "default/vtns/" + tnameDummy + "/vbridges/" + bname + "/vlanmaps", HTTP_POST,
            requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test POST VLAN Mapping expecting 404, setting dummy vbridge
    result = getJsonResult(baseURL + bnameDummy + "/vlanmaps", HTTP_POST, requestBody);
    assertResponse(HTTP_NOT_FOUND);

    // Test POST VLAN Mapping expecting 400
    // Specifying a negative VLAN ID.
    requestBody = "{\"vlan\":\"" + vlanNegative + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\""
            + nodeid1 + "\"}}";
    result = getJsonResult(baseURL + bname + "/vlanmaps", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifyin too large VLAN ID.
    requestBody = "{\"vlan\":\"" + vlanOver + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + nodeid1
            + "\"}}";
    result = getJsonResult(baseURL + bname + "/vlanmaps", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Specifyin invalid node type.
    requestBody = "{\"vlan\":\"" + vlan + "\",\"node\":{\"type\":\"" + "ERROR_TEST" + "\",\"id\":\"" + nodeid1
            + "\"}}";
    result = getJsonResult(baseURL + bname + "/vlanmaps", HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST VLAN Mapping
    requestBody = "{\"vlan\":\"" + vlan1 + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + nodeid1
            + "\"}}";
    String requestUri = baseURL + bname + "/vlanmaps";
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    String loc = requestUri + "/" + nodeType + "-" + nodeid1 + "." + vlan1;
    Assert.assertEquals(loc, httpLocation);

    // Test POST VLAN Mapping expecting 409
    requestBody = "{\"vlan\":\"" + vlan1 + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + nodeid1
            + "\"}}";
    result = getJsonResult(baseURL + bname + "/vlanmaps", HTTP_POST, requestBody);
    assertResponse(HTTP_CONFLICT);

    // Test GET VLAN Mapping
    result = getJsonResult(baseURL + bname + "/vlanmaps");
    assertResponse(HTTP_OK);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vLANMapArray = json.getJSONArray("vlanmap");
    Assert.assertEquals(1, vLANMapArray.length());

    // Test POST VLAN Mapping
    requestBody = "{\"vlan\":\"" + vlan2 + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + nodeid2
            + "\"}}";
    result = getJsonResult(baseURL + bname + "/vlanmaps", HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    loc = requestUri + "/" + nodeType + "-" + nodeid2 + "." + vlan2;
    Assert.assertEquals(loc, httpLocation);

    // Test GET all  VLAN Mapping in default container
    result = getJsonResult(baseURL + bname + "/vlanmaps");
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vLANMapArray = json.getJSONArray("vlanmap");
    JSONObject vLANMap;

    assertResponse(HTTP_OK);
    Assert.assertEquals(2, vLANMapArray.length());

    for (int i = 0; i < vLANMapArray.length(); i++) {
        vLANMap = vLANMapArray.getJSONObject(i);
        JSONObject nodeinfo = vLANMap.getJSONObject("node");
        if (vLANMap.getString("id").equals(nodeType + "-" + nodeid1 + "." + vlan1)) {
            Assert.assertEquals(vlan1, vLANMap.getString("vlan"));
            Assert.assertEquals(nodeType, nodeinfo.getString("type"));
            Assert.assertEquals(nodeid1, nodeinfo.getString("id"));

        } else if (vLANMap.getString("id").equals(nodeType + "-" + nodeid2 + "." + vlan2)) {
            Assert.assertEquals(vlan2, vLANMap.getString("vlan"));
            Assert.assertEquals(nodeType, nodeinfo.getString("type"));
            Assert.assertEquals(nodeid2, nodeinfo.getString("id"));
        } else {
            // Unexpected VLAN Mapping
            Assert.assertTrue(false);
        }
    }

    // Test POST VLAN Mapping, setting 0 to vlan
    requestBody = "{\"vlan\":\"" + vlan0 + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + nodeid1
            + "\"}}";
    requestUri = baseURL + bname2 + "/vlanmaps";
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    loc = requestUri + "/" + nodeType + "-" + nodeid1 + "." + vlan0;
    Assert.assertEquals(loc, httpLocation);

    // Test POST VLAN Mapping, except vlan
    requestBody = "{\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + nodeid1 + "\"}}";
    requestUri = baseURL + bname + "/vlanmaps";
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CONFLICT);

    // Authentication failure.
    checkVlanMapError(GlobalConstants.DEFAULT.toString(), tname, bname, bname2, false,
            HttpURLConnection.HTTP_UNAUTHORIZED);

    // Invalid container name.
    String[] invalid = { "bad_container", "version" };
    for (String inv : invalid) {
        checkVlanMapError(inv, tname, bname, bname2, true, HttpURLConnection.HTTP_NOT_FOUND);
        checkVlanMapError(inv, tname, bname, bname2, false, HttpURLConnection.HTTP_UNAUTHORIZED);
    }

    // Test DELETE VLAN Mapping
    result = getJsonResult(baseURL + bname2 + "/vlanmaps/" + nodeType + "-" + nodeid1 + "." + vlan0,
            HTTP_DELETE);
    assertResponse(HTTP_OK);

    // Test not supported Content-Type
    requestBody = "{\"vlan\":\"" + vlan0 + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + nodeid1
            + "\"}}";
    requestUri = baseURL + bname + "/vlanmaps";
    result = getJsonResult(requestUri, HTTP_POST, requestBody, "text/plain");
    assertResponse(HTTP_UNSUPPORTED_TYPE);

    // Test POST VLAN Mapping, except vlan
    requestBody = "{\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + nodeid1 + "\"}}";
    requestUri = baseURL + bname + "/vlanmaps";
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    loc = requestUri + "/" + nodeType + "-" + nodeid1 + "." + vlan0;
    Assert.assertEquals(loc, httpLocation);

    // Test POST VLAN Mapping, specifying invalid node which does not
    // contain node ID.
    requestBody = "{\"vlan\":\"" + vlan + "\",\"node\":{\"type\":\"" + nodeType + "\"}}";
    requestUri = baseURL + bname + "/vlanmaps";
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST VLAN Mapping, specifying invalid node which does not
    // contain node type.
    requestBody = "{\"vlan\":\"" + vlan + "\",\"node\":{\"id\":\"" + nodeid1 + "\"}}";
    requestUri = baseURL + bname2 + "/vlanmaps";
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test POST VLAN Mapping, setting requestBody without node elements
    requestBody = "{\"vlan\":\"" + vlan3 + "\"}";
    requestUri = baseURL + bname2 + "/vlanmaps";

    // Ensure that query parameters are eliminated from Location.
    result = getJsonResult(requestUri + "?param1=1&param2=2", HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);
    loc = requestUri + "/" + "ANY" + "." + vlan3;
    Assert.assertEquals(loc, httpLocation);

    // Test POST VLAN Mapping expecting 400
    // setting invalid value to node id
    requestBody = "{\"vlan\":\"" + vlan + "\",\"node\":{\"type\":\"" + nodeType + "\",\"id\":\"" + "ERROR_TEST"
            + "\"}}";
    requestUri = baseURL + bname2 + "/vlanmaps";
    result = getJsonResult(requestUri, HTTP_POST, requestBody);
    assertResponse(HTTP_BAD_REQUEST);

    // Test GET VLAN Mapping expecting 404
    // setting dummy vtn
    result = getJsonResult(url + "default/vtns/" + tnameDummy + "/vbridges/" + bname + "/vlanmaps" + nodeType
            + "-" + nodeid1 + "." + vlan1);
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge
    result = getJsonResult(baseURL + bnameDummy + "/vlanmaps/" + nodeType + "-" + nodeid1 + "." + vlan1);
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vlan
    result = getJsonResult(baseURL + bname + "/vlanmaps/" + nodeType + "-" + nodeid1 + "." + vlan);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET VLAN Mapping
    result = getJsonResult(baseURL + bname + "/vlanmaps/" + nodeType + "-" + nodeid1 + "." + vlan1);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    assertResponse(HTTP_OK);

    String mapId = nodeType + "-" + nodeid1 + "." + vlan1;
    Assert.assertEquals(mapId, json.getString("id"));
    Assert.assertEquals(vlan1, json.getString("vlan"));
    JSONObject nodeinfo = json.getJSONObject("node");
    Assert.assertEquals(nodeType, nodeinfo.getString("type"));
    Assert.assertEquals(nodeid1, nodeinfo.getString("id"));

    result = getJsonResult(
            baseURL + bname + searchByConf + "?vlan=" + vlan1 + "&node=" + nodeType + "|" + nodeid1);
    assertResponse(HTTP_OK);
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    Assert.assertEquals(mapId, json.getString("id"));
    Assert.assertEquals(vlan1, json.getString("vlan"));
    nodeinfo = json.getJSONObject("node");
    Assert.assertEquals(nodeType, nodeinfo.getString("type"));
    Assert.assertEquals(nodeid1, nodeinfo.getString("id"));

    // Test GET all  VLAN Mapping in default container
    result = getJsonResult(baseURL + bname + "/vlanmaps");
    jt = new JSONTokener(result);
    json = new JSONObject(jt);
    vLANMapArray = json.getJSONArray("vlanmap");

    assertResponse(HTTP_OK);
    Assert.assertEquals(3, vLANMapArray.length());
}

From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java

/**
 * Test case for MAC Address APIs./* w  w w.j av a2 s. co m*/
 *
 * <p>
 *   This method is called by {@link #testVBridgeAPI(String, String)}.
 * </p>
 *
 * @param tname     A tenant name.
 *                  Specified tenant is necessary to be configured
 *                  before method is called.
 * @param bname     A vBridge name.
 *                  Specified vBridge is necessary to be configured
 *                  before method is called.
 * @throws Exception  An error occurred.
 */
private void testMacAddressAPI(String tname, String bname) throws Exception {
    LOG.info("Starting MAC address JAX-RS client.");

    String baseURL = new StringBuilder(VTN_BASE_URL).append("default/vtns/").append(tname).append("/vbridges/")
            .toString();

    String dummy = "dummy";
    String macaddr = "00:00:00:00:00:01";

    // Test GET all MAC address
    String vbrUri = baseURL + bname;
    String macTableUri = createRelativeURI(vbrUri, "mac");
    String result = getJsonResult(macTableUri);
    assertResponse(HTTP_OK);
    JSONTokener jt = new JSONTokener(result);
    JSONObject json = new JSONObject(jt);
    JSONArray macArray = json.getJSONArray("macentry");
    Assert.assertEquals(0, macArray.length());

    // Test GET all MAC address expecting 404
    // setting dummy vtn
    result = getJsonResult(VTN_BASE_URL + "default/vtns/" + dummy + "/vbridges/" + bname + "/mac");
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge
    result = getJsonResult(baseURL + dummy + "/mac");
    assertResponse(HTTP_NOT_FOUND);

    // Test DELETE all MAC address
    result = getJsonResult(baseURL + bname + "/mac", HTTP_DELETE);
    assertResponse(HTTP_OK);

    // Test DELETE all MAC address expecting 404
    // setting dummy vtn
    result = getJsonResult(VTN_BASE_URL + "default/vtns/" + dummy + "/vbridges/" + bname + "/mac", HTTP_DELETE);
    assertResponse(HTTP_NOT_FOUND);

    // setting dummy vbridge
    result = getJsonResult(baseURL + dummy + "/mac", HTTP_DELETE);
    assertResponse(HTTP_NOT_FOUND);

    // Test GET MAC address expecting 404
    // setting MAC address that don't exist
    String badUri = createRelativeURI(macTableUri, macaddr);
    result = getJsonResult(badUri);
    assertResponse(HTTP_NOT_FOUND);

    // Test DELETE MAC address expecting 404
    // setting MAC address that don't exist
    result = getJsonResult(badUri, HTTP_DELETE);
    assertResponse(HTTP_NOT_FOUND);

    // Map VLAN 0 to the test vBridge using VLAN mapping.
    short vlan = 0;
    String requestBody = "{\"vlan\":\"" + vlan + "\"}";
    result = getJsonResult(baseURL + bname + "/vlanmaps", HTTP_POST, requestBody);
    assertResponse(HTTP_CREATED);

    // Collect edge ports, and create test hosts.
    VBridgePath bpath = new VBridgePath(tname, bname);
    BridgeNetwork bridge = new BridgeNetwork(bpath);
    int hostIdx = 1;
    List<TestHost> hosts = new ArrayList<>();
    for (String nid : ofMockService.getNodes()) {
        for (String pid : ofMockService.getPorts(nid, true)) {
            TestHost host = new TestHost(hostIdx, pid, vlan);
            hostIdx++;
            hosts.add(host);
            bridge.addHost(nid, host);
        }
        for (String pid : ofMockService.getPorts(nid, false)) {
            bridge.setUnmappedPort(pid);
        }
    }

    Map<String, Set<Short>> allPorts = bridge.getMappedVlans();
    Set<MacAddressEntry> expected = new HashSet<>();
    for (TestHost host : hosts) {
        assertTrue(expected.add(host.getMacAddressEntry()));
        learnHost(ofMockService, allPorts, host);
        checkMacTableEntry(macTableUri, expected);
    }

    // Test GET all MAC address expecting 404
    // setting dummy vtn
    result = getJsonResult(VTN_BASE_URL + "default/vtns/" + dummy + "/vbridges/" + bname + "/mac");
    assertResponse(HTTP_NOT_FOUND);

    // Try to get MAC address table in the vBridge that is not present.
    String dummyMacUri = createRelativeURI(baseURL, dummy, "mac");
    getJsonResult(dummyMacUri);
    assertResponse(HTTP_NOT_FOUND);

    String testMac = null;
    for (Iterator<MacAddressEntry> it = expected.iterator(); it.hasNext();) {
        MacAddressEntry ment = it.next();
        it.remove();

        // Try to remove MAC address from the vBridge that is not present.
        DataLinkAddress dladdr = ment.getAddress();
        assertTrue(dladdr instanceof EthernetAddress);
        EthernetAddress eaddr = (EthernetAddress) dladdr;
        testMac = eaddr.getMacAddress();
        String uri = createRelativeURI(dummyMacUri, testMac);
        getJsonResult(uri, HTTP_DELETE);
        assertResponse(HTTP_NOT_FOUND);

        // Remove all MAC addresses from the MAC address table by
        // specifying MAC address.
        uri = createRelativeURI(macTableUri, testMac);
        getJsonResult(uri, HTTP_DELETE);
        assertResponse(HTTP_OK);
        checkMacTableEntry(macTableUri, expected);

        getJsonResult(uri, HTTP_DELETE);
        assertResponse(HTTP_NOT_FOUND);
    }
    assertTrue(expected.isEmpty());
    assertNotNull(testMac);

    // Add more hosts and learn all hosts.
    for (String nid : ofMockService.getNodes()) {
        for (String pid : ofMockService.getPorts(nid, true)) {
            TestHost host = new TestHost(hostIdx, pid, vlan);
            hostIdx++;
            hosts.add(host);
            bridge.addHost(nid, host);
        }
    }

    expected = bridge.getMacAddressEntries();
    for (TestHost host : hosts) {
        learnHost(ofMockService, allPorts, host);
    }
    checkMacTableEntry(macTableUri, expected);

    // Authentication failure.
    checkMacError(GlobalConstants.DEFAULT.toString(), tname, bname, testMac, false,
            HttpURLConnection.HTTP_UNAUTHORIZED);

    // Invalid container name.
    String[] invalid = { "bad_container", "version" };
    for (String inv : invalid) {
        checkMacError(inv, tname, bname, testMac, true, HttpURLConnection.HTTP_NOT_FOUND);
        checkMacError(inv, tname, bname, testMac, false, HttpURLConnection.HTTP_UNAUTHORIZED);
    }

    // Flush MAC address table.
    getJsonResult(macTableUri, HTTP_DELETE);
    assertResponse(HTTP_OK);
    checkMacTableEntry(macTableUri, Collections.<MacAddressEntry>emptySet());
}