Merge "BUG-4117: add support of Old Notif. for Flow Statistics"
[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 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;
25
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;
31
32
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);
37     }
38
39     protected AbstractRequestCallback(final RequestContext<T> context,
40                                       final Class<?> requestType,
41                                       final MessageSpy spy,
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;
47     }
48
49     protected final void setResult(@Nullable final RpcResult<T> result) {
50         context.setResult(result);
51         context.close();
52     }
53
54     protected final void spyMessage(@Nonnull final STATISTIC_GROUP group) {
55         spy.spyMessage(requestType, Preconditions.checkNotNull(group));
56     }
57
58     public EventIdentifier getEventIdentifier() {
59         return eventIdentifier;
60     }
61
62     @Override
63     public final void onFailure(final Throwable t) {
64         final RpcResultBuilder<T> builder;
65         if (null != eventIdentifier) {
66             EventsTimeCounter.markEnd(eventIdentifier);
67         }
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());
71
72             builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, errorString, t);
73             spyMessage(MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_FAILURE);
74         } else {
75             builder = RpcResultBuilder.<T>failed().withError(RpcError.ErrorType.APPLICATION, t.getMessage(), t);
76             spyMessage(MessageSpy.STATISTIC_GROUP.TO_SWITCH_SUBMIT_ERROR);
77         }
78
79         context.setResult(builder.build());
80         RequestContextUtil.closeRequestContext(context);
81     }
82 }