Merge "Turn libraries/pom.xml into an aggregator"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / AbstractRequestCallback.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.openflowplugin.impl.services;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.util.concurrent.FutureCallback;
12 import org.eclipse.jdt.annotation.NonNull;
13 import org.eclipse.jdt.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.StatisticsGroup;
19 import org.opendaylight.openflowplugin.impl.services.util.RequestContextUtil;
20 import org.opendaylight.openflowplugin.impl.statistics.ofpspecific.EventsTimeCounter;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.Error;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23 import org.opendaylight.yangtools.yang.common.RpcError;
24 import org.opendaylight.yangtools.yang.common.RpcResult;
25 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
26
27 public abstract class AbstractRequestCallback<T> implements FutureCallback<OfHeader> {
28     private final RequestContext<T> context;
29     private final Class<?> requestType;
30     private final MessageSpy spy;
31     private EventIdentifier eventIdentifier;
32
33     AbstractRequestCallback(final RequestContext<T> context,
34                             final Class<?> requestType,
35                             final MessageSpy spy,
36                             final EventIdentifier eventIdentifier) {
37         this.context = Preconditions.checkNotNull(context);
38         this.requestType = Preconditions.checkNotNull(requestType);
39         this.spy = Preconditions.checkNotNull(spy);
40         this.eventIdentifier = eventIdentifier;
41     }
42
43     protected final void setResult(@Nullable final RpcResult<T> result) {
44         context.setResult(result);
45         context.close();
46     }
47
48     protected final void spyMessage(@NonNull final StatisticsGroup group) {
49         spy.spyMessage(requestType, Preconditions.checkNotNull(group));
50     }
51
52     public EventIdentifier getEventIdentifier() {
53         return eventIdentifier;
54     }
55
56     @Override
57     public final void onFailure(@NonNull final Throwable throwable) {
58         final RpcResultBuilder<T> builder;
59         if (null != eventIdentifier) {
60             EventsTimeCounter.markEnd(eventIdentifier);
61         }
62         if (throwable instanceof DeviceRequestFailedException) {
63             final Error err = ((DeviceRequestFailedException) throwable).getError();
64             final String errorString = String.format("Device reported error type %s code %s",
65                                                      err.getTypeString(),
66                                                      err.getCodeString());
67
68             builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, errorString, throwable);
69             spyMessage(StatisticsGroup.TO_SWITCH_SUBMIT_FAILURE);
70         } else {
71             builder = RpcResultBuilder.<T>failed()
72                     .withError(RpcError.ErrorType.APPLICATION, throwable.getMessage(), throwable);
73             spyMessage(StatisticsGroup.TO_SWITCH_SUBMIT_ERROR);
74         }
75
76         context.setResult(builder.build());
77         RequestContextUtil.closeRequestContext(context);
78     }
79 }