2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.openflowplugin.impl.services;
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import javax.annotation.Nonnull;
13 import javax.annotation.Nullable;
14 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
15 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
16 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy.STATISTIC_GROUP;
19 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
22 import org.opendaylight.yangtools.yang.common.RpcError;
23 import org.opendaylight.yangtools.yang.common.RpcResult;
24 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26 abstract class AbstractRequestCallback<T> implements FutureCallback<OfHeader> {
27 private final RequestContext<T> context;
28 private final Class<?> requestType;
29 private final MessageSpy spy;
30 private EventIdentifier eventIdentifier;
33 protected AbstractRequestCallback(final RequestContext<T> context, final Class<?> requestType, final MessageSpy spy) {
34 this.context = Preconditions.checkNotNull(context);
35 this.requestType = Preconditions.checkNotNull(requestType);
36 this.spy = Preconditions.checkNotNull(spy);
39 protected AbstractRequestCallback(final RequestContext<T> context,
40 final Class<?> requestType,
42 final EventIdentifier eventIdentifier) {
43 this.context = Preconditions.checkNotNull(context);
44 this.requestType = Preconditions.checkNotNull(requestType);
45 this.spy = Preconditions.checkNotNull(spy);
46 this.eventIdentifier = eventIdentifier;
49 protected final void setResult(@Nullable final RpcResult<T> result) {
50 context.setResult(result);
54 protected final void spyMessage(@Nonnull final STATISTIC_GROUP group) {
55 spy.spyMessage(requestType, Preconditions.checkNotNull(group));
58 public EventIdentifier getEventIdentifier() {
59 return eventIdentifier;
63 public final void onFailure(final Throwable t) {
64 final RpcResultBuilder<T> builder;
65 if (null != eventIdentifier) {
66 EventsTimeCounter.markEnd(eventIdentifier);
68 if (t instanceof DeviceRequestFailedException) {
69 final Error err = ((DeviceRequestFailedException) t).getError();
70 final String errorString = String.format("Device reported error type %s code %s", err.getTypeString(), err.getCodeString());
72 builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, errorString, t);
73 spyMessage(MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
75 builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, t.getMessage(), t);
76 spyMessage(MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_ERROR);
79 context.setResult(builder.build());
80 RequestContextUtil.closeRequestContext(context);