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 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 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
38 * Simple bundle extension service.
40 public class SalBundleServiceImpl implements SalBundleService {
42 private static final Logger LOG = LoggerFactory.getLogger(SalBundleServiceImpl.class);
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 LOG.debug("Control message for device {} and bundle type {}", input.getNode(), input.getType());
53 final SendExperimenterInputBuilder experimenterInputBuilder = new SendExperimenterInputBuilder();
54 experimenterInputBuilder.setNode(input.getNode());
55 experimenterInputBuilder.setExperimenterMessageOfChoice(
56 new BundleControlSalBuilder().setSalControlData(new SalControlDataBuilder(input).build()).build());
57 return Futures.transform(experimenterMessageService.sendExperimenter(
58 experimenterInputBuilder.build()), sendExperimenterOutputRpcResult -> {
59 if (sendExperimenterOutputRpcResult.isSuccessful()) {
60 return RpcResultBuilder.<ControlBundleOutput>success().build();
62 return RpcResultBuilder.<ControlBundleOutput>failed().build();
64 }, MoreExecutors.directExecutor());
68 public ListenableFuture<RpcResult<AddBundleMessagesOutput>> addBundleMessages(AddBundleMessagesInput input) {
69 final List<ListenableFuture<RpcResult<SendExperimenterOutput>>> partialResults = new ArrayList<>();
70 final SendExperimenterInputBuilder experimenterInputBuilder = new SendExperimenterInputBuilder();
71 final BundleAddMessageSalBuilder bundleAddMessageBuilder = new BundleAddMessageSalBuilder();
72 final SalAddMessageDataBuilder dataBuilder = new SalAddMessageDataBuilder();
73 experimenterInputBuilder.setNode(input.getNode());
74 dataBuilder.setNode(input.getNode());
75 dataBuilder.setBundleId(input.getBundleId());
76 dataBuilder.setFlags(input.getFlags());
77 dataBuilder.setBundleProperty(input.getBundleProperty());
78 for (Message message : input.getMessages().getMessage()) {
79 dataBuilder.setBundleInnerMessage(message.getBundleInnerMessage());
80 experimenterInputBuilder.setExperimenterMessageOfChoice(
81 bundleAddMessageBuilder.setSalAddMessageData(dataBuilder.build()).build());
82 partialResults.add(experimenterMessageService.sendExperimenter(experimenterInputBuilder.build()));
84 return processResults(partialResults);
87 private static ListenableFuture<RpcResult<AddBundleMessagesOutput>> processResults(
88 final List<ListenableFuture<RpcResult<SendExperimenterOutput>>> partialResults) {
89 final SettableFuture<RpcResult<AddBundleMessagesOutput>> result = SettableFuture.create();
90 Futures.addCallback(Futures.successfulAsList(partialResults),new FutureCallback<
91 List<RpcResult<SendExperimenterOutput>>>() {
93 public void onSuccess(List<RpcResult<SendExperimenterOutput>> results) {
94 final ArrayList<RpcError> errors = new ArrayList<>();
95 final RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder;
96 for (RpcResult<SendExperimenterOutput> res : results) {
98 errors.add(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "BundleExtensionService",
99 "RpcResult is null."));
100 } else if (!res.isSuccessful()) {
101 errors.addAll(res.getErrors());
104 if (errors.isEmpty()) {
105 rpcResultBuilder = RpcResultBuilder.success();
107 rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
109 result.set(rpcResultBuilder.build());
113 public void onFailure(Throwable throwable) {
114 RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder = RpcResultBuilder.failed();
115 result.set(rpcResultBuilder.build());
117 }, MoreExecutors.directExecutor());