Merge "OPNFLWPLUG-983 Group and flow removal stats are not reported in order"
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / singlelayer / SingleLayerExperimenterMultipartService.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies s.r.o. 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
9 package org.opendaylight.openflowplugin.impl.services.singlelayer;
10
11 import com.google.common.util.concurrent.FutureCallback;
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.util.List;
16 import java.util.concurrent.Future;
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;
38
39 public class SingleLayerExperimenterMultipartService extends AbstractExperimenterMultipartService<MultipartReply> {
40
41     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SingleLayerExperimenterMultipartService.class);
42
43     public SingleLayerExperimenterMultipartService(RequestContextStack requestContextStack, DeviceContext deviceContext,
44                                                    ExtensionConverterProvider extensionConverterProvider) {
45         super(requestContextStack, deviceContext, extensionConverterProvider);
46     }
47
48     @Override
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())
55             .build();
56     }
57
58     @Override
59     public Future<RpcResult<SendExperimenterMpRequestOutput>> handleAndReply(SendExperimenterMpRequestInput input) {
60         final SettableFuture<RpcResult<SendExperimenterMpRequestOutput>> future = SettableFuture.create();
61
62         Futures.addCallback(handleServiceCall(input), new FutureCallback<RpcResult<List<MultipartReply>>>() {
63             @Override
64             public void onSuccess(@Nonnull final RpcResult<List<MultipartReply>> result) {
65                 if (result.isSuccessful()) {
66                     future.set(RpcResultBuilder
67                         .success(new SendExperimenterMpRequestOutputBuilder()
68                             .setExperimenterCoreMessageItem(result
69                                 .getResult()
70                                 .stream()
71                                 .map(MultipartReply::getMultipartReplyBody)
72                                 .filter(MultipartReplyExperimenter.class::isInstance)
73                                 .map(experimenter -> new ExperimenterCoreMessageItemBuilder()
74                                     .setExperimenterMessageOfChoice(MultipartReplyExperimenter.class
75                                         .cast(experimenter)
76                                         .getExperimenterMessageOfChoice())
77                                     .build())
78                                 .collect(Collectors.toList()))
79                             .build())
80                         .build());
81                 } else {
82                     LOG.warn("OnSuccess, rpc result unsuccessful,"
83                             + " multipart response for rpc sendExperimenterMpRequest was unsuccessful.");
84                     future.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed()
85                             .withRpcErrors(result.getErrors()).build());
86                 }
87             }
88
89             @Override
90             public void onFailure(final Throwable throwable) {
91                 LOG.warn("Failure multipart response for Experimenter-Mp request. Exception: {}", throwable);
92                 future.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed()
93                         .withError(ErrorType.RPC, "Future error", throwable).build());
94             }
95         }, MoreExecutors.directExecutor());
96
97         return future;
98     }
99 }