Use ByteBuf.readRetainedSlice()
[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 static java.util.Objects.requireNonNull;
11
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;
27
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;
33
34     AbstractRequestCallback(final RequestContext<T> context,
35                             final Class<?> requestType,
36                             final MessageSpy spy,
37                             final EventIdentifier eventIdentifier) {
38         this.context = requireNonNull(context);
39         this.requestType = requireNonNull(requestType);
40         this.spy = requireNonNull(spy);
41         this.eventIdentifier = eventIdentifier;
42     }
43
44     protected final void setResult(@Nullable final RpcResult<T> result) {
45         context.setResult(result);
46         context.close();
47     }
48
49     protected final void spyMessage(@NonNull final StatisticsGroup group) {
50         spy.spyMessage(requestType, requireNonNull(group));
51     }
52
53     public EventIdentifier getEventIdentifier() {
54         return eventIdentifier;
55     }
56
57     @Override
58     public final void onFailure(final Throwable throwable) {
59         final RpcResultBuilder<T> builder;
60         if (null != eventIdentifier) {
61             EventsTimeCounter.markEnd(eventIdentifier);
62         }
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",
66                                                      err.getTypeString(),
67                                                      err.getCodeString());
68
69             builder = RpcResultBuilder.<T>failed().withError(ErrorType.APPLICATION, errorString, throwable);
70             spyMessage(StatisticsGroup.TO_SWITCH_SUBMIT_FAILURE);
71         } else {
72             if (throwable != null) {
73                 builder = RpcResultBuilder.<T>failed()
74                         .withError(ErrorType.APPLICATION, throwable.getMessage(), throwable);
75             } else {
76                 Throwable deviceReadFailedThrowable = new Throwable("Failed to read from device.");
77                 builder = RpcResultBuilder.<T>failed()
78                         .withError(ErrorType.APPLICATION, deviceReadFailedThrowable.getMessage(),
79                                 deviceReadFailedThrowable);
80             }
81             spyMessage(StatisticsGroup.TO_SWITCH_SUBMIT_ERROR);
82         }
83
84         context.setResult(builder.build());
85         RequestContextUtil.closeRequestContext(context);
86     }
87 }