Added set-controller functionality. 19/2419/1
authorBrent Salisbury <brent.salisbury@gmail.com>
Wed, 6 Nov 2013 01:06:35 +0000 (20:06 -0500)
committerBrent Salisbury <brent.salisbury@gmail.com>
Wed, 6 Nov 2013 01:12:00 +0000 (20:12 -0500)
Signed-off-by: Brent Salisbury <brent.salisbury@gmail.com>
ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConfigurationService.java
ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConnectionService.java

index 4ddd98073769e2aeb5f65f2098af39584121a61c..84191ca54aca088cdf95fdbb72bfb900c18b3ac9 100755 (executable)
@@ -19,9 +19,11 @@ import org.opendaylight.ovsdb.lib.notation.Condition;
 import org.opendaylight.ovsdb.lib.notation.Function;
 import org.opendaylight.ovsdb.lib.notation.Mutation;
 import org.opendaylight.ovsdb.lib.notation.Mutator;
+import org.opendaylight.ovsdb.lib.notation.OvsDBMap;
 import org.opendaylight.ovsdb.lib.notation.OvsDBSet;
 import org.opendaylight.ovsdb.lib.notation.UUID;
 import org.opendaylight.ovsdb.lib.table.Bridge;
+import org.opendaylight.ovsdb.lib.table.Controller;
 import org.opendaylight.ovsdb.lib.table.Interface;
 import org.opendaylight.ovsdb.lib.table.Open_vSwitch;
 import org.opendaylight.ovsdb.lib.table.Port;
@@ -222,6 +224,9 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
                                                                                        result.getDetails());
                 status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
             }
+            if (status.isSuccess()) {
+                setBridgeOFController(node, bridgeIdentifier);
+            }
             return status;
         } catch(Exception e){
             e.printStackTrace();
@@ -527,6 +532,98 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
         return controllers;
     }
 
+    public Boolean setBridgeOFController(Node node, String bridgeIdentifier) {
+        try{
+            if (connectionService == null) {
+                logger.error("Couldn't refer to the ConnectionService");
+                return false;
+            }
+            Connection connection = this.getConnection(node);
+            if (connection == null) {
+                return false;
+            }
+
+            if (connection != null) {
+                List<InetAddress> ofControllerAddrs = getControllerIPAddresses();
+                short ofControllerPort = getControllerOFPort();
+                OvsDBSet<UUID> controllerUUIDs = new OvsDBSet<UUID>();
+                List<Operation> controllerInsertOperations = new ArrayList<Operation>();
+                Map<String, Table<?>> controllerCache = inventoryServiceInternal.getTableCache(node, Controller.NAME.getName());
+
+                int count = 0;
+                for (InetAddress ofControllerAddress : ofControllerAddrs) {
+                    String cntrlUuid = null;
+                    String newController = "tcp:"+ofControllerAddress.getHostAddress()+":"+ofControllerPort;
+                    if (controllerCache != null) {
+                        for (String uuid : controllerCache.keySet()) {
+                            Controller controller = (Controller)controllerCache.get(uuid);
+                            if (controller.getTarget().equals(newController)) {
+                                cntrlUuid = uuid;
+                                controllerUUIDs.add(new UUID(uuid));
+                                break;
+                            }
+                        }
+                    }
+                    if (cntrlUuid == null) {
+                        count++;
+                        String uuid_name = "new_controller_"+count;
+                        controllerUUIDs.add(new UUID(uuid_name));
+                        Controller controllerRow = new Controller();
+                        controllerRow.setTarget(newController);
+                        InsertOperation addCtlRequest = new InsertOperation(Controller.NAME.getName(), uuid_name, controllerRow);
+                        controllerInsertOperations.add(addCtlRequest);
+                    }
+                }
+                String brCntrlUuid = null;
+                Map<String, Table<?>> brTableCache = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
+                for (String uuid : brTableCache.keySet()) {
+                    Bridge bridge = (Bridge)brTableCache.get(uuid);
+                    if (bridge.getName().contains(bridgeIdentifier)) {
+                        brCntrlUuid = uuid;
+                    }
+                }
+                Operation addControlRequest = null;
+                Mutation bm = new Mutation("controller", Mutator.INSERT, controllerUUIDs);
+                List<Mutation> mutations = new ArrayList<Mutation>();
+                mutations.add(bm);
+
+                UUID uuid = new UUID(brCntrlUuid);
+                Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
+                List<Condition> where = new ArrayList<Condition>();
+                where.add(condition);
+                addControlRequest = new MutateOperation(Bridge.NAME.getName(), where, mutations);
+
+                TransactBuilder transaction = new TransactBuilder();
+                transaction.addOperations(controllerInsertOperations);
+                transaction.addOperation(addControlRequest);
+
+                ListenableFuture<List<OperationResult>> transResponse = connection.getRpc().transact(transaction);
+                List<OperationResult> tr = transResponse.get();
+                List<Operation> requests = transaction.getRequests();
+                Status status = new Status(StatusCode.SUCCESS);
+                for (int i = 0; i < tr.size() ; i++) {
+                    if (i < requests.size()) requests.get(i).setResult(tr.get(i));
+                    if (tr.get(i) != null && tr.get(i).getError() != null && tr.get(i).getError().trim().length() > 0) {
+                        OperationResult result = tr.get(i);
+                        status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
+                    }
+                }
+
+                if (tr.size() > requests.size()) {
+                    OperationResult result = tr.get(tr.size()-1);
+                    logger.error("Error creating Bridge : {}\n Error : {}\n Details : {}", bridgeIdentifier,
+                            result.getError(),
+                            result.getDetails());
+                    status = new Status(StatusCode.BADREQUEST, result.getError() + " : " + result.getDetails());
+
+                }
+            }
+        }catch(Exception e){
+            e.printStackTrace();
+        }
+        return true;
+    }
+
     public void _ovsconnect (CommandInterpreter ci) {
         String bridgeName = ci.nextArgument();
         if (bridgeName == null) {
index a8e7acc56f89b2ce2180f78f7c210a365f60c6f9..df8b3a5e51450539f49bec7a5a09c0e7ac8bac4d 100755 (executable)
@@ -326,6 +326,7 @@ public class ConnectionService implements IPluginInConnectionService, IConnectio
 
     @Override
     public void update(Node node, UpdateNotification updateNotification) {
+        if (updateNotification == null) return;
         inventoryServiceInternal.processTableUpdates(node, updateNotification.getUpdate());
     }