2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.devices.web;
11 import java.lang.reflect.Type;
12 import java.util.ArrayList;
13 import java.util.HashMap;
14 import java.util.Iterator;
15 import java.util.List;
17 import java.util.Map.Entry;
19 import java.util.TreeMap;
20 import java.util.concurrent.ConcurrentMap;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
25 import org.codehaus.jackson.map.ObjectMapper;
26 import org.opendaylight.controller.forwarding.staticrouting.IForwardingStaticRouting;
27 import org.opendaylight.controller.forwarding.staticrouting.StaticRouteConfig;
28 import org.opendaylight.controller.sal.authorization.Privilege;
29 import org.opendaylight.controller.sal.authorization.UserLevel;
30 import org.opendaylight.controller.sal.core.Config;
31 import org.opendaylight.controller.sal.core.Description;
32 import org.opendaylight.controller.sal.core.ForwardingMode;
33 import org.opendaylight.controller.sal.core.Name;
34 import org.opendaylight.controller.sal.core.Node;
35 import org.opendaylight.controller.sal.core.NodeConnector;
36 import org.opendaylight.controller.sal.core.Property;
37 import org.opendaylight.controller.sal.core.State;
38 import org.opendaylight.controller.sal.core.Tier;
39 import org.opendaylight.controller.sal.utils.GlobalConstants;
40 import org.opendaylight.controller.sal.utils.HexEncode;
41 import org.opendaylight.controller.sal.utils.ServiceHelper;
42 import org.opendaylight.controller.sal.utils.Status;
43 import org.opendaylight.controller.sal.utils.TierHelper;
44 import org.opendaylight.controller.switchmanager.ISwitchManager;
45 import org.opendaylight.controller.switchmanager.SpanConfig;
46 import org.opendaylight.controller.switchmanager.SubnetConfig;
47 import org.opendaylight.controller.switchmanager.Switch;
48 import org.opendaylight.controller.switchmanager.SwitchConfig;
49 import org.opendaylight.controller.web.DaylightWebUtil;
50 import org.opendaylight.controller.web.IDaylightWeb;
51 import org.springframework.stereotype.Controller;
52 import org.springframework.web.bind.annotation.RequestMapping;
53 import org.springframework.web.bind.annotation.RequestMethod;
54 import org.springframework.web.bind.annotation.RequestParam;
55 import org.springframework.web.bind.annotation.ResponseBody;
57 import com.google.gson.Gson;
58 import com.google.gson.reflect.TypeToken;
62 public class Devices implements IDaylightWeb {
63 private static final UserLevel AUTH_LEVEL = UserLevel.CONTAINERUSER;
64 private static final String WEB_NAME = "Devices";
65 private static final String WEB_ID = "devices";
66 private static final short WEB_ORDER = 1;
69 ServiceHelper.registerGlobalService(IDaylightWeb.class, this, null);
73 public String getWebName() {
78 public String getWebId() {
83 public short getWebOrder() {
88 public boolean isAuthorized(UserLevel userLevel) {
89 return userLevel.ordinal() <= AUTH_LEVEL.ordinal();
92 @RequestMapping(value = "/nodesLearnt", method = RequestMethod.GET)
94 public DevicesJsonBean getNodesLearnt(HttpServletRequest request, @RequestParam(required = false) String container) {
95 Gson gson = new Gson();
96 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
98 // Derive the privilege this user has on the current container
99 String userName = request.getUserPrincipal().getName();
100 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
102 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
104 List<Map<String, String>> nodeData = new ArrayList<Map<String, String>>();
105 if (switchManager != null && privilege != Privilege.NONE) {
106 for (Switch device : switchManager.getNetworkDevices()) {
107 HashMap<String, String> nodeDatum = new HashMap<String, String>();
108 Node node = device.getNode();
109 Tier tier = (Tier) switchManager.getNodeProp(node, Tier.TierPropName);
110 nodeDatum.put("containerName", containerName);
111 Description description = (Description) switchManager.getNodeProp(node, Description.propertyName);
112 String desc = (description == null) ? "" : description.getValue();
113 nodeDatum.put("nodeName", desc);
114 nodeDatum.put("nodeId", node.toString());
115 int tierNumber = (tier == null) ? TierHelper.unknownTierNumber : tier.getValue();
116 nodeDatum.put("tierName", TierHelper.getTierName(tierNumber) + " (Tier-" + tierNumber + ")");
117 nodeDatum.put("tier", tierNumber + "");
118 String modeStr = "0";
119 ForwardingMode mode = null;
120 if (!containerName.equals(GlobalConstants.DEFAULT.toString())) {
121 ISwitchManager switchManagerDefault = (ISwitchManager) ServiceHelper.getInstance(
122 ISwitchManager.class, GlobalConstants.DEFAULT.toString(), this);
123 mode = (ForwardingMode) switchManagerDefault.getNodeProp(node, ForwardingMode.name);
125 mode = (ForwardingMode) switchManager.getNodeProp(node, ForwardingMode.name);
128 modeStr = String.valueOf(mode.getValue());
130 nodeDatum.put("mode", modeStr);
132 nodeDatum.put("json", gson.toJson(nodeDatum));
133 nodeDatum.put("mac", HexEncode.bytesToHexStringFormat(device.getDataLayerAddress()));
134 StringBuffer sb1 = new StringBuffer();
135 Set<NodeConnector> nodeConnectorSet = device.getNodeConnectors();
136 if (nodeConnectorSet != null && nodeConnectorSet.size() > 0) {
137 Map<Short, String> portList = new HashMap<Short, String>();
138 List<String> intfList = new ArrayList<String>();
139 for (NodeConnector nodeConnector : nodeConnectorSet) {
140 String nodeConnectorNumberToStr = nodeConnector.getID().toString();
141 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName));
142 Config portStatus = ((Config) switchManager.getNodeConnectorProp(nodeConnector,
143 Config.ConfigPropName));
144 State portState = ((State) switchManager.getNodeConnectorProp(nodeConnector,
145 State.StatePropName));
146 String nodeConnectorName = (ncName != null) ? ncName.getValue() : "";
147 nodeConnectorName += " (" + nodeConnector.getID() + ")";
149 if (portStatus != null) {
150 if (portStatus.getValue() == Config.ADMIN_UP) {
151 if (portState.getValue() == State.EDGE_UP) {
152 nodeConnectorName = "<span class='admin-up'>" + nodeConnectorName + "</span>";
153 } else if (portState.getValue() == State.EDGE_DOWN) {
154 nodeConnectorName = "<span class='edge-down'>" + nodeConnectorName + "</span>";
156 } else if (portStatus.getValue() == Config.ADMIN_DOWN) {
157 nodeConnectorName = "<span class='admin-down'>" + nodeConnectorName + "</span>";
161 Class<?> idClass = nodeConnector.getID().getClass();
162 if (idClass.equals(Short.class)) {
163 portList.put(Short.parseShort(nodeConnectorNumberToStr), nodeConnectorName);
165 intfList.add(nodeConnectorName);
169 if (portList.size() > 0) {
170 Map<Short, String> sortedPortList = new TreeMap<Short, String>(portList);
172 for (Entry<Short, String> e : sortedPortList.entrySet()) {
173 sb1.append(e.getValue());
176 } else if (intfList.size() > 0) {
177 for (String intf : intfList) {
183 nodeDatum.put("ports", sb1.toString());
184 nodeData.add(nodeDatum);
188 DevicesJsonBean result = new DevicesJsonBean();
189 result.setNodeData(nodeData);
190 result.setPrivilege(privilege);
191 List<String> columnNames = new ArrayList<String>();
192 columnNames.add("Node ID");
193 columnNames.add("Node Name");
194 columnNames.add("Tier");
195 columnNames.add("Mac Address");
196 columnNames.add("Ports");
197 columnNames.add("Port Status");
199 result.setColumnNames(columnNames);
203 @RequestMapping(value = "/tiers", method = RequestMethod.GET)
205 public List<String> getTiers() {
206 return TierHelper.getTiers();
209 @RequestMapping(value = "/nodesLearnt/update", method = RequestMethod.GET)
211 public StatusJsonBean updateLearntNode(@RequestParam("nodeName") String nodeName,
212 @RequestParam("nodeId") String nodeId, @RequestParam("tier") String tier,
213 @RequestParam("operationMode") String operationMode, HttpServletRequest request,
214 @RequestParam(required = false) String container) {
215 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
217 // Authorization check
218 String userName = request.getUserPrincipal().getName();
219 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
220 return unauthorizedMessage();
223 StatusJsonBean resultBean = new StatusJsonBean();
225 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
226 containerName, this);
227 Map<String, Property> nodeProperties = new HashMap<String, Property>();
228 Property desc = new Description(nodeName);
229 nodeProperties.put(desc.getName(), desc);
230 Property nodeTier = new Tier(Integer.parseInt(tier));
231 nodeProperties.put(nodeTier.getName(), nodeTier);
232 if (containerName.equals(GlobalConstants.DEFAULT.toString())) {
233 Property mode = new ForwardingMode(Integer.parseInt(operationMode));
234 nodeProperties.put(mode.getName(), mode);
236 SwitchConfig cfg = new SwitchConfig(nodeId, nodeProperties);
237 Status result = switchManager.updateNodeConfig(cfg);
238 if (!result.isSuccess()) {
239 resultBean.setStatus(false);
240 resultBean.setMessage(result.getDescription());
242 resultBean.setStatus(true);
243 resultBean.setMessage("Updated node information successfully");
244 DaylightWebUtil.auditlog("Property", userName, "updated",
245 "of Node " + DaylightWebUtil.getNodeDesc(Node.fromString(nodeId), switchManager));
247 } catch (Exception e) {
248 resultBean.setStatus(false);
249 resultBean.setMessage("Error updating node information. " + e.getMessage());
254 @RequestMapping(value = "/staticRoutes", method = RequestMethod.GET)
256 public DevicesJsonBean getStaticRoutes(HttpServletRequest request, @RequestParam(required = false) String container) {
257 Gson gson = new Gson();
258 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
260 // Derive the privilege this user has on the current container
261 String userName = request.getUserPrincipal().getName();
262 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
264 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper.getInstance(
265 IForwardingStaticRouting.class, containerName, this);
266 if (staticRouting == null) {
269 List<Map<String, String>> staticRoutes = new ArrayList<Map<String, String>>();
270 ConcurrentMap<String, StaticRouteConfig> routeConfigs = staticRouting.getStaticRouteConfigs();
271 if (routeConfigs == null) {
274 if (privilege != Privilege.NONE) {
275 for (StaticRouteConfig conf : routeConfigs.values()) {
276 Map<String, String> staticRoute = new HashMap<String, String>();
277 staticRoute.put("name", conf.getName());
278 staticRoute.put("staticRoute", conf.getStaticRoute());
279 staticRoute.put("nextHopType", conf.getNextHopType());
280 staticRoute.put("nextHop", conf.getNextHop());
281 staticRoute.put("json", gson.toJson(conf));
282 staticRoutes.add(staticRoute);
285 DevicesJsonBean result = new DevicesJsonBean();
286 result.setPrivilege(privilege);
287 result.setColumnNames(StaticRouteConfig.getGuiFieldsNames());
288 result.setNodeData(staticRoutes);
292 @RequestMapping(value = "/staticRoute/add", method = RequestMethod.GET)
294 public StatusJsonBean addStaticRoute(@RequestParam("routeName") String routeName,
295 @RequestParam("staticRoute") String staticRoute, @RequestParam("nextHop") String nextHop,
296 HttpServletRequest request, @RequestParam(required = false) String container) {
297 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
299 // Authorization check
300 String userName = request.getUserPrincipal().getName();
301 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
302 return unauthorizedMessage();
305 StatusJsonBean result = new StatusJsonBean();
307 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper.getInstance(
308 IForwardingStaticRouting.class, containerName, this);
309 StaticRouteConfig config = new StaticRouteConfig();
310 config.setName(routeName);
311 config.setStaticRoute(staticRoute);
312 config.setNextHop(nextHop);
313 Status addStaticRouteResult = staticRouting.addStaticRoute(config);
314 if (addStaticRouteResult.isSuccess()) {
315 result.setStatus(true);
316 result.setMessage("Static Route saved successfully");
317 DaylightWebUtil.auditlog("Static Route", userName, "added", routeName, containerName);
319 result.setStatus(false);
320 result.setMessage(addStaticRouteResult.getDescription());
322 } catch (Exception e) {
323 result.setStatus(false);
324 result.setMessage("Error - " + e.getMessage());
329 @RequestMapping(value = "/staticRoute/delete", method = RequestMethod.GET)
331 public StatusJsonBean deleteStaticRoute(@RequestParam("routesToDelete") String routesToDelete,
332 HttpServletRequest request, @RequestParam(required = false) String container) {
333 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
335 // Authorization check
336 String userName = request.getUserPrincipal().getName();
337 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
338 return unauthorizedMessage();
341 StatusJsonBean resultBean = new StatusJsonBean();
343 IForwardingStaticRouting staticRouting = (IForwardingStaticRouting) ServiceHelper.getInstance(
344 IForwardingStaticRouting.class, containerName, this);
345 String[] routes = routesToDelete.split(",");
347 resultBean.setStatus(true);
348 resultBean.setMessage("Successfully deleted selected static routes");
349 for (String route : routes) {
350 result = staticRouting.removeStaticRoute(route);
351 if (!result.isSuccess()) {
352 resultBean.setStatus(false);
353 resultBean.setMessage(result.getDescription());
356 DaylightWebUtil.auditlog("Static Route", userName, "removed", route, containerName);
358 } catch (Exception e) {
359 resultBean.setStatus(false);
360 resultBean.setMessage("Error occurred while deleting static routes. " + e.getMessage());
365 @RequestMapping(value = "/subnets", method = RequestMethod.GET)
367 public DevicesJsonBean getSubnetGateways(HttpServletRequest request,
368 @RequestParam(required = false) String container) {
369 Gson gson = new Gson();
370 List<Map<String, String>> subnets = new ArrayList<Map<String, String>>();
371 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
373 // Derive the privilege this user has on the current container
374 String userName = request.getUserPrincipal().getName();
375 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
377 if (privilege != Privilege.NONE) {
378 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
379 containerName, this);
380 if (switchManager != null) {
381 for (SubnetConfig conf : switchManager.getSubnetsConfigList()) {
382 Map<String, String> subnet = new HashMap<String, String>();
383 subnet.put("name", conf.getName());
384 subnet.put("subnet", conf.getSubnet());
385 List<SubnetGatewayPortBean> portsList = new ArrayList<SubnetGatewayPortBean>();
386 Iterator<NodeConnector> itor = conf.getNodeConnectors().iterator();
387 while (itor.hasNext()) {
388 SubnetGatewayPortBean bean = new SubnetGatewayPortBean();
389 NodeConnector nodeConnector = itor.next();
390 String nodeName = getNodeDesc(nodeConnector.getNode().toString(), containerName);
391 Name ncName = ((Name) switchManager.getNodeConnectorProp(nodeConnector, Name.NamePropName));
392 String nodeConnectorName = (ncName != null) ? ncName.getValue() : "";
393 bean.setNodeName(nodeName);
394 bean.setNodePortName(nodeConnectorName);
395 bean.setNodeId(nodeConnector.getNode().toString());
396 bean.setNodePortId(nodeConnector.toString());
399 subnet.put("nodePorts", gson.toJson(portsList));
404 DevicesJsonBean result = new DevicesJsonBean();
405 result.setPrivilege(privilege);
406 result.setColumnNames(SubnetConfig.getGuiFieldsNames());
407 result.setNodeData(subnets);
411 @RequestMapping(value = "/subnetGateway/add", method = RequestMethod.GET)
413 public StatusJsonBean addSubnetGateways(@RequestParam("gatewayName") String gatewayName,
414 @RequestParam("gatewayIPAddress") String gatewayIPAddress, HttpServletRequest request,
415 @RequestParam(required = false) String container) {
416 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
418 // Authorization check
419 String userName = request.getUserPrincipal().getName();
420 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
421 return unauthorizedMessage();
424 StatusJsonBean resultBean = new StatusJsonBean();
426 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
427 containerName, this);
428 SubnetConfig cfgObject = new SubnetConfig(gatewayName, gatewayIPAddress, new ArrayList<String>());
429 Status result = switchManager.addSubnet(cfgObject);
430 if (result.isSuccess()) {
431 resultBean.setStatus(true);
432 resultBean.setMessage("Added gateway address successfully");
433 DaylightWebUtil.auditlog("Subnet Gateway", userName, "added", gatewayName, containerName);
435 resultBean.setStatus(false);
436 resultBean.setMessage(result.getDescription());
438 } catch (Exception e) {
439 resultBean.setStatus(false);
440 resultBean.setMessage(e.getMessage());
445 @RequestMapping(value = "/subnetGateway/delete", method = RequestMethod.GET)
447 public StatusJsonBean deleteSubnetGateways(@RequestParam("gatewaysToDelete") String gatewaysToDelete,
448 HttpServletRequest request, @RequestParam(required = false) String container) {
449 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
451 // Authorization check
452 String userName = request.getUserPrincipal().getName();
453 if (DaylightWebUtil.getContainerPrivilege(userName, container, this) != Privilege.WRITE) {
454 return unauthorizedMessage();
457 StatusJsonBean resultBean = new StatusJsonBean();
459 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
460 containerName, this);
461 String[] subnets = gatewaysToDelete.split(",");
462 resultBean.setStatus(true);
463 resultBean.setMessage("Added gateway address successfully");
464 for (String subnet : subnets) {
465 Status result = switchManager.removeSubnet(subnet);
466 if (!result.isSuccess()) {
467 resultBean.setStatus(false);
468 resultBean.setMessage(result.getDescription());
471 DaylightWebUtil.auditlog("Subnet Gateway", userName, "removed", subnet, containerName);
473 } catch (Exception e) {
474 resultBean.setStatus(false);
475 resultBean.setMessage(e.getMessage());
480 @RequestMapping(value = "/subnetGateway/ports/add", method = RequestMethod.GET)
482 public StatusJsonBean addSubnetGatewayPort(@RequestParam("portsName") String portsName,
483 @RequestParam("ports") String ports, @RequestParam("nodeId") String nodeId, HttpServletRequest request,
484 @RequestParam(required = false) String container) {
485 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
487 // Authorization check
488 String userName = request.getUserPrincipal().getName();
489 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
490 return unauthorizedMessage();
493 StatusJsonBean resultBean = new StatusJsonBean();
495 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
496 containerName, this);
497 List<String> toAdd = new ArrayList<String>();
498 for (String port : ports.split(",")) {
501 Status result = switchManager.addPortsToSubnet(portsName, toAdd);
503 if (result.isSuccess()) {
504 resultBean.setStatus(true);
505 resultBean.setMessage("Added ports to subnet gateway address successfully");
506 for (String port : toAdd) {
507 DaylightWebUtil.auditlog("Port", userName, "added",
508 DaylightWebUtil.getPortName(NodeConnector.fromString(port), switchManager)
509 + " to Subnet Gateway " + portsName, containerName);
512 resultBean.setStatus(false);
513 resultBean.setMessage(result.getDescription());
515 } catch (Exception e) {
516 resultBean.setStatus(false);
517 resultBean.setMessage(e.getMessage());
522 @RequestMapping(value = "/subnetGateway/ports/delete", method = RequestMethod.GET)
524 public StatusJsonBean deleteSubnetGatewayPort(@RequestParam("gatewayName") String gatewayName,
525 @RequestParam("nodePort") String nodePort, HttpServletRequest request,
526 @RequestParam(required = false) String container) {
527 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
529 // Authorization check
530 String userName = request.getUserPrincipal().getName();
531 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
532 return unauthorizedMessage();
535 StatusJsonBean resultBean = new StatusJsonBean();
537 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
538 containerName, this);
539 List<String> toRemove = new ArrayList<String>();
540 for (String port : nodePort.split(",")) {
543 Status result = switchManager.removePortsFromSubnet(gatewayName, toRemove);
545 if (result.isSuccess()) {
546 resultBean.setStatus(true);
547 resultBean.setMessage("Deleted port from subnet gateway address successfully");
548 for (String port : toRemove) {
549 DaylightWebUtil.auditlog("Port", userName, "removed",
550 DaylightWebUtil.getPortName(NodeConnector.fromString(port), switchManager)
551 + " from Subnet Gateway " + gatewayName, containerName);
554 resultBean.setStatus(false);
555 resultBean.setMessage(result.getDescription());
557 } catch (Exception e) {
558 resultBean.setStatus(false);
559 resultBean.setMessage(e.getMessage());
564 @RequestMapping(value = "/spanPorts", method = RequestMethod.GET)
566 public DevicesJsonBean getSpanPorts(HttpServletRequest request, @RequestParam(required = false) String container) {
567 Gson gson = new Gson();
568 List<Map<String, String>> spanConfigs = new ArrayList<Map<String, String>>();
569 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
571 // Derive the privilege this user has on the current container
572 String userName = request.getUserPrincipal().getName();
573 Privilege privilege = DaylightWebUtil.getContainerPrivilege(userName, containerName, this);
575 if (privilege != Privilege.NONE) {
576 List<String> spanConfigs_json = new ArrayList<String>();
577 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
578 containerName, this);
579 if (switchManager != null) {
580 for (SpanConfig conf : switchManager.getSpanConfigList()) {
581 spanConfigs_json.add(gson.toJson(conf));
584 ObjectMapper mapper = new ObjectMapper();
586 for (String config_json : spanConfigs_json) {
588 @SuppressWarnings("unchecked")
589 Map<String, String> config_data = mapper.readValue(config_json, HashMap.class);
590 Map<String, String> config = new HashMap<String, String>();
591 for (String name : config_data.keySet()) {
592 config.put(name, config_data.get(name));
593 // Add switch portName value (non-configuration field)
594 config.put("nodeName", getNodeDesc(config_data.get("nodeId"), containerName));
595 NodeConnector spanPortNodeConnector = NodeConnector.fromString(config_data.get("spanPort"));
596 Name ncName = ((Name) switchManager.getNodeConnectorProp(spanPortNodeConnector,
598 String spanPortName = (ncName != null) ? ncName.getValue() : "";
599 config.put("spanPortName", spanPortName);
601 config.put("json", config_json);
602 spanConfigs.add(config);
603 } catch (Exception e) {
604 // TODO: Handle the exception.
609 DevicesJsonBean result = new DevicesJsonBean();
610 result.setPrivilege(privilege);
611 result.setColumnNames(SpanConfig.getGuiFieldsNames());
612 result.setNodeData(spanConfigs);
616 @RequestMapping(value = "/nodeports")
618 public String getNodePorts(HttpServletRequest request, @RequestParam(required = false) String container) {
619 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
621 // Derive the privilege this user has on the current container
622 String userName = request.getUserPrincipal().getName();
623 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) == Privilege.NONE) {
627 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
629 if (switchManager == null) {
632 List<NodeJsonBean> nodeJsonBeans = new ArrayList<NodeJsonBean>();
634 for (Switch node : switchManager.getNetworkDevices()) {
635 NodeJsonBean nodeJsonBean = new NodeJsonBean();
636 List<PortJsonBean> port = new ArrayList<PortJsonBean>();
637 Set<NodeConnector> nodeConnectorSet = node.getNodeConnectors();
638 if (nodeConnectorSet != null) {
639 for (NodeConnector nodeConnector : nodeConnectorSet) {
640 String nodeConnectorName = ((Name) switchManager.getNodeConnectorProp(nodeConnector,
641 Name.NamePropName)).getValue();
642 port.add(new PortJsonBean(nodeConnector.getID().toString(), nodeConnectorName, nodeConnector
646 nodeJsonBean.setNodeId(node.getNode().toString());
647 nodeJsonBean.setNodeName(getNodeDesc(node.getNode().toString(), containerName));
648 nodeJsonBean.setNodePorts(port);
649 nodeJsonBeans.add(nodeJsonBean);
652 return new Gson().toJson(nodeJsonBeans);
655 @RequestMapping(value = "/spanPorts/add", method = RequestMethod.GET)
657 public StatusJsonBean addSpanPort(@RequestParam("jsonData") String jsonData, HttpServletRequest request,
658 @RequestParam(required = false) String container) {
659 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
661 // Authorization check
662 String userName = request.getUserPrincipal().getName();
663 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
664 return unauthorizedMessage();
667 StatusJsonBean resultBean = new StatusJsonBean();
669 Gson gson = new Gson();
670 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
671 containerName, this);
672 SpanConfig cfgObject = gson.fromJson(jsonData, SpanConfig.class);
673 Status result = switchManager.addSpanConfig(cfgObject);
674 if (result.isSuccess()) {
675 resultBean.setStatus(true);
676 resultBean.setMessage("SPAN Port added successfully");
677 DaylightWebUtil.auditlog("SPAN Port", userName, "added",
678 DaylightWebUtil.getPortName(NodeConnector.fromString(cfgObject.getSpanPort()), switchManager),
681 resultBean.setStatus(false);
682 resultBean.setMessage(result.getDescription());
684 } catch (Exception e) {
685 resultBean.setStatus(false);
686 resultBean.setMessage("Error occurred while adding span port. " + e.getMessage());
691 @RequestMapping(value = "/spanPorts/delete", method = RequestMethod.GET)
693 public StatusJsonBean deleteSpanPorts(@RequestParam("spanPortsToDelete") String spanPortsToDelete,
694 HttpServletRequest request, @RequestParam(required = false) String container) {
695 String containerName = (container == null) ? GlobalConstants.DEFAULT.toString() : container;
697 // Authorization check
698 String userName = request.getUserPrincipal().getName();
699 if (DaylightWebUtil.getContainerPrivilege(userName, containerName, this) != Privilege.WRITE) {
700 return unauthorizedMessage();
703 StatusJsonBean resultBean = new StatusJsonBean();
705 Gson gson = new Gson();
706 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
707 containerName, this);
708 Type collectionType = new TypeToken<List<SpanPortJsonBean>>() {
710 List<SpanPortJsonBean> jsonBeanList = gson.fromJson(spanPortsToDelete, collectionType);
711 for (SpanPortJsonBean jsonBean : jsonBeanList) {
712 SpanConfig cfgObject = gson.fromJson(gson.toJson(jsonBean), SpanConfig.class);
713 Status result = switchManager.removeSpanConfig(cfgObject);
714 if (!result.isSuccess()) {
715 resultBean.setStatus(false);
716 resultBean.setMessage(result.getDescription());
719 DaylightWebUtil.auditlog("SPAN Port", userName, "removed",
720 DaylightWebUtil.getPortName(NodeConnector.fromString(cfgObject.getSpanPort()), switchManager),
723 resultBean.setStatus(true);
724 resultBean.setMessage("SPAN Port(s) deleted successfully");
725 } catch (Exception e) {
726 resultBean.setStatus(false);
727 resultBean.setMessage("Error occurred while deleting span port. " + e.getMessage());
732 private String getNodeDesc(String nodeId, String containerName) {
733 ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, containerName,
735 String description = "";
736 if (switchManager != null) {
737 Description desc = (Description) switchManager.getNodeProp(Node.fromString(nodeId),
738 Description.propertyName);
740 description = desc.getValue();
743 return (description.isEmpty() || description.equalsIgnoreCase("none")) ? nodeId : description;
746 private StatusJsonBean unauthorizedMessage() {
747 StatusJsonBean message = new StatusJsonBean();
748 message.setStatus(false);
749 message.setMessage("Operation not authorized");
753 @RequestMapping(value = "login")
754 public String login(final HttpServletRequest request, final HttpServletResponse response) {
756 * IUserManager userManager = (IUserManager) ServiceHelper
757 * .getGlobalInstance(IUserManager.class, this); if (userManager ==
758 * null) { return "User Manager is not available"; }
760 * String username = request.getUserPrincipal().getName();
763 * model.addAttribute("username", username); model.addAttribute("role",
764 * userManager.getUserLevel(username).toNumber());
766 return "forward:" + "/";