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.MessageSpy;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.MessageSpy.STATISTIC_GROUP;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
20 import org.opendaylight.yangtools.yang.common.RpcError;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
24 abstract class AbstractRequestCallback<T> implements FutureCallback<OfHeader> {
25 private final RequestContext<T> context;
26 private final Class<?> requestType;
27 private final MessageSpy spy;
29 protected AbstractRequestCallback(final RequestContext<T> context, final Class<?> requestType, final MessageSpy spy) {
30 this.context = Preconditions.checkNotNull(context);
31 this.requestType = Preconditions.checkNotNull(requestType);
32 this.spy = Preconditions.checkNotNull(spy);
35 protected final void setResult(@Nullable final RpcResult<T> result) {
36 context.setResult(result);
40 protected final void spyMessage(@Nonnull final STATISTIC_GROUP group) {
41 spy.spyMessage(requestType, Preconditions.checkNotNull(group));
45 public final void onFailure(final Throwable t) {
46 final RpcResultBuilder<T> builder;
47 if (t instanceof DeviceRequestFailedException) {
48 final Error err = ((DeviceRequestFailedException) t).getError();
49 final String errorString = String.format("Device reported error type %s code %s", err.getTypeString(), err.getCodeString());
51 builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, errorString, t);
52 spyMessage(MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
54 builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, t.getMessage(), t);
55 spyMessage(MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_ERROR);
58 context.setResult(builder.build());
59 RequestContextUtil.closeRequstContext(context);