Fix checkstyle
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / AbstractMultipartRequestCallback.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 java.util.List;
11 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
12 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
13 import org.opendaylight.openflowplugin.api.openflow.device.handlers.MultiMsgCollector;
14 import org.opendaylight.openflowplugin.api.openflow.statistics.ofpspecific.EventIdentifier;
15 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
16 import org.opendaylight.yangtools.yang.common.RpcError;
17 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 public abstract class AbstractMultipartRequestCallback<T extends OfHeader> extends AbstractRequestCallback<List<T>> {
22     private static final Logger LOG = LoggerFactory.getLogger(AbstractMultipartRequestCallback.class);
23     private final MultiMsgCollector<T> collector;
24
25     public AbstractMultipartRequestCallback(
26             final RequestContext<List<T>> context,
27             final Class<?> requestType,
28             final DeviceContext deviceContext,
29             final EventIdentifier eventIdentifier) {
30         super(context, requestType, deviceContext.getMessageSpy(), eventIdentifier);
31         collector = deviceContext.getMultiMsgCollector(context);
32     }
33
34     @Override
35     @SuppressWarnings("unchecked")
36     public void onSuccess(final OfHeader result) {
37         if (result == null) {
38             LOG.info("Response received was null.");
39             collector.endCollecting(getEventIdentifier());
40             return;
41         }
42
43         if (!isMultipart(result)) {
44             LOG.info("Unexpected response type received: {}.", result.getClass());
45
46             setResult(RpcResultBuilder
47                     .<List<T>>failed()
48                     .withError(RpcError.ErrorType.APPLICATION,
49                             String.format("Unexpected response type received: %s.", result.getClass()))
50                     .build());
51         } else {
52             final T resultCast = (T) result;
53             collector.addMultipartMsg(resultCast, isReqMore(resultCast), getEventIdentifier());
54         }
55     }
56
57     /**
58      * Check if result is multipart.
59      * @param result result
60      * @return true if result is multipart
61      */
62     protected abstract boolean isMultipart(OfHeader result);
63
64     /**
65      * Check if result requests more multiparts.
66      * @param result result
67      * @return true if result requests more multiparts
68      */
69     protected abstract boolean isReqMore(T result);
70
71 }