X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fweb%2Fdevices%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fdevices%2Fweb%2FDevices.java;h=6ca60e857d8a86963be038ca1c4714050d7bff39;hp=ece5959dc0256e6bdef294075161cf99d57c30cd;hb=33446bc3f844db6d0f4763d7c3080499c6d6543f;hpb=ccc77a1f4f9ec898ee54ac4695f22b59cb56828f diff --git a/opendaylight/web/devices/src/main/java/org/opendaylight/controller/devices/web/Devices.java b/opendaylight/web/devices/src/main/java/org/opendaylight/controller/devices/web/Devices.java index ece5959dc0..6ca60e857d 100644 --- a/opendaylight/web/devices/src/main/java/org/opendaylight/controller/devices/web/Devices.java +++ b/opendaylight/web/devices/src/main/java/org/opendaylight/controller/devices/web/Devices.java @@ -8,8 +8,10 @@ package org.opendaylight.controller.devices.web; +import java.lang.reflect.Type; import java.util.ArrayList; import java.util.HashMap; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; @@ -17,21 +19,22 @@ import java.util.Set; import java.util.TreeMap; import java.util.concurrent.ConcurrentMap; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + import org.codehaus.jackson.map.ObjectMapper; -import org.opendaylight.controller.usermanager.IUserManager; -import org.opendaylight.controller.web.IDaylightWeb; -import org.springframework.security.core.context.SecurityContextHolder; -import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.ResponseBody; import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting; import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig; +import org.opendaylight.controller.sal.authorization.Privilege; import org.opendaylight.controller.sal.authorization.UserLevel; +import org.opendaylight.controller.sal.core.Config; +import org.opendaylight.controller.sal.core.Description; +import org.opendaylight.controller.sal.core.ForwardingMode; import org.opendaylight.controller.sal.core.Name; import org.opendaylight.controller.sal.core.Node; import org.opendaylight.controller.sal.core.NodeConnector; +import org.opendaylight.controller.sal.core.Property; +import org.opendaylight.controller.sal.core.State; import org.opendaylight.controller.sal.core.Tier; import org.opendaylight.controller.sal.utils.GlobalConstants; import org.opendaylight.controller.sal.utils.HexEncode; @@ -43,17 +46,24 @@ import org.opendaylight.controller.switchmanager.SpanConfig; import org.opendaylight.controller.switchmanager.SubnetConfig; import org.opendaylight.controller.switchmanager.Switch; import org.opendaylight.controller.switchmanager.SwitchConfig; +import org.opendaylight.controller.web.DaylightWebUtil; +import org.opendaylight.controller.web.IDaylightWeb; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; import com.google.gson.Gson; +import com.google.gson.reflect.TypeToken; @Controller @RequestMapping("/") public class Devices implements IDaylightWeb { private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER; - private final String WEB_NAME = "Devices"; - private final String WEB_ID = "devices"; - private final short WEB_ORDER = 1; - private final String containerName = GlobalConstants.DEFAULT.toString(); + private static final String WEB_NAME = "Devices"; + private static final String WEB_ID = "devices"; + private static final short WEB_ORDER = 1; public Devices() { ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null); @@ -81,67 +91,122 @@ public class Devices implements IDaylightWeb { @RequestMapping(value = "/nodesLearnt", method = RequestMethod.GET) @ResponseBody - public DevicesJsonBean getNodesLearnt() { + public DevicesJsonBean getNodesLearnt(HttpServletRequest request, + @RequestParam(required = false) String container) { Gson gson = new Gson(); - ISwitchManager switchManager = (ISwitchManager) ServiceHelper - .getInstance(ISwitchManager.class, containerName, this); + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Derive the privilege this user has on the current container + String userName = request.getUserPrincipal().getName(); + Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this); + + ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName, + this); List> nodeData = new ArrayList>(); - for (Switch device : switchManager.getNetworkDevices()) { - HashMap nodeDatum = new HashMap(); - Node node = device.getNode(); - Tier tier = (Tier) switchManager.getNodeProp(node, - Tier.TierPropName); - - nodeDatum.put("containerName", containerName); - nodeDatum.put("nodeName", switchManager.getNodeDescription(node)); - nodeDatum.put("nodeId", node.getNodeIDString()); - int tierNumber = (tier == null) ? TierHelper.unknownTierNumber - : tier.getValue(); - nodeDatum.put("tierName", TierHelper.getTierName(tierNumber) - + " (Tier-" + tierNumber + ")"); - nodeDatum.put("tier", tierNumber + ""); - SwitchConfig sc = switchManager.getSwitchConfig(device.getNode() - .getNodeIDString()); - String modeStr = (sc != null) ? sc.getMode() : "0"; - nodeDatum.put("mode", modeStr); - - nodeDatum.put("json", gson.toJson(nodeDatum)); - nodeDatum.put("mac", - HexEncode.bytesToHexString(device.getDataLayerAddress())); - StringBuffer sb1 = new StringBuffer(); - Set nodeConnectorSet = device.getNodeConnectors(); - String nodeConnectorName; - String nodeConnectorNumberToStr; - if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) { - Map portList = new HashMap(); - for (NodeConnector nodeConnector : nodeConnectorSet) { - nodeConnectorNumberToStr = nodeConnector.getID().toString(); - Name ncName = ((Name) switchManager - .getNodeConnectorProp(nodeConnector, - Name.NamePropName)); - nodeConnectorName = (ncName != null) ? ncName.getValue() : ""; - portList.put(Short.parseShort(nodeConnectorNumberToStr), - nodeConnectorName); + if (switchManager != null && privilege != Privilege.NONE) { + for (Switch device : switchManager.getNetworkDevices()) { + HashMap nodeDatum = new HashMap(); + Node node = device.getNode(); + Tier tier = (Tier) switchManager.getNodeProp(node, Tier.TierPropName); + nodeDatum.put("containerName", containerName); + Description description = (Description) switchManager.getNodeProp(node, Description.propertyName); + String desc = (description == null) ? "" : description.getValue(); + nodeDatum.put("nodeName", desc); + nodeDatum.put("nodeId", node.toString()); + int tierNumber = (tier == null) ? TierHelper.unknownTierNumber : tier.getValue(); + nodeDatum.put("tierName", TierHelper.getTierName(tierNumber) + " (Tier-" + tierNumber + ")"); + nodeDatum.put("tier", tierNumber + ""); + String modeStr = "0"; + ForwardingMode mode = null; + if (!containerName.equals(GlobalConstants.DEFAULT.toString())) { + ISwitchManager switchManagerDefault = (ISwitchManager) ServiceHelper.getInstance( + ISwitchManager.class, GlobalConstants.DEFAULT.toString(), this); + mode = (ForwardingMode) switchManagerDefault.getNodeProp(node, ForwardingMode.name); + } else { + mode = (ForwardingMode) switchManager.getNodeProp(node, ForwardingMode.name); + } + if (mode != null) { + modeStr = String.valueOf(mode.getValue()); } - Map sortedPortList = new TreeMap( - portList); - for (Entry e : sortedPortList.entrySet()) { - sb1.append(e.getValue() + "(" + e.getKey() + ")"); - sb1.append("
"); + nodeDatum.put("mode", modeStr); + + nodeDatum.put("json", gson.toJson(nodeDatum)); + nodeDatum.put("mac", HexEncode.bytesToHexString(device.getDataLayerAddress())); + StringBuffer sb1 = new StringBuffer(); + Set nodeConnectorSet = device.getNodeConnectors(); + if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) { + Map portList = new HashMap(); + List intfList = new ArrayList(); + for (NodeConnector nodeConnector : nodeConnectorSet) { + String nodeConnectorNumberToStr = nodeConnector.getID().toString(); + Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName)); + Config portStatus = ((Config) switchManager + .getNodeConnectorProp(nodeConnector, + Config.ConfigPropName)); + State portState = ((State) switchManager + .getNodeConnectorProp(nodeConnector, + State.StatePropName)); + String nodeConnectorName = (ncName != null) ? ncName + .getValue() : ""; + nodeConnectorName += " (" + nodeConnector.getID() + ")"; + + if (portStatus != null) { + if (portStatus.getValue() == Config.ADMIN_UP) { + if (portState.getValue() == State.EDGE_UP) { + nodeConnectorName = "" + + nodeConnectorName + ""; + } else if (portState.getValue() == State.EDGE_DOWN) { + nodeConnectorName = "" + + nodeConnectorName + ""; + } + } else if (portStatus.getValue() == Config.ADMIN_DOWN) { + nodeConnectorName = "" + + nodeConnectorName + ""; + } + } + + Class idClass = nodeConnector.getID().getClass(); + if (idClass.equals(Short.class)) { + portList.put( + Short.parseShort(nodeConnectorNumberToStr), + nodeConnectorName); + } else { + intfList.add(nodeConnectorName); + } + } + + if (portList.size() > 0) { + Map sortedPortList = new TreeMap( + portList); + + for (Entry e : sortedPortList.entrySet()) { + sb1.append(e.getValue()); + sb1.append("
"); + } + } else if (intfList.size() > 0) { + for (String intf : intfList) { + sb1.append(intf); + sb1.append("
"); + } + } } + nodeDatum.put("ports", sb1.toString()); + nodeData.add(nodeDatum); } - nodeDatum.put("ports", sb1.toString()); - nodeData.add(nodeDatum); } + DevicesJsonBean result = new DevicesJsonBean(); result.setNodeData(nodeData); + result.setPrivilege(privilege); List columnNames = new ArrayList(); columnNames.add("Node ID"); columnNames.add("Node Name"); columnNames.add("Tier"); columnNames.add("Mac Address"); columnNames.add("Ports"); - + columnNames.add("Port Status"); + result.setColumnNames(columnNames); return result; } @@ -151,27 +216,49 @@ public class Devices implements IDaylightWeb { public List getTiers() { return TierHelper.getTiers(); } - + @RequestMapping(value = "/nodesLearnt/update", method = RequestMethod.GET) @ResponseBody public StatusJsonBean updateLearntNode( @RequestParam("nodeName") String nodeName, @RequestParam("nodeId") String nodeId, @RequestParam("tier") String tier, - @RequestParam("operationMode") String operationMode) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } - - StatusJsonBean resultBean = new StatusJsonBean(); + @RequestParam("operationMode") String operationMode, + HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil + .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } + + StatusJsonBean resultBean = new StatusJsonBean(); try { ISwitchManager switchManager = (ISwitchManager) ServiceHelper .getInstance(ISwitchManager.class, containerName, this); - SwitchConfig cfg = new SwitchConfig(nodeId, nodeName, tier, - operationMode); - switchManager.updateSwitchConfig(cfg); - resultBean.setStatus(true); - resultBean.setMessage("Updated node information successfully"); + Map nodeProperties = new HashMap(); + Property desc = new Description(nodeName); + nodeProperties.put(desc.getName(), desc); + Property nodeTier = new Tier(Integer.parseInt(tier)); + nodeProperties.put(nodeTier.getName(), nodeTier); + if (containerName.equals(GlobalConstants.DEFAULT.toString())) { + Property mode = new ForwardingMode(Integer.parseInt(operationMode)); + nodeProperties.put(mode.getName(), mode); + } + SwitchConfig cfg = new SwitchConfig(nodeId, nodeProperties); + Status result = switchManager.updateNodeConfig(cfg); + if (!result.isSuccess()) { + resultBean.setStatus(false); + resultBean.setMessage(result.getDescription()); + } else { + resultBean.setStatus(true); + resultBean.setMessage("Updated node information successfully"); + DaylightWebUtil.auditlog("Node", userName, "updated", nodeId + " to "+ nodeName, containerName); + } } catch (Exception e) { resultBean.setStatus(false); resultBean.setMessage("Error updating node information. " @@ -182,26 +269,41 @@ public class Devices implements IDaylightWeb { @RequestMapping(value = "/staticRoutes", method = RequestMethod.GET) @ResponseBody - public DevicesJsonBean getStaticRoutes() { + public DevicesJsonBean getStaticRoutes(HttpServletRequest request, + @RequestParam(required = false) String container) { Gson gson = new Gson(); + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Derive the privilege this user has on the current container + String userName = request.getUserPrincipal().getName(); + Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this); + IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper - .getInstance(IForwardingStaticRouting.class, containerName, this); + .getInstance(IForwardingStaticRouting.class, containerName, + this); + if (staticRouting == null) { + return null; + } List> staticRoutes = new ArrayList>(); ConcurrentMap routeConfigs = staticRouting .getStaticRouteConfigs(); if (routeConfigs == null) { return null; } - for (StaticRouteConfig conf : routeConfigs.values()) { - Map staticRoute = new HashMap(); - staticRoute.put("name", conf.getName()); - staticRoute.put("staticRoute", conf.getStaticRoute()); - staticRoute.put("nextHopType", conf.getNextHopType()); - staticRoute.put("nextHop", conf.getNextHop()); - staticRoute.put("json", gson.toJson(conf)); - staticRoutes.add(staticRoute); + if (privilege != Privilege.NONE) { + for (StaticRouteConfig conf : routeConfigs.values()) { + Map staticRoute = new HashMap(); + staticRoute.put("name", conf.getName()); + staticRoute.put("staticRoute", conf.getStaticRoute()); + staticRoute.put("nextHopType", conf.getNextHopType()); + staticRoute.put("nextHop", conf.getNextHop()); + staticRoute.put("json", gson.toJson(conf)); + staticRoutes.add(staticRoute); + } } DevicesJsonBean result = new DevicesJsonBean(); + result.setPrivilege(privilege); result.setColumnNames(StaticRouteConfig.getGuiFieldsNames()); result.setNodeData(staticRoutes); return result; @@ -212,11 +314,19 @@ public class Devices implements IDaylightWeb { public StatusJsonBean addStaticRoute( @RequestParam("routeName") String routeName, @RequestParam("staticRoute") String staticRoute, - @RequestParam("nextHop") String nextHop) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } - + @RequestParam("nextHop") String nextHop, + HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil + .getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } + StatusJsonBean result = new StatusJsonBean(); try { IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper @@ -230,6 +340,7 @@ public class Devices implements IDaylightWeb { if (addStaticRouteResult.isSuccess()) { result.setStatus(true); result.setMessage("Static Route saved successfully"); + DaylightWebUtil.auditlog("Static Route", userName, "added", routeName, containerName); } else { result.setStatus(false); result.setMessage(addStaticRouteResult.getDescription()); @@ -244,11 +355,18 @@ public class Devices implements IDaylightWeb { @RequestMapping(value = "/staticRoute/delete", method = RequestMethod.GET) @ResponseBody public StatusJsonBean deleteStaticRoute( - @RequestParam("routesToDelete") String routesToDelete) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } - + @RequestParam("routesToDelete") String routesToDelete, + HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } + StatusJsonBean resultBean = new StatusJsonBean(); try { IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper @@ -266,6 +384,7 @@ public class Devices implements IDaylightWeb { resultBean.setMessage(result.getDescription()); break; } + DaylightWebUtil.auditlog("Static Route", userName, "removed", route, containerName); } } catch (Exception e) { resultBean.setStatus(false); @@ -278,32 +397,65 @@ public class Devices implements IDaylightWeb { @RequestMapping(value = "/subnets", method = RequestMethod.GET) @ResponseBody - public DevicesJsonBean getSubnetGateways() { + public DevicesJsonBean getSubnetGateways(HttpServletRequest request, + @RequestParam(required = false) String container) { Gson gson = new Gson(); List> subnets = new ArrayList>(); - ISwitchManager switchManager = (ISwitchManager) ServiceHelper - .getInstance(ISwitchManager.class, containerName, this); - for (SubnetConfig conf : switchManager.getSubnetsConfigList()) { - Map subnet = new HashMap(); - subnet.put("name", conf.getName()); - subnet.put("subnet", conf.getSubnet()); - subnet.put("json", gson.toJson(conf)); - subnets.add(subnet); + String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container; + + // Derive the privilege this user has on the current container + String userName = request.getUserPrincipal().getName(); + Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this); + + if (privilege != Privilege.NONE) { + ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, + containerName, this); + if (switchManager != null) { + for (SubnetConfig conf : switchManager.getSubnetsConfigList()) { + Map subnet = new HashMap(); + subnet.put("name", conf.getName()); + subnet.put("subnet", conf.getSubnet()); + List portsList = new ArrayList(); + Iterator itor = conf.getNodeConnectors().iterator(); + while(itor.hasNext()) { + SubnetGatewayPortBean bean = new SubnetGatewayPortBean(); + NodeConnector nodeConnector = itor.next(); + String nodeName = getNodeDesc(nodeConnector.getNode().toString(), containerName); + Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName)); + String nodeConnectorName = (ncName != null) ? ncName.getValue() : ""; + bean.setNodeName(nodeName); + bean.setNodePortName(nodeConnectorName); + bean.setNodeId(nodeConnector.getNode().toString()); + bean.setNodePortId(nodeConnector.toString()); + portsList.add(bean); + } + subnet.put("nodePorts", gson.toJson(portsList)); + subnets.add(subnet); + } + } } DevicesJsonBean result = new DevicesJsonBean(); + result.setPrivilege(privilege); result.setColumnNames(SubnetConfig.getGuiFieldsNames()); result.setNodeData(subnets); return result; } - + @RequestMapping(value = "/subnetGateway/add", method = RequestMethod.GET) @ResponseBody public StatusJsonBean addSubnetGateways( @RequestParam("gatewayName") String gatewayName, - @RequestParam("gatewayIPAddress") String gatewayIPAddress) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } + @RequestParam("gatewayIPAddress") String gatewayIPAddress, + HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } StatusJsonBean resultBean = new StatusJsonBean(); try { @@ -315,6 +467,7 @@ public class Devices implements IDaylightWeb { if (result.isSuccess()) { resultBean.setStatus(true); resultBean.setMessage("Added gateway address successfully"); + DaylightWebUtil.auditlog("Subnet Gateway", userName, "added", gatewayName, containerName); } else { resultBean.setStatus(false); resultBean.setMessage(result.getDescription()); @@ -329,11 +482,18 @@ public class Devices implements IDaylightWeb { @RequestMapping(value = "/subnetGateway/delete", method = RequestMethod.GET) @ResponseBody public StatusJsonBean deleteSubnetGateways( - @RequestParam("gatewaysToDelete") String gatewaysToDelete) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } - + @RequestParam("gatewaysToDelete") String gatewaysToDelete, + HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil.getContainerPrivilege(userName, container, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } + StatusJsonBean resultBean = new StatusJsonBean(); try { ISwitchManager switchManager = (ISwitchManager) ServiceHelper @@ -348,6 +508,7 @@ public class Devices implements IDaylightWeb { resultBean.setMessage(result.getDescription()); break; } + DaylightWebUtil.auditlog("Subnet Gateway", userName, "removed", subnet, containerName); } } catch (Exception e) { resultBean.setStatus(false); @@ -361,22 +522,32 @@ public class Devices implements IDaylightWeb { public StatusJsonBean addSubnetGatewayPort( @RequestParam("portsName") String portsName, @RequestParam("ports") String ports, - @RequestParam("nodeId") String nodeId) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } - + @RequestParam("nodeId") String nodeId, HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } + StatusJsonBean resultBean = new StatusJsonBean(); try { ISwitchManager switchManager = (ISwitchManager) ServiceHelper .getInstance(ISwitchManager.class, containerName, this); - Status result = switchManager.addPortsToSubnet(portsName, nodeId - + "/" + ports); + List toAdd = new ArrayList(); + for (String port : ports.split(",")) { + toAdd.add(port); + } + Status result = switchManager.addPortsToSubnet(portsName, toAdd); if (result.isSuccess()) { resultBean.setStatus(true); resultBean .setMessage("Added ports to subnet gateway address successfully"); + DaylightWebUtil.auditlog("Ports to Subnet Gateway", userName, "added", ports, containerName); } else { resultBean.setStatus(false); resultBean.setMessage(result.getDescription()); @@ -392,22 +563,32 @@ public class Devices implements IDaylightWeb { @ResponseBody public StatusJsonBean deleteSubnetGatewayPort( @RequestParam("gatewayName") String gatewayName, - @RequestParam("nodePort") String nodePort) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } - + @RequestParam("nodePort") String nodePort, + HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } + StatusJsonBean resultBean = new StatusJsonBean(); try { ISwitchManager switchManager = (ISwitchManager) ServiceHelper .getInstance(ISwitchManager.class, containerName, this); - Status result = switchManager.removePortsFromSubnet(gatewayName, - nodePort); + List toRemove = new ArrayList(); + for (String port : nodePort.split(",")) { + toRemove.add(port); + } + Status result = switchManager.removePortsFromSubnet(gatewayName, toRemove); if (result.isSuccess()) { resultBean.setStatus(true); - resultBean - .setMessage("Deleted port from subnet gateway address successfully"); + resultBean.setMessage("Deleted port from subnet gateway address successfully"); + DaylightWebUtil.auditlog("Ports from Subnet Gateway", userName, "removed", nodePort, containerName); } else { resultBean.setStatus(false); resultBean.setMessage(result.getDescription()); @@ -421,35 +602,56 @@ public class Devices implements IDaylightWeb { @RequestMapping(value = "/spanPorts", method = RequestMethod.GET) @ResponseBody - public DevicesJsonBean getSpanPorts() { + public DevicesJsonBean getSpanPorts(HttpServletRequest request, + @RequestParam(required = false) String container) { Gson gson = new Gson(); - List spanConfigs_json = new ArrayList(); - ISwitchManager switchManager = (ISwitchManager) ServiceHelper - .getInstance(ISwitchManager.class, containerName, this); - for (SpanConfig conf : switchManager.getSpanConfigList()) { - spanConfigs_json.add(gson.toJson(conf)); - } - ObjectMapper mapper = new ObjectMapper(); List> spanConfigs = new ArrayList>(); - for (String config_json : spanConfigs_json) { - try { - @SuppressWarnings("unchecked") - Map config_data = mapper.readValue(config_json, - HashMap.class); - Map config = new HashMap(); - for (String name : config_data.keySet()) { - config.put(name, config_data.get(name)); - // Add switch name value (non-configuration field) - config.put("nodeName", - getNodeDesc(config_data.get("nodeId"))); + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Derive the privilege this user has on the current container + String userName = request.getUserPrincipal().getName(); + Privilege privilege = DaylightWebUtil.getContainerPrivilege( + userName, containerName, this); + + if (privilege != Privilege.NONE) { + List spanConfigs_json = new ArrayList(); + ISwitchManager switchManager = (ISwitchManager) ServiceHelper + .getInstance(ISwitchManager.class, containerName, this); + if (switchManager != null) { + for (SpanConfig conf : switchManager.getSpanConfigList()) { + spanConfigs_json.add(gson.toJson(conf)); + } + } + ObjectMapper mapper = new ObjectMapper(); + + for (String config_json : spanConfigs_json) { + try { + @SuppressWarnings("unchecked") + Map config_data = mapper.readValue(config_json, + HashMap.class); + Map config = new HashMap(); + for (String name : config_data.keySet()) { + config.put(name, config_data.get(name)); + // Add switch portName value (non-configuration field) + config.put("nodeName", + getNodeDesc(config_data.get("nodeId"), containerName)); + NodeConnector spanPortNodeConnector = NodeConnector.fromString(config_data.get("spanPort")); + Name ncName = ((Name) switchManager.getNodeConnectorProp(spanPortNodeConnector, + Name.NamePropName)); + String spanPortName = (ncName != null) ? ncName.getValue() : ""; + config.put("spanPortName", spanPortName); + } + config.put("json", config_json); + spanConfigs.add(config); + } catch (Exception e) { + // TODO: Handle the exception. } - config.put("json", config_json); - spanConfigs.add(config); - } catch (Exception e) { - // TODO: Handle the exception. } } + DevicesJsonBean result = new DevicesJsonBean(); + result.setPrivilege(privilege); result.setColumnNames(SpanConfig.getGuiFieldsNames()); result.setNodeData(spanConfigs); return result; @@ -457,41 +659,57 @@ public class Devices implements IDaylightWeb { @RequestMapping(value = "/nodeports") @ResponseBody - public Map getNodePorts() { + public String getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container; + + // Derive the privilege this user has on the current container + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) { + return null; + } + ISwitchManager switchManager = (ISwitchManager) ServiceHelper .getInstance(ISwitchManager.class, containerName, this); - if (switchManager == null) + if (switchManager == null) { return null; - - Map nodes = new HashMap(); - Map port; + } + List nodeJsonBeans = new ArrayList(); for (Switch node : switchManager.getNetworkDevices()) { - port = new HashMap(); // new port + NodeJsonBean nodeJsonBean = new NodeJsonBean(); + List port = new ArrayList(); Set nodeConnectorSet = node.getNodeConnectors(); - - if (nodeConnectorSet != null) + if (nodeConnectorSet != null) { for (NodeConnector nodeConnector : nodeConnectorSet) { String nodeConnectorName = ((Name) switchManager - .getNodeConnectorProp(nodeConnector, - Name.NamePropName)).getValue(); - port.put((Short) nodeConnector.getID(), nodeConnectorName - + "(" + nodeConnector.getID() + ")"); + .getNodeConnectorProp(nodeConnector, Name.NamePropName)).getValue(); + port.add(new PortJsonBean(nodeConnector.getID().toString(), nodeConnectorName, nodeConnector.toString())); } - - nodes.put(node.getNode().toString(), port); + } + nodeJsonBean.setNodeId(node.getNode().toString()); + nodeJsonBean.setNodeName(getNodeDesc(node.getNode().toString(), containerName)); + nodeJsonBean.setNodePorts(port); + nodeJsonBeans.add(nodeJsonBean); } - return nodes; + return new Gson().toJson(nodeJsonBeans); } @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET) @ResponseBody - public StatusJsonBean addSpanPort(@RequestParam("jsonData") String jsonData) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } - + public StatusJsonBean addSpanPort( + @RequestParam("jsonData") String jsonData, + HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } + StatusJsonBean resultBean = new StatusJsonBean(); try { Gson gson = new Gson(); @@ -502,6 +720,7 @@ public class Devices implements IDaylightWeb { if (result.isSuccess()) { resultBean.setStatus(true); resultBean.setMessage("SPAN Port added successfully"); + DaylightWebUtil.auditlog("SPAN Port", userName, "added", cfgObject.getNodeId(), containerName); } else { resultBean.setStatus(false); resultBean.setMessage(result.getDescription()); @@ -517,31 +736,38 @@ public class Devices implements IDaylightWeb { @RequestMapping(value = "/spanPorts/delete", method = RequestMethod.GET) @ResponseBody public StatusJsonBean deleteSpanPorts( - @RequestParam("spanPortsToDelete") String spanPortsToDelete) { - if (!authorize(UserLevel.NETWORKADMIN)) { - return unauthorizedMessage(); - } - + @RequestParam("spanPortsToDelete") String spanPortsToDelete, + HttpServletRequest request, + @RequestParam(required = false) String container) { + String containerName = (container == null) ? GlobalConstants.DEFAULT + .toString() : container; + + // Authorization check + String userName = request.getUserPrincipal().getName(); + if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) { + return unauthorizedMessage(); + } + StatusJsonBean resultBean = new StatusJsonBean(); try { Gson gson = new Gson(); ISwitchManager switchManager = (ISwitchManager) ServiceHelper .getInstance(ISwitchManager.class, containerName, this); - String[] spans = spanPortsToDelete.split("###"); - resultBean.setStatus(true); - resultBean.setMessage("SPAN Port(s) deleted successfully"); - for (String span : spans) { - if (!span.isEmpty()) { - SpanConfig cfgObject = gson - .fromJson(span, SpanConfig.class); - Status result = switchManager.removeSpanConfig(cfgObject); - if (!result.isSuccess()) { - resultBean.setStatus(false); - resultBean.setMessage(result.getDescription()); - break; - } + Type collectionType = new TypeToken>() {}.getType(); + List jsonBeanList = gson.fromJson(spanPortsToDelete, collectionType); + for (SpanPortJsonBean jsonBean : jsonBeanList) { + SpanConfig cfgObject = gson + .fromJson(gson.toJson(jsonBean), SpanConfig.class); + Status result = switchManager.removeSpanConfig(cfgObject); + if (!result.isSuccess()) { + resultBean.setStatus(false); + resultBean.setMessage(result.getDescription()); + break; } + DaylightWebUtil.auditlog("SPAN Port", userName, "removed", cfgObject.getNodeId(), containerName); } + resultBean.setStatus(true); + resultBean.setMessage("SPAN Port(s) deleted successfully"); } catch (Exception e) { resultBean.setStatus(false); resultBean.setMessage("Error occurred while deleting span port. " @@ -550,44 +776,43 @@ public class Devices implements IDaylightWeb { return resultBean; } - private String getNodeDesc(String nodeId) { + private String getNodeDesc(String nodeId, String containerName) { ISwitchManager switchManager = (ISwitchManager) ServiceHelper .getInstance(ISwitchManager.class, containerName, this); String description = ""; if (switchManager != null) { - description = switchManager - .getNodeDescription(Node.fromString(nodeId)); + Description desc = (Description) switchManager.getNodeProp(Node.fromString(nodeId), Description.propertyName); + if(desc != null) { + description = desc.getValue(); + } } - return (description.isEmpty() || description.equalsIgnoreCase("none"))? - nodeId : description; + return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId + : description; } - - /** - * Is the operation permitted for the given level - * - * @param level - */ - private boolean authorize(UserLevel level) { - IUserManager userManager = (IUserManager) ServiceHelper - .getGlobalInstance(IUserManager.class, this); - if (userManager == null) { - return false; - } - - String username = SecurityContextHolder.getContext().getAuthentication().getName(); - UserLevel userLevel = userManager.getUserLevel(username); - if (userLevel.toNumber() <= level.toNumber()) { - return true; - } - return false; - } - private StatusJsonBean unauthorizedMessage() { - StatusJsonBean message = new StatusJsonBean(); - message.setStatus(false); - message.setMessage("Operation not authorized"); - return message; + StatusJsonBean message = new StatusJsonBean(); + message.setStatus(false); + message.setMessage("Operation not authorized"); + return message; + } + + @RequestMapping(value = "login") + public String login(final HttpServletRequest request, + final HttpServletResponse response) { + // response.setHeader("X-Page-Location", "/login"); + /* + * IUserManager userManager = (IUserManager) ServiceHelper + * .getGlobalInstance(IUserManager.class, this); if (userManager == + * null) { return "User Manager is not available"; } + * + * String username = request.getUserPrincipal().getName(); + * + * + * model.addAttribute("username", username); model.addAttribute("role", + * userManager.getUserLevel(username).toNumber()); + */ + return "forward:" + "/"; } }