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