Fix checkstyle warnings for services package
[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.SettableFuture;
15 import java.math.BigInteger;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Optional;
20 import java.util.concurrent.Future;
21 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
22 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
23 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
24 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
25 import org.opendaylight.openflowplugin.impl.services.AbstractTableMultipartService;
26 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
27 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
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) throws ServiceException {
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
83     public Future<RpcResult<UpdateTableOutput>> handleAndReply(UpdateTableInput input) {
84         final ListenableFuture<RpcResult<List<MultipartReply>>> multipartFuture = handleServiceCall(input);
85         final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
86
87         class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
88             @Override
89             @SuppressWarnings("checkstyle:IllegalCatch")
90             public void onSuccess(final RpcResult<List<MultipartReply>> result) {
91
92                 if (result.isSuccessful()) {
93                     final List<MultipartReply> multipartReplies = result.getResult();
94                     if (multipartReplies.isEmpty()) {
95                         LOG.debug("Multipart reply to table features request shouldn't be empty list.");
96                         finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
97                             .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
98                     } else {
99                         final Long xid = multipartReplies.get(0).getXid();
100                         LOG.debug(
101                             "OnSuccess, rpc result successful,"
102                                     + " multipart response for rpc update-table with xid {} obtained.",
103                             xid);
104                         final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
105                         updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
106                         finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
107                         try {
108                             storeStatistics(convertToSalTableFeatures(multipartReplies));
109                         } catch (Exception e) {
110                             LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
111                         }
112                     }
113                 } else {
114                     LOG.debug("OnSuccess, rpc result unsuccessful,"
115                             + " multipart response for rpc update-table was unsuccessful.");
116                     finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
117                         .build());
118                 }
119             }
120
121             @Override
122             public void onFailure(final Throwable throwable) {
123                 LOG.error("Failure multipart response for table features request. Exception: {}", throwable);
124                 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
125                     .withError(ErrorType.RPC, "Future error", throwable).build());
126             }
127         }
128
129         Futures.addCallback(multipartFuture, new CallBackImpl());
130
131         return finalFuture;
132     }
133
134     protected List<org.opendaylight.yang.gen.v1.urn
135             .opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
136             final List<MultipartReply> multipartReplies) {
137         final List<org.opendaylight.yang.gen.v1.urn
138                 .opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesAll =
139                 new ArrayList<>();
140         for (final MultipartReply multipartReply : multipartReplies) {
141             if (multipartReply.getType().equals(MultipartType.OFPMPTABLEFEATURES)) {
142                 final MultipartReplyBody multipartReplyBody = multipartReply.getMultipartReplyBody();
143                 if (multipartReplyBody instanceof MultipartReplyTableFeaturesCase) {
144                     final MultipartReplyTableFeaturesCase tableFeaturesCase =
145                             ((MultipartReplyTableFeaturesCase) multipartReplyBody);
146                     final MultipartReplyTableFeatures salTableFeatures = tableFeaturesCase
147                             .getMultipartReplyTableFeatures();
148
149                     final Optional<List<TableFeatures>> salTableFeaturesPartial =
150                             convertorExecutor.convert(salTableFeatures, data);
151
152                     salTableFeaturesPartial.ifPresent(salTableFeaturesAll::addAll);
153
154                     LOG.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
155                 }
156             }
157         }
158
159         return salTableFeaturesAll;
160     }
161 }