8d75d6b5d0518e027cee45f8cf9e6053e00378db
[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.util.Collections;
17 import java.util.List;
18 import java.util.Map;
19 import java.util.Optional;
20 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
21 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
22 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
23 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
24 import org.opendaylight.openflowplugin.impl.services.AbstractTableMultipartService;
25 import org.opendaylight.openflowplugin.impl.services.util.RequestInputUtils;
26 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
27 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
41 import org.opendaylight.yangtools.yang.binding.util.BindingMap;
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.opendaylight.yangtools.yang.common.Uint32;
46 import org.opendaylight.yangtools.yang.common.Uint64;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 public class MultiLayerTableMultipartService extends AbstractTableMultipartService<MultipartReply> {
51
52     private static final Logger LOG = LoggerFactory.getLogger(MultiLayerTableMultipartService.class);
53     private final VersionConvertorData data;
54     private final ConvertorExecutor convertorExecutor;
55
56     public MultiLayerTableMultipartService(final RequestContextStack requestContextStack,
57                                            final DeviceContext deviceContext,
58                                            final ConvertorExecutor convertorExecutor,
59                                            final MultipartWriterProvider multipartWriterProvider) {
60         super(requestContextStack, deviceContext, multipartWriterProvider);
61         this.convertorExecutor = convertorExecutor;
62         data = new VersionConvertorData(getVersion());
63     }
64
65     @Override
66     protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
67         final Optional<List<org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart
68                 .request.multipart.request.body.multipart.request.table.features._case.multipart.request
69                 .table.features.TableFeatures>> tableFeatures =
70                 convertorExecutor.convert(input.getUpdatedTable(), data);
71
72         return RequestInputUtils.createMultipartHeader(MultipartType.OFPMPTABLEFEATURES, xid.getValue(), getVersion())
73                 .setMultipartRequestBody(new MultipartRequestTableFeaturesCaseBuilder()
74                         .setMultipartRequestTableFeatures(new MultipartRequestTableFeaturesBuilder()
75                                 .setTableFeatures(tableFeatures
76                                         .orElseGet(Collections::emptyList))
77                                 .build())
78                         .build())
79                 .build();
80     }
81
82     @Override
83     public ListenableFuture<RpcResult<UpdateTableOutput>> handleAndReply(final 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 Uint32 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(Uint64.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", throwable);
124                 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
125                     .withError(ErrorType.RPC, "Future error", throwable).build());
126             }
127         }
128
129         Futures.addCallback(multipartFuture, new CallBackImpl(), MoreExecutors.directExecutor());
130
131         return finalFuture;
132     }
133
134     protected Map<org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey,
135                   org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures>
136                         convertToSalTableFeatures(final List<MultipartReply> multipartReplies) {
137         final var salTableFeaturesAll = BindingMap.<
138             org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeaturesKey,
139             org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures>
140                 orderedBuilder();
141         for (final MultipartReply multipartReply : multipartReplies) {
142             if (multipartReply.getType().equals(MultipartType.OFPMPTABLEFEATURES)) {
143                 final MultipartReplyBody multipartReplyBody = multipartReply.getMultipartReplyBody();
144                 if (multipartReplyBody instanceof MultipartReplyTableFeaturesCase) {
145                     final MultipartReplyTableFeaturesCase tableFeaturesCase =
146                             (MultipartReplyTableFeaturesCase) multipartReplyBody;
147                     final MultipartReplyTableFeatures salTableFeatures = tableFeaturesCase
148                             .getMultipartReplyTableFeatures();
149
150                     final Optional<List<TableFeatures>> salTableFeaturesPartial =
151                             convertorExecutor.convert(salTableFeatures, data);
152
153                     salTableFeaturesPartial.ifPresent(salTableFeaturesAll::addAll);
154
155                     LOG.debug("TableFeature {} for xid {}.", salTableFeatures, multipartReply.getXid());
156                 }
157             }
158         }
159
160         return salTableFeaturesAll.build();
161     }
162 }