Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package aasdntool; import java.awt.Dimension; import java.awt.event.MouseEvent; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.InetAddress; import java.net.URL; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JList; import javax.swing.JOptionPane; import javax.swing.table.DefaultTableModel; import com.fasterxml.jackson.databind.ObjectMapper; import aasdntool.Device.AttachmentPoint; import edu.uci.ics.jung.algorithms.layout.CircleLayout; import edu.uci.ics.jung.algorithms.layout.Layout; import edu.uci.ics.jung.graph.Graph; import edu.uci.ics.jung.graph.SparseMultigraph; import edu.uci.ics.jung.visualization.VisualizationViewer; import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse; import edu.uci.ics.jung.visualization.control.GraphMouseListener; import edu.uci.ics.jung.visualization.control.ModalGraphMouse; import edu.uci.ics.jung.visualization.decorators.ToStringLabeller; /** * * @author ateethkumar */ public class AASDNTool extends javax.swing.JFrame { /** * Creates new form AASDNTool */ public static String controllerIP = ""; public static JList<String> list; Graph<String, String> networkGraph; public AASDNTool() { initComponents(); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always * regenerated by the Form Editor. */ // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { aaSDNTool = new javax.swing.JPanel(); controllerConnect = new javax.swing.JButton(); controllerIpAddress = new javax.swing.JTextField(); openFlowSwitches = new javax.swing.JButton(); devicePane = new javax.swing.JScrollPane(); switchPane = new javax.swing.JScrollPane(); switchTable = new javax.swing.JTable(); macTable = new javax.swing.JTable(); devices = new javax.swing.JButton(); topology = new javax.swing.JButton(); controllerLabel = new javax.swing.JLabel(); topologyLabel = new javax.swing.JLabel(); setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); controllerConnect.setText("Connect"); controllerConnect.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { controllerConnectActionPerformed(evt); } }); controllerIpAddress.setText(""); controllerIpAddress.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { controllerIpAddressActionPerformed(evt); } }); openFlowSwitches.setText("OpenFlow Switches"); openFlowSwitches.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { openFlowSwitchesActionPerformed(evt); } }); switchTable.setModel(new javax.swing.table.DefaultTableModel(new Object[][] { }, new String[] { "Switch DPID", "Switch IP" })); switchPane.setViewportView(switchTable); devices.setText("MACs"); devices.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { devicesActionPerformed(evt); } }); topology.setText("Topology"); topology.addActionListener(new java.awt.event.ActionListener() { public void actionPerformed(java.awt.event.ActionEvent evt) { topologyActionPerformed(evt); } }); controllerLabel.setText("Enter Controller IP (Floodlight) : "); topologyLabel.setText("View Topology and Login to Switches :"); macTable.setModel( new javax.swing.table.DefaultTableModel(new Object[][] {}, new String[] { "MAC Address" })); devicePane.setViewportView(macTable); javax.swing.GroupLayout aaSDNToolLayout = new javax.swing.GroupLayout(aaSDNTool); aaSDNTool.setLayout(aaSDNToolLayout); aaSDNToolLayout.setHorizontalGroup(aaSDNToolLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(aaSDNToolLayout.createSequentialGroup().addGap(56, 56, 56) .addGroup(aaSDNToolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(controllerLabel) .addComponent(controllerConnect, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(controllerIpAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE) .addComponent(topology).addComponent(topologyLabel)) .addGap(74, 74, 74) .addGroup(aaSDNToolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(aaSDNToolLayout.createSequentialGroup().addGap(6, 6, 6).addComponent( switchPane, javax.swing.GroupLayout.PREFERRED_SIZE, 334, javax.swing.GroupLayout.PREFERRED_SIZE)) .addComponent(openFlowSwitches)) .addGap(46, 46, 46) .addGroup(aaSDNToolLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addComponent(devices).addComponent(devicePane, javax.swing.GroupLayout.PREFERRED_SIZE, 176, javax.swing.GroupLayout.PREFERRED_SIZE)) .addContainerGap(235, Short.MAX_VALUE))); aaSDNToolLayout .setVerticalGroup(aaSDNToolLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(aaSDNToolLayout .createSequentialGroup().addGroup(aaSDNToolLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup( aaSDNToolLayout.createSequentialGroup().addGap(70, 70, 70) .addGroup(aaSDNToolLayout .createParallelGroup( javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(openFlowSwitches) .addComponent(devices))) .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, aaSDNToolLayout .createSequentialGroup().addContainerGap() .addComponent(controllerLabel))) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addGroup(aaSDNToolLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(aaSDNToolLayout.createSequentialGroup() .addComponent(controllerIpAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(controllerConnect) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 402, Short.MAX_VALUE) .addComponent(topologyLabel) .addPreferredGap( javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(topology).addGap(96, 96, 96)) .addGroup(aaSDNToolLayout.createSequentialGroup().addGroup(aaSDNToolLayout .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false) .addComponent(switchPane, javax.swing.GroupLayout.DEFAULT_SIZE, 214, Short.MAX_VALUE) .addComponent(devicePane, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)) .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(aaSDNTool, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 0, Short.MAX_VALUE))); layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(aaSDNTool, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE) .addGap(0, 34, Short.MAX_VALUE))); pack(); }// </editor-fold> private void controllerIpAddressActionPerformed(java.awt.event.ActionEvent evt) { // nothing to be handled } private void controllerConnectActionPerformed(java.awt.event.ActionEvent evt) { int timeOut = 5000; try { if (!InetAddress.getByName(controllerIpAddress.getText()).isReachable(timeOut)) { JOptionPane.showMessageDialog(new javax.swing.JFrame(), "Sorry! The connection was unsuccessful or You've entered the wrong controller IP address"); } else { JOptionPane.showMessageDialog(new javax.swing.JFrame(), "Successful!Connected to the controller"); AASDNTool.controllerIP = controllerIpAddress.getText(); } } catch (IOException ex) { System.err.println("Error connecting" + ex); } } private void openFlowSwitchesActionPerformed(java.awt.event.ActionEvent evt) { StringBuffer response = new StringBuffer(); try { URL obj = new URL("http://" + controllerIP + ":8080/wm/core/controller/switches/json"); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + "http://" + controllerIP + ":8080/wm/core/controller/switches/json"); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); ObjectMapper mapper = new ObjectMapper(); List<Switches> switchList = mapper.readValue(response.toString(), mapper.getTypeFactory().constructCollectionType(List.class, Switches.class)); DefaultTableModel dtm = new DefaultTableModel(0, 0); // add header of the table String header[] = new String[] { "Switch DPID", "IP Address" }; // add header in table model dtm.setColumnIdentifiers(header); switchTable.setModel(dtm); // add row dynamically into the table for (Switches sw : switchList) { dtm.addRow(new Object[] { sw.getDpid(), sw.getInetAddress() }); } } catch (Exception e) { System.out.println("Exception Occured:" + e); } } private void devicesActionPerformed(java.awt.event.ActionEvent evt) { StringBuffer response = new StringBuffer(); try { URL obj = new URL("http://" + controllerIP + ":8080/wm/device/"); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + "http://" + controllerIP + ":8080/wm/device/"); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); ObjectMapper mapper = new ObjectMapper(); List<Device> devList = mapper.readValue(response.toString(), mapper.getTypeFactory().constructCollectionType(List.class, Device.class)); DefaultTableModel dtm = new DefaultTableModel(0, 0); // add header of the table String header[] = new String[] { "MAC Address" }; // add header in table model dtm.setColumnIdentifiers(header); macTable.setModel(dtm); for (Device dev : devList) { dtm.addRow(new Object[] { dev.getMac() }); } } catch (Exception e) { System.out.println("Exception occured:" + e); } } @SuppressWarnings("unchecked") private void topologyActionPerformed(java.awt.event.ActionEvent evt) { try { StringBuffer response = new StringBuffer(); URL obj = new URL("http://" + controllerIP + ":8080/wm/topology/links/json"); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + "http://" + controllerIP + ":8080/wm/topology/links/json"); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); ObjectMapper mapper = new ObjectMapper(); List<Topology> topology_links = mapper.readValue(response.toString(), mapper.getTypeFactory().constructCollectionType(List.class, Topology.class)); // Create a graph of the topology networkGraph = new SparseMultigraph<String, String>(); // Add switches to the graph for (Topology topo : topology_links) { networkGraph.addVertex(topo.getSrc_switch()); } for (Topology topo : topology_links) { networkGraph.addVertex(topo.getDst_switch()); } // Add links to the switches for (Topology topo : topology_links) { try { networkGraph.addEdge( topo.getSrc_switch().substring(topo.getSrc_switch().length() - 4) + " to " + topo.getDst_switch().substring(topo.getDst_switch().length() - 4), topo.getSrc_switch(), topo.getDst_switch()); } catch (java.lang.IllegalArgumentException e) { System.out.println("Double link detected"); } } } catch (Exception e) { System.out.println("Exception occured:" + e); } try { StringBuffer response = new StringBuffer(); URL obj = new URL("http://" + controllerIP + ":8080/wm/device/"); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); // optional default is GET con.setRequestMethod("GET"); int responseCode = con.getResponseCode(); System.out.println("\nSending 'GET' request to URL : " + "http://" + controllerIP + ":8080/wm/device/"); System.out.println("Response Code : " + responseCode); BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream())); String inputLine; while ((inputLine = in.readLine()) != null) { response.append(inputLine); } in.close(); ObjectMapper deviceMapper = new ObjectMapper(); List<Device> topology_links_devices = deviceMapper.readValue(response.toString(), deviceMapper.getTypeFactory().constructCollectionType(List.class, Device.class)); for (Device deviceLinks : topology_links_devices) { networkGraph.addVertex(deviceLinks.getMac().get(0)); ArrayList<AttachmentPoint> deviceLinkAttachementPoint = deviceLinks.getAttachmentPoint(); if (deviceLinkAttachementPoint != null && deviceLinkAttachementPoint.size() > 0) { networkGraph.addEdge( deviceLinks.getMac().get(0) + deviceLinkAttachementPoint.get(0).getSwitchDPID(), deviceLinks.getMac().get(0), deviceLinkAttachementPoint.get(0).getSwitchDPID()); } else { System.out.println("Device " + deviceLinks.getMac().get(0) + " is not connected to any switch"); } } // The Layout<V, E> is parameterized by the vertex and edge types Layout<Integer, String> layout = new CircleLayout(networkGraph); layout.setSize(new Dimension(800, 800)); // sets the initial size of // the layout space // The BasicVisualizationServer<V,E> is parameterized by the vertex // and edge types VisualizationViewer<Integer, String> vv = new VisualizationViewer<Integer, String>(layout); vv.setPreferredSize(new Dimension(1000, 1000)); // Sets the viewing // area size vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller()); vv.getRenderContext().setEdgeLabelTransformer(new ToStringLabeller()); final DefaultModalGraphMouse graphMouse = new DefaultModalGraphMouse(); vv.setGraphMouse(graphMouse); graphMouse.setMode(ModalGraphMouse.Mode.PICKING); vv.addGraphMouseListener(new GraphMouseListener() { public void graphClicked(Object vertex, MouseEvent mouseEvent) { if (mouseEvent.getClickCount() == 2) { System.out.println("Double clicked " + vertex); Login login = new Login(); login.initialize(vertex.toString()); } } public void graphPressed(Object arg0, MouseEvent arg1) { // TODO Auto-generated method stub } public void graphReleased(Object arg0, MouseEvent arg1) { // TODO Auto-generated method stub } }); JFrame frame = new JFrame("Topology"); //frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(vv); frame.pack(); frame.setVisible(true); } catch (Exception e) { System.out.println("Exception occured:" + e); } } /** * @param args * the command line arguments */ public static void main(String args[]) { /* Set the Nimbus look and feel */ // <editor-fold defaultstate="collapsed" desc=" Look and feel setting // code (optional) "> /* * If Nimbus (introduced in Java SE 6) is not available, stay with the * default look and feel. For details see * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf. * html */ try { for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) { if ("Nimbus".equals(info.getName())) { javax.swing.UIManager.setLookAndFeel(info.getClassName()); break; } } } catch (ClassNotFoundException ex) { java.util.logging.Logger.getLogger(AASDNTool.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (InstantiationException ex) { java.util.logging.Logger.getLogger(AASDNTool.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (IllegalAccessException ex) { java.util.logging.Logger.getLogger(AASDNTool.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } catch (javax.swing.UnsupportedLookAndFeelException ex) { java.util.logging.Logger.getLogger(AASDNTool.class.getName()).log(java.util.logging.Level.SEVERE, null, ex); } // </editor-fold> /* Create and display the form */ java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new AASDNTool().setVisible(true); } }); } // Variables declaration - do not modify private javax.swing.JPanel aaSDNTool; private javax.swing.JButton controllerConnect; private javax.swing.JTextField controllerIpAddress; private javax.swing.JButton devices; private javax.swing.JScrollPane devicePane; private javax.swing.JScrollPane switchPane; private javax.swing.JTable switchTable; private javax.swing.JTable macTable; private javax.swing.JButton openFlowSwitches; private javax.swing.JButton topology; private javax.swing.JLabel topologyLabel; private javax.swing.JLabel controllerLabel; // End of variables declaration }