Merge "Update docs header to Magnesium in the master"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / AbstractRequestCallback.java
index 47692c7956af6dd2934c99dea2b33d2fa884d222..1fdfa45edd3bd6a5dc54a8c6dd7c38850489dafe 100644 (file)
@@ -1,4 +1,4 @@
-/**
+/*
  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
@@ -13,23 +13,31 @@ import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
-import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy.STATISTIC_GROUP;
+import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy.StatisticsGroup;
+import org.opendaylight.openflowplugin.impl.services.util.RequestContextUtil;
+import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
 import org.opendaylight.yangtools.yang.common.RpcError;
 import org.opendaylight.yangtools.yang.common.RpcResult;
 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
 
-abstract class AbstractRequestCallback<T> implements FutureCallback<OfHeader> {
+public abstract class AbstractRequestCallback<T> implements FutureCallback<OfHeader> {
     private final RequestContext<T> context;
     private final Class<?> requestType;
     private final MessageSpy spy;
+    private EventIdentifier eventIdentifier;
 
-    protected AbstractRequestCallback(final RequestContext<T> context, final Class<?> requestType, final MessageSpy spy) {
+    AbstractRequestCallback(final RequestContext<T> context,
+                            final Class<?> requestType,
+                            final MessageSpy spy,
+                            final EventIdentifier eventIdentifier) {
         this.context = Preconditions.checkNotNull(context);
         this.requestType = Preconditions.checkNotNull(requestType);
         this.spy = Preconditions.checkNotNull(spy);
+        this.eventIdentifier = eventIdentifier;
     }
 
     protected final void setResult(@Nullable final RpcResult<T> result) {
@@ -37,25 +45,35 @@ abstract class AbstractRequestCallback<T> implements FutureCallback<OfHeader> {
         context.close();
     }
 
-    protected final void spyMessage(@Nonnull final STATISTIC_GROUP group) {
+    protected final void spyMessage(@Nonnull final StatisticsGroup group) {
         spy.spyMessage(requestType, Preconditions.checkNotNull(group));
     }
 
+    public EventIdentifier getEventIdentifier() {
+        return eventIdentifier;
+    }
+
     @Override
-    public final void onFailure(final Throwable t) {
+    public final void onFailure(@Nonnull final Throwable throwable) {
         final RpcResultBuilder<T> builder;
-        if (t instanceof DeviceRequestFailedException) {
-            final Error err = ((DeviceRequestFailedException) t).getError();
-            final String errorString = String.format("Device reported error type %s code %s", err.getTypeString(), err.getCodeString());
+        if (null != eventIdentifier) {
+            EventsTimeCounter.markEnd(eventIdentifier);
+        }
+        if (throwable instanceof DeviceRequestFailedException) {
+            final Error err = ((DeviceRequestFailedException) throwable).getError();
+            final String errorString = String.format("Device reported error type %s code %s",
+                                                     err.getTypeString(),
+                                                     err.getCodeString());
 
-            builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, errorString, t);
-            spyMessage(MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
+            builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, errorString, throwable);
+            spyMessage(StatisticsGroup.TO_SWITCH_SUBMIT_FAILURE);
         } else {
-            builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, t.getMessage(), t);
-            spyMessage(MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_ERROR);
+            builder = RpcResultBuilder.<T>failed()
+                    .withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
+            spyMessage(StatisticsGroup.TO_SWITCH_SUBMIT_ERROR);
         }
 
         context.setResult(builder.build());
-        RequestContextUtil.closeRequstContext(context);
+        RequestContextUtil.closeRequestContext(context);
     }
 }