Merge "Spec: OFPGC_ADD_OR_MOD support in openflowplugin"
[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 java.util.concurrent.Future;
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.impl.services.util.ServiceException;
29 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.ConvertorExecutor;
30 import org.opendaylight.openflowplugin.openflow.md.core.sal.convertor.data.VersionConvertorData;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.common.types.rev130731.MultipartType;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.MultipartReply;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.MultipartReplyBody;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.MultipartReplyTableFeaturesCase;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.reply.multipart.reply.body.multipart.reply.table.features._case.MultipartReplyTableFeatures;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.MultipartRequestTableFeaturesCaseBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.multipart.request.multipart.request.body.multipart.request.table.features._case.MultipartRequestTableFeaturesBuilder;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.table.features.TableFeatures;
44 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
45 import org.opendaylight.yangtools.yang.common.RpcResult;
46 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
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(RequestContextStack requestContextStack,
57                                            DeviceContext deviceContext,
58                                            ConvertorExecutor convertorExecutor,
59                                            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) throws ServiceException {
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
84     public Future<RpcResult<UpdateTableOutput>> handleAndReply(UpdateTableInput input) {
85         final ListenableFuture<RpcResult<List<MultipartReply>>> multipartFuture = handleServiceCall(input);
86         final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
87
88         class CallBackImpl implements FutureCallback<RpcResult<List<MultipartReply>>> {
89             @Override
90             @SuppressWarnings("checkstyle:IllegalCatch")
91             public void onSuccess(final RpcResult<List<MultipartReply>> result) {
92
93                 if (result.isSuccessful()) {
94                     final List<MultipartReply> multipartReplies = result.getResult();
95                     if (multipartReplies.isEmpty()) {
96                         LOG.debug("Multipart reply to table features request shouldn't be empty list.");
97                         finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
98                             .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
99                     } else {
100                         final Long xid = multipartReplies.get(0).getXid();
101                         LOG.debug(
102                             "OnSuccess, rpc result successful,"
103                                     + " multipart response for rpc update-table with xid {} obtained.",
104                             xid);
105                         final UpdateTableOutputBuilder updateTableOutputBuilder = new UpdateTableOutputBuilder();
106                         updateTableOutputBuilder.setTransactionId(new TransactionId(BigInteger.valueOf(xid)));
107                         finalFuture.set(RpcResultBuilder.success(updateTableOutputBuilder.build()).build());
108                         try {
109                             storeStatistics(convertToSalTableFeatures(multipartReplies));
110                         } catch (Exception e) {
111                             LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
112                         }
113                     }
114                 } else {
115                     LOG.debug("OnSuccess, rpc result unsuccessful,"
116                             + " multipart response for rpc update-table was unsuccessful.");
117                     finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
118                         .build());
119                 }
120             }
121
122             @Override
123             public void onFailure(final Throwable throwable) {
124                 LOG.error("Failure multipart response for table features request. Exception: {}", throwable);
125                 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
126                     .withError(ErrorType.RPC, "Future error", throwable).build());
127             }
128         }
129
130         Futures.addCallback(multipartFuture, new CallBackImpl(), MoreExecutors.directExecutor());
131
132         return finalFuture;
133     }
134
135     protected List<org.opendaylight.yang.gen.v1.urn
136             .opendaylight.table.types.rev131026.table.features.TableFeatures> convertToSalTableFeatures(
137             final List<MultipartReply> multipartReplies) {
138         final List<org.opendaylight.yang.gen.v1.urn
139                 .opendaylight.table.types.rev131026.table.features.TableFeatures> salTableFeaturesAll =
140                 new ArrayList<>();
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;
161     }
162 }