List of usage examples for java.net HttpURLConnection HTTP_UNAUTHORIZED
int HTTP_UNAUTHORIZED
To view the source code for java.net HttpURLConnection HTTP_UNAUTHORIZED.
Click Source Link
From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java
/** * Test case for Port Mapping APIs./*from w ww .java 2s.c o m*/ * * 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.eclipse.mylyn.internal.bugzilla.core.BugzillaClient.java
private HeadMethod connectHead(String requestURL, IProgressMonitor monitor) throws IOException, CoreException { hostConfiguration = WebUtil.createHostConfiguration(httpClient, location, monitor); for (int attempt = 0; attempt < 2; attempt++) { // force authentication authenticate(monitor);/*from w w w . ja va 2s . c o m*/ HeadMethod headMethod = new HeadMethod(WebUtil.getRequestPath(requestURL)); if (requestURL.contains(QUERY_DELIMITER)) { headMethod.setQueryString(requestURL.substring(requestURL.indexOf(QUERY_DELIMITER))); } headMethod.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=" //$NON-NLS-1$ //$NON-NLS-2$ + getCharacterEncoding()); // WARNING!! Setting browser compatability breaks Bugzilla // authentication // getMethod.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY); // headMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new BugzillaRetryHandler()); headMethod.setDoAuthentication(true); int code; try { code = WebUtil.execute(httpClient, hostConfiguration, headMethod, monitor); } catch (IOException e) { // ignore the response WebUtil.releaseConnection(headMethod, monitor); throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, RepositoryStatus.ERROR_IO, repositoryUrl.toString(), e)); } if (code == HttpURLConnection.HTTP_OK) { return headMethod; } else if (code == HttpURLConnection.HTTP_UNAUTHORIZED || code == HttpURLConnection.HTTP_FORBIDDEN) { // ignore the response WebUtil.releaseConnection(headMethod, monitor); loggedIn = false; authenticate(monitor); } else if (code == HttpURLConnection.HTTP_PROXY_AUTH) { loggedIn = false; // ignore the response WebUtil.releaseConnection(headMethod, monitor); throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, RepositoryStatus.ERROR_REPOSITORY_LOGIN, repositoryUrl.toString(), "Proxy authentication required")); //$NON-NLS-1$ } else { // ignore the response WebUtil.releaseConnection(headMethod, monitor); throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, RepositoryStatus.ERROR_NETWORK, "Http error: " + HttpStatus.getStatusText(code))); //$NON-NLS-1$ // throw new IOException("HttpClient connection error response // code: " + code); } } throw new CoreException(new BugzillaStatus(IStatus.ERROR, BugzillaCorePlugin.ID_PLUGIN, RepositoryStatus.ERROR_REPOSITORY_LOGIN, "All connection attempts to " + repositoryUrl.toString() //$NON-NLS-1$ + " failed. Please verify connection and authentication information.")); //$NON-NLS-1$ }
From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java
/** * Test case for VLAN Mapping APIs./*from w w w. j a v a2 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¶m2=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 ww . j a v a 2 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()); }