Bump MRI upstreams
[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.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.ErrorTag;
32 import org.opendaylight.yangtools.yang.common.ErrorType;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * Simple bundle extension service.
41  */
42 public class SalBundleServiceImpl implements SalBundleService {
43     private static final Logger LOG = LoggerFactory.getLogger(SalBundleServiceImpl.class);
44     private final SalExperimenterMessageService experimenterMessageService;
45
46     public SalBundleServiceImpl(final SalExperimenterMessageService experimenterMessageService) {
47         this.experimenterMessageService = Preconditions
48                 .checkNotNull(experimenterMessageService, "SalExperimenterMessageService can not be null!");
49     }
50
51     @Override
52     public ListenableFuture<RpcResult<ControlBundleOutput>> controlBundle(final ControlBundleInput input) {
53         LOG.debug("Control message for device {} and bundle type {}", input.getNode(), input.getType());
54         final SendExperimenterInputBuilder experimenterInputBuilder = new SendExperimenterInputBuilder();
55         experimenterInputBuilder.setNode(input.getNode());
56         experimenterInputBuilder.setExperimenterMessageOfChoice(
57                 new BundleControlSalBuilder().setSalControlData(new SalControlDataBuilder(input).build()).build());
58         return Futures.transform(experimenterMessageService.sendExperimenter(
59                 experimenterInputBuilder.build()), sendExperimenterOutputRpcResult -> {
60                 if (sendExperimenterOutputRpcResult.isSuccessful()) {
61                     return RpcResultBuilder.<ControlBundleOutput>success().build();
62                 } else {
63                     return RpcResultBuilder.<ControlBundleOutput>failed().build();
64                 }
65             }, MoreExecutors.directExecutor());
66     }
67
68     @Override
69     public ListenableFuture<RpcResult<AddBundleMessagesOutput>> addBundleMessages(final AddBundleMessagesInput input) {
70         final List<ListenableFuture<RpcResult<SendExperimenterOutput>>> partialResults = new ArrayList<>();
71         final SendExperimenterInputBuilder experimenterInputBuilder = new SendExperimenterInputBuilder();
72         final BundleAddMessageSalBuilder bundleAddMessageBuilder = new BundleAddMessageSalBuilder();
73         final SalAddMessageDataBuilder dataBuilder = new SalAddMessageDataBuilder();
74         experimenterInputBuilder.setNode(input.getNode());
75         dataBuilder.setNode(input.getNode());
76         dataBuilder.setBundleId(input.getBundleId());
77         dataBuilder.setFlags(input.getFlags());
78         dataBuilder.setBundleProperty(input.getBundleProperty());
79         for (Message message : input.getMessages().getMessage()) {
80             dataBuilder.setBundleInnerMessage(message.getBundleInnerMessage());
81             experimenterInputBuilder.setExperimenterMessageOfChoice(
82                     bundleAddMessageBuilder.setSalAddMessageData(dataBuilder.build()).build());
83             partialResults.add(experimenterMessageService.sendExperimenter(experimenterInputBuilder.build()));
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(final 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                         // FIXME: this should never happen
100                         errors.add(RpcResultBuilder.newError(ErrorType.APPLICATION,
101                             new ErrorTag("BundleExtensionService"), "RpcResult is null."));
102                     } else if (!res.isSuccessful()) {
103                         errors.addAll(res.getErrors());
104                     }
105                 }
106                 if (errors.isEmpty()) {
107                     rpcResultBuilder = RpcResultBuilder.success();
108                 } else {
109                     rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
110                 }
111                 result.set(rpcResultBuilder.build());
112             }
113
114             @Override
115             public void onFailure(final Throwable throwable) {
116                 RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder = RpcResultBuilder.failed();
117                 result.set(rpcResultBuilder.build());
118             }
119         }, MoreExecutors.directExecutor());
120         return result;
121     }
122 }