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.MoreExecutors;
14 import com.google.common.util.concurrent.SettableFuture;
15 import java.math.BigInteger;
16 import java.util.List;
17 import java.util.concurrent.Future;
18 import java.util.stream.Collectors;
19 import org.opendaylight.openflowplugin.api.openflow.device.DeviceContext;
20 import org.opendaylight.openflowplugin.api.openflow.device.RequestContextStack;
21 import org.opendaylight.openflowplugin.api.openflow.device.Xid;
22 import org.opendaylight.openflowplugin.impl.datastore.MultipartWriterProvider;
23 import org.opendaylight.openflowplugin.impl.services.AbstractTableMultipartService;
24 import org.opendaylight.openflowplugin.impl.services.util.ServiceException;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.transaction.rev150304.TransactionId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartReply;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.multipart.types.rev170112.MultipartRequestBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableInput;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutput;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.service.rev131026.UpdateTableOutputBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.reply.multipart.reply.body.MultipartReplyTableFeatures;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.table.types.rev131026.multipart.request.multipart.request.body.MultipartRequestTableFeaturesBuilder;
34 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
35 import org.opendaylight.yangtools.yang.common.RpcResult;
36 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class SingleLayerTableMultipartService extends AbstractTableMultipartService<MultipartReply> {
42 private static final Logger LOG = LoggerFactory.getLogger(SingleLayerTableMultipartService.class);
44 public SingleLayerTableMultipartService(RequestContextStack requestContextStack,
45 DeviceContext deviceContext,
46 MultipartWriterProvider multipartWriterProvider) {
47 super(requestContextStack, deviceContext, multipartWriterProvider);
52 protected OfHeader buildRequest(final Xid xid, final UpdateTableInput input) throws ServiceException {
53 return new MultipartRequestBuilder()
54 .setXid(xid.getValue())
55 .setVersion(getVersion())
56 .setRequestMore(false)
57 .setMultipartRequestBody(new MultipartRequestTableFeaturesBuilder(input.getUpdatedTable())
63 public Future<RpcResult<UpdateTableOutput>> handleAndReply(UpdateTableInput input) {
64 final SettableFuture<RpcResult<UpdateTableOutput>> finalFuture = SettableFuture.create();
66 Futures.addCallback(handleServiceCall(input), new FutureCallback<RpcResult<List<MultipartReply>>>() {
68 @SuppressWarnings("checkstyle:IllegalCatch")
69 public void onSuccess(final RpcResult<List<MultipartReply>> result) {
70 if (result.isSuccessful()) {
71 final List<MultipartReply> multipartReplies = result.getResult();
72 if (multipartReplies.isEmpty()) {
73 LOG.debug("Multipart reply to table features request shouldn't be empty list.");
74 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
75 .withError(ErrorType.RPC, "Multipart reply list is empty.").build());
77 finalFuture.set(RpcResultBuilder
78 .success(new UpdateTableOutputBuilder()
80 new TransactionId(BigInteger.valueOf(multipartReplies.get(0).getXid())))
85 storeStatistics(multipartReplies
87 .map(MultipartReply::getMultipartReplyBody)
88 .filter(MultipartReplyTableFeatures.class::isInstance)
89 .flatMap(multipartReplyBody -> MultipartReplyTableFeatures.class
90 .cast(multipartReplyBody)
93 .collect(Collectors.toList()));
94 } catch (Exception e) {
95 LOG.warn("Not able to write to operational datastore: {}", e.getMessage());
99 LOG.debug("OnSuccess, rpc result unsuccessful,"
100 + " multipart response for rpc update-table was unsuccessful.");
101 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed().withRpcErrors(result.getErrors())
107 public void onFailure(Throwable throwable) {
108 LOG.error("Failure multipart response for table features request. Exception: {}", throwable);
109 finalFuture.set(RpcResultBuilder.<UpdateTableOutput>failed()
110 .withError(ErrorType.RPC, "Future error", throwable).build());
112 }, MoreExecutors.directExecutor());