Sonar clean-up: braces for control statements
[netvirt.git] / plugin / src / main / java / org / opendaylight / ovsdb / plugin / impl / ConnectionServiceImpl.java
index 64a0fa1e9a77237bd2611bd1d327067ba18f3217..8582174d835096e3e94dc1c4e2049dad70dbbf69 100644 (file)
@@ -15,7 +15,6 @@ import java.io.IOException;
 import java.net.InetAddress;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
@@ -23,12 +22,6 @@ import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentMap;
 import java.util.concurrent.ExecutionException;
 
-import org.opendaylight.controller.sal.connection.ConnectionConstants;
-import org.opendaylight.controller.sal.connection.IPluginInConnectionService;
-import org.opendaylight.controller.sal.core.Node;
-import org.opendaylight.controller.sal.core.Property;
-import org.opendaylight.controller.sal.utils.Status;
-import org.opendaylight.controller.sal.utils.StatusCode;
 import org.opendaylight.ovsdb.lib.MonitorCallBack;
 import org.opendaylight.ovsdb.lib.OvsdbClient;
 import org.opendaylight.ovsdb.lib.OvsdbConnection;
@@ -41,13 +34,14 @@ import org.opendaylight.ovsdb.lib.message.TableUpdates;
 import org.opendaylight.ovsdb.lib.schema.DatabaseSchema;
 import org.opendaylight.ovsdb.lib.schema.GenericTableSchema;
 import org.opendaylight.ovsdb.lib.schema.TableSchema;
-import org.opendaylight.ovsdb.plugin.IConnectionServiceInternal;
 import org.opendaylight.ovsdb.plugin.api.Connection;
-import org.opendaylight.ovsdb.plugin.internal.IPAddressProperty;
-import org.opendaylight.ovsdb.plugin.internal.L4PortProperty;
+import org.opendaylight.ovsdb.plugin.api.ConnectionConstants;
+import org.opendaylight.ovsdb.plugin.api.Status;
+import org.opendaylight.ovsdb.plugin.api.StatusCode;
 import org.opendaylight.ovsdb.plugin.api.OvsdbConnectionService;
 import org.opendaylight.ovsdb.plugin.api.OvsdbInventoryService;
-
+import org.opendaylight.ovsdb.utils.config.ConfigProperties;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -58,15 +52,18 @@ import com.google.common.collect.Lists;
  * Represents the openflow plugin component in charge of programming the flows
  * the flow programming and relay them to functional modules above SAL.
  */
-public class ConnectionServiceImpl implements IPluginInConnectionService,
-                                              OvsdbConnectionService,
-                                              IConnectionServiceInternal,
+public class ConnectionServiceImpl implements OvsdbConnectionService,
                                               OvsdbConnectionListener {
     protected static final Logger logger = LoggerFactory.getLogger(ConnectionServiceImpl.class);
 
     // Properties that can be set in config.ini
-    private static final Integer defaultOvsdbPort = 6640;
+    private static final Integer DEFAULT_OVSDB_PORT = 6640;
+    private static final String OVSDB_LISTENPORT = "ovsdb.listenPort";
+
 
+    public void putOvsdbConnection (String identifier, Connection connection) {
+        ovsdbConnections.put(identifier, connection);
+    }
 
     private ConcurrentMap<String, Connection> ovsdbConnections = new ConcurrentHashMap<String, Connection>();
     private List<ChannelHandler> handlers = null;
@@ -98,8 +95,21 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
      * the services provided by the class are registered in the service registry
      */
     void start() {
+        /* Start ovsdb server before getting connection clients */
+        String portString = ConfigProperties.getProperty(OvsdbConnectionService.class, OVSDB_LISTENPORT);
+        int ovsdbListenPort = DEFAULT_OVSDB_PORT;
+        if (portString != null) {
+            ovsdbListenPort = Integer.decode(portString).intValue();
+        }
+
+        if (!connectionLib.startOvsdbManager(ovsdbListenPort)) {
+            logger.warn("Start OVSDB manager call from ConnectionService was not necessary");
+        }
+
+        /* Then get connection clients */
         Collection<OvsdbClient> connections = connectionLib.getConnections();
         for (OvsdbClient client : connections) {
+            logger.info("CONNECT start connected clients client = {}", client);
             this.connected(client);
         }
     }
@@ -115,12 +125,10 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
         }
     }
 
-    @Override
     public Status disconnect(Node node) {
-        String identifier = (String) node.getID();
-        Connection connection = ovsdbConnections.get(identifier);
+        Connection connection = getConnection(node);
         if (connection != null) {
-            ovsdbConnections.remove(identifier);
+            ovsdbConnections.remove(normalizeId(node.getId().getValue()));
             connection.disconnect();
             ovsdbInventoryService.removeNode(node);
             return new Status(StatusCode.SUCCESS);
@@ -129,7 +137,6 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
         }
     }
 
-    @Override
     public Node connect(String identifier, Map<ConnectionConstants, String> params) {
         InetAddress address;
         Integer port;
@@ -143,9 +150,11 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
 
         try {
             port = Integer.parseInt(params.get(ConnectionConstants.PORT));
-            if (port == 0) port = defaultOvsdbPort;
+            if (port == 0) {
+                port = DEFAULT_OVSDB_PORT;
+            }
         } catch (Exception e) {
-            port = defaultOvsdbPort;
+            port = DEFAULT_OVSDB_PORT;
         }
 
         try {
@@ -167,10 +176,30 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
         this.handlers = handlers;
     }
 
+    private String normalizeId (String identifier) {
+        String id = identifier;
+
+        String[] pair = identifier.split("\\|");
+        if (pair[0].equals("OVS")) {
+            id = pair[1];
+        }
+
+        return id;
+    }
+
     @Override
     public Connection getConnection(Node node) {
-        String identifier = (String) node.getID();
-        return ovsdbConnections.get(identifier);
+        return ovsdbConnections.get(normalizeId(node.getId().getValue()));
+    }
+
+    @Override
+    public Node getNode (String identifier) {
+        Connection connection = ovsdbConnections.get(normalizeId(identifier));
+        if (connection != null) {
+            return connection.getNode();
+        } else {
+            return null;
+        }
     }
 
     @Override
@@ -182,14 +211,6 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
         return nodes;
     }
 
-    @Override
-    public void notifyClusterViewChanged() {
-    }
-
-    @Override
-    public void notifyNodeDisconnectFromMaster(Node arg0) {
-    }
-
     private Node handleNewConnection(String identifier, OvsdbClient client) throws InterruptedException, ExecutionException {
         Connection connection = new Connection(identifier, client);
         Node node = connection.getNode();
@@ -232,13 +253,6 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
         OvsdbClient client = connection.getClient();
         InetAddress address = client.getConnectionInfo().getRemoteAddress();
         int port = client.getConnectionInfo().getRemotePort();
-        IPAddressProperty addressProp = new IPAddressProperty(address);
-        L4PortProperty l4Port = new L4PortProperty(port);
-        Set<Property> props = new HashSet<Property>();
-        props.add(addressProp);
-        props.add(l4Port);
-        logger.info("Add node to ovsdb inventory service {}", connection.getNode().toString());
-        ovsdbInventoryService.addNode(connection.getNode(), props);
 
         List<String> databases = client.getDatabases().get();
         if (databases == null) {
@@ -255,8 +269,7 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
     }
 
     public TableUpdates monitorTables(Node node, DatabaseSchema dbSchema) throws ExecutionException, InterruptedException, IOException {
-        String identifier = (String) node.getID();
-        Connection connection = ovsdbConnections.get(identifier);
+        Connection connection = getConnection(node);
         OvsdbClient client = connection.getClient();
         if (dbSchema == null) {
             logger.error("Unable to get Database Schema for the ovsdb connection : {}", client.getConnectionInfo());
@@ -279,7 +292,7 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
      * As per RFC 7047, section 4.1.5, if a Monitor request is sent without any columns, the update response will not include
      * the _uuid column.
      * ----------------------------------------------------------------------------------------------------------------------------------
-     * Each <monitor-request> specifies one or more columns and the manner in which the columns (or the entire table) are to be monitored.
+     * Each &lt;monitor-request&gt; specifies one or more columns and the manner in which the columns (or the entire table) are to be monitored.
      * The "columns" member specifies the columns whose values are monitored. It MUST NOT contain duplicates.
      * If "columns" is omitted, all columns in the table, except for "_uuid", are monitored.
      * ----------------------------------------------------------------------------------------------------------------------------------
@@ -332,7 +345,9 @@ public class ConnectionServiceImpl implements IPluginInConnectionService,
     @Override
     public void disconnected(OvsdbClient client) {
         Connection connection = ovsdbConnections.get(this.getConnectionIdentifier(client));
-        if (connection == null) return;
+        if (connection == null) {
+            return;
+        }
         this.disconnect(connection.getNode());
     }
 }