Advertisement
anilj

simple ovsdb client

May 24th, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.05 KB | None | 0 0
  1. package com.my.testprog.clientsocket;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.BufferedWriter;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.OutputStreamWriter;
  8. import java.net.InetAddress;
  9. import java.net.InetSocketAddress;
  10. import java.net.Socket;
  11. import java.net.SocketAddress;
  12. import java.net.SocketTimeoutException;
  13.  
  14. import javax.json.JsonArray;
  15. import javax.swing.SortingFocusTraversalPolicy;
  16.  
  17. import com.fasterxml.jackson.core.JsonParser;
  18. import com.fasterxml.jackson.databind.JsonNode;
  19. import com.fasterxml.jackson.databind.ObjectMapper;
  20.  
  21. import net.minidev.json.JSONArray;
  22. import net.minidev.json.JSONObject;
  23.  
  24. public class ClientSocket {
  25.  
  26.     private String hostName;
  27.     private int port;
  28.  
  29.     public ClientSocket(String hostName, int port) {
  30.         this.hostName = hostName;
  31.         this.port = port;
  32.     }
  33.    
  34.     /**
  35.      * Open a socket connection to the given server on the given port. This
  36.      * method currently sets the socket timeout value to 10 seconds. (A second
  37.      * version of this method could allow the user to specify this timeout.)
  38.      */
  39.     private Socket openSocket(String server, int port) throws Exception {
  40.         Socket socket;
  41.  
  42.         // create a socket with a timeout
  43.         try {
  44.             InetAddress inteAddress = InetAddress.getByName(server);
  45.             SocketAddress socketAddress = new InetSocketAddress(inteAddress, port);
  46.  
  47.             // create a socket
  48.             socket = new Socket();
  49.  
  50.             // this method will block no more than timeout ms.
  51.             socket.connect(socketAddress);
  52.             return socket;
  53.         } catch (SocketTimeoutException ste) {
  54.             System.err.println("Timed out waiting for the socket.");
  55.             ste.printStackTrace();
  56.             throw ste;
  57.         }
  58.     }
  59.    
  60.     private void writeToSocket(Socket socket, String writeTo) throws Exception {
  61.         try {
  62.             // write text to the socket
  63.             BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
  64.             bufferedWriter.write(writeTo);
  65.             bufferedWriter.flush();
  66.           } catch (IOException e) {
  67.             e.printStackTrace();
  68.             throw e;
  69.         }
  70.     }
  71.    
  72.     private String readFromSocket(Socket socket) throws Exception {
  73.        
  74.         // memory buffer for the socket data.
  75.         byte[] bytes = new byte[4096];
  76.         int len;
  77.  
  78.         while (true) {
  79.             // while the socket has more data (ie: not closed)
  80.             while ((len = socket.getInputStream().read(bytes)) > 0) {
  81.    
  82.                 // convert the bytes to a string.
  83.                 String message = new String(bytes, 0, len);
  84.                 System.out.println("Received data from client: " + message + "\n");
  85.                
  86.                 processMessage(socket, message);
  87.                 for (int j = 0; j < 4096; j++) {
  88.                     bytes[j] = 0;
  89.                 }
  90.             }
  91.         }
  92.     }
  93.  
  94.     private void processMessage(Socket socket, String message) {
  95.                
  96.         ObjectMapper mapper = new ObjectMapper();
  97.         try {
  98.             JsonNode jsonNode = mapper.readTree(message);
  99.             String response = "";
  100.            
  101.             // Get request method
  102.             String method = jsonNode.get("method").asText();
  103.  
  104.             if (method.equals("echo")) {
  105.                 response = processEchoMethod(socket, jsonNode);
  106.             } else if (method.equals("get_schema")) {
  107.                 response = processGetSchemaMethod(socket, jsonNode);
  108.             } else if (method.equals("monitor")) {
  109.                 response = processMonitorMethod(socket, jsonNode);
  110.             }
  111.            
  112.             // Send the response message to the server
  113.             try {
  114.                 writeToSocket(socket, response);
  115.             } catch (Exception e) {
  116.                 // TODO Auto-generated catch block
  117.                 e.printStackTrace();
  118.             }
  119.            
  120.         } catch (IOException e) {
  121.             // TODO Auto-generated catch block
  122.             e.printStackTrace();
  123.         }
  124.     }
  125.  
  126.     private String processMonitorMethod(Socket socket, JsonNode jsonNode) {
  127.  
  128.         String monitorResponse = "\"error\":null,\"result\":{\"Physical_Port\":{\"8d94a127-0162-4d6b-b4ca-bb5075da10a4\":{\"new\":{\"_uuid\":[\"uuid\",\"8d94a127-0162-4d6b-b4ca-bb5075da10a4\"],\"vlan_stats\":[\"map\",[]],\"vlan_bindings\":[\"map\",[]],\"name\":\"po100\",\"description\":\"\",\"port_fault_status\":[\"set\",[]]}}},\"Physical_Switch\":{\"d61d43dd-b275-4cd6-aecd-936535e580b3\":{\"new\":{\"_uuid\":[\"uuid\",\"d61d43dd-b275-4cd6-aecd-936535e580b3\"],\"tunnels\":[\"set\",[]],\"ports\":[\"uuid\",\"8d94a127-0162-4d6b-b4ca-bb5075da10a4\"],\"management_ips\":\"10.23.236.51\",\"tunnel_ips\":\"1.1.1.1\",\"name\":\"NSX-1\",\"description\":\"\",\"switch_fault_status\":[\"set\",[]]}}}}";
  129.         String reqUuid = jsonNode.get("id").asText();
  130.  
  131.         return "{" + "\"id\":\"" + reqUuid + "\"" + "," + monitorResponse + "}";
  132.     }
  133.  
  134.     private String processGetSchemaMethod(Socket socket, JsonNode jsonNode) {
  135.        
  136.         String schemaResponse = "\"error\":null,\"result\":{\"name\":\"hardware_vtep\",\"tables\":{\"Arp_Sources_Local\":{\"columns\":{\"locator\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"src_mac\":{\"type\":\"string\"}},\"isRoot\":true},\"Arp_Sources_Remote\":{\"columns\":{\"locator\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"src_mac\":{\"type\":\"string\"}},\"isRoot\":true},\"Global\":{\"columns\":{\"managers\":{\"type\":{\"key\":{\"refTable\":\"Manager\",\"type\":\"uuid\"},\"max\":\"unlimited\",\"min\":0}},\"switches\":{\"type\":{\"key\":{\"refTable\":\"Physical_Switch\",\"type\":\"uuid\"},\"max\":\"unlimited\",\"min\":0}}},\"isRoot\":true,\"maxRows\":1},\"Logical_Binding_Stats\":{\"columns\":{\"bytes_from_local\":{\"type\":\"integer\",\"ephemeral\":true},\"bytes_to_local\":{\"type\":\"integer\",\"ephemeral\":true},\"packets_from_local\":{\"type\":\"integer\",\"ephemeral\":true},\"packets_to_local\":{\"type\":\"integer\",\"ephemeral\":true}}},\"Logical_Router\":{\"columns\":{\"description\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"static_routes\":{\"type\":{\"key\":{\"type\":\"string\"},\"value\":{\"type\":\"string\"},\"min\":0,\"max\":\"unlimited\"}},\"switch_binding\":{\"type\":{\"key\":{\"type\":\"string\"},\"value\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"},\"min\":0,\"max\":\"unlimited\"}}},\"indexes\":[[\"name\"]],\"isRoot\":true},\"Logical_Switch\":{\"columns\":{\"description\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"tunnel_key\":{\"type\":{\"key\":\"integer\",\"min\":0,\"max\":1}}},\"indexes\":[[\"name\"]],\"isRoot\":true},\"Manager\":{\"columns\":{\"inactivity_probe\":{\"type\":{\"key\":\"integer\",\"min\":0,\"max\":1}},\"is_connected\":{\"ephemeral\":true,\"type\":\"boolean\"},\"max_backoff\":{\"type\":{\"key\":{\"minInteger\":1000,\"type\":\"integer\"},\"min\":0,\"max\":1}},\"other_config\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"status\":{\"ephemeral\":true,\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"target\":{\"type\":\"string\"}},\"indexes\":[[\"target\"]],\"isRoot\":false},\"Mcast_Macs_Local\":{\"columns\":{\"MAC\":{\"type\":\"string\"},\"ipaddr\":{\"type\":\"string\"},\"locator_set\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator_Set\",\"type\":\"uuid\"}}},\"logical_switch\":{\"type\":{\"key\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}}},\"isRoot\":true},\"Mcast_Macs_Remote\":{\"columns\":{\"MAC\":{\"type\":\"string\"},\"ipaddr\":{\"type\":\"string\"},\"locator_set\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator_Set\",\"type\":\"uuid\"}}},\"logical_switch\":{\"type\":{\"key\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}}},\"isRoot\":true},\"Physical_Locator\":{\"columns\":{\"dst_ip\":{\"mutable\":false,\"type\":\"string\"},\"encapsulation_type\":{\"mutable\":false,\"type\":{\"key\":{\"enum\":[\"set\",[\"vxlan_over_ipv4\"]],\"type\":\"string\"}}}},\"indexes\":[[\"encapsulation_type\",\"dst_ip\"]]},\"Physical_Locator_Set\":{\"columns\":{\"locators\":{\"mutable\":false,\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"},\"min\":1,\"max\":\"unlimited\"}}}},\"Physical_Port\":{\"columns\":{\"description\":{\"type\":\"string\"},\"name\":{\"type\":\"string\"},\"port_fault_status\":{\"ephemeral\":true,\"type\":{\"key\":\"string\",\"min\":0,\"max\":\"unlimited\"}},\"vlan_bindings\":{\"type\":{\"key\":{\"maxInteger\":4095,\"minInteger\":0,\"type\":\"integer\"},\"max\":\"unlimited\",\"min\":0,\"value\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}},\"vlan_stats\":{\"type\":{\"key\":{\"maxInteger\":4095,\"minInteger\":0,\"type\":\"integer\"},\"max\":\"unlimited\",\"min\":0,\"value\":{\"refTable\":\"Logical_Binding_Stats\",\"type\":\"uuid\"}},\"ephemeral\":true}}},\"Physical_Switch\":{\"columns\":{\"description\":{\"type\":\"string\"},\"management_ips\":{\"type\":{\"key\":{\"type\":\"string\"},\"min\":0,\"max\":\"unlimited\"}},\"name\":{\"type\":\"string\"},\"ports\":{\"type\":{\"key\":{\"refTable\":\"Physical_Port\",\"type\":\"uuid\"},\"max\":\"unlimited\",\"min\":0}},\"switch_fault_status\":{\"ephemeral\":true,\"type\":{\"key\":\"string\",\"min\":0,\"max\":\"unlimited\"}},\"tunnel_ips\":{\"type\":{\"key\":{\"type\":\"string\"},\"min\":0,\"max\":\"unlimited\"}},\"tunnels\":{\"type\":{\"key\":{\"refTable\":\"Tunnel\",\"type\":\"uuid\"},\"max\":\"unlimited\",\"min\":0}}},\"indexes\":[[\"name\"]]},\"Tunnel\":{\"columns\":{\"bfd_config_local\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"bfd_config_remote\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"bfd_params\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"}},\"bfd_status\":{\"type\":{\"key\":\"string\",\"max\":\"unlimited\",\"min\":0,\"value\":\"string\"},\"ephemeral\":true},\"local\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"remote\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}}}},\"Ucast_Macs_Local\":{\"columns\":{\"MAC\":{\"type\":\"string\"},\"ipaddr\":{\"type\":\"string\"},\"locator\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"logical_switch\":{\"type\":{\"key\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}}},\"isRoot\":true},\"Ucast_Macs_Remote\":{\"columns\":{\"MAC\":{\"type\":\"string\"},\"ipaddr\":{\"type\":\"string\"},\"locator\":{\"type\":{\"key\":{\"refTable\":\"Physical_Locator\",\"type\":\"uuid\"}}},\"logical_switch\":{\"type\":{\"key\":{\"refTable\":\"Logical_Switch\",\"type\":\"uuid\"}}}},\"isRoot\":true}},\"version\":\"1.3.0\"}";
  137.         String reqUuid = jsonNode.get("id").asText();
  138.  
  139.         return "{" + "\"id\":\"" + reqUuid + "\"" + "," + schemaResponse + "}";
  140.     }
  141.    
  142.     private String processEchoMethod(Socket socket, JsonNode jsonNode) {
  143.        
  144.         // Request: {"id":"5bd2390a-61ea-4449-be84-7e9b3ca83079","method":"echo","params":[]}
  145.         // Resonse: {"result":[],"error":null,"id":"5bd2390a-61ea-4449-be84-7e9b3ca83079"}
  146.        
  147.         String reqUuid = jsonNode.get("id").asText();
  148.        
  149.         JSONObject resObj = new JSONObject();
  150.         resObj.put("id", reqUuid);
  151.         resObj.put("result", new JSONArray());
  152.         resObj.put("error", null);
  153.        
  154.         return resObj.toJSONString();
  155.     }
  156.  
  157.     // call our constructor to start the program
  158.     public static void main(String[] args) {
  159.        
  160.         String hostName = "localhost";
  161.         int port = 6640;
  162.        
  163.         ClientSocket clientSocket = new ClientSocket(hostName, port);
  164.        
  165.         try {
  166.             // open a socket
  167.             Socket socket = clientSocket.openSocket(hostName, port);
  168.  
  169.             // Keep reading from the socket.
  170.             clientSocket.readFromSocket(socket);                    
  171.         } catch (Exception e) {
  172.             e.printStackTrace();
  173.         }
  174.     }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement