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.SalExperimenterMessageService;
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.AddBundleMessagesInput;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.AddBundleMessagesOutput;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleInput;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.ControlBundleOutput;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.SalBundleService;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.add.bundle.messages.input.messages.Message;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleAddMessageSalBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.onf.bundle.service.rev170124.send.experimenter.input.experimenter.message.of.choice.BundleControlSalBuilder;
30 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;
31 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;
32 import org.opendaylight.yangtools.yang.common.ErrorTag;
33 import org.opendaylight.yangtools.yang.common.ErrorType;
34 import org.opendaylight.yangtools.yang.common.RpcError;
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;
41 * Simple bundle extension service.
43 public class SalBundleServiceImpl implements SalBundleService {
44 private static final Logger LOG = LoggerFactory.getLogger(SalBundleServiceImpl.class);
46 private final SalExperimenterMessageService experimenterMessageService;
48 public SalBundleServiceImpl(final SalExperimenterMessageService experimenterMessageService) {
49 this.experimenterMessageService = requireNonNull(experimenterMessageService,
50 "SalExperimenterMessageService can not be null!");
54 public ListenableFuture<RpcResult<ControlBundleOutput>> controlBundle(final ControlBundleInput input) {
55 LOG.debug("Control message for device {} and bundle type {}", input.getNode(), input.getType());
56 final SendExperimenterInputBuilder experimenterInputBuilder = new SendExperimenterInputBuilder();
57 experimenterInputBuilder.setNode(input.getNode());
58 experimenterInputBuilder.setExperimenterMessageOfChoice(
59 new BundleControlSalBuilder().setSalControlData(new SalControlDataBuilder(input).build()).build());
60 return Futures.transform(experimenterMessageService.sendExperimenter(
61 experimenterInputBuilder.build()), sendExperimenterOutputRpcResult -> {
62 if (sendExperimenterOutputRpcResult.isSuccessful()) {
63 return RpcResultBuilder.<ControlBundleOutput>success().build();
65 return RpcResultBuilder.<ControlBundleOutput>failed().build();
67 }, MoreExecutors.directExecutor());
71 public ListenableFuture<RpcResult<AddBundleMessagesOutput>> addBundleMessages(final AddBundleMessagesInput input) {
72 final List<ListenableFuture<RpcResult<SendExperimenterOutput>>> partialResults = new ArrayList<>();
73 final SendExperimenterInputBuilder experimenterInputBuilder = new SendExperimenterInputBuilder();
74 final BundleAddMessageSalBuilder bundleAddMessageBuilder = new BundleAddMessageSalBuilder();
75 final SalAddMessageDataBuilder dataBuilder = new SalAddMessageDataBuilder();
76 experimenterInputBuilder.setNode(input.getNode());
77 dataBuilder.setNode(input.getNode());
78 dataBuilder.setBundleId(input.getBundleId());
79 dataBuilder.setFlags(input.getFlags());
80 dataBuilder.setBundleProperty(input.getBundleProperty());
81 for (Message message : input.getMessages().getMessage()) {
82 dataBuilder.setBundleInnerMessage(message.getBundleInnerMessage());
83 experimenterInputBuilder.setExperimenterMessageOfChoice(
84 bundleAddMessageBuilder.setSalAddMessageData(dataBuilder.build()).build());
85 partialResults.add(experimenterMessageService.sendExperimenter(experimenterInputBuilder.build()));
87 return processResults(partialResults);
90 private static ListenableFuture<RpcResult<AddBundleMessagesOutput>> processResults(
91 final List<ListenableFuture<RpcResult<SendExperimenterOutput>>> partialResults) {
92 final SettableFuture<RpcResult<AddBundleMessagesOutput>> result = SettableFuture.create();
93 Futures.addCallback(Futures.successfulAsList(partialResults),new FutureCallback<
94 List<RpcResult<SendExperimenterOutput>>>() {
96 public void onSuccess(final List<RpcResult<SendExperimenterOutput>> results) {
97 final ArrayList<RpcError> errors = new ArrayList<>();
98 final RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder;
99 for (RpcResult<SendExperimenterOutput> res : results) {
101 // FIXME: this should never happen
102 errors.add(RpcResultBuilder.newError(ErrorType.APPLICATION,
103 new ErrorTag("BundleExtensionService"), "RpcResult is null."));
104 } else if (!res.isSuccessful()) {
105 errors.addAll(res.getErrors());
108 if (errors.isEmpty()) {
109 rpcResultBuilder = RpcResultBuilder.success();
111 rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
113 result.set(rpcResultBuilder.build());
117 public void onFailure(final Throwable throwable) {
118 RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder = RpcResultBuilder.failed();
119 result.set(rpcResultBuilder.build());
121 }, MoreExecutors.directExecutor());