diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebServices.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebServices.java index 4ce2b54..6be24f0 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebServices.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/RMWebServices.java @@ -93,6 +93,7 @@ import org.apache.hadoop.yarn.api.records.FinalApplicationStatus; import org.apache.hadoop.yarn.api.records.LocalResource; import org.apache.hadoop.yarn.api.records.NodeId; +import org.apache.hadoop.yarn.api.records.NodeLabel; import org.apache.hadoop.yarn.api.records.NodeState; import org.apache.hadoop.yarn.api.records.Priority; import org.apache.hadoop.yarn.api.records.QueueACL; @@ -796,7 +797,7 @@ public Response updateAppState(AppState targetState, @GET @Path("/get-node-to-labels") @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) - public NodeToLabelsInfo getNodeToLabels(@Context HttpServletRequest hsr) + public NodeToLabelsInfo getNodeToLabels(@Context HttpServletRequest hsr) throws IOException { init(); @@ -853,7 +854,7 @@ public Response replaceLabelsOnNodes(final NodeToLabelsInfo newNodeToLabels, .getNodeToLabels().entrySet()) { nodeIdToLabels.put( ConverterUtils.toNodeIdWithDefaultPort(nitle.getKey()), - new HashSet(nitle.getValue().getNodeLabels())); + new HashSet(nitle.getValue().getNodeLabelsName())); } return replaceLabelsOnNode(nodeIdToLabels, hsr, "/replace-node-to-labels"); @@ -869,7 +870,7 @@ public Response replaceLabelsOnNode(NodeLabelsInfo newNodeLabelsInfo, Map> newLabelsForNode = new HashMap>(); newLabelsForNode.put(nid, - new HashSet(newNodeLabelsInfo.getNodeLabels())); + new HashSet(newNodeLabelsInfo.getNodeLabelsName())); return replaceLabelsOnNode(newLabelsForNode, hsr, "/nodes/nodeid/replace-labels"); } @@ -909,9 +910,9 @@ public NodeLabelsInfo getClusterNodeLabels(@Context HttpServletRequest hsr) throws IOException { init(); - NodeLabelsInfo ret = - new NodeLabelsInfo(rm.getRMContext().getNodeLabelManager() - .getClusterNodeLabelNames()); + List nodeLabels = rm.getRMContext().getNodeLabelManager() + .getClusterNodeLabels(); + NodeLabelsInfo ret = new NodeLabelsInfo(nodeLabels); return ret; } @@ -937,8 +938,7 @@ public Response addToClusterNodeLabels(final NodeLabelsInfo newNodeLabels, } rm.getRMContext().getNodeLabelManager() - .addToCluserNodeLabelsWithDefaultExclusivity(new HashSet( - newNodeLabels.getNodeLabels())); + .addToCluserNodeLabels(newNodeLabels.getNodeLabels()); return Response.status(Status.OK).build(); @@ -966,7 +966,7 @@ public Response removeFromCluserNodeLabels(final NodeLabelsInfo oldNodeLabels, rm.getRMContext().getNodeLabelManager() .removeFromClusterNodeLabels(new HashSet( - oldNodeLabels.getNodeLabels())); + oldNodeLabels.getNodeLabelsName())); return Response.status(Status.OK).build(); diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeLabelInfo.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeLabelInfo.java new file mode 100644 index 0000000..d2627c1 --- /dev/null +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeLabelInfo.java @@ -0,0 +1,59 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.yarn.server.resourcemanager.webapp.dao; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlRootElement; + +import org.apache.hadoop.yarn.api.records.NodeLabel; + +@XmlRootElement(name = "nodeLabelInfo") +@XmlAccessorType(XmlAccessType.FIELD) +public class NodeLabelInfo { + + protected String name; + protected boolean exclusivity; + + public NodeLabelInfo() { + } // JAXB needs this + + public NodeLabelInfo(String name) { + this.name = name; + this.exclusivity = true; + } + + public NodeLabelInfo(String name, boolean exclusivity) { + this.name = name; + this.exclusivity = exclusivity; + } + + public NodeLabelInfo(NodeLabel label) { + this.name = label.getName(); + this.exclusivity = label.isExclusive(); + } + + public String getName() { + return name; + } + + public boolean getExclusivity() { + return exclusivity; + } +} diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeLabelsInfo.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeLabelsInfo.java index 1cb895a..be2cfa2 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeLabelsInfo.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeLabelsInfo.java @@ -22,31 +22,61 @@ import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; +import org.apache.hadoop.yarn.api.records.NodeLabel; + @XmlRootElement(name = "nodeLabelsInfo") @XmlAccessorType(XmlAccessType.FIELD) public class NodeLabelsInfo { - protected ArrayList nodeLabels = new ArrayList(); + @XmlElement(name = "nodeLabels") + protected ArrayList nodeLabelsInfo = new ArrayList(); public NodeLabelsInfo() { } // JAXB needs this - - public NodeLabelsInfo(ArrayList nodeLabels) { - this.nodeLabels = nodeLabels; + + public NodeLabelsInfo(ArrayList nodeLabels) { + this.nodeLabelsInfo = nodeLabels; } - - public NodeLabelsInfo(Set nodeLabelsSet) { - this.nodeLabels = new ArrayList(nodeLabelsSet); + + public NodeLabelsInfo(List nodeLabels) { + this.nodeLabelsInfo = new ArrayList(); + for (NodeLabel label : nodeLabels) { + this.nodeLabelsInfo.add(new NodeLabelInfo(label)); + } } - public ArrayList getNodeLabels() { + public NodeLabelsInfo(Set nodeLabelsName) { + this.nodeLabelsInfo = new ArrayList(); + for (String labelName : nodeLabelsName) { + this.nodeLabelsInfo.add(new NodeLabelInfo(labelName)); + } + } + + public ArrayList getNodeLabelsInfo() { + return nodeLabelsInfo; + } + + public Set getNodeLabels() { + Set nodeLabels = new HashSet(); + for (NodeLabelInfo label : nodeLabelsInfo) { + nodeLabels.add(NodeLabel.newInstance(label.getName(), + label.getExclusivity())); + } return nodeLabels; } - public void setNodeLabels(ArrayList nodeLabels) { - this.nodeLabels = nodeLabels; + public List getNodeLabelsName() { + ArrayList nodeLabelsName = new ArrayList(); + for (NodeLabelInfo label : nodeLabelsInfo) { + nodeLabelsName.add(label.getName()); + } + return nodeLabelsName; + } + + public void setNodeLabelsInfo(ArrayList nodeLabelsInfo) { + this.nodeLabelsInfo = nodeLabelsInfo; } - } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeToLabelsInfo.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeToLabelsInfo.java index f2e6441..a1802ff 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeToLabelsInfo.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/main/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/dao/NodeToLabelsInfo.java @@ -29,13 +29,12 @@ public class NodeToLabelsInfo { protected HashMap nodeToLabels = - new HashMap(); + new HashMap(); public NodeToLabelsInfo() { } // JAXB needs this - + public HashMap getNodeToLabels() { - return nodeToLabels; + return nodeToLabels; } - } diff --git a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodeLabels.java b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodeLabels.java index 2d5518d..ca1c352 100644 --- a/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodeLabels.java +++ b/hadoop-yarn-project/hadoop-yarn/hadoop-yarn-server/hadoop-yarn-server-resourcemanager/src/test/java/org/apache/hadoop/yarn/server/resourcemanager/webapp/TestRMWebServicesNodeLabels.java @@ -34,13 +34,12 @@ import org.apache.hadoop.yarn.server.resourcemanager.MockRM; import org.apache.hadoop.yarn.server.resourcemanager.ResourceManager; import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.LabelsToNodesInfo; +import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.NodeLabelInfo; import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.NodeLabelsInfo; import org.apache.hadoop.yarn.server.resourcemanager.webapp.dao.NodeToLabelsInfo; import org.apache.hadoop.yarn.webapp.GenericExceptionHandler; import org.apache.hadoop.yarn.webapp.JerseyTestBase; -import org.codehaus.jettison.json.JSONArray; import org.codehaus.jettison.json.JSONException; -import org.codehaus.jettison.json.JSONObject; import org.junit.Test; import com.google.inject.Guice; @@ -113,32 +112,35 @@ public void testNodeLabels() throws JSONException, Exception { WebResource r = resource(); ClientResponse response; - JSONObject json; - JSONArray jarr; - + // Add a label + NodeLabelsInfo nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("a")); response = r.path("ws").path("v1").path("cluster") .path("add-node-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\":\"a\"}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), MediaType.APPLICATION_JSON) .post(ClientResponse.class); // Verify response = r.path("ws").path("v1").path("cluster") .path("get-node-labels").queryParam("user.name", userName) - .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - assertEquals("a", json.getString("nodeLabels")); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertEquals("a", nlsifo.getNodeLabelsInfo().get(0).getName()); + assertEquals(1, nlsifo.getNodeLabels().size()); // Add another + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("b")); response = r.path("ws").path("v1").path("cluster") .path("add-node-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\":\"b\"}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), MediaType.APPLICATION_JSON) .post(ClientResponse.class); // Verify @@ -147,43 +149,48 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-node-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - jarr = json.getJSONArray("nodeLabels"); - assertEquals(2, jarr.length()); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertEquals(2, nlsifo.getNodeLabels().size()); // Add labels to a node + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("a")); response = r.path("ws").path("v1").path("cluster") .path("nodes").path("nid:0") .path("replace-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\": [\"a\"]}", - MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); LOG.info("posted node nodelabel"); // Add labels to another node + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("b")); response = r.path("ws").path("v1").path("cluster") .path("nodes").path("nid1:0") .path("replace-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\": [\"b\"]}", - MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); LOG.info("posted node nodelabel"); // Add labels to another node + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("b")); response = r.path("ws").path("v1").path("cluster") .path("nodes").path("nid2:0") .path("replace-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\": [\"b\"]}", - MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); LOG.info("posted node nodelabel"); @@ -223,18 +230,21 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - assertEquals("a", json.getString("nodeLabels")); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertTrue(nlsifo.getNodeLabelsName().contains("a")); // Replace + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("b")); response = r.path("ws").path("v1").path("cluster") .path("nodes").path("nid:0") .path("replace-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\":\"b\"}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); LOG.info("posted node nodelabel"); @@ -245,13 +255,13 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - assertEquals("b", json.getString("nodeLabels")); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertTrue(nlsifo.getNodeLabelsName().contains("b")); // Replace labels using node-to-labels NodeToLabelsInfo ntli = new NodeToLabelsInfo(); NodeLabelsInfo nli = new NodeLabelsInfo(); - nli.getNodeLabels().add("a"); + nli.getNodeLabelsInfo().add(new NodeLabelInfo("a")); ntli.getNodeToLabels().put("nid:0", nli); response = r.path("ws").path("v1").path("cluster") @@ -271,16 +281,19 @@ public void testNodeLabels() throws JSONException, Exception { ntli = response.getEntity(NodeToLabelsInfo.class); nli = ntli.getNodeToLabels().get("nid:0"); assertEquals(1, nli.getNodeLabels().size()); - assertTrue(nli.getNodeLabels().contains("a")); + assertTrue(nli.getNodeLabelsName().contains("a")); // Remove all + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("")); response = r.path("ws").path("v1").path("cluster") .path("nodes").path("nid:0") .path("replace-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\"}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); LOG.info("posted node nodelabel"); // Verify @@ -290,18 +303,20 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - assertEquals("", json.getString("nodeLabels")); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertTrue(nlsifo.getNodeLabelsName().contains("")); // Add a label back for auth tests + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("a")); response = r.path("ws").path("v1").path("cluster") .path("nodes").path("nid:0") .path("replace-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\": \"a\"}", - MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); LOG.info("posted node nodelabel"); @@ -312,18 +327,20 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - assertEquals("a", json.getString("nodeLabels")); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertTrue(nlsifo.getNodeLabelsName().contains("a")); // Auth fail replace labels on node + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("b")); response = r.path("ws").path("v1").path("cluster") .path("nodes").path("nid:0") .path("replace-labels") .queryParam("user.name", notUserName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\": [\"b\"]}", - MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); // Verify response = @@ -332,8 +349,8 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - assertEquals("a", json.getString("nodeLabels")); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertTrue(nlsifo.getNodeLabelsName().contains("a")); // Fail to add a label with post response = @@ -349,17 +366,19 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-node-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - jarr = json.getJSONArray("nodeLabels"); - assertEquals(2, jarr.length()); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertEquals(2, nlsifo.getNodeLabels().size()); // Remove cluster label (succeed, we no longer need it) + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("b")); response = r.path("ws").path("v1").path("cluster") .path("remove-node-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\":\"b\"}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); // Verify response = @@ -367,17 +386,20 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-node-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - assertEquals("a", json.getString("nodeLabels")); - + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertEquals("a", nlsifo.getNodeLabelsInfo().get(0).getName()); + assertEquals(1, nlsifo.getNodeLabels().size()); // Remove cluster label with post + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("a")); response = r.path("ws").path("v1").path("cluster") .path("remove-node-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\":\"a\"}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); // Verify response = @@ -385,12 +407,15 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-node-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - String res = response.getEntity(String.class); - assertTrue(res.equals("null")); - + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertEquals(0, nlsifo.getNodeLabels().size()); + // Following test cases are to test replace when distributed node label // configuration is on // Reset for testing : add cluster labels + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("x")); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("y")); response = r.path("ws") .path("v1") @@ -398,14 +423,17 @@ public void testNodeLabels() throws JSONException, Exception { .path("add-node-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\":[\"x\",\"y\"]}", + .entity(toJson(nlsifo, NodeLabelsInfo.class), MediaType.APPLICATION_JSON).post(ClientResponse.class); // Reset for testing : Add labels to a node + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("y")); response = r.path("ws").path("v1").path("cluster").path("nodes").path("nid:0") .path("replace-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\": [\"y\"]}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); LOG.info("posted node nodelabel"); @@ -415,7 +443,7 @@ public void testNodeLabels() throws JSONException, Exception { // Case1 : Replace labels using node-to-labels ntli = new NodeToLabelsInfo(); nli = new NodeLabelsInfo(); - nli.getNodeLabels().add("x"); + nli.getNodeLabelsInfo().add(new NodeLabelInfo("x")); ntli.getNodeToLabels().put("nid:0", nli); response = r.path("ws") @@ -436,14 +464,17 @@ public void testNodeLabels() throws JSONException, Exception { ntli = response.getEntity(NodeToLabelsInfo.class); nli = ntli.getNodeToLabels().get("nid:0"); assertEquals(1, nli.getNodeLabels().size()); - assertFalse(nli.getNodeLabels().contains("x")); + assertFalse(nli.getNodeLabelsName().contains("x")); // Case2 : failure to Replace labels using replace-labels + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("x")); response = r.path("ws").path("v1").path("cluster").path("nodes").path("nid:0") .path("replace-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\": [\"x\"]}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); LOG.info("posted node nodelabel"); @@ -459,12 +490,15 @@ public void testNodeLabels() throws JSONException, Exception { assertFalse(nli.getNodeLabels().contains("x")); // Case3 : Remove cluster label should be successfull + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("x")); response = r.path("ws").path("v1").path("cluster") .path("remove-node-labels") .queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON) - .entity("{\"nodeLabels\":\"x\"}", MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) .post(ClientResponse.class); // Verify response = @@ -472,8 +506,53 @@ public void testNodeLabels() throws JSONException, Exception { .path("get-node-labels").queryParam("user.name", userName) .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); - json = response.getEntity(JSONObject.class); - assertEquals("y", json.getString("nodeLabels")); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertEquals("y", nlsifo.getNodeLabelsInfo().get(0).getName()); + + // Remove y + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("y")); + response = + r.path("ws").path("v1").path("cluster") + .path("remove-node-labels") + .queryParam("user.name", userName) + .accept(MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON) + .post(ClientResponse.class); + + // Verify + response = + r.path("ws").path("v1").path("cluster") + .path("get-node-labels").queryParam("user.name", userName) + .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertTrue(nlsifo.getNodeLabelsInfo().isEmpty()); + + // add a new nodelabel with exclusity + nlsifo = new NodeLabelsInfo(); + nlsifo.getNodeLabelsInfo().add(new NodeLabelInfo("z", false)); + response = + r.path("ws") + .path("v1") + .path("cluster") + .path("add-node-labels") + .queryParam("user.name", userName) + .accept(MediaType.APPLICATION_JSON) + .entity(toJson(nlsifo, NodeLabelsInfo.class), + MediaType.APPLICATION_JSON).post(ClientResponse.class); + + // Verify + response = + r.path("ws").path("v1").path("cluster") + .path("get-node-labels").queryParam("user.name", userName) + .accept(MediaType.APPLICATION_JSON).get(ClientResponse.class); + assertEquals(MediaType.APPLICATION_JSON_TYPE, response.getType()); + nlsifo = response.getEntity(NodeLabelsInfo.class); + assertEquals("z", nlsifo.getNodeLabelsInfo().get(0).getName()); + assertEquals(false, nlsifo.getNodeLabelsInfo().get(0).getExclusivity()); + assertEquals(1, nlsifo.getNodeLabels().size()); } @SuppressWarnings("rawtypes") -- 1.9.4.msysgit.1