- 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;
+
+ @Context
+ public void setSecurityContext(SecurityContext context) {
+ 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
+ * 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) {
+
+ 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 null;
+ }
+
+ 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);
+ }
+
+ return new Nodes(res);
+ }