2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
9 package org.opendaylight.openflowplugin.impl.services.singlelayer;
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.List;
17 import java.util.stream.Collectors;
18 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
19 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
20 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
21 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
22 import org.opendaylight.openflowplugin.impl.services.AbstractTableMultipartService;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequestBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.reply.multipart.reply.body.MultipartReplyTableFeatures;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.request.multipart.request.body.MultipartRequestTableFeaturesBuilder;
32 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
33 import org.opendaylight.yangtools.yang.common.RpcResult;
34 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
35 import org.opendaylight.yangtools.yang.common.Uint64;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
39 public class SingleLayerTableMultipartService extends AbstractTableMultipartService<MultipartReply> {
41 private static final Logger LOG = LoggerFactory.getLogger(SingleLayerTableMultipartService.class);
43 public SingleLayerTableMultipartService(final RequestContextStack requestContextStack,
44 final DeviceContext deviceContext,
45 final MultipartWriterProvider multipartWriterProvider) {
46 super(requestContextStack, deviceContext, multipartWriterProvider);
51 protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) {
52 return new MultipartRequestBuilder()
53 .setXid(xid.getValue())
54 .setVersion(getVersion())
55 .setRequestMore(false)
56 .setMultipartRequestBody(new MultipartRequestTableFeaturesBuilder(input.getUpdatedTable())
62 public ListenableFuture<RpcResult<UpdateTableOutput>> handleAndReply(final UpdateTableInput input) {
63 final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
65 Futures.addCallback(handleServiceCall(input), new FutureCallback<RpcResult<List<MultipartReply>>>() {
67 @SuppressWarnings("checkstyle:IllegalCatch")
68 public void onSuccess(final RpcResult<List<MultipartReply>> result) {
69 if (result.isSuccessful()) {
70 final List<MultipartReply> multipartReplies = result.getResult();
71 if (multipartReplies.isEmpty()) {
72 LOG.debug("Multipart reply to table features request shouldn't be empty list.");
73 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
74 .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
76 finalFuture.set(RpcResultBuilder
77 .success(new UpdateTableOutputBuilder()
79 new TransactionId(Uint64.valueOf(multipartReplies.get(0).getXid())))
84 storeStatistics(multipartReplies
86 .map(MultipartReply::getMultipartReplyBody)
87 .filter(MultipartReplyTableFeatures.class::isInstance)
88 .flatMap(multipartReplyBody -> ((MultipartReplyTableFeatures) multipartReplyBody)
89 .nonnullTableFeatures().values()
91 .collect(Collectors.toList()));
92 } catch (Exception e) {
93 LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
97 LOG.debug("OnSuccess, rpc result unsuccessful,"
98 + " multipart response for rpc update-table was unsuccessful.");
99 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
105 public void onFailure(final Throwable throwable) {
106 LOG.error("Failure multipart response for table features request", throwable);
107 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
108 .withError(ErrorType.RPC, "Future error", throwable).build());
110 }, MoreExecutors.directExecutor());