Add single layer deserialization support
[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.SettableFuture;
14 import java.math.BigInteger;
15 import java.util.List;
16 import java.util.concurrent.Future;
17 import java.util.stream.Collectors;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
20 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
21 import org.opendaylight.openflowplugin.impl.services.AbstractTableMultipartService;
22 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.multipart.reply.multipart.reply.body.MultipartReplyTableFeatures;
29 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35 public class SingleLayerTableMultipartService extends AbstractTableMultipartService<MultipartReply> {
36
37     private static final Logger LOG = LoggerFactory.getLogger(SingleLayerTableMultipartService.class);
38
39     public SingleLayerTableMultipartService(RequestContextStack requestContextStack,
40                                             DeviceContext deviceContext,
41                                             ConvertorExecutor convertorExecutor,
42                                             MultipartWriterProvider multipartWriterProvider) {
43         super(requestContextStack, deviceContext, convertorExecutor, multipartWriterProvider);
44     }
45
46     @Override
47     public Future<RpcResult<UpdateTableOutput>> handleAndReply(UpdateTableInput input) {
48         final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
49
50         Futures.addCallback(handleServiceCall(input), new FutureCallback<RpcResult<List<MultipartReply>>>() {
51             @Override
52             public void onSuccess(final RpcResult<List<MultipartReply>> result) {
53                 if (result.isSuccessful()) {
54                     final List<MultipartReply> multipartReplies = result.getResult();
55                     if (multipartReplies.isEmpty()) {
56                         LOG.debug("Multipart reply to table features request shouldn't be empty list.");
57                         finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
58                             .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
59                     } else {
60                         finalFuture.set(RpcResultBuilder
61                             .success(new UpdateTableOutputBuilder()
62                                 .setTransactionId(new TransactionId(BigInteger.valueOf(multipartReplies.get(0).getXid())))
63                                 .build())
64                             .build());
65
66                         try {
67                             storeStatistics(multipartReplies
68                                 .stream()
69                                 .map(MultipartReply::getMultipartReplyBody)
70                                 .filter(MultipartReplyTableFeatures.class::isInstance)
71                                 .flatMap(multipartReplyBody -> MultipartReplyTableFeatures.class
72                                     .cast(multipartReplyBody)
73                                     .getTableFeatures()
74                                     .stream())
75                                 .collect(Collectors.toList()));
76                         } catch (Exception e) {
77                             LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
78                         }
79                     }
80                 } else {
81                     LOG.debug("OnSuccess, rpc result unsuccessful, multipart response for rpc update-table was unsuccessful.");
82                     finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
83                         .build());
84                 }
85             }
86
87             @Override
88             public void onFailure(Throwable t) {
89                 LOG.error("Failure multipart response for table features request. Exception: {}", t);
90                 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
91                     .withError(ErrorType.RPC, "Future error", t).build());
92             }
93         });
94
95         return finalFuture;
96     }
97
98 }