Bump MRI upstreams
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / singlelayer / SingleLayerTableMultipartService.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.ListenableFuture;
14 import com.google.common.util.concurrent.MoreExecutors;
15 import com.google.common.util.concurrent.SettableFuture;
16 import java.util.List;
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.impl.datastore.MultipartWriterProvider;
21 import org.opendaylight.openflowplugin.impl.services.AbstractTableMultipartService;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequestBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.reply.multipart.reply.body.MultipartReplyTableFeatures;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.request.multipart.request.body.MultipartRequestTableFeaturesBuilder;
31 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
32 import org.opendaylight.yangtools.yang.common.ErrorType;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.opendaylight.yangtools.yang.common.Uint64;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class SingleLayerTableMultipartService extends AbstractTableMultipartService<MultipartReply> {
40
41     private static final Logger LOG = LoggerFactory.getLogger(SingleLayerTableMultipartService.class);
42
43     public SingleLayerTableMultipartService(final RequestContextStack requestContextStack,
44                                             final DeviceContext deviceContext,
45                                             final MultipartWriterProvider multipartWriterProvider) {
46         super(requestContextStack, deviceContext, multipartWriterProvider);
47     }
48
49
50     @Override
51     protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
52         return new MultipartRequestBuilder()
53                 .setXid(xid.getValue())
54                 .setVersion(getVersion())
55                 .setRequestMore(false)
56                 .setMultipartRequestBody(new MultipartRequestTableFeaturesBuilder(input.getUpdatedTable())
57                         .build())
58                 .build();
59     }
60
61     @Override
62     public ListenableFuture<RpcResult<UpdateTableOutput>> handleAndReply(final UpdateTableInput input) {
63         final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
64
65         Futures.addCallback(handleServiceCall(input), new FutureCallback<RpcResult<List<MultipartReply>>>() {
66             @Override
67             @SuppressWarnings("checkstyle:IllegalCatch")
68             public void onSuccess(final RpcResult<List<MultipartReply>> result) {
69                 if (result.isSuccessful()) {
70                     final List<MultipartReply> multipartReplies = result.getResult();
71                     if (multipartReplies.isEmpty()) {
72                         LOG.debug("Multipart reply to table features request shouldn't be empty list.");
73                         finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
74                             .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
75                     } else {
76                         finalFuture.set(RpcResultBuilder
77                             .success(new UpdateTableOutputBuilder()
78                                 .setTransactionId(
79                                         new TransactionId(Uint64.valueOf(multipartReplies.get(0).getXid())))
80                                 .build())
81                             .build());
82
83                         try {
84                             storeStatistics(multipartReplies
85                                 .stream()
86                                 .map(MultipartReply::getMultipartReplyBody)
87                                 .filter(MultipartReplyTableFeatures.class::isInstance)
88                                 .flatMap(multipartReplyBody -> ((MultipartReplyTableFeatures) multipartReplyBody)
89                                     .nonnullTableFeatures().values()
90                                     .stream())
91                                 .collect(BindingMap.toOrderedMap()));
92                         } catch (Exception e) {
93                             LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
94                         }
95                     }
96                 } else {
97                     LOG.debug("OnSuccess, rpc result unsuccessful,"
98                             + " multipart response for rpc update-table was unsuccessful.");
99                     finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
100                         .build());
101                 }
102             }
103
104             @Override
105             public void onFailure(final Throwable throwable) {
106                 LOG.error("Failure multipart response for table features request", throwable);
107                 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
108                     .withError(ErrorType.RPC, "Future error", throwable).build());
109             }
110         }, MoreExecutors.directExecutor());
111
112         return finalFuture;
113     }
114 }