6e38acb771c4ff5b3724d856bdb6851be63b9455
[openflowplugin.git] / AbstractMultipartService.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.util.concurrent.FutureCallback;
11 import com.google.common.util.concurrent.ListenableFuture;
12 import java.util.List;
13 import java.util.function.Function;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
16 import org.opendaylight.openflowplugin.api.openflow.device.RequestContext;
17 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
18 import org.opendaylight.openflowplugin.impl.services.multilayer.MultiLayerMultipartRequestCallback;
19 import org.opendaylight.openflowplugin.impl.services.singlelayer.SingleLayerMultipartRequestCallback;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23
24 public abstract class AbstractMultipartService<I, T extends OfHeader> extends AbstractService<I, List<T>> {
25
26     private static final Function<OfHeader, Boolean> ALTERNATE_IS_COMPLETE = message ->
27         !(message instanceof MultipartReply) || !((MultipartReply) message).isRequestMore();
28
29     protected AbstractMultipartService(final RequestContextStack requestContextStack,
30                                        final DeviceContext deviceContext) {
31         super(requestContextStack, deviceContext);
32     }
33
34     @Override
35     protected FutureCallback<OfHeader> createCallback(RequestContext<List<T>> context, Class<?> requestType) {
36         return canUseSingleLayerSerialization()
37             ? new SingleLayerMultipartRequestCallback<>(context, requestType, getDeviceContext(), getEventIdentifier())
38             : new MultiLayerMultipartRequestCallback<>(context, requestType, getDeviceContext(), getEventIdentifier());
39     }
40
41     @Override
42     public final ListenableFuture<RpcResult<List<T>>> handleServiceCall(@NonNull final I input) {
43         return canUseSingleLayerSerialization()
44             ? super.handleServiceCall(input, ALTERNATE_IS_COMPLETE)
45             : super.handleServiceCall(input);
46     }
47
48 }