Upgrade powermock to 2.0.0
[ovsdb.git] / southbound / southbound-impl / src / main / java / org / opendaylight / ovsdb / southbound / OvsdbConnectionInstance.java
index ce8181c87202c3885bc0209671e30ef4f6a19e26..299c3928b236105036ac03f1d28d333d32b18d03 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright © 2015, 2017 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
@@ -9,21 +9,21 @@ package org.opendaylight.ovsdb.southbound;
 
 import static org.opendaylight.ovsdb.lib.operations.Operations.op;
 
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.util.concurrent.ListenableFuture;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ExecutionException;
-
-import javax.annotation.Nonnull;
-
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
-import org.opendaylight.controller.md.sal.common.api.clustering.Entity;
-import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipCandidateRegistration;
-import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
-import org.opendaylight.ovsdb.lib.EchoServiceCallbackFilters;
+import org.opendaylight.mdsal.eos.binding.api.Entity;
+import org.opendaylight.mdsal.eos.binding.api.EntityOwnershipCandidateRegistration;
 import org.opendaylight.ovsdb.lib.LockAquisitionCallback;
 import org.opendaylight.ovsdb.lib.LockStolenCallback;
 import org.opendaylight.ovsdb.lib.MonitorCallBack;
@@ -47,6 +47,7 @@ import org.opendaylight.ovsdb.lib.schema.typed.TypedBaseTable;
 import org.opendaylight.ovsdb.lib.schema.typed.TyperUtils;
 import org.opendaylight.ovsdb.schema.openvswitch.OpenVSwitch;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.BridgeOperationalState;
+import org.opendaylight.ovsdb.southbound.ovsdb.transact.DataChangeEvent;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactCommand;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvoker;
 import org.opendaylight.ovsdb.southbound.ovsdb.transact.TransactInvokerImpl;
@@ -60,19 +61,15 @@ import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.ovsdb.re
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.NodeId;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.Node;
 import org.opendaylight.yang.gen.v1.urn.tbd.params.xml.ns.yang.network.topology.rev131021.network.topology.topology.NodeKey;
-import org.opendaylight.yangtools.yang.binding.DataObject;
 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
-import com.google.common.collect.Lists;
-import com.google.common.util.concurrent.ListenableFuture;
-
-public class OvsdbConnectionInstance implements OvsdbClient {
+public class OvsdbConnectionInstance {
     private static final Logger LOG = LoggerFactory.getLogger(OvsdbConnectionInstance.class);
-    private OvsdbClient client;
+    private final OvsdbClient client;
     private ConnectionInfo connectionInfo;
-    private TransactionInvoker txInvoker;
+    private final TransactionInvoker txInvoker;
     private Map<DatabaseSchema,TransactInvoker> transactInvokers;
     private MonitorCallBack callback;
     private InstanceIdentifier<Node> instanceIdentifier;
@@ -81,8 +78,8 @@ public class OvsdbConnectionInstance implements OvsdbClient {
     private EntityOwnershipCandidateRegistration deviceOwnershipCandidateRegistration;
     private OvsdbNodeAugmentation initialCreateData = null;
 
-    OvsdbConnectionInstance(ConnectionInfo key,OvsdbClient client,TransactionInvoker txInvoker,
-            InstanceIdentifier<Node> iid) {
+    OvsdbConnectionInstance(ConnectionInfo key, OvsdbClient client, TransactionInvoker txInvoker,
+                            InstanceIdentifier<Node> iid) {
         this.connectionInfo = key;
         this.client = client;
         this.txInvoker = txInvoker;
@@ -96,11 +93,12 @@ public class OvsdbConnectionInstance implements OvsdbClient {
      * @param command The command to run.
      * @param state The current bridge state.
      * @param events The events to process.
+     * @param instanceIdentifierCodec The instance identifier codec to use.
      */
     public void transact(TransactCommand command, BridgeOperationalState state,
-                 AsyncDataChangeEvent<InstanceIdentifier<?>, DataObject> events) {
+            DataChangeEvent events, InstanceIdentifierCodec instanceIdentifierCodec) {
         for (TransactInvoker transactInvoker : transactInvokers.values()) {
-            transactInvoker.invoke(command, state, events);
+            transactInvoker.invoke(command, state, events, instanceIdentifierCodec);
         }
     }
 
@@ -110,18 +108,24 @@ public class OvsdbConnectionInstance implements OvsdbClient {
      * @param command The command to run.
      * @param state The current bridge state.
      * @param modifications The modifications to process.
+     * @param instanceIdentifierCodec The instance identifier codec to use.
      */
     public void transact(TransactCommand command, BridgeOperationalState state,
-                 Collection<DataTreeModification<Node>> modifications) {
+            Collection<DataTreeModification<Node>> modifications, InstanceIdentifierCodec instanceIdentifierCodec) {
         for (TransactInvoker transactInvoker : transactInvokers.values()) {
-            transactInvoker.invoke(command, state, modifications);
+            transactInvoker.invoke(command, state, modifications, instanceIdentifierCodec);
         }
     }
 
-    public void registerCallbacks() {
-        if ( this.callback == null) {
-            if (this.initialCreateData != null ) {
-                this.updateConnectionAttributes();
+    public ListenableFuture<List<OperationResult>> transact(
+            DatabaseSchema dbSchema, List<Operation> operations) {
+        return client.transact(dbSchema, operations);
+    }
+
+    public void registerCallbacks(InstanceIdentifierCodec instanceIdentifierCodec) {
+        if (this.callback == null) {
+            if (this.initialCreateData != null) {
+                this.updateConnectionAttributes(instanceIdentifierCodec);
             }
 
             try {
@@ -129,8 +133,8 @@ public class OvsdbConnectionInstance implements OvsdbClient {
                 DatabaseSchema dbSchema = getSchema(database).get();
                 if (dbSchema != null) {
                     LOG.info("Monitoring database: {}", database);
-                    callback = new OvsdbMonitorCallback(this, txInvoker);
-                    monitorAllTables(database, dbSchema);
+                    callback = new OvsdbMonitorCallback(instanceIdentifierCodec, this, txInvoker);
+                    monitorTables(database, dbSchema);
                 } else {
                     LOG.info("No database {} found on {}", database, connectionInfo);
                 }
@@ -145,7 +149,7 @@ public class OvsdbConnectionInstance implements OvsdbClient {
             try {
                 transactInvokers = new HashMap<>();
                 DatabaseSchema dbSchema = getSchema(SouthboundConstants.OPEN_V_SWITCH).get();
-                if(dbSchema != null) {
+                if (dbSchema != null) {
                     transactInvokers.put(dbSchema, new TransactInvokerImpl(this,dbSchema));
                 }
             } catch (InterruptedException | ExecutionException e) {
@@ -154,40 +158,48 @@ public class OvsdbConnectionInstance implements OvsdbClient {
         }
     }
 
-    private void monitorAllTables(String database, DatabaseSchema dbSchema) {
+    @VisibleForTesting
+    void monitorTables(String database, DatabaseSchema dbSchema) {
         Set<String> tables = dbSchema.getTables();
         if (tables != null) {
-            List<MonitorRequest> monitorRequests = Lists.newArrayList();
+            List<MonitorRequest> monitorRequests = new ArrayList<>();
             for (String tableName : tables) {
-                LOG.info("Southbound monitoring table {} in {}", tableName, dbSchema.getName());
-                GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
-                Set<String> columns = tableSchema.getColumns();
-                MonitorRequestBuilder<GenericTableSchema> monitorBuilder = MonitorRequestBuilder.builder(tableSchema);
-                for (String column : columns) {
-                    monitorBuilder.addColumn(column);
+                if (!SouthboundConstants.SKIP_OVSDB_TABLE.contains(tableName)) {
+                    LOG.info("Southbound monitoring OVSDB schema table {}", tableName);
+                    GenericTableSchema tableSchema = dbSchema.table(tableName, GenericTableSchema.class);
+                    // We copy the columns so we can clean the set up later
+                    Set<String> columns = new HashSet<>(tableSchema.getColumns());
+                    List<String> skipColumns = SouthboundConstants.SKIP_COLUMN_FROM_TABLE.get(tableName);
+                    if (skipColumns != null) {
+                        LOG.info("Southbound NOT monitoring columns {} in table {}", skipColumns, tableName);
+                        columns.removeAll(skipColumns);
+                    }
+                    monitorRequests.add(new MonitorRequestBuilder<>(tableSchema)
+                            .addColumns(columns)
+                            .with(new MonitorSelect(true, true, true, true)).build());
                 }
-                monitorRequests.add(monitorBuilder.with(new MonitorSelect(true, true, true, true)).build());
             }
-            this.callback.update(monitor(dbSchema, monitorRequests, callback),dbSchema);
+            this.callback.update(monitor(dbSchema, monitorRequests, callback), dbSchema);
         } else {
             LOG.warn("No tables for schema {} for database {} for key {}",dbSchema,database,connectionInfo);
         }
     }
 
-    private void updateConnectionAttributes() {
+    private void updateConnectionAttributes(InstanceIdentifierCodec instanceIdentifierCodec) {
         LOG.debug("Update attributes of ovsdb node ip: {} port: {}",
                     this.initialCreateData.getConnectionInfo().getRemoteIp(),
                     this.initialCreateData.getConnectionInfo().getRemotePort());
-        for ( Map.Entry<DatabaseSchema,TransactInvoker> entry: transactInvokers.entrySet()) {
+        for (Map.Entry<DatabaseSchema,TransactInvoker> entry: transactInvokers.entrySet()) {
 
-            TransactionBuilder transaction = new TransactionBuilder(this, entry.getKey());
+            TransactionBuilder transaction = new TransactionBuilder(this.client, entry.getKey());
 
             // OpenVSwitchPart
             OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
 
             List<OpenvswitchExternalIds> externalIds = this.initialCreateData.getOpenvswitchExternalIds();
 
-            stampInstanceIdentifier(transaction,this.instanceIdentifier.firstIdentifierOf(Node.class));
+            stampInstanceIdentifier(transaction, this.instanceIdentifier.firstIdentifierOf(Node.class),
+                    instanceIdentifierCodec);
 
             try {
                 ovs.setExternalIds(
@@ -222,13 +234,12 @@ public class OvsdbConnectionInstance implements OvsdbClient {
         }
     }
 
-    private void stampInstanceIdentifier(TransactionBuilder transaction,InstanceIdentifier<Node> iid) {
+    private void stampInstanceIdentifier(TransactionBuilder transaction,InstanceIdentifier<Node> iid,
+            InstanceIdentifierCodec instanceIdentifierCodec) {
         OpenVSwitch ovs = TyperUtils.getTypedRowWrapper(transaction.getDatabaseSchema(), OpenVSwitch.class);
-        ovs.setExternalIds(Collections.<String,String>emptyMap());
-        TransactUtils.stampInstanceIdentifier(transaction,
-                iid,
-                ovs.getSchema(),
-                ovs.getExternalIdsColumn().getSchema());
+        ovs.setExternalIds(Collections.emptyMap());
+        TransactUtils.stampInstanceIdentifier(transaction, iid, ovs.getSchema(), ovs.getExternalIdsColumn().getSchema(),
+                instanceIdentifierCodec);
     }
 
     private void invoke(TransactionBuilder txBuilder) {
@@ -238,7 +249,7 @@ public class OvsdbConnectionInstance implements OvsdbClient {
             try {
                 List<OperationResult> got = result.get();
                 LOG.debug("OVSDB transaction result: {}", got);
-            } catch (Exception e) {
+            } catch (InterruptedException | ExecutionException e) {
                 LOG.warn("Transact execution exception: ", e);
             }
             LOG.trace("invoke exit tb: {}", txBuilder);
@@ -258,15 +269,16 @@ public class OvsdbConnectionInstance implements OvsdbClient {
         return client.transactBuilder(dbSchema);
     }
 
-    public ListenableFuture<List<OperationResult>> transact(
-            DatabaseSchema dbSchema, List<Operation> operations) {
-        return client.transact(dbSchema, operations);
+    public <E extends TableSchema<E>> TableUpdates monitor(
+            DatabaseSchema schema, List<MonitorRequest> monitorRequests,
+            MonitorHandle monitorHandle, MonitorCallBack callbackArgument) {
+        return null;
     }
 
     public <E extends TableSchema<E>> TableUpdates monitor(
             DatabaseSchema schema, List<MonitorRequest> monitorRequests,
-            MonitorCallBack callback) {
-        return client.monitor(schema, monitorRequests, callback);
+            MonitorCallBack callbackArgument) {
+        return client.monitor(schema, monitorRequests, callbackArgument);
     }
 
     public void cancelMonitor(MonitorHandle handler) {
@@ -286,14 +298,6 @@ public class OvsdbConnectionInstance implements OvsdbClient {
         return client.unLock(lockId);
     }
 
-    public void startEchoService(EchoServiceCallbackFilters callbackFilters) {
-        client.startEchoService(callbackFilters);
-    }
-
-    public void stopEchoService() {
-        client.stopEchoService();
-    }
-
     public boolean isActive() {
         return client.isActive();
     }
@@ -337,7 +341,7 @@ public class OvsdbConnectionInstance implements OvsdbClient {
     }
 
     public NodeKey getNodeKey() {
-        return getInstanceIdentifier().firstKeyOf(Node.class, NodeKey.class);
+        return getInstanceIdentifier().firstKeyOf(Node.class);
     }
 
     public NodeId getNodeId() {
@@ -348,18 +352,11 @@ public class OvsdbConnectionInstance implements OvsdbClient {
         this.instanceIdentifier = iid;
     }
 
-    @Override
-    public <E extends TableSchema<E>> TableUpdates monitor(
-            DatabaseSchema schema, List<MonitorRequest> monitorRequests,
-            MonitorHandle monitorHandle, MonitorCallBack callback) {
-        return null;
-    }
-
     public Entity getConnectedEntity() {
         return this.connectedEntity;
     }
 
-    public void setConnectedEntity(Entity entity ) {
+    public void setConnectedEntity(Entity entity) {
         this.connectedEntity = entity;
     }
 
@@ -373,11 +370,13 @@ public class OvsdbConnectionInstance implements OvsdbClient {
 
     public void setHasDeviceOwnership(Boolean hasDeviceOwnership) {
         if (hasDeviceOwnership != null) {
+            LOG.debug("Ownership status for {} old {} new {}",
+                    instanceIdentifier, this.hasDeviceOwnership, hasDeviceOwnership);
             this.hasDeviceOwnership = hasDeviceOwnership;
         }
     }
 
-    public void setDeviceOwnershipCandidateRegistration(@Nonnull EntityOwnershipCandidateRegistration registration) {
+    public void setDeviceOwnershipCandidateRegistration(@NonNull EntityOwnershipCandidateRegistration registration) {
         this.deviceOwnershipCandidateRegistration = registration;
     }
 
@@ -391,12 +390,12 @@ public class OvsdbConnectionInstance implements OvsdbClient {
     public OvsdbNodeAugmentation getOvsdbNodeAugmentation() {
         return this.initialCreateData;
     }
+
     public void setOvsdbNodeAugmentation(OvsdbNodeAugmentation ovsdbNodeCreateData) {
         this.initialCreateData = ovsdbNodeCreateData;
     }
 
-    @Override
-    public void echo() {
-        client.echo();
+    public OvsdbClient getOvsdbClient() {
+        return client;
     }
 }