Update multipart request services for single layer
[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.SettableFuture;
14 import java.util.List;
15 import java.util.concurrent.Future;
16 import java.util.stream.Collectors;
17 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
18 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
19 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
20 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterProvider;
21 import org.opendaylight.openflowplugin.impl.services.AbstractExperimenterMultipartService;
22 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.mp.message.service.rev151020.SendExperimenterMpRequestInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.mp.message.service.rev151020.SendExperimenterMpRequestOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.mp.message.service.rev151020.SendExperimenterMpRequestOutputBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.mp.message.service.rev151020.send.experimenter.mp.request.output.ExperimenterCoreMessageItemBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequestBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.multipart.reply.multipart.reply.body.MultipartReplyExperimenter;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.experimenter.types.rev151020.multipart.request.multipart.request.body.MultipartRequestExperimenterBuilder;
32 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.slf4j.Logger;
36
37 public class SingleLayerExperimenterMultipartService extends AbstractExperimenterMultipartService<MultipartReply> {
38
39     private static final Logger LOG = org.slf4j.LoggerFactory.getLogger(SingleLayerExperimenterMultipartService.class);
40
41     public SingleLayerExperimenterMultipartService(RequestContextStack requestContextStack, DeviceContext deviceContext,
42                                                    ExtensionConverterProvider extensionConverterProvider) {
43         super(requestContextStack, deviceContext, extensionConverterProvider);
44     }
45
46     @Override
47     protected OfHeader buildRequest(final Xid xid, final SendExperimenterMpRequestInput input) throws ServiceException {
48         return new MultipartRequestBuilder()
49             .setXid(xid.getValue())
50             .setVersion(getVersion())
51             .setRequestMore(false)
52             .setMultipartRequestBody(new MultipartRequestExperimenterBuilder(input).build())
53             .build();
54     }
55
56     @Override
57     public Future<RpcResult<SendExperimenterMpRequestOutput>> handleAndReply(SendExperimenterMpRequestInput input) {
58         final SettableFuture<RpcResult<SendExperimenterMpRequestOutput>> future = SettableFuture.create();
59
60         Futures.addCallback(handleServiceCall(input), new FutureCallback<RpcResult<List<MultipartReply>>>() {
61             @Override
62             public void onSuccess(final RpcResult<List<MultipartReply>> result) {
63                 if (result.isSuccessful()) {
64                     future.set(RpcResultBuilder
65                         .success(new SendExperimenterMpRequestOutputBuilder()
66                             .setExperimenterCoreMessageItem(result
67                                 .getResult()
68                                 .stream()
69                                 .map(MultipartReply::getMultipartReplyBody)
70                                 .filter(MultipartReplyExperimenter.class::isInstance)
71                                 .map(experimenter -> new ExperimenterCoreMessageItemBuilder()
72                                     .setExperimenterMessageOfChoice(MultipartReplyExperimenter.class
73                                         .cast(experimenter)
74                                         .getExperimenterMessageOfChoice())
75                                     .build())
76                                 .collect(Collectors.toList()))
77                             .build())
78                         .build());
79                 } else {
80                     LOG.warn("OnSuccess, rpc result unsuccessful, multipart response for rpc sendExperimenterMpRequest was unsuccessful.");
81                     future.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed().withRpcErrors(result.getErrors()).build());
82                 }
83             }
84
85             @Override
86             public void onFailure(final Throwable t) {
87                 LOG.warn("Failure multipart response for Experimenter-Mp request. Exception: {}", t);
88                 future.set(RpcResultBuilder.<SendExperimenterMpRequestOutput>failed().withError(ErrorType.RPC, "Future error", t).build());
89             }
90         });
91
92         return future;
93     }
94
95 }