Added the ability to set the OF controller by the OVSDB Manager
authorBrent Salisbury <brent.salisbury@gmail.com>
Thu, 17 Oct 2013 02:31:21 +0000 (22:31 -0400)
committerBrent Salisbury <brent.salisbury@gmail.com>
Thu, 17 Oct 2013 02:31:21 +0000 (22:31 -0400)
Adds controller:ip:port to the Controller table, bridge row with the column target and the bridge UUID to the controller column in the Bridge table. This is for basic functionality and testing while the jsonrpc4j library is being replaced with Netty in another branch.

Signed-off-by: Brent Salisbury <brent.salisbury@gmail.com>
ovsdb/src/main/java/org/opendaylight/ovsdb/internal/ConfigurationService.java
ovsdb/src/test/java/org/opendaylight/ovsdb/OvsdbTestSetOFControler.java [new file with mode: 0644]

index 0a4390bbfcdeb0cec51308b362ea9da8f7000f41..cf3385030bb794945d57f8057cccc7c048b833c6 100755 (executable)
@@ -6,6 +6,7 @@ import java.util.*;
 
 import org.eclipse.osgi.framework.console.CommandInterpreter;
 import org.eclipse.osgi.framework.console.CommandProvider;
 
 import org.eclipse.osgi.framework.console.CommandInterpreter;
 import org.eclipse.osgi.framework.console.CommandProvider;
+import org.opendaylight.controller.sal.utils.NetUtils;
 import org.opendaylight.ovsdb.database.OVSBridge;
 import org.opendaylight.ovsdb.database.OVSPort;
 import org.opendaylight.ovsdb.database.OVSInstance;
 import org.opendaylight.ovsdb.database.OVSBridge;
 import org.opendaylight.ovsdb.database.OVSPort;
 import org.opendaylight.ovsdb.database.OVSInstance;
@@ -493,6 +494,86 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
         return bridgeDomains;
     }
 
         return bridgeDomains;
     }
 
+    public Boolean setBridgeOFController(Node node, String bridgeIdentifier, Map<ConfigConstants, Object> configs) {
+
+        try{
+            if (connectionService == null) {
+                logger.error("Couldn't refer to the ConnectionService");
+                return false;
+            }
+            Connection connection = this.getConnection(node);
+            if (connection == null || connection.getSocket() == null) {
+                return false;
+            }
+            if (connection != null) {
+                String controllerAddr = (String)configs.get(ConfigConstants.DEST_IP);
+                String ofPort = (String)configs.get(ConfigConstants.CUSTOM);
+
+                try {
+                    controllerAddr = InetAddress.getByName(controllerAddr).getHostAddress();
+                }  catch (Exception e) {
+                    e.printStackTrace();
+                    logger.error("Invalid Address <" + controllerAddr +
+                            "> Please enter valid IP-Address");
+                    return false;
+                }
+                if ((controllerAddr != null) && (controllerAddr.trim().length() > 0) &&
+                        !NetUtils.isIPv4AddressValid(controllerAddr)) {
+                    logger.debug("Invalid ip address " + controllerAddr);
+                }
+
+                Map<String, OVSBridge> existingBridges = OVSBridge.monitorBridge(connection);
+                OVSBridge bridge = existingBridges.get(bridgeIdentifier);
+
+                if (bridge != null) {
+                    Map ctrlouter = new LinkedHashMap();
+                    Map ctrlinner = new LinkedHashMap();
+                    Map brouter = new LinkedHashMap();
+                    Map brinner = new LinkedHashMap();
+                    ArrayList bralist1 = new ArrayList();
+                    ArrayList bralist2 = new ArrayList();
+                    ArrayList bralist3 = new ArrayList();
+                    ArrayList bralist4 = new ArrayList();
+
+                    //Controller Table Insert
+                    ctrlouter.put("table", "Controller");
+                    ctrlouter.put("uuid-name", "row" + bridgeIdentifier);
+                    ctrlouter.put("op", "insert");
+                    ctrlinner.put("target", "tcp:" + controllerAddr + ":" + ofPort);
+                    ctrlouter.put("row", ctrlinner);
+
+                    //Bridge Table Update
+                    brouter.put("table","Bridge");
+                    bralist1.add(bralist2);
+                    bralist2.add("_uuid");
+                    bralist2.add("==");
+                    bralist2.add(bralist3);
+                    bralist3.add("uuid");
+                    bralist3.add(bridge.getUuid());
+                    brouter.put("where",bralist1);
+                    brouter.put("op","update");
+                    brouter.put("row",brinner);
+                    bralist4.add("named-uuid");
+                    bralist4.add("row" + bridgeIdentifier);
+                    brinner.put("controller",bralist4);
+
+                    Object[] params = {"Open_vSwitch", ctrlouter, brouter};
+                    OvsdbMessage msg = new OvsdbMessage("transact", params);
+
+                    //Send the Method and Param
+                    connection.sendMessage(msg);
+                }
+                else {
+                    logger.error(bridgeIdentifier +
+                            " Does not exist on " + connection.getIdentifier());
+                }
+            }
+        }catch(Exception e){
+            e.printStackTrace();
+        }
+        return true;
+    }
+
     @Override
     public NodeConnector getNodeConnector(Node arg0, String arg1, String arg2) {
         return null;
     @Override
     public NodeConnector getNodeConnector(Node arg0, String arg1, String arg2) {
         return null;
diff --git a/ovsdb/src/test/java/org/opendaylight/ovsdb/OvsdbTestSetOFControler.java b/ovsdb/src/test/java/org/opendaylight/ovsdb/OvsdbTestSetOFControler.java
new file mode 100644 (file)
index 0000000..5fcaee7
--- /dev/null
@@ -0,0 +1,53 @@
+package org.opendaylight.ovsdb;
+
+import org.junit.Test;
+import org.opendaylight.controller.sal.connection.ConnectionConstants;
+import org.opendaylight.controller.sal.core.Node;
+import org.opendaylight.controller.sal.core.NodeConnector;
+import org.opendaylight.controller.sal.networkconfig.bridgedomain.ConfigConstants;
+import org.opendaylight.ovsdb.internal.ConfigurationService;
+import org.opendaylight.ovsdb.internal.ConnectionService;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class OvsdbTestSetOFControler {
+    private static final Logger logger = LoggerFactory
+            .getLogger(OvsdbTestSetOFControler.class);
+
+    @Test
+    public void setController() throws Throwable{
+        Node.NodeIDType.registerIDType("OVS", String.class);
+        NodeConnector.NodeConnectorIDType.registerIDType("OVS", String.class, "OVS");
+
+        ConnectionService connectionService = new ConnectionService();
+        connectionService.init();
+        String identifier = "TEST";
+        Map<ConnectionConstants, String> params = new HashMap<ConnectionConstants, String>();
+        params.put(ConnectionConstants.ADDRESS, "192.168.254.128");
+        params.put(ConnectionConstants.PORT, "6640");
+
+        Node node = connectionService.connect(identifier, params);
+        if(node == null){
+            logger.error("Could not connect to ovsdb server");
+            return;
+        }
+        /**
+         * Implements ovs-vsctl set−controller bridge target..
+         *
+         * @param node Node serving this configuration service
+         * @param bridgeDomainIdentifier String representation of a Bridge Domain
+         * @param configs String passes the OF controllers IP and Port values
+         *
+         */
+        Map<ConfigConstants, Object> configs = new HashMap<ConfigConstants, Object>();
+        configs.put(ConfigConstants.DEST_IP, "192.168.254.1");
+        configs.put(ConfigConstants.CUSTOM, "6633");
+        ConfigurationService configurationService = new ConfigurationService();
+        configurationService.setConnectionServiceInternal(connectionService);
+        configurationService.setBridgeOFController(node, "br0", configs);
+
+    }
+}