Fix for DPNs not connecting to controller
[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.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;
36
37 /**
38  * Simple bundle extension service.
39  */
40 public class SalBundleServiceImpl implements SalBundleService {
41
42     private static final Logger LOG = LoggerFactory.getLogger(SalBundleServiceImpl.class);
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         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();
61                 } else {
62                     return RpcResultBuilder.<ControlBundleOutput>failed().build();
63                 }
64             }, MoreExecutors.directExecutor());
65     }
66
67     @Override
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()));
83         }
84         return processResults(partialResults);
85     }
86
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>>>() {
92             @Override
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) {
97                     if (res == null) {
98                         errors.add(RpcResultBuilder.newError(RpcError.ErrorType.APPLICATION, "BundleExtensionService",
99                                                              "RpcResult is null."));
100                     } else if (!res.isSuccessful()) {
101                         errors.addAll(res.getErrors());
102                     }
103                 }
104                 if (errors.isEmpty()) {
105                     rpcResultBuilder = RpcResultBuilder.success();
106                 } else {
107                     rpcResultBuilder = RpcResultBuilder.<AddBundleMessagesOutput>failed().withRpcErrors(errors);
108                 }
109                 result.set(rpcResultBuilder.build());
110             }
111
112             @Override
113             public void onFailure(Throwable throwable) {
114                 RpcResultBuilder<AddBundleMessagesOutput> rpcResultBuilder = RpcResultBuilder.failed();
115                 result.set(rpcResultBuilder.build());
116             }
117         }, MoreExecutors.directExecutor());
118         return result;
119     }
120 }