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