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 static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FutureCallback;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.eclipse.jdt.annotation.Nullable;
15 import org.opendaylight.openflowjava.protocol.api.connection.DeviceRequestFailedException;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy;
19 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy.StatisticsGroup;
20 import org.opendaylight.openflowplugin.impl.services.util.RequestContextUtil;
21 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
24 import org.opendaylight.yangtools.yang.common.ErrorType;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
28 public abstract class AbstractRequestCallback<T> implements FutureCallback<OfHeader> {
29 private final RequestContext<T> context;
30 private final Class<?> requestType;
31 private final MessageSpy spy;
32 private final EventIdentifier eventIdentifier;
34 AbstractRequestCallback(final RequestContext<T> context,
35 final Class<?> requestType,
37 final EventIdentifier eventIdentifier) {
38 this.context = requireNonNull(context);
39 this.requestType = requireNonNull(requestType);
40 this.spy = requireNonNull(spy);
41 this.eventIdentifier = eventIdentifier;
44 protected final void setResult(@Nullable final RpcResult<T> result) {
45 context.setResult(result);
49 protected final void spyMessage(@NonNull final StatisticsGroup group) {
50 spy.spyMessage(requestType, requireNonNull(group));
53 public EventIdentifier getEventIdentifier() {
54 return eventIdentifier;
58 public final void onFailure(final Throwable throwable) {
59 final RpcResultBuilder<T> builder;
60 if (null != eventIdentifier) {
61 EventsTimeCounter.markEnd(eventIdentifier);
63 if (throwable instanceof DeviceRequestFailedException) {
64 final Error err = ((DeviceRequestFailedException) throwable).getError();
65 final String errorString = String.format("Device reported error type %s code %s",
69 builder = RpcResultBuilder.<T>failed().withError(ErrorType.APPLICATION, errorString, throwable);
70 spyMessage(StatisticsGroup.TO_SWITCH_SUBMIT_FAILURE);
72 if (throwable != null) {
73 builder = RpcResultBuilder.<T>failed()
74 .withError(ErrorType.APPLICATION, throwable.getMessage(), throwable);
76 Throwable deviceReadFailedThrowable = new Throwable("Failed to read from device.");
77 builder = RpcResultBuilder.<T>failed()
78 .withError(ErrorType.APPLICATION, deviceReadFailedThrowable.getMessage(),
79 deviceReadFailedThrowable);
81 spyMessage(StatisticsGroup.TO_SWITCH_SUBMIT_ERROR);
84 context.setResult(builder.build());
85 RequestContextUtil.closeRequestContext(context);