2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.services.singlelayer;
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.List;
17 import java.util.stream.Collectors;
18 import javax.annotation.Nonnull;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
21 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
22 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
23 import org.opendaylight.openflowplugin.impl.services.AbstractExperimenterMultipartService;
24 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.mp.message.service.rev151020.SendExperimenterMpRequestInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.mp.message.service.rev151020.SendExperimenterMpRequestOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.mp.message.service.rev151020.SendExperimenterMpRequestOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.mp.message.service.rev151020.send.experimenter.mp.request.output.ExperimenterCoreMessageItemBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequestBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.multipart.reply.multipart.reply.body.MultipartReplyExperimenter;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.multipart.request.multipart.request.body.MultipartRequestExperimenterBuilder;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37 import org.slf4j.Logger;
39 public class SingleLayerExperimenterMultipartService extends AbstractExperimenterMultipartService<MultipartReply> {
41 private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SingleLayerExperimenterMultipartService.class);
43 public SingleLayerExperimenterMultipartService(RequestContextStack requestContextStack, DeviceContext deviceContext,
44 ExtensionConverterProvider extensionConverterProvider) {
45 super(requestContextStack, deviceContext, extensionConverterProvider);
49 protected OfHeader buildRequest(final Xid xid, final SendExperimenterMpRequestInput input) throws ServiceException {
50 return new MultipartRequestBuilder()
51 .setXid(xid.getValue())
52 .setVersion(getVersion())
53 .setRequestMore(false)
54 .setMultipartRequestBody(new MultipartRequestExperimenterBuilder(input).build())
59 public ListenableFuture<RpcResult<SendExperimenterMpRequestOutput>> handleAndReply(
60 SendExperimenterMpRequestInput input) {
61 final SettableFuture<RpcResult<SendExperimenterMpRequestOutput>> future = SettableFuture.create();
63 Futures.addCallback(handleServiceCall(input), new FutureCallback<RpcResult<List<MultipartReply>>>() {
65 public void onSuccess(@Nonnull final RpcResult<List<MultipartReply>> result) {
66 if (result.isSuccessful()) {
67 future.set(RpcResultBuilder
68 .success(new SendExperimenterMpRequestOutputBuilder()
69 .setExperimenterCoreMessageItem(result
72 .map(MultipartReply::getMultipartReplyBody)
73 .filter(MultipartReplyExperimenter.class::isInstance)
74 .map(experimenter -> new ExperimenterCoreMessageItemBuilder()
75 .setExperimenterMessageOfChoice(MultipartReplyExperimenter.class
77 .getExperimenterMessageOfChoice())
79 .collect(Collectors.toList()))
83 LOG.warn("OnSuccess, rpc result unsuccessful,"
84 + " multipart response for rpc sendExperimenterMpRequest was unsuccessful.");
85 future.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed()
86 .withRpcErrors(result.getErrors()).build());
91 public void onFailure(final Throwable throwable) {
92 LOG.warn("Failure multipart response for Experimenter-Mp request. Exception: {}", throwable);
93 future.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed()
94 .withError(ErrorType.RPC, "Future error", throwable).build());
96 }, MoreExecutors.directExecutor());