Decompose RPC implementation classes
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / AddBundleMessagesImpl.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 package org.opendaylight.openflowplugin.impl.services.sal;
9
10 import static java.util.Objects.requireNonNull;
11
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;
32
33 public final class AddBundleMessagesImpl implements AddBundleMessages {
34     private final SendExperimenter sendExperimenter;
35
36     public AddBundleMessagesImpl(final SendExperimenter sendExperimenter) {
37         this.sendExperimenter = requireNonNull(sendExperimenter);
38     }
39
40     @Override
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()));
56         }
57
58         final var result = SettableFuture.<RpcResult<AddBundleMessagesOutput>>create();
59         Futures.addCallback(Futures.successfulAsList(partialResults),new FutureCallback<>() {
60             @Override
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) {
65                     if (res == null) {
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());
71                     }
72                 }
73                 if (errors.isEmpty()) {
74                     rpcResultBuilder = RpcResultBuilder.success();
75                 } else {
76                     rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
77                 }
78                 result.set(rpcResultBuilder.build());
79             }
80
81             @Override
82             public void onFailure(final Throwable throwable) {
83                 result.set(RpcResultBuilder.<AddBundleMessagesOutput>failed().build());
84             }
85         }, MoreExecutors.directExecutor());
86         return result;
87     }
88 }