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