Remove unused routedRpcRegistration
[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 static java.util.Objects.requireNonNull;
11
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;
39
40 /**
41  * Simple bundle extension service.
42  */
43 public class SalBundleServiceImpl implements SalBundleService {
44     private static final Logger LOG = LoggerFactory.getLogger(SalBundleServiceImpl.class);
45
46     private final SalExperimenterMessageService experimenterMessageService;
47
48     public SalBundleServiceImpl(final SalExperimenterMessageService experimenterMessageService) {
49         this.experimenterMessageService = requireNonNull(experimenterMessageService,
50             "SalExperimenterMessageService can not be null!");
51     }
52
53     @Override
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();
64                 } else {
65                     return RpcResultBuilder.<ControlBundleOutput>failed().build();
66                 }
67             }, MoreExecutors.directExecutor());
68     }
69
70     @Override
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()));
86         }
87         return processResults(partialResults);
88     }
89
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>>>() {
95             @Override
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) {
100                     if (res == null) {
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());
106                     }
107                 }
108                 if (errors.isEmpty()) {
109                     rpcResultBuilder = RpcResultBuilder.success();
110                 } else {
111                     rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
112                 }
113                 result.set(rpcResultBuilder.build());
114             }
115
116             @Override
117             public void onFailure(final Throwable throwable) {
118                 RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder = RpcResultBuilder.failed();
119                 result.set(rpcResultBuilder.build());
120             }
121         }, MoreExecutors.directExecutor());
122         return result;
123     }
124 }