- private ISwitchManager getIfSwitchManagerService(String containerName) {
- IContainerManager containerManager = (IContainerManager) ServiceHelper
- .getGlobalInstance(IContainerManager.class, this);
- if (containerManager == null) {
- throw new ServiceUnavailableException("Container "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- boolean found = false;
- List<String> containerNames = containerManager.getContainerNames();
- for (String cName : containerNames) {
- if (cName.trim().equalsIgnoreCase(containerName.trim())) {
- found = true;
- }
- }
-
- if (found == false) {
- throw new ResourceNotFoundException(containerName + " "
- + RestMessages.NOCONTAINER.toString());
- }
-
- ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(
- ISwitchManager.class, containerName, this);
-
- if (switchManager == null) {
- throw new ServiceUnavailableException("Switch Manager "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- return switchManager;
- }
-
- /**
- *
- * Retrieve a list of all the nodes and their properties in the network
- *
- * @param containerName The container for which we want to retrieve the list
- * @return A list of Pair each pair represents a
- * {@link org.opendaylight.controller.sal.core.Node} and Set of
- * {@link org.opendaylight.controller.sal.core.Property} attached to
- * it.
- */
- @Path("/{containerName}/nodes")
- @GET
- @Produces( { MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
- @TypeHint(Nodes.class)
- @StatusCodes( {
- @ResponseCode(code = 200, condition = "Operation successful"),
- @ResponseCode(code = 404, condition = "The containerName is not found"),
- @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable") })
- public Nodes getNodes(
- @PathParam("containerName") String containerName) {
- ISwitchManager switchManager = (ISwitchManager) getIfSwitchManagerService(containerName);
- if (switchManager == null) {
- throw new ServiceUnavailableException("Switch Manager "
- + RestMessages.SERVICEUNAVAILABLE.toString());
- }
-
- List<NodeProperties> res = new ArrayList<NodeProperties>();
- Set<Node> nodes = switchManager.getNodes();
- if (nodes == null) {
- return null;
- }
-
- byte[] controllerMac = switchManager.getControllerMAC();
- for (Node node : nodes) {
- Map<String, Property> propMap = switchManager.getNodeProps(node);
- if (propMap == null) {
- continue;
- }
- Set<Property> props = new HashSet<Property>(propMap.values());
-
- byte[] nodeMac = switchManager.getNodeMAC(node);
- Property macAddr = new MacAddress(controllerMac, nodeMac);
- props.add(macAddr);
-
- NodeProperties nodeProps = new NodeProperties(node, props);
- res.add(nodeProps);
- }
-
- return new Nodes(res);
- }
+ private String username;
+ private QueryContext queryContext;
+
+ @Context
+ public void setQueryContext(ContextResolver<QueryContext> queryCtxResolver) {
+ if (queryCtxResolver != null) {
+ queryContext = queryCtxResolver.getContext(QueryContext.class);
+ }
+ }
+
+ @Context
+ public void setSecurityContext(SecurityContext context) {
+ if (context != null && context.getUserPrincipal() != null) {
+ username = context.getUserPrincipal().getName();
+ }
+ }
+
+ protected String getUserName() {
+ return username;
+ }
+
+ private ISwitchManager getIfSwitchManagerService(String containerName) {
+ IContainerManager containerManager = (IContainerManager) ServiceHelper.getGlobalInstance(
+ IContainerManager.class, this);
+ if (containerManager == null) {
+ throw new ServiceUnavailableException("Container " + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ boolean found = false;
+ List<String> containerNames = containerManager.getContainerNames();
+ for (String cName : containerNames) {
+ if (cName.trim().equalsIgnoreCase(containerName.trim())) {
+ found = true;
+ break;
+ }
+ }
+
+ if (found == false) {
+ throw new ResourceNotFoundException(containerName + " " + RestMessages.NOCONTAINER.toString());
+ }
+
+ ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
+ this);
+
+ if (switchManager == null) {
+ throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ return switchManager;
+ }
+
+ /**
+ *
+ * Retrieve a list of all the nodes and their properties in the network
+ *
+ * @param containerName
+ * Name of the Container (Eg. 'default')
+ * @return A list of Pair each pair represents a
+ * {@link org.opendaylight.controller.sal.core.Node} and Set of
+ * {@link org.opendaylight.controller.sal.core.Property} attached to
+ * it.
+ *
+ * <pre>
+ *
+ * Example:
+ *
+ * Request URL:
+ * http://localhost:8080/controller/nb/v2/switchmanager/default/nodes
+ *
+ * Response body in XML:
+ * <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+ * <list>
+ *    <nodeProperties>
+ *       <node>
+ *          <id>00:00:00:00:00:00:00:02</id>
+ *          <type>OF</type>
+ *       </node>
+ *       <properties>
+ *          <tables>
+ *             <value>-1</value>
+ *          </tables>
+ *          <description>
+ *             <value>Switch2</value>
+ *          </description>
+ *          <actions>
+ *             <value>4095</value>
+ *          </actions>
+ *          <macAddress>
+ *             <value>00:00:00:00:00:02</value>
+ *          </macAddress>
+ *          <capabilities>
+ *             <value>199</value>
+ *          </capabilities>
+ *          <timeStamp>
+ *             <value>1377291227877</value>
+ *             <name>connectedSince</name>
+ *          </timeStamp>
+ *          <buffers>
+ *             <value>256</value>
+ *          </buffers>
+ *       </properties>
+ *    </nodeProperties>
+ * </list>
+ *
+ * Response body in JSON:
+ * {
+ * "nodeProperties":[
+ * {
+ * "node":{
+ * "id":"00:00:00:00:00:00:00:02",
+ * "type":"OF"
+ * },
+ * "properties":{
+ * "tables":{
+ * "value":"-1"
+ * },
+ * "description":{
+ * "value":"None"
+ * },
+ * "actions":{
+ * "value":"4095"
+ * },
+ * "macAddress":{
+ * "value":"00:00:00:00:00:02"
+ * },
+ * "capabilities":{
+ * "value":"199"
+ * },
+ * "timeStamp":{
+ * "value":"1377291227877",
+ * "name":"connectedSince"
+ * },
+ * "buffers":{
+ * "value":"256"
+ * }
+ * }
+ * }
+ * ]
+ * }
+ *
+ * </pre>
+ */
+ @Path("/{containerName}/nodes")
+ @GET
+ @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
+ @TypeHint(Nodes.class)
+ @StatusCodes({ @ResponseCode(code = 200, condition = "Operation successful"),
+ @ResponseCode(code = 401, condition = "User not authorized to perform this operation"),
+ @ResponseCode(code = 404, condition = "The containerName is not found"),
+ @ResponseCode(code = 503, condition = "One or more of Controller Services are unavailable"),
+ @ResponseCode(code = 400, condition = "Incorrect query syntex") })
+ public Nodes getNodes(@PathParam("containerName") String containerName, @QueryParam("_q") String queryString) {
+
+ if (!isValidContainer(containerName)) {
+ throw new ResourceNotFoundException("Container " + containerName + " does not exist.");
+ }
+
+ if (!NorthboundUtils.isAuthorized(getUserName(), containerName, Privilege.READ, this)) {
+ throw new UnauthorizedException("User is not authorized to perform this operation on container "
+ + containerName);
+ }
+
+ ISwitchManager switchManager = getIfSwitchManagerService(containerName);
+ if (switchManager == null) {
+ throw new ServiceUnavailableException("Switch Manager " + RestMessages.SERVICEUNAVAILABLE.toString());
+ }
+
+ List<NodeProperties> res = new ArrayList<NodeProperties>();
+ Set<Node> nodes = switchManager.getNodes();
+ if (nodes == null) {
+ return new Nodes(res);
+ }
+
+ for (Node node : nodes) {
+ Map<String, Property> propMap = switchManager.getNodeProps(node);
+ if (propMap == null) {
+ continue;
+ }
+ Set<Property> props = new HashSet<Property>(propMap.values());
+
+ NodeProperties nodeProps = new NodeProperties(node, props);
+ res.add(nodeProps);
+ }
+ Nodes result = new Nodes(res);
+ if (queryString != null) {
+ queryContext.createQuery(queryString, Nodes.class)
+ .filter(result, NodeProperties.class);
+ }
+ return result;
+ }