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
8 package org.opendaylight.openflowplugin.impl.services.sal;
10 import static java.util.Objects.requireNonNull;
12 import com.google.common.util.concurrent.FutureCallback;
13 import com.google.common.util.concurrent.Futures;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import com.google.common.util.concurrent.MoreExecutors;
16 import com.google.common.util.concurrent.SettableFuture;
17 import java.util.ArrayList;
18 import java.util.List;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SendExperimenter;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SendExperimenterInputBuilder;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SendExperimenterOutput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessages;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleAddMessageSalBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.bundle.add.message.sal.SalAddMessageDataBuilder;
27 import org.opendaylight.yangtools.yang.common.ErrorTag;
28 import org.opendaylight.yangtools.yang.common.ErrorType;
29 import org.opendaylight.yangtools.yang.common.RpcError;
30 import org.opendaylight.yangtools.yang.common.RpcResult;
31 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
33 public final class AddBundleMessagesImpl implements AddBundleMessages {
34 private final SendExperimenter sendExperimenter;
36 public AddBundleMessagesImpl(final SendExperimenter sendExperimenter) {
37 this.sendExperimenter = requireNonNull(sendExperimenter);
41 public ListenableFuture<RpcResult<AddBundleMessagesOutput>> invoke(final AddBundleMessagesInput input) {
42 final var partialResults = new ArrayList<ListenableFuture<RpcResult<SendExperimenterOutput>>>();
43 final var experimenterInputBuilder = new SendExperimenterInputBuilder()
44 .setNode(input.getNode());
45 final var bundleAddMessageBuilder = new BundleAddMessageSalBuilder();
46 final var dataBuilder = new SalAddMessageDataBuilder()
47 .setNode(input.getNode())
48 .setBundleId(input.getBundleId())
49 .setFlags(input.getFlags())
50 .setBundleProperty(input.getBundleProperty());
51 for (var message : input.nonnullMessages().getMessage()) {
52 dataBuilder.setBundleInnerMessage(message.getBundleInnerMessage());
53 experimenterInputBuilder.setExperimenterMessageOfChoice(
54 bundleAddMessageBuilder.setSalAddMessageData(dataBuilder.build()).build());
55 partialResults.add(sendExperimenter.invoke(experimenterInputBuilder.build()));
58 final var result = SettableFuture.<RpcResult<AddBundleMessagesOutput>>create();
59 Futures.addCallback(Futures.successfulAsList(partialResults),new FutureCallback<>() {
61 public void onSuccess(final List<RpcResult<SendExperimenterOutput>> results) {
62 final var errors = new ArrayList<RpcError>();
63 final RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder;
64 for (var res : results) {
66 // FIXME: this should never happen
67 errors.add(RpcResultBuilder.newError(ErrorType.APPLICATION,
68 new ErrorTag("BundleExtensionService"), "RpcResult is null."));
69 } else if (!res.isSuccessful()) {
70 errors.addAll(res.getErrors());
73 if (errors.isEmpty()) {
74 rpcResultBuilder = RpcResultBuilder.success();
76 rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
78 result.set(rpcResultBuilder.build());
82 public void onFailure(final Throwable throwable) {
83 result.set(RpcResultBuilder.<AddBundleMessagesOutput>failed().build());
85 }, MoreExecutors.directExecutor());