531c2905df37868a27d9958408cff86ad7e9f849
[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
9 package org.opendaylight.openflowplugin.impl.services.sal;
10
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;
37
38 /**
39  * Simple bundle extension service.
40  */
41 public class SalBundleServiceImpl implements SalBundleService {
42
43     private final SalExperimenterMessageService experimenterMessageService;
44
45     public SalBundleServiceImpl(final SalExperimenterMessageService experimenterMessageService) {
46         this.experimenterMessageService = Preconditions
47                 .checkNotNull(experimenterMessageService, "SalExperimenterMessageService can not be null!");
48     }
49
50     @Override
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();
60                 } else {
61                     return RpcResultBuilder.<ControlBundleOutput>failed().build();
62                 }
63             }, MoreExecutors.directExecutor());
64     }
65
66     @Override
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);
84         }
85         return processResults(partialResults);
86     }
87
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>>>() {
93             @Override
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) {
98                     if (res == null) {
99                         errors.add(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "BundleExtensionService",
100                                                              "RpcResult is null."));
101                     } else if (!res.isSuccessful()) {
102                         errors.addAll(res.getErrors());
103                     }
104                 }
105                 if (errors.isEmpty()) {
106                     rpcResultBuilder = RpcResultBuilder.success();
107                 } else {
108                     rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
109                 }
110                 result.set(rpcResultBuilder.build());
111             }
112
113             @Override
114             public void onFailure(Throwable throwable) {
115                 RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder = RpcResultBuilder.failed();
116                 result.set(rpcResultBuilder.build());
117             }
118         }, MoreExecutors.directExecutor());
119         return result;
120     }
121 }