testNodeConnectorProperties(nodeConnectorProperties, nodeConnectorId_3, ncType, nodeId_3, nodeType, ncState,
ncCapabilities, ncBandwidth);
- // Test delete node property
- // Delete timestamp property from node1
- result = getJsonResult(baseURL + "node/STUB/" + nodeId_1 + "/property/timeStamp", "DELETE");
- Assert.assertEquals(200, httpResponseCode.intValue());
-
- // Check node1
- result = getJsonResult(baseURL + "nodes");
- jt = new JSONTokener(result);
- json = new JSONObject(jt);
- node = getJsonInstance(json, "nodeProperties", nodeId_1);
- Assert.assertNotNull(node);
- testNodeProperties(node, nodeId_1, nodeType, null, null, actionsValue_1, capabilitiesValue_1, tablesValue_1,
- buffersValue_1);
-
- // Delete actions property from node2
- result = getJsonResult(baseURL + "node/STUB/" + nodeId_2 + "/property/actions", "DELETE");
- Assert.assertEquals(200, httpResponseCode.intValue());
-
- // Check node2
- result = getJsonResult(baseURL + "nodes");
- jt = new JSONTokener(result);
- json = new JSONObject(jt);
- node = getJsonInstance(json, "nodeProperties", nodeId_2);
- Assert.assertNotNull(node);
- testNodeProperties(node, nodeId_2, nodeType, timestamp_1, timestampName_1, null, capabilitiesValue_1,
- tablesValue_1, buffersValue_1);
-
// Test add property to node
- // Add Tier and Bandwidth property to node1
+ // Add Tier and Description property to node1
result = getJsonResult(baseURL + "node/STUB/" + nodeId_1 + "/property/tier/1001", "PUT");
- Assert.assertEquals(201, httpResponseCode.intValue());
- result = getJsonResult(baseURL + "node/STUB/" + nodeId_1 + "/property/bandwidth/1002", "PUT");
- Assert.assertEquals(201, httpResponseCode.intValue());
+ Assert.assertEquals(200, httpResponseCode.intValue());
+ result = getJsonResult(baseURL + "node/STUB/" + nodeId_1 + "/property/description/node1", "PUT");
+ Assert.assertEquals(200, httpResponseCode.intValue());
// Test for first node
result = getJsonResult(baseURL + "nodes");
node = getJsonInstance(json, "nodeProperties", nodeId_1);
Assert.assertNotNull(node);
Assert.assertEquals(1001, node.getJSONObject("properties").getJSONObject("tier").getInt("tierValue"));
- Assert.assertEquals(1002, node.getJSONObject("properties").getJSONObject("bandwidth").getInt("bandwidthValue"));
+ Assert.assertEquals("node1", node.getJSONObject("properties").getJSONObject("description").getString("descriptionValue"));
// Test delete nodeConnector property
// Delete state property of nodeconnector1
package org.opendaylight.controller.switchmanager.northbound;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import org.opendaylight.controller.containermanager.IContainerManager;
import org.opendaylight.controller.northbound.commons.RestMessages;
import org.opendaylight.controller.northbound.commons.exception.InternalServerErrorException;
-import org.opendaylight.controller.northbound.commons.exception.ResourceConflictException;
import org.opendaylight.controller.northbound.commons.exception.ResourceNotFoundException;
import org.opendaylight.controller.northbound.commons.exception.ServiceUnavailableException;
import org.opendaylight.controller.northbound.commons.exception.UnauthorizedException;
import org.opendaylight.controller.sal.utils.GlobalConstants;
import org.opendaylight.controller.sal.utils.ServiceHelper;
import org.opendaylight.controller.sal.utils.Status;
+import org.opendaylight.controller.sal.utils.StatusCode;
import org.opendaylight.controller.switchmanager.ISwitchManager;
+import org.opendaylight.controller.switchmanager.SwitchConfig;
/**
* The class provides Northbound REST APIs to access the nodes, node connectors
@ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
public Nodes getNodes(@PathParam("containerName") String containerName) {
+ if (!isValidContainer(containerName)) {
+ throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ }
+
if (!NorthboundUtils.isAuthorized(
getUserName(), containerName, Privilege.READ, this)) {
throw new UnauthorizedException(
}
/**
- * Add a Name/Tier property to a node
+ * Add a Name, Tier and Forwarding mode property to a node.
+ *
+ * <pre>
+ * Example Request:
+ * http://localhost:8080/controller/nb/v2/switch/red/node/OF/00:00:00:00:00:03/property/description/Switch3
+ * (Valid properties that can be configured are: description, forwarding(only for default container) and tier)
+ * </pre>
*
* @param containerName
* Name of the Container
@TypeHint(Response.class)
@StatusCodes({
@ResponseCode(code = 200, condition = "Operation successful"),
- @ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
+ @ResponseCode(code = 400, condition = "The nodeId or configuration is invalid"),
+ @ResponseCode(code = 404, condition = "The Container Name or node or configuration name is not found"),
+ @ResponseCode(code = 406, condition = "The property cannot be configured in non-default container"),
+ @ResponseCode(code = 409, condition = "Unable to update configuration due to cluster conflict"),
@ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
public Response addNodeProperty(
@PathParam("containerName") String containerName,
@PathParam("propName") String propName,
@PathParam("propValue") String propValue) {
+ if (!isValidContainer(containerName)) {
+ throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ }
if (!NorthboundUtils.isAuthorized(
getUserName(), containerName, Privilege.WRITE, this)) {
throw new UnauthorizedException(
"User is not authorized to perform this operation on container "
+ containerName);
}
- handleDefaultDisabled(containerName);
-
ISwitchManager switchManager = getIfSwitchManagerService(containerName);
if (switchManager == null) {
throw new ServiceUnavailableException("Switch Manager "
handleNodeAvailability(containerName, nodeType, nodeId);
Node node = Node.fromString(nodeType, nodeId);
-
Property prop = switchManager.createProperty(propName, propValue);
if (prop == null) {
- throw new ResourceNotFoundException(
- RestMessages.INVALIDDATA.toString());
- }
-
- switchManager.setNodeProp(node, prop);
- return Response.status(Response.Status.CREATED).build();
+ throw new ResourceNotFoundException("Property with name " + propName + " does not exist.");
+ }
+ SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
+ Map<String, Property> nodeProperties = (switchConfig == null) ? new HashMap<String, Property>()
+ : new HashMap<String, Property>(switchConfig.getNodeProperties());
+ nodeProperties.put(prop.getName(), prop);
+ SwitchConfig newSwitchConfig = new SwitchConfig(node.toString(), nodeProperties);
+ Status status = switchManager.updateNodeConfig(newSwitchConfig);
+ return NorthboundUtils.getResponse(status);
}
/**
* Delete a property of a node
*
+ * <pre>
+ * Example Request:
+ * http://localhost:8080/controller/nb/v2/switch/default/node/OF/00:00:00:00:00:03/property/forwarding
+ * </pre>
+ *
* @param containerName
* Name of the Container
* @param nodeType
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@StatusCodes({
@ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 400, condition = "The nodeId or configuration is invalid"),
@ResponseCode(code = 404, condition = "The Container Name or nodeId or configuration name is not found"),
+ @ResponseCode(code = 409, condition = "Unable to delete property due to cluster conflict"),
@ResponseCode(code = 503, condition = "One or more of Controller services are unavailable") })
public Response deleteNodeProperty(
@PathParam("containerName") String containerName,
@PathParam("nodeId") String nodeId,
@PathParam("propertyName") String propertyName) {
+ if (!isValidContainer(containerName)) {
+ throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ }
if (!NorthboundUtils.isAuthorized(
getUserName(), containerName, Privilege.WRITE, this)) {
throw new UnauthorizedException(
"User is not authorized to perform this operation on container "
+ containerName);
}
- handleDefaultDisabled(containerName);
-
ISwitchManager switchManager = getIfSwitchManagerService(containerName);
if (switchManager == null) {
throw new ServiceUnavailableException("Switch Manager "
handleNodeAvailability(containerName, nodeType, nodeId);
Node node = Node.fromString(nodeType, nodeId);
- Status ret = switchManager.removeNodeProp(node, propertyName);
- if (ret.isSuccess()) {
- return Response.ok().build();
+
+ SwitchConfig switchConfig = switchManager.getSwitchConfig(node.toString());
+ Status status;
+ if (switchConfig == null) {
+ status = new Status(StatusCode.NOTFOUND, "Switch Configuration does not exist");
+ } else {
+ Map<String, Property> nodeProperties = new HashMap<String, Property>(switchConfig.getNodeProperties());
+ if (!nodeProperties.containsKey(propertyName.toLowerCase())) {
+ String msg = "Property " + propertyName + " does not exist or not configured for switch " + nodeId;
+ status = new Status(StatusCode.NOTFOUND, msg);
+ } else {
+ nodeProperties.remove(propertyName.toLowerCase());
+ SwitchConfig newSwitchConfig = new SwitchConfig(node.toString(), nodeProperties);
+ status = switchManager.updateNodeConfig(newSwitchConfig);
+ }
}
- throw new ResourceNotFoundException(ret.getDescription());
+ return NorthboundUtils.getResponse(status);
}
/**
@PathParam("nodeType") String nodeType,
@PathParam("nodeId") String nodeId) {
+ if (!isValidContainer(containerName)) {
+ throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ }
if (!NorthboundUtils.isAuthorized(
getUserName(), containerName, Privilege.READ, this)) {
throw new UnauthorizedException(
@PathParam("propName") String propName,
@PathParam("propValue") String propValue) {
+ if (!isValidContainer(containerName)) {
+ throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ }
if (!NorthboundUtils.isAuthorized(
getUserName(), containerName, Privilege.WRITE, this)) {
throw new UnauthorizedException(
+ containerName);
}
- handleDefaultDisabled(containerName);
-
ISwitchManager switchManager = getIfSwitchManagerService(containerName);
if (switchManager == null) {
throw new ServiceUnavailableException("Switch Manager "
@PathParam("nodeConnectorId") String nodeConnectorId,
@PathParam("propertyName") String propertyName) {
+ if (!isValidContainer(containerName)) {
+ throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ }
if (!NorthboundUtils.isAuthorized(
getUserName(), containerName, Privilege.WRITE, this)) {
throw new UnauthorizedException(
+ containerName);
}
- handleDefaultDisabled(containerName);
-
ISwitchManager switchManager = getIfSwitchManagerService(containerName);
if (switchManager == null) {
throw new ServiceUnavailableException("Switch Manager "
public Response saveSwitchConfig(
@PathParam("containerName") String containerName) {
+ if (!isValidContainer(containerName)) {
+ throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ }
if (!NorthboundUtils.isAuthorized(
getUserName(), containerName, Privilege.WRITE, this)) {
throw new UnauthorizedException(
throw new InternalServerErrorException(ret.getDescription());
}
- private void handleDefaultDisabled(String containerName) {
- IContainerManager containerManager = (IContainerManager) ServiceHelper
- .getGlobalInstance(IContainerManager.class, this);
- if (containerManager == null) {
- throw new InternalServerErrorException(
- RestMessages.INTERNALERROR.toString());
- }
- if (containerName.equals(GlobalConstants.DEFAULT.toString())
- && containerManager.hasNonDefaultContainer()) {
- throw new ResourceConflictException(
- RestMessages.DEFAULTDISABLED.toString());
- }
- }
-
private Node handleNodeAvailability(String containerName, String nodeType,
String nodeId) {
}
}
+ private boolean isValidContainer(String containerName) {
+ if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
+ return true;
+ }
+ IContainerManager containerManager = (IContainerManager) ServiceHelper
+ .getGlobalInstance(IContainerManager.class, this);
+ if (containerManager == null) {
+ throw new InternalServerErrorException(
+ RestMessages.INTERNALERROR.toString());
+ }
+ if (containerManager.getContainerNames().contains(containerName)) {
+ return true;
+ }
+ return false;
+ }
+
}