+ @RequestMapping(value = "/connect/nodes", method = RequestMethod.GET)
+ @ResponseBody
+ public List<NodeJsonBean> getNodes(HttpServletRequest request) {
+ IConnectionManager connectionManager = (IConnectionManager) ServiceHelper.getGlobalInstance(
+ IConnectionManager.class, this);
+ if (connectionManager == null) {
+ return null;
+ }
+ ISwitchManager switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class,
+ GlobalConstants.DEFAULT.toString(), this);
+ if (switchManager == null) {
+ return null;
+ }
+
+ Set<Node> nodes = connectionManager.getLocalNodes();
+ List<NodeJsonBean> result = new LinkedList<NodeJsonBean>();
+ for (Node node : nodes) {
+ Description descriptionProperty = (Description) switchManager.getNodeProp(node, "description");
+ String description = descriptionProperty.getValue();
+ NodeJsonBean nodeBean = new NodeJsonBean();
+ nodeBean.setNodeId(node.getNodeIDString());
+ nodeBean.setNodeType(node.getType());
+ if (description.equals("None")) {
+ nodeBean.setNodeName(node.toString());
+ } else {
+ nodeBean.setNodeName(description);
+ }
+ result.add(nodeBean);
+ }
+
+ return result;
+ }
+
+ @RequestMapping(value = "/connect/{nodeId}", method = RequestMethod.POST)
+ @ResponseBody
+ public Status addNode(HttpServletRequest request, @PathVariable("nodeId") String nodeId,
+ @RequestParam(required = true) String ipAddress, @RequestParam(required = true) String port,
+ @RequestParam(required = false) String nodeType) {
+ IConnectionManager connectionManager = (IConnectionManager) ServiceHelper.getGlobalInstance(
+ IConnectionManager.class, this);
+ if (connectionManager == null) {
+ return new Status(StatusCode.NOTFOUND, "Service not found");
+ }
+
+ if (!NetUtils.isIPv4AddressValid(ipAddress)) {
+ return new Status(StatusCode.NOTACCEPTABLE, "Invalid IP Address: " + ipAddress);
+ }
+
+ try {
+ Integer.parseInt(port);
+ } catch (Exception e) {
+ return new Status(StatusCode.NOTACCEPTABLE, "Invalid Layer 4 Port: " + port);
+ }
+
+ Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
+ params.put(ConnectionConstants.ADDRESS, ipAddress);
+ params.put(ConnectionConstants.PORT, port);
+
+ Node node = null;
+ if (nodeType != null) {
+ node = connectionManager.connect(nodeType, nodeId, params);
+ } else {
+ node = connectionManager.connect(nodeId, params);
+ }
+ if (node == null) {
+ return new Status(StatusCode.NOTFOUND, "Failed to connect to Node at " + ipAddress + ":" + port);
+ }
+ return new Status(StatusCode.SUCCESS);
+ }
+
+ @RequestMapping(value = "/disconnect/{nodeId}", method = RequestMethod.POST)
+ @ResponseBody
+ public Status removeNode(HttpServletRequest request, @PathVariable("nodeId") String nodeId,
+ @RequestParam(required = true) String nodeType) {
+ IConnectionManager connectionManager = (IConnectionManager) ServiceHelper.getGlobalInstance(
+ IConnectionManager.class, this);
+ if (connectionManager == null) {
+ return new Status(StatusCode.NOTFOUND, "Service not found");
+ }
+
+ try {
+ Node node = new Node(nodeType, nodeId);
+ return connectionManager.disconnect(node);
+ } catch (Exception e) {
+ return new Status(StatusCode.NOTFOUND, "Resource not found");
+ }
+ }
+