Add single layer deserialization support
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / 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 javax.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, final DeviceContext deviceContext) {
30         super(requestContextStack, deviceContext);
31     }
32
33     @Override
34     protected FutureCallback<OfHeader> createCallback(RequestContext<List<T>> context, Class<?> requestType) {
35         return canUseSingleLayerSerialization()
36             ? new SingleLayerMultipartRequestCallback<>(context, requestType, getDeviceContext(), getEventIdentifier())
37             : new MultiLayerMultipartRequestCallback<>(context, requestType, getDeviceContext(), getEventIdentifier());
38     }
39
40     @Override
41     public final ListenableFuture<RpcResult<List<T>>> handleServiceCall(@Nonnull final I input) {
42         return canUseSingleLayerSerialization()
43             ? super.handleServiceCall(input, ALTERNATE_IS_COMPLETE)
44             : super.handleServiceCall(input);
45     }
46
47 }