*/
package org.opendaylight.netvirt.statistics;
-import com.google.common.base.Optional;
import com.google.common.util.concurrent.CheckedFuture;
import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
-import com.google.common.util.concurrent.JdkFutureAdapters;
+import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
+import com.google.common.util.concurrent.SettableFuture;
+import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
-import javax.annotation.Nonnull;
-import org.opendaylight.controller.md.sal.binding.api.DataBroker;
-import org.opendaylight.controller.md.sal.binding.api.ReadOnlyTransaction;
-import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
-import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
-import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.eclipse.jdt.annotation.NonNull;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunner;
+import org.opendaylight.genius.infra.ManagedNewTransactionRunnerImpl;
import org.opendaylight.genius.interfacemanager.globals.InterfaceInfo;
import org.opendaylight.genius.interfacemanager.interfaces.IInterfaceManager;
import org.opendaylight.genius.mdsalutil.MDSALUtil;
import org.opendaylight.genius.mdsalutil.MatchInfoBase;
import org.opendaylight.genius.mdsalutil.interfaces.IMdsalApiManager;
-import org.opendaylight.infrautils.counters.api.OccurenceCounter;
+import org.opendaylight.infrautils.utils.concurrent.ListenableFutures;
+import org.opendaylight.mdsal.binding.api.DataBroker;
+import org.opendaylight.mdsal.binding.api.ReadTransaction;
+import org.opendaylight.mdsal.binding.api.ReadWriteTransaction;
+import org.opendaylight.mdsal.binding.api.WriteTransaction;
+import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
+import org.opendaylight.mdsal.common.api.ReadFailedException;
import org.opendaylight.netvirt.statistics.api.ICountersInterfaceChangeHandler;
import org.opendaylight.netvirt.vpnmanager.api.InterfaceUtils;
import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.interfaces.rev140508.interfaces.state.Interface;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.AllocateIdOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.CreateIdPoolOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdManagerService;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.IdPools;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdInputBuilder;
+import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.ReleaseIdOutput;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPool;
import org.opendaylight.yang.gen.v1.urn.opendaylight.genius.idmanager.rev160406.id.pools.IdPoolKey;
import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeConnectorId;
public class StatisticsImpl implements StatisticsService, ICountersInterfaceChangeHandler {
private static final Logger LOG = LoggerFactory.getLogger(StatisticsImpl.class);
private final DataBroker db;
+ private final ManagedNewTransactionRunner txRunner;
private final CounterRetriever counterRetriever;
private final IInterfaceManager interfaceManager;
private final IMdsalApiManager mdsalApiManager;
private final IdManagerService idManagerService;
+ private final StatisticsCounters statisticsCounters;
public StatisticsImpl(DataBroker db, CounterRetriever counterRetriever, IInterfaceManager interfaceManager,
- IMdsalApiManager mdsalApiManager, IdManagerService idManagerService) {
+ IMdsalApiManager mdsalApiManager, IdManagerService idManagerService,
+ StatisticsCounters statisticsCounters) {
this.db = db;
+ this.txRunner = new ManagedNewTransactionRunnerImpl(db);
this.counterRetriever = counterRetriever;
this.interfaceManager = interfaceManager;
this.mdsalApiManager = mdsalApiManager;
this.idManagerService = idManagerService;
+ this.statisticsCounters = statisticsCounters;
initializeCountrsConfigDataSrore();
}
@Override
@SuppressWarnings("checkstyle:illegalCatch")
- public Future<RpcResult<GetNodeCountersOutput>> getNodeCounters(GetNodeCountersInput input) {
+ public ListenableFuture<RpcResult<GetNodeCountersOutput>> getNodeCounters(GetNodeCountersInput input) {
BigInteger dpId = input.getNodeId();
LOG.trace("getting node counters for node {}", dpId);
GetNodeCountersOutputBuilder gncob = new GetNodeCountersOutputBuilder();
List<CounterResult> counterResults = new ArrayList<>();
try {
if (!getNodeResult(counterResults, dpId)) {
- StatisticsPluginImplCounters.failed_getting_node_counters.inc();
+ statisticsCounters.failedGettingNodeCounters();
return RpcResultBuilder.<GetNodeCountersOutput>failed()
.withError(ErrorType.APPLICATION, "failed to get node counters for node: " + dpId)
.buildFuture();
}
} catch (RuntimeException e) {
- LOG.warn("failed to get counter result for node " + dpId, e);
+ LOG.warn("failed to get counter result for node {}", dpId, e);
return RpcResultBuilder.<GetNodeCountersOutput>failed()
.withError(ErrorType.APPLICATION, "failed to get node counters for node: " + dpId).buildFuture();
}
@Override
@SuppressWarnings("checkstyle:illegalCatch")
- public Future<RpcResult<GetNodeAggregatedCountersOutput>> getNodeAggregatedCounters(
+ public ListenableFuture<RpcResult<GetNodeAggregatedCountersOutput>> getNodeAggregatedCounters(
GetNodeAggregatedCountersInput input) {
BigInteger dpId = input.getNodeId();
LOG.trace("getting aggregated node counters for node {}", dpId);
List<CounterResult> aggregatedCounterResults = new ArrayList<>();
try {
if (!getNodeAggregatedResult(aggregatedCounterResults, dpId)) {
- StatisticsPluginImplCounters.failed_getting_aggregated_node_counters.inc();
+ statisticsCounters.failedGettingAggregatedNodeCounters();
return RpcResultBuilder.<GetNodeAggregatedCountersOutput>failed()
.withError(ErrorType.APPLICATION, "failed to get node aggregated counters for node " + dpId)
.buildFuture();
}
} catch (Exception e) {
- LOG.warn("failed to get counter result for node " + dpId, e);
+ LOG.warn("failed to get counter result for node {}", dpId, e);
return RpcResultBuilder.<GetNodeAggregatedCountersOutput>failed()
.withError(ErrorType.APPLICATION, "failed to get node aggregated counters for node " + dpId)
.buildFuture();
@Override
@SuppressWarnings("checkstyle:illegalCatch")
- public Future<RpcResult<GetNodeConnectorCountersOutput>> getNodeConnectorCounters(
+ public ListenableFuture<RpcResult<GetNodeConnectorCountersOutput>> getNodeConnectorCounters(
GetNodeConnectorCountersInput input) {
String portId = input.getPortId();
LOG.trace("getting port counters of port {}", portId);
try {
if (!getNodeConnectorResult(counterResults, dpId, portNumber)) {
- StatisticsPluginImplCounters.failed_getting_node_connector_counters.inc();
+ statisticsCounters.failedGettingNodeConnectorCounters();
return RpcResultBuilder.<GetNodeConnectorCountersOutput>failed()
.withError(ErrorType.APPLICATION, "failed to get port counters").buildFuture();
}
} catch (RuntimeException e) {
- LOG.warn("failed to get counter result for port " + portId, e);
+ LOG.warn("failed to get counter result for port {}", portId, e);
}
GetNodeConnectorCountersOutputBuilder gpcob = new GetNodeConnectorCountersOutputBuilder();
}
@Override
- public Future<RpcResult<AcquireElementCountersRequestHandlerOutput>> acquireElementCountersRequestHandler(
+ public ListenableFuture<RpcResult<AcquireElementCountersRequestHandlerOutput>> acquireElementCountersRequestHandler(
AcquireElementCountersRequestHandlerInput input) {
AcquireElementCountersRequestHandlerOutputBuilder aecrhob =
new AcquireElementCountersRequestHandlerOutputBuilder();
- ReadWriteTransaction transaction = db.newReadWriteTransaction();
UUID randomNumber = UUID.randomUUID();
Integer intRequestKey = allocateId(randomNumber.toString());
if (intRequestKey == null) {
LOG.warn("failed generating unique request identifier");
- StatisticsPluginImplCounters.failed_generating_unique_request_id.inc();
+ statisticsCounters.failedGeneratingUniqueRequestId();
return RpcResultBuilder.<AcquireElementCountersRequestHandlerOutput>failed()
.withError(ErrorType.APPLICATION, "failed generating unique request identifier").buildFuture();
}
String requestKey = String.valueOf(intRequestKey);
-
- try {
- if (input.getIncomingTraffic() != null) {
- CheckedFuture<Optional<EgressElementCountersRequestConfig>, ReadFailedException> eecrc =
- transaction.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.EECRC_IDENTIFIER);
- Optional<EgressElementCountersRequestConfig> eecrcOpt = eecrc.get();
- if (!eecrcOpt.isPresent()) {
- LOG.warn("failed creating incoming traffic counter request data container in DB");
- StatisticsPluginImplCounters.failed_creating_egress_counter_data_config.inc();
- return RpcResultBuilder.<AcquireElementCountersRequestHandlerOutput>failed()
- .withError(ErrorType.APPLICATION,
- "failed creating egress counter request data container in DB")
- .buildFuture();
- }
- if (!isIdenticalCounterRequestExist(input.getPortId(), ElementCountersDirection.EGRESS.toString(),
- input.getIncomingTraffic().getFilters(), eecrcOpt.get().getCounterRequests())) {
- installCounterSpecificRules(input.getPortId(), getLportTag(input.getPortId()),
- getDpn(input.getPortId()), ElementCountersDirection.EGRESS,
- input.getIncomingTraffic().getFilters());
+ SettableFuture<RpcResult<AcquireElementCountersRequestHandlerOutput>> result = SettableFuture.create();
+
+ ListenableFutures.addErrorLogging(
+ txRunner.callWithNewReadWriteTransactionAndSubmit(transaction -> {
+ if (input.getIncomingTraffic() != null) {
+ Optional<EgressElementCountersRequestConfig> eecrcOpt =
+ transaction.read(LogicalDatastoreType.CONFIGURATION,
+ CountersServiceUtils.EECRC_IDENTIFIER).get();
+ if (!eecrcOpt.isPresent()) {
+ LOG.warn("failed creating incoming traffic counter request data container in DB");
+ statisticsCounters.failedCreatingEgressCounterDataConfig();
+ result.setFuture(RpcResultBuilder.<AcquireElementCountersRequestHandlerOutput>failed()
+ .withError(ErrorType.APPLICATION,
+ "failed creating egress counter request data container in DB")
+ .buildFuture());
+ return;
+ }
+ if (!isIdenticalCounterRequestExist(input.getPortId(),
+ ElementCountersDirection.EGRESS.toString(),
+ input.getIncomingTraffic().getFilters(), eecrcOpt.get().getCounterRequests())) {
+ installCounterSpecificRules(input.getPortId(), getLportTag(input.getPortId()),
+ getDpn(input.getPortId()), ElementCountersDirection.EGRESS,
+ input.getIncomingTraffic().getFilters());
+ }
+ putEgressElementCounterRequestInConfig(input, ElementCountersDirection.EGRESS, transaction,
+ requestKey, CountersServiceUtils.EECRC_IDENTIFIER, eecrcOpt, randomNumber.toString());
+
+ aecrhob.setIncomingTrafficHandler(requestKey);
+
+ bindCountersServiceIfUnbound(input.getPortId(), ElementCountersDirection.EGRESS);
}
- putEgressElementCounterRequestInConfig(input, ElementCountersDirection.EGRESS, transaction, requestKey,
- CountersServiceUtils.EECRC_IDENTIFIER, eecrcOpt, randomNumber.toString());
- transaction.submit();
- aecrhob.setIncomingTrafficHandler(requestKey);
- bindCountersServiceIfUnbound(input.getPortId(), ElementCountersDirection.EGRESS);
- }
- if (input.getOutgoingTraffic() != null) {
- transaction = db.newReadWriteTransaction();
- CheckedFuture<Optional<IngressElementCountersRequestConfig>, ReadFailedException> iecrc =
- transaction.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.IECRC_IDENTIFIER);
- Optional<IngressElementCountersRequestConfig> iecrcOpt = iecrc.get();
- if (!iecrcOpt.isPresent()) {
- LOG.warn("failed creating outgoing traffc counter request data container in DB");
- StatisticsPluginImplCounters.failed_creating_ingress_counter_data_config.inc();
- return RpcResultBuilder.<AcquireElementCountersRequestHandlerOutput>failed()
- .withError(ErrorType.APPLICATION,
- "failed creating ingress counter request data container in DB")
- .buildFuture();
+ if (input.getOutgoingTraffic() != null) {
+ Optional<IngressElementCountersRequestConfig> iecrcOpt =
+ transaction.read(LogicalDatastoreType.CONFIGURATION,
+ CountersServiceUtils.IECRC_IDENTIFIER).get();
+ if (!iecrcOpt.isPresent()) {
+ LOG.warn("failed creating outgoing traffc counter request data container in DB");
+ statisticsCounters.failedCreatingIngressCounterDataConfig();
+ result.setFuture(RpcResultBuilder.<AcquireElementCountersRequestHandlerOutput>failed()
+ .withError(ErrorType.APPLICATION,
+ "failed creating ingress counter request data container in DB")
+ .buildFuture());
+ return;
+ }
+ if (!isIdenticalCounterRequestExist(input.getPortId(),
+ ElementCountersDirection.INGRESS.toString(),
+ input.getOutgoingTraffic().getFilters(), iecrcOpt.get().getCounterRequests())) {
+ installCounterSpecificRules(input.getPortId(), getLportTag(input.getPortId()),
+ getDpn(input.getPortId()), ElementCountersDirection.INGRESS,
+ input.getOutgoingTraffic().getFilters());
+ }
+ putIngressElementCounterRequestInConfig(input, ElementCountersDirection.INGRESS, transaction,
+ requestKey, CountersServiceUtils.IECRC_IDENTIFIER, iecrcOpt, randomNumber.toString());
+
+ aecrhob.setIncomingTrafficHandler(requestKey);
+
+ bindCountersServiceIfUnbound(input.getPortId(), ElementCountersDirection.INGRESS);
+
+ result.setFuture(RpcResultBuilder.success(aecrhob.build()).buildFuture());
}
- if (!isIdenticalCounterRequestExist(input.getPortId(), ElementCountersDirection.INGRESS.toString(),
- input.getOutgoingTraffic().getFilters(), iecrcOpt.get().getCounterRequests())) {
- installCounterSpecificRules(input.getPortId(), getLportTag(input.getPortId()),
- getDpn(input.getPortId()), ElementCountersDirection.INGRESS,
- input.getOutgoingTraffic().getFilters());
- }
- putIngressElementCounterRequestInConfig(input, ElementCountersDirection.INGRESS, transaction,
- requestKey, CountersServiceUtils.IECRC_IDENTIFIER, iecrcOpt, randomNumber.toString());
- transaction.submit();
- aecrhob.setIncomingTrafficHandler(requestKey);
-
- bindCountersServiceIfUnbound(input.getPortId(), ElementCountersDirection.INGRESS);
- }
- } catch (InterruptedException | ExecutionException e) {
- LOG.warn("failed to get counter request data from DB");
- return RpcResultBuilder.<AcquireElementCountersRequestHandlerOutput>failed()
- .withError(ErrorType.APPLICATION, "failed to get counter request data from DB").buildFuture();
- }
+ }), LOG, "Error acquiring element counters");
- return RpcResultBuilder.success(aecrhob.build()).buildFuture();
+ return result;
}
@Override
- public Future<RpcResult<ReleaseElementCountersRequestHandlerOutput>> releaseElementCountersRequestHandler(
+ public ListenableFuture<RpcResult<ReleaseElementCountersRequestHandlerOutput>> releaseElementCountersRequestHandler(
ReleaseElementCountersRequestHandlerInput input) {
InstanceIdentifier<CounterRequests> ingressPath =
InstanceIdentifier.builder(IngressElementCountersRequestConfig.class)
InstanceIdentifier.builder(EgressElementCountersRequestConfig.class)
.child(CounterRequests.class, new CounterRequestsKey(input.getHandler())).build();
- ReadWriteTransaction tx = db.newReadWriteTransaction();
- CheckedFuture<Optional<CounterRequests>, ReadFailedException> ingressRequestData =
- tx.read(LogicalDatastoreType.CONFIGURATION, ingressPath);
- CheckedFuture<Optional<CounterRequests>, ReadFailedException> egressRequestData =
- tx.read(LogicalDatastoreType.CONFIGURATION, egressPath);
- CheckedFuture<Optional<IngressElementCountersRequestConfig>, ReadFailedException> iecrc =
- tx.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.IECRC_IDENTIFIER);
- CheckedFuture<Optional<EgressElementCountersRequestConfig>, ReadFailedException> eecrc =
- tx.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.EECRC_IDENTIFIER);
-
- try {
- Optional<IngressElementCountersRequestConfig> iecrcOpt = iecrc.get();
- Optional<EgressElementCountersRequestConfig> eecrcOpt = eecrc.get();
+ SettableFuture<RpcResult<ReleaseElementCountersRequestHandlerOutput>> result = SettableFuture.create();
+ ListenableFutures.addErrorLogging(txRunner.callWithNewReadWriteTransactionAndSubmit(tx -> {
+ Optional<IngressElementCountersRequestConfig> iecrcOpt =
+ tx.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.IECRC_IDENTIFIER).get();
+ Optional<EgressElementCountersRequestConfig> eecrcOpt =
+ tx.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.EECRC_IDENTIFIER).get();
if (!iecrcOpt.isPresent() || !eecrcOpt.isPresent()) {
LOG.warn("Couldn't read element counters config data from DB");
- StatisticsPluginImplCounters.failed_reading_counter_data_from_config.inc();
- return RpcResultBuilder.<ReleaseElementCountersRequestHandlerOutput>failed()
+ statisticsCounters.failedReadingCounterDataFromConfig();
+ result.setFuture(RpcResultBuilder.<ReleaseElementCountersRequestHandlerOutput>failed()
.withError(ErrorType.APPLICATION, "Couldn't read element counters config data from DB")
- .buildFuture();
+ .buildFuture());
+ return;
}
- if (!ingressRequestData.get().isPresent() && !egressRequestData.get().isPresent()) {
+ Optional<CounterRequests> ingressRequestOpt =
+ tx.read(LogicalDatastoreType.CONFIGURATION, ingressPath).get();
+ Optional<CounterRequests> egressRequestOpt =
+ tx.read(LogicalDatastoreType.CONFIGURATION, egressPath).get();
+ if (!ingressRequestOpt.isPresent() && !egressRequestOpt.isPresent()) {
LOG.warn("Handler does not exists");
- StatisticsPluginImplCounters.unknown_request_handler.inc();
- return RpcResultBuilder.<ReleaseElementCountersRequestHandlerOutput>failed()
- .withError(ErrorType.APPLICATION, "Handler does not exists").buildFuture();
+ statisticsCounters.unknownRequestHandler();
+ result.setFuture(RpcResultBuilder.<ReleaseElementCountersRequestHandlerOutput>failed()
+ .withError(ErrorType.APPLICATION, "Handler does not exists").buildFuture());
+ return;
}
String generatedKey = null;
- if (ingressRequestData.get().isPresent()) {
- handleReleaseTransaction(input, ingressPath, ingressRequestData, iecrcOpt.get().getCounterRequests());
- generatedKey = ingressRequestData.get().get().getGeneratedUniqueId();
+ if (ingressRequestOpt.isPresent()) {
+ handleReleaseTransaction(tx, input, ingressPath, ingressRequestOpt,
+ iecrcOpt.get().getCounterRequests());
+ generatedKey = ingressRequestOpt.get().getGeneratedUniqueId();
}
- if (egressRequestData.get().isPresent()) {
- handleReleaseTransaction(input, egressPath, egressRequestData, eecrcOpt.get().getCounterRequests());
- generatedKey = egressRequestData.get().get().getGeneratedUniqueId();
+ if (egressRequestOpt.isPresent()) {
+ handleReleaseTransaction(tx, input, egressPath, egressRequestOpt, eecrcOpt.get().getCounterRequests());
+ generatedKey = egressRequestOpt.get().getGeneratedUniqueId();
}
releaseId(generatedKey);
- } catch (InterruptedException | ExecutionException e) {
- LOG.warn("failed to get counter request data from DB");
- return RpcResultBuilder.<ReleaseElementCountersRequestHandlerOutput>failed()
- .withError(ErrorType.APPLICATION, "failed to get counter request data from DB").buildFuture();
- }
+ result.setFuture(RpcResultBuilder.<ReleaseElementCountersRequestHandlerOutput>success().buildFuture());
+ }), LOG, "Error releasing element counters");
- return RpcResultBuilder.<ReleaseElementCountersRequestHandlerOutput>success().buildFuture();
+ return result;
}
@Override
- public Future<RpcResult<GetElementCountersByHandlerOutput>> getElementCountersByHandler(
+ public ListenableFuture<RpcResult<GetElementCountersByHandlerOutput>> getElementCountersByHandler(
GetElementCountersByHandlerInput input) {
InstanceIdentifier<CounterRequests> ingressPath =
InstanceIdentifier.builder(IngressElementCountersRequestConfig.class)
InstanceIdentifier.builder(EgressElementCountersRequestConfig.class)
.child(CounterRequests.class, new CounterRequestsKey(input.getHandler())).build();
- ReadOnlyTransaction tx = db.newReadOnlyTransaction();
+ ReadTransaction tx = db.newReadOnlyTransaction();
CheckedFuture<Optional<CounterRequests>, ReadFailedException> ingressRequestData =
tx.read(LogicalDatastoreType.CONFIGURATION, ingressPath);
CheckedFuture<Optional<CounterRequests>, ReadFailedException> egressRequestData =
CounterResultDataStructure ingressCounterResultDS = createElementCountersResult(ingressCounterRequest);
if (ingressCounterResultDS == null) {
LOG.warn("Unable to get counter results");
- StatisticsPluginImplCounters.failed_getting_counter_results.inc();
+ statisticsCounters.failedGettingCounterResults();
return RpcResultBuilder.<GetElementCountersByHandlerOutput>failed()
.withError(ErrorType.APPLICATION, "Unable to get counter results").buildFuture();
}
CounterResultDataStructure egressCounterResultDS = createElementCountersResult(egressCounterRequest);
if (egressCounterResultDS == null) {
LOG.warn("Unable to get counter results");
- StatisticsPluginImplCounters.failed_getting_counter_results.inc();
+ statisticsCounters.failedGettingCounterResults();
return RpcResultBuilder.<GetElementCountersByHandlerOutput>failed()
.withError(ErrorType.APPLICATION, "Unable to get counter results").buildFuture();
}
LOG.warn("failed to get counter request data from DB");
return RpcResultBuilder.<GetElementCountersByHandlerOutput>failed()
.withError(ErrorType.APPLICATION, "failed to get counter request data from DB").buildFuture();
+ } finally {
+ tx.close();
}
GetElementCountersByHandlerOutputBuilder gecbhob = new GetElementCountersByHandlerOutputBuilder();
public void handleInterfaceRemoval(String interfaceId) {
CheckedFuture<Optional<IngressElementCountersRequestConfig>, ReadFailedException> iecrc;
CheckedFuture<Optional<EgressElementCountersRequestConfig>, ReadFailedException> eecrc;
- try (ReadOnlyTransaction tx = db.newReadOnlyTransaction()) {
+ try (ReadTransaction tx = db.newReadOnlyTransaction()) {
iecrc = tx.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.IECRC_IDENTIFIER);
eecrc = tx.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.EECRC_IDENTIFIER);
}
Optional<EgressElementCountersRequestConfig> eecrcOpt = eecrc.get();
if (!iecrcOpt.isPresent() || !eecrcOpt.isPresent()) {
LOG.warn("Couldn't read element counters config data from DB");
- StatisticsPluginImplCounters.failed_reading_counter_data_from_config.inc();
+ statisticsCounters.failedReadingCounterDataFromConfig();
return;
}
removeAllElementCounterRequestsOnPort(interfaceId, iecrcOpt.get().getCounterRequests());
removeAllElementCounterRequestsOnPort(interfaceId, eecrcOpt.get().getCounterRequests());
} catch (InterruptedException | ExecutionException e) {
LOG.warn("failed to get counter request data from DB");
- StatisticsPluginImplCounters.failed_getting_counter_results_port_removal.inc();
+ statisticsCounters.failedGettingCounterResultsPortRemoval();
return;
}
}
@Override
- public Future<RpcResult<CleanAllElementCounterRequestsOutput>> cleanAllElementCounterRequests(
+ public ListenableFuture<RpcResult<CleanAllElementCounterRequestsOutput>> cleanAllElementCounterRequests(
CleanAllElementCounterRequestsInput input) {
- ReadOnlyTransaction tx = db.newReadOnlyTransaction();
+ ReadTransaction tx = db.newReadOnlyTransaction();
CheckedFuture<Optional<IngressElementCountersRequestConfig>, ReadFailedException> iecrc =
tx.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.IECRC_IDENTIFIER);
CheckedFuture<Optional<EgressElementCountersRequestConfig>, ReadFailedException> eecrc =
Optional<EgressElementCountersRequestConfig> eecrcOpt = eecrc.get();
if (!iecrcOpt.isPresent() || !eecrcOpt.isPresent()) {
LOG.warn("Couldn't read element counters config data from DB");
- StatisticsPluginImplCounters.failed_reading_counter_data_from_config.inc();
+ statisticsCounters.failedReadingCounterDataFromConfig();
return RpcResultBuilder.<CleanAllElementCounterRequestsOutput>failed()
.withError(ErrorType.APPLICATION, "Couldn't read element counters config data from DB")
.buildFuture();
LOG.warn("failed to get counter request data from DB");
return RpcResultBuilder.<CleanAllElementCounterRequestsOutput>failed()
.withError(ErrorType.APPLICATION, "failed to get counter request data from DB").buildFuture();
-
+ } finally {
+ tx.close();
}
return RpcResultBuilder.<CleanAllElementCounterRequestsOutput>success().buildFuture();
}
}
private void deleteCounterRequest(CounterRequests counterRequest, ElementCountersDirection direction) {
- WriteTransaction tx = db.newWriteOnlyTransaction();
- if (ElementCountersDirection.INGRESS.equals(direction)) {
- tx.delete(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier
- .builder(IngressElementCountersRequestConfig.class)
- .child(CounterRequests.class, new CounterRequestsKey(counterRequest.getKey().getRequestId()))
- .build());
- } else if (ElementCountersDirection.EGRESS.equals(direction)) {
- tx.delete(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier
- .builder(EgressElementCountersRequestConfig.class)
- .child(CounterRequests.class, new CounterRequestsKey(counterRequest.getKey().getRequestId()))
- .build());
- }
- tx.submit();
+ ListenableFutures.addErrorLogging(
+ txRunner.callWithNewWriteOnlyTransactionAndSubmit(tx -> {
+ if (ElementCountersDirection.INGRESS.equals(direction)) {
+ tx.delete(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier
+ .builder(IngressElementCountersRequestConfig.class)
+ .child(CounterRequests.class,
+ new CounterRequestsKey(counterRequest.key().getRequestId()))
+ .build());
+ } else if (ElementCountersDirection.EGRESS.equals(direction)) {
+ tx.delete(LogicalDatastoreType.CONFIGURATION, InstanceIdentifier
+ .builder(EgressElementCountersRequestConfig.class)
+ .child(CounterRequests.class,
+ new CounterRequestsKey(counterRequest.key().getRequestId()))
+ .build());
+ }
+ }), LOG, "Error deleting counter");
}
private CounterResultDataStructure createElementCountersResult(CounterRequests counterRequest) {
List<MatchInfoBase> matches = CountersServiceUtils.getCounterFlowMatch(ecr, lportTag,
ElementCountersDirection.valueOf(counterRequest.getTrafficDirection()));
Match match = MDSALUtil.buildMatches(matches);
- CounterResultDataStructure counterResultDS = counterRetriever.getSwitchFlowCountersDirect(dpId, match,
- CountersServiceUtils.getTableId(ecr.getDirection()));
- return counterResultDS;
+ return counterRetriever.getSwitchFlowCountersDirect(dpId, match);
}
private void initializeCountrsConfigDataSrore() {
- ReadWriteTransaction transaction = db.newReadWriteTransaction();
- CheckedFuture<Optional<IngressElementCountersRequestConfig>, ReadFailedException> iecrc =
- transaction.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.IECRC_IDENTIFIER);
- CheckedFuture<Optional<EgressElementCountersRequestConfig>, ReadFailedException> eecrc =
- transaction.read(LogicalDatastoreType.CONFIGURATION, CountersServiceUtils.EECRC_IDENTIFIER);
- try {
- Optional<IngressElementCountersRequestConfig> iecrcOpt = iecrc.get();
- if (!iecrcOpt.isPresent()) {
- creatIngressEelementCountersContainerInConfig(transaction, CountersServiceUtils.IECRC_IDENTIFIER);
- }
-
- Optional<EgressElementCountersRequestConfig> eecrcOpt = eecrc.get();
- if (!eecrcOpt.isPresent()) {
- creatEgressEelementCountersContainerInConfig(transaction, CountersServiceUtils.EECRC_IDENTIFIER);
- }
- transaction.submit();
- } catch (InterruptedException | ExecutionException e) {
- StatisticsPluginImplCounters.failed_creating_counters_config.inc();
- LOG.warn("failed creating counters config data structure in DB");
- }
+ ListenableFutures.addErrorLogging(
+ txRunner.callWithNewReadWriteTransactionAndSubmit(transaction -> {
+ Optional<IngressElementCountersRequestConfig> iecrcOpt =
+ transaction.read(LogicalDatastoreType.CONFIGURATION,
+ CountersServiceUtils.IECRC_IDENTIFIER).get();
+ Optional<EgressElementCountersRequestConfig> eecrcOpt =
+ transaction.read(LogicalDatastoreType.CONFIGURATION,
+ CountersServiceUtils.EECRC_IDENTIFIER).get();
+ if (!iecrcOpt.isPresent()) {
+ creatIngressEelementCountersContainerInConfig(transaction,
+ CountersServiceUtils.IECRC_IDENTIFIER);
+ }
+ if (!eecrcOpt.isPresent()) {
+ creatEgressEelementCountersContainerInConfig(transaction,
+ CountersServiceUtils.EECRC_IDENTIFIER);
+ }
+ }), LOG, "Failed to create counters in config datastore");
}
- private void handleReleaseTransaction(ReleaseElementCountersRequestHandlerInput input,
- InstanceIdentifier<CounterRequests> path,
- CheckedFuture<Optional<CounterRequests>, ReadFailedException> requestData,
- List<CounterRequests> counterRequests) throws InterruptedException, ExecutionException {
- WriteTransaction wt = db.newWriteOnlyTransaction();
- wt.delete(LogicalDatastoreType.CONFIGURATION, path);
- wt.submit();
- CounterRequests counterRequest = requestData.get().get();
- if (shouldUnbindCountersService(counterRequest.getPortId(), counterRequest.getKey().getRequestId(),
+ private void handleReleaseTransaction(WriteTransaction transaction, ReleaseElementCountersRequestHandlerInput input,
+ InstanceIdentifier<CounterRequests> path, Optional<CounterRequests> requestData,
+ List<CounterRequests> counterRequests) {
+ transaction.delete(LogicalDatastoreType.CONFIGURATION, path);
+ CounterRequests counterRequest = requestData.get();
+ if (shouldUnbindCountersService(counterRequest.getPortId(), counterRequest.key().getRequestId(),
counterRequests)) {
unbindCountersServiceIfBound(counterRequest.getPortId(),
ElementCountersDirection.valueOf(counterRequest.getTrafficDirection()));
}
}
+ @SuppressFBWarnings("SLF4J_FORMAT_SHOULD_BE_CONST")
private void logElementCounterRequests(List<ElementCountersRequest> ecrList) {
for (ElementCountersRequest counterRequest : ecrList) {
LOG.debug(counterRequest.toString());
CountersServiceUtils.INGRESS_COUNTERS_SERVICE_INDEX, interfaceId)) {
IngressCountersServiceImpl icsi = new IngressCountersServiceImpl(db, interfaceManager, mdsalApiManager);
icsi.bindService(interfaceId);
- StatisticsPluginImplCounters.ingress_counters_service_bind.inc();
+ statisticsCounters.ingressCountersServiceBind();
} else if (ElementCountersDirection.EGRESS.equals(direction) && !interfaceManager
.isServiceBoundOnInterfaceForEgress(CountersServiceUtils.EGRESS_COUNTERS_SERVICE_INDEX, interfaceId)) {
EgressCountersServiceImpl ecsi = new EgressCountersServiceImpl(db, interfaceManager, mdsalApiManager);
ecsi.bindService(interfaceId);
- StatisticsPluginImplCounters.egress_counters_service_bind.inc();
+ statisticsCounters.egressCountersServiceBind();
}
}
CountersServiceUtils.INGRESS_COUNTERS_SERVICE_INDEX, interfaceId)) {
IngressCountersServiceImpl icsi = new IngressCountersServiceImpl(db, interfaceManager, mdsalApiManager);
icsi.unBindService(interfaceId);
- StatisticsPluginImplCounters.ingress_counters_service_unbind.inc();
+ statisticsCounters.ingressCountersServiceUnbind();
} else if (ElementCountersDirection.EGRESS.equals(direction) && interfaceManager
.isServiceBoundOnInterfaceForEgress(CountersServiceUtils.EGRESS_COUNTERS_SERVICE_INDEX, interfaceId)) {
EgressCountersServiceImpl ecsi = new EgressCountersServiceImpl(db, interfaceManager, mdsalApiManager);
ecsi.unBindService(interfaceId);
- StatisticsPluginImplCounters.egress_counters_service_unbind.inc();
+ statisticsCounters.egressCountersServiceUnbind();
}
}
private boolean shouldUnbindCountersService(String portId, String requesId, List<CounterRequests> counterRequests) {
for (CounterRequests counterRequest : counterRequests) {
- if (portId.equals(counterRequest.getPortId()) && !requesId.equals(counterRequest.getKey().getRequestId())) {
+ if (portId.equals(counterRequest.getPortId()) && !requesId.equals(counterRequest.key().getRequestId())) {
return false;
}
}
IngressElementCountersRequestConfig requestConfig = iecrcOpt.get();
CounterRequestsBuilder crb = new CounterRequestsBuilder();
crb.setRequestId(requestKey);
- crb.setKey(new CounterRequestsKey(requestKey));
+ crb.withKey(new CounterRequestsKey(requestKey));
crb.setFilters(input.getOutgoingTraffic().getFilters());
crb.setPortId(input.getPortId());
crb.setLportTag(getLportTag(input.getPortId()));
IngressElementCountersRequestConfigBuilder ecrcb = new IngressElementCountersRequestConfigBuilder();
ecrcb.setCounterRequests(counterRequests);
requestConfig = ecrcb.build();
- transaction.put(LogicalDatastoreType.CONFIGURATION, ecrcIdentifier, requestConfig,
- WriteTransaction.CREATE_MISSING_PARENTS);
+ transaction.mergeParentStructurePut(LogicalDatastoreType.CONFIGURATION, ecrcIdentifier, requestConfig);
}
private void putEgressElementCounterRequestInConfig(AcquireElementCountersRequestHandlerInput input,
EgressElementCountersRequestConfig requestConfig = eecrcOpt.get();
CounterRequestsBuilder crb = new CounterRequestsBuilder();
crb.setRequestId(requestKey);
- crb.setKey(new CounterRequestsKey(requestKey));
+ crb.withKey(new CounterRequestsKey(requestKey));
crb.setFilters(input.getIncomingTraffic().getFilters());
crb.setPortId(input.getPortId());
crb.setLportTag(getLportTag(input.getPortId()));
EgressElementCountersRequestConfigBuilder ecrcb = new EgressElementCountersRequestConfigBuilder();
ecrcb.setCounterRequests(counterRequests);
requestConfig = ecrcb.build();
- transaction.put(LogicalDatastoreType.CONFIGURATION, ecrcIdentifier, requestConfig,
- WriteTransaction.CREATE_MISSING_PARENTS);
+ transaction.put(LogicalDatastoreType.CONFIGURATION, ecrcIdentifier, requestConfig);
}
private void creatIngressEelementCountersContainerInConfig(ReadWriteTransaction transaction,
List<CounterRequests> counterRequests = new ArrayList<>();
iecrcb.setCounterRequests(counterRequests);
IngressElementCountersRequestConfig iecrc = iecrcb.build();
- transaction.put(LogicalDatastoreType.CONFIGURATION, ecrcIdentifier, iecrc,
- WriteTransaction.CREATE_MISSING_PARENTS);
+ transaction.put(LogicalDatastoreType.CONFIGURATION, ecrcIdentifier, iecrc);
}
private void creatEgressEelementCountersContainerInConfig(ReadWriteTransaction transaction,
List<CounterRequests> counterRequests = new ArrayList<>();
eecrcb.setCounterRequests(counterRequests);
EgressElementCountersRequestConfig eecrc = eecrcb.build();
- transaction.put(LogicalDatastoreType.CONFIGURATION, ecrcIdentifier, eecrc,
- WriteTransaction.CREATE_MISSING_PARENTS);
+ transaction.put(LogicalDatastoreType.CONFIGURATION, ecrcIdentifier, eecrc);
}
private Integer allocateId(String idKey) {
CreateIdPoolInput createPool = new CreateIdPoolInputBuilder()
.setPoolName(CountersServiceUtils.COUNTERS_PULL_NAME).setLow(CountersServiceUtils.COUNTERS_PULL_START)
.setHigh(CountersServiceUtils.COUNTERS_PULL_START + CountersServiceUtils.COUNTERS_PULL_END).build();
- Future<RpcResult<Void>> result = idManagerService.createIdPool(createPool);
- Futures.addCallback(JdkFutureAdapters.listenInPoolThread(result), new FutureCallback<RpcResult<Void>>() {
+ ListenableFuture<RpcResult<CreateIdPoolOutput>> result = idManagerService.createIdPool(createPool);
+ Futures.addCallback(result, new FutureCallback<RpcResult<CreateIdPoolOutput>>() {
@Override
public void onFailure(Throwable error) {
}
@Override
- public void onSuccess(@Nonnull RpcResult<Void> rpcResult) {
+ public void onSuccess(@NonNull RpcResult<CreateIdPoolOutput> rpcResult) {
if (rpcResult.isSuccessful()) {
LOG.debug("Created IdPool for tap");
} else {
ReleaseIdInput idInput = new ReleaseIdInputBuilder().setPoolName(CountersServiceUtils.COUNTERS_PULL_NAME)
.setIdKey(idKey).build();
try {
- Future<RpcResult<Void>> result = idManagerService.releaseId(idInput);
- RpcResult<Void> rpcResult = result.get();
+ RpcResult<ReleaseIdOutput> rpcResult = idManagerService.releaseId(idInput).get();
if (!rpcResult.isSuccessful()) {
- LOG.warn("RPC Call to release Id {} with Key {} returned with Errors {}", idKey, rpcResult.getErrors());
+ LOG.warn("RPC Call to release Id with Key {} returned with Errors {}", idKey, rpcResult.getErrors());
}
} catch (InterruptedException | ExecutionException e) {
LOG.warn("Exception when releasing Id for key {}", idKey, e);
}
private boolean checkPoolExists() {
- ReadOnlyTransaction roTransaction = db.newReadOnlyTransaction();
+ ReadTransaction roTransaction = db.newReadOnlyTransaction();
InstanceIdentifier<IdPool> path = InstanceIdentifier.create(IdPools.class).child(IdPool.class,
new IdPoolKey(CountersServiceUtils.COUNTERS_PULL_NAME));
CheckedFuture<Optional<IdPool>, ReadFailedException> pool =
}
} catch (InterruptedException | ExecutionException e) {
return false;
+ } finally {
+ roTransaction.close();
}
return false;
}
for (CounterRequests counterRequest : counterRequests) {
if (portId.equals(counterRequest.getPortId()) && dirction.equals(counterRequest.getTrafficDirection())
- && !counterRequest.getKey().getRequestId().equals(requesId)) {
+ && !counterRequest.key().getRequestId().equals(requesId)) {
if (areFiltersEqual(filters, counterRequest.getFilters())) {
return true;
}
return ecrList;
}
-
- enum StatisticsPluginImplCounters {
- failed_getting_node_counters, //
- failed_getting_node_connector_counters, //
- failed_getting_aggregated_node_counters, //
- failed_generating_unique_request_id, //
- unknown_request_handler, //
- failed_creating_ingress_counter_data_config, //
- failed_creating_egress_counter_data_config, //
- failed_reading_counter_data_from_config, //
- failed_getting_counter_results, //
- failed_creating_counters_config, //
- failed_getting_counter_results_port_removal, //
- ingress_counters_service_bind, //
- egress_counters_service_bind, //
- ingress_counters_service_unbind, //
- egress_counters_service_unbind, //
- ;
- private OccurenceCounter counter;
-
- StatisticsPluginImplCounters() {
- counter = new OccurenceCounter(getClass().getEnclosingClass().getSimpleName(), name(), "");
- }
-
- public void inc() {
- counter.inc();
- }
- }
}