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