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