Implemented the deleteBridgeDomain method in ConfigurationService
authorBrent Salisbury <brent.salisbury@gmail.com>
Fri, 8 Nov 2013 08:59:31 +0000 (03:59 -0500)
committerBrent Salisbury <brent.salisbury@gmail.com>
Sat, 9 Nov 2013 03:49:34 +0000 (22:49 -0500)
I missed the last comment from Madhu in p2. I verfied all
methods modified. Thanks very much for the assist.
Change-Id: I628f31ed8af66d4c18ac2b61b6200043a8b779d8
Signed-off-by: Brent Salisbury <brent.salisbury@gmail.com>
ovsdb/src/main/java/org/opendaylight/ovsdb/plugin/ConfigurationService.java

index 61d7a017de189f50e5ef1069eb95b41b934ed9a4..feb63d7bcde8b490d5902ed3aef0ed1665708d13 100755 (executable)
@@ -8,7 +8,6 @@ import java.util.*;
 
 import org.eclipse.osgi.framework.console.CommandInterpreter;
 import org.eclipse.osgi.framework.console.CommandProvider;
-import org.opendaylight.ovsdb.lib.database.OVSBridge;
 import org.opendaylight.ovsdb.lib.database.OVSInstance;
 import org.opendaylight.ovsdb.lib.database.OvsdbType;
 import org.opendaylight.ovsdb.lib.message.TransactBuilder;
@@ -452,30 +451,30 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
     @Override
     public Status deletePort(Node node, String bridgeIdentifier, String portIdentifier) {
 
-        try{
-            if (connectionService == null) {
-                logger.error("Couldn't refer to the ConnectionService");
-                return new Status(StatusCode.NOSERVICE);
-            }
+            try{
+                if (connectionService == null) {
+                    logger.error("Couldn't refer to the ConnectionService");
+                    return new Status(StatusCode.NOSERVICE);
+                }
 
-            Connection connection = this.getConnection(node);
-            if (connection == null) {
-                return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
-            }
+                Connection connection = this.getConnection(node);
+                if (connection == null) {
+                    return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
+                }
 
-            Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
-            Map<String, Table<?>> portTable = inventoryServiceInternal.getTableCache(node, Port.NAME.getName());
-            Operation delPortRequest = null;
-            String brUuid = null;
-            String portUuid = null;
-            if(brTable != null){
-                for (String uuid : brTable.keySet()) {
-                    Bridge bridge = (Bridge) brTable.get(uuid);
-                    if (bridge.getName().contains(bridgeIdentifier)) {
-                        brUuid = uuid;
+                Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
+                Map<String, Table<?>> portTable = inventoryServiceInternal.getTableCache(node, Port.NAME.getName());
+                Operation delPortRequest = null;
+                String brUuid = null;
+                String portUuid = null;
+                if(brTable != null){
+                    for (String uuid : brTable.keySet()) {
+                        Bridge bridge = (Bridge) brTable.get(uuid);
+                        if (bridge.getName().contains(bridgeIdentifier)) {
+                            brUuid = uuid;
+                        }
                     }
                 }
-            }
             if(portTable != null){
                 for (String uuid : portTable.keySet()) {
                     Port port = (Port) portTable.get(uuid);
@@ -554,8 +553,70 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
 
     @Override
     public Status deleteBridgeDomain(Node node, String bridgeIdentifier) {
-        // TODO Auto-generated method stub
-        return null;
+
+        try {
+            if (connectionService == null) {
+                logger.error("Couldn't refer to the ConnectionService");
+                return new Status(StatusCode.NOSERVICE);
+            }
+            Connection connection = this.getConnection(node);
+            if (connection == null) {
+                return new Status(StatusCode.NOSERVICE, "Connection to ovsdb-server not available");
+            }
+            Map<String, Table<?>> ovsTable = inventoryServiceInternal.getTableCache(node, Open_vSwitch.NAME.getName());
+            Map<String, Table<?>> brTable = inventoryServiceInternal.getTableCache(node, Bridge.NAME.getName());
+            Operation delBrRequest = null;
+            String ovsUuid = null;
+            String brUuid = null;
+
+            if (brTable != null) {
+                for (String uuid : brTable.keySet()) {
+                    Bridge bridge = (Bridge) brTable.get(uuid);
+                    if (bridge.getName().contains(bridgeIdentifier)) {
+                        brUuid = uuid;
+                    }
+                }
+            }
+            if (ovsTable != null) {
+                ovsUuid = (String) ovsTable.keySet().toArray()[0];
+            }
+            UUID bridgeUuidPair = new UUID(brUuid);
+            Mutation bm = new Mutation("bridges", Mutator.DELETE, bridgeUuidPair);
+            List<Mutation> mutations = new ArrayList<Mutation>();
+            mutations.add(bm);
+
+            UUID uuid = new UUID(ovsUuid);
+            Condition condition = new Condition("_uuid", Function.EQUALS, uuid);
+            List<Condition> where = new ArrayList<Condition>();
+            where.add(condition);
+            delBrRequest = new MutateOperation(Open_vSwitch.NAME.getName(), where, mutations);
+
+            TransactBuilder transaction = new TransactBuilder();
+            transaction.addOperations(new ArrayList<Operation>(Arrays.asList(delBrRequest)));
+
+            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());
+            }
+            return status;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return new Status(StatusCode.INTERNALERROR);
     }
 
     @Override
@@ -764,15 +825,20 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
             ci.println("Please enter Node Name");
             return;
         }
-
         String bridgeName = ci.nextArgument();
         if (bridgeName == null) {
             ci.println("Please enter Bridge Name");
             return;
         }
         Status status;
+
         try {
-            status = this.createBridgeDomain(Node.fromString(nodeName), bridgeName, null);
+            Node node = Node.fromString(nodeName);
+            if (node == null) {
+                ci.println("Invalid Node");
+                return;
+            }
+            status = this.createBridgeDomain(node, bridgeName, null);
             ci.println("Bridge creation status : "+status.toString());
         } catch (Throwable e) {
             // TODO Auto-generated catch block
@@ -788,9 +854,15 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
             return;
         }
         Status status;
+
         List<String> brlist = new ArrayList<String>();
         try {
-            brlist = this.getBridgeDomains(Node.fromString(nodeName));
+            Node node = Node.fromString(nodeName);
+            brlist = this.getBridgeDomains(node);
+            if (node == null) {
+                ci.println("Invalid Node");
+                return;
+            }
             ci.println("Existing Bridges: "+brlist.toString());
         } catch (Throwable e) {
             e.printStackTrace();
@@ -798,6 +870,32 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
         }
     }
 
+    public void _deleteBridgeDomain (CommandInterpreter ci) {
+        String nodeName = ci.nextArgument();
+        if (nodeName == null) {
+            ci.println("Please enter Node Name");
+            return;
+        }
+        String bridgeName = ci.nextArgument();
+        if (bridgeName == null) {
+            ci.println("Please enter Bridge Name");
+            return;
+        }
+        Status status;
+        try {
+            Node node = Node.fromString(nodeName);
+            if (node == null) {
+                ci.println("Invalid Node");
+                return;
+            }
+            status = this.deleteBridgeDomain(node, bridgeName);
+            ci.println("Bridge deletion status : "+status.toString());
+        } catch (Throwable e) {
+            e.printStackTrace();
+            ci.println("Failed to delete Bridge "+bridgeName);
+        }
+    }
+
     public void _addPort (CommandInterpreter ci) {
         String nodeName = ci.nextArgument();
         if (nodeName == null) {
@@ -841,7 +939,12 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
         }
         Status status;
         try {
-            status = this.addPort(Node.fromString(nodeName), bridgeName, portName, customConfigs);
+            Node node = Node.fromString(nodeName);
+            if (node == null) {
+                ci.println("Invalid Node");
+                return;
+            }
+            status = this.addPort(node, bridgeName, portName, customConfigs);
             ci.println("Port creation status : "+status.toString());
         } catch (Throwable e) {
             // TODO Auto-generated catch block
@@ -871,7 +974,12 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
 
         Status status;
         try {
-            status = this.deletePort(Node.fromString(nodeName), bridgeName, portName);
+            Node node = Node.fromString(nodeName);
+            if (node == null) {
+                ci.println("Invalid Node");
+                return;
+            }
+            status = this.deletePort(node, bridgeName, portName);
             ci.println("Port deletion status : "+status.toString());
         } catch (Throwable e) {
             // TODO Auto-generated catch block
@@ -918,7 +1026,12 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
 
         Status status;
         try {
-            status = this.addPort(Node.fromString(nodeName), bridgeName, portName, configs);
+            Node node = Node.fromString(nodeName);
+            if (node == null) {
+                ci.println("Invalid Node");
+                return;
+            }
+            status = this.addPort(node, bridgeName, portName, configs);
             ci.println("Port creation status : "+status.toString());
         } catch (Throwable e) {
             // TODO Auto-generated catch block
@@ -973,7 +1086,12 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
 
         Status status;
         try {
-            status = this.addPort(Node.fromString(nodeName), bridgeName, portName, configs);
+            Node node = Node.fromString(nodeName);
+            if (node == null) {
+                ci.println("Invalid Node");
+                return;
+            }
+            status = this.addPort(node, bridgeName, portName, configs);
             ci.println("Port creation status : "+status.toString());
         } catch (Throwable e) {
             // TODO Auto-generated catch block
@@ -988,7 +1106,12 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
             ci.println("Please enter Node Name");
             return;
         }
-        inventoryServiceInternal.printCache(Node.fromString(nodeName));
+        Node node = Node.fromString(nodeName);
+        if (node == null) {
+            ci.println("Invalid Node");
+            return;
+        }
+        inventoryServiceInternal.printCache(node);
     }
 
     public void _forceConnect (CommandInterpreter ci) {
@@ -1006,9 +1129,10 @@ public class ConfigurationService implements IPluginInBridgeDomainConfigService,
         help.append("---OVSDB CLI---\n");
         help.append("\t ovsconnect <ConnectionName> <ip-address>                        - Connect to OVSDB\n");
         help.append("\t addBridge <Node> <BridgeName>                                   - Add Bridge\n");
-        help.append("\t getBridgeDomains                                                - Get Bridges\n");
+        help.append("\t getBridgeDomains <Node>                                         - Get Bridges\n");
+        help.append("\t deleteBridgeDomain <Node> <BridgeName>                          - Delete a Bridge\n");
         help.append("\t addPort <Node> <BridgeName> <PortName> <type> <options pairs>   - Add Port\n");
-        help.append("\t delPort <Node> <BridgeName> <PortName>                          - Delete Port\n");
+        help.append("\t deletePort <Node> <BridgeName> <PortName>                       - Delete Port\n");
         help.append("\t addPortVlan <Node> <BridgeName> <PortName> <vlan>               - Add Port, Vlan\n");
         help.append("\t addTunnel <Node> <Bridge> <Port> <tunnel-type> <remote-ip>      - Add Tunnel\n");
         help.append("\t printCache <Node>                                               - Prints Table Cache");