Fix spotbugs logging complaints
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / multilayer / MultiLayerTableMultipartService.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.multilayer;
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.ArrayList;
18 import java.util.Collections;
19 import java.util.List;
20 import java.util.Optional;
21 import javax.annotation.Nonnull;
22 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
23 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
24 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
25 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
26 import org.opendaylight.openflowplugin.impl.services.AbstractTableMultipartService;
27 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
28 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
43 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
44 import org.opendaylight.yangtools.yang.common.RpcResult;
45 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
48
49 public class MultiLayerTableMultipartService extends AbstractTableMultipartService<MultipartReply> {
50
51     private static final Logger LOG = LoggerFactory.getLogger(MultiLayerTableMultipartService.class);
52     private final VersionConvertorData data;
53     private final ConvertorExecutor convertorExecutor;
54
55     public MultiLayerTableMultipartService(RequestContextStack requestContextStack,
56                                            DeviceContext deviceContext,
57                                            ConvertorExecutor convertorExecutor,
58                                            MultipartWriterProvider multipartWriterProvider) {
59         super(requestContextStack, deviceContext, multipartWriterProvider);
60         this.convertorExecutor = convertorExecutor;
61         data = new VersionConvertorData(getVersion());
62     }
63
64     @Override
65     protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
66         final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart
67                 .request.multipart.request.body.multipart.request.table.features._case.multipart.request
68                 .table.features.TableFeatures>> tableFeatures =
69                 convertorExecutor.convert(input.getUpdatedTable(), data);
70
71         return RequestInputUtils.createMultipartHeader(MultipartType.OFPMPTABLEFEATURES, xid.getValue(), getVersion())
72                 .setMultipartRequestBody(new MultipartRequestTableFeaturesCaseBuilder()
73                         .setMultipartRequestTableFeatures(new MultipartRequestTableFeaturesBuilder()
74                                 .setTableFeatures(tableFeatures
75                                         .orElseGet(Collections::emptyList))
76                                 .build())
77                         .build())
78                 .build();
79     }
80
81     @Override
82     public ListenableFuture<RpcResult<UpdateTableOutput>> handleAndReply(UpdateTableInput input) {
83         final ListenableFuture<RpcResult<List<MultipartReply>>> multipartFuture = handleServiceCall(input);
84         final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
85
86         class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
87             @Override
88             @SuppressWarnings("checkstyle:IllegalCatch")
89             public void onSuccess(@Nonnull final RpcResult<List<MultipartReply>> result) {
90
91                 if (result.isSuccessful()) {
92                     final List<MultipartReply> multipartReplies = result.getResult();
93                     if (multipartReplies.isEmpty()) {
94                         LOG.debug("Multipart reply to table features request shouldn't be empty list.");
95                         finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
96                             .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
97                     } else {
98                         final Long xid = multipartReplies.get(0).getXid();
99                         LOG.debug(
100                             "OnSuccess, rpc result successful,"
101                                     + " multipart response for rpc update-table with xid {} obtained.",
102                             xid);
103                         final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
104                         updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
105                         finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
106                         try {
107                             storeStatistics(convertToSalTableFeatures(multipartReplies));
108                         } catch (Exception e) {
109                             LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
110                         }
111                     }
112                 } else {
113                     LOG.debug("OnSuccess, rpc result unsuccessful,"
114                             + " multipart response for rpc update-table was unsuccessful.");
115                     finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
116                         .build());
117                 }
118             }
119
120             @Override
121             public void onFailure(final Throwable throwable) {
122                 LOG.error("Failure multipart response for table features request", throwable);
123                 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
124                     .withError(ErrorType.RPC, "Future error", throwable).build());
125             }
126         }
127
128         Futures.addCallback(multipartFuture, new CallBackImpl(), MoreExecutors.directExecutor());
129
130         return finalFuture;
131     }
132
133     protected List<org.opendaylight.yang.gen.v1.urn
134             .opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
135             final List<MultipartReply> multipartReplies) {
136         final List<org.opendaylight.yang.gen.v1.urn
137                 .opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesAll =
138                 new ArrayList<>();
139         for (final MultipartReply multipartReply : multipartReplies) {
140             if (multipartReply.getType().equals(MultipartType.OFPMPTABLEFEATURES)) {
141                 final MultipartReplyBody multipartReplyBody = multipartReply.getMultipartReplyBody();
142                 if (multipartReplyBody instanceof MultipartReplyTableFeaturesCase) {
143                     final MultipartReplyTableFeaturesCase tableFeaturesCase =
144                             (MultipartReplyTableFeaturesCase) multipartReplyBody;
145                     final MultipartReplyTableFeatures salTableFeatures = tableFeaturesCase
146                             .getMultipartReplyTableFeatures();
147
148                     final Optional<List<TableFeatures>> salTableFeaturesPartial =
149                             convertorExecutor.convert(salTableFeatures, data);
150
151                     salTableFeaturesPartial.ifPresent(salTableFeaturesAll::addAll);
152
153                     LOG.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
154                 }
155             }
156         }
157
158         return salTableFeaturesAll;
159     }
160 }