Reduce use of JdkFutureAdapters
[openflowplugin.git] / openflowplugin-impl / src / main / java / org / opendaylight / openflowplugin / impl / services / sal / SalBundleServiceImpl.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 com.google.common.base.Preconditions;
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.ArrayList;
17 import java.util.List;
18 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SalExperimenterMessageService;
19 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SendExperimenterInputBuilder;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.experimenter.message.service.rev151020.SendExperimenterOutput;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesInput;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesOutput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleInput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleOutput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.Message;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleAddMessageSalBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleControlSalBuilder;
29 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;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.bundle.control.sal.SalControlDataBuilder;
31 import org.opendaylight.yangtools.yang.common.RpcError;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
34
35 /**
36  * Simple bundle extension service.
37  */
38 public class SalBundleServiceImpl implements SalBundleService {
39
40     private final SalExperimenterMessageService experimenterMessageService;
41
42     public SalBundleServiceImpl(final SalExperimenterMessageService experimenterMessageService) {
43         this.experimenterMessageService = Preconditions
44                 .checkNotNull(experimenterMessageService, "SalExperimenterMessageService can not be null!");
45     }
46
47     @Override
48     public ListenableFuture<RpcResult<ControlBundleOutput>> controlBundle(ControlBundleInput input) {
49         final SendExperimenterInputBuilder experimenterInputBuilder = new SendExperimenterInputBuilder();
50         experimenterInputBuilder.setNode(input.getNode());
51         experimenterInputBuilder.setExperimenterMessageOfChoice(
52                 new BundleControlSalBuilder().setSalControlData(new SalControlDataBuilder(input).build()).build());
53         return Futures.transform(experimenterMessageService.sendExperimenter(
54                 experimenterInputBuilder.build()), sendExperimenterOutputRpcResult -> {
55                 if (sendExperimenterOutputRpcResult.isSuccessful()) {
56                     return RpcResultBuilder.<ControlBundleOutput>success().build();
57                 } else {
58                     return RpcResultBuilder.<ControlBundleOutput>failed().build();
59                 }
60             }, MoreExecutors.directExecutor());
61     }
62
63     @Override
64     public ListenableFuture<RpcResult<AddBundleMessagesOutput>> addBundleMessages(AddBundleMessagesInput input) {
65         final List<ListenableFuture<RpcResult<SendExperimenterOutput>>> partialResults = new ArrayList<>();
66         final SendExperimenterInputBuilder experimenterInputBuilder = new SendExperimenterInputBuilder();
67         final BundleAddMessageSalBuilder bundleAddMessageBuilder = new BundleAddMessageSalBuilder();
68         final SalAddMessageDataBuilder dataBuilder = new SalAddMessageDataBuilder();
69         experimenterInputBuilder.setNode(input.getNode());
70         dataBuilder.setNode(input.getNode());
71         dataBuilder.setBundleId(input.getBundleId());
72         dataBuilder.setFlags(input.getFlags());
73         dataBuilder.setBundleProperty(input.getBundleProperty());
74         for (Message message : input.getMessages().getMessage()) {
75             dataBuilder.setBundleInnerMessage(message.getBundleInnerMessage());
76             experimenterInputBuilder.setExperimenterMessageOfChoice(
77                     bundleAddMessageBuilder.setSalAddMessageData(dataBuilder.build()).build());
78             partialResults.add(experimenterMessageService.sendExperimenter(experimenterInputBuilder.build()));
79         }
80         return processResults(partialResults);
81     }
82
83     private static ListenableFuture<RpcResult<AddBundleMessagesOutput>> processResults(
84             final List<ListenableFuture<RpcResult<SendExperimenterOutput>>> partialResults) {
85         final SettableFuture<RpcResult<AddBundleMessagesOutput>> result = SettableFuture.create();
86         Futures.addCallback(Futures.successfulAsList(partialResults),new FutureCallback<
87                 List<RpcResult<SendExperimenterOutput>>>() {
88             @Override
89             public void onSuccess(List<RpcResult<SendExperimenterOutput>> results) {
90                 final ArrayList<RpcError> errors = new ArrayList<>();
91                 final RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder;
92                 for (RpcResult<SendExperimenterOutput> res : results) {
93                     if (res == null) {
94                         errors.add(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "BundleExtensionService",
95                                                              "RpcResult is null."));
96                     } else if (!res.isSuccessful()) {
97                         errors.addAll(res.getErrors());
98                     }
99                 }
100                 if (errors.isEmpty()) {
101                     rpcResultBuilder = RpcResultBuilder.success();
102                 } else {
103                     rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
104                 }
105                 result.set(rpcResultBuilder.build());
106             }
107
108             @Override
109             public void onFailure(Throwable throwable) {
110                 RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder = RpcResultBuilder.failed();
111                 result.set(rpcResultBuilder.build());
112             }
113         }, MoreExecutors.directExecutor());
114         return result;
115     }
116 }