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