Move data processing to update thread
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / DeviceActionFactoryImpl.java
1 /*
2  * Copyright © 2019 FRINX s.r.o. 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.netconf.client.mdsal;
9
10 import static java.util.Objects.requireNonNull;
11
12 import com.google.common.util.concurrent.Futures;
13 import com.google.common.util.concurrent.MoreExecutors;
14 import javax.inject.Singleton;
15 import org.opendaylight.mdsal.dom.api.DOMActionService;
16 import org.opendaylight.netconf.client.mdsal.api.ActionTransformer;
17 import org.opendaylight.netconf.client.mdsal.api.DeviceActionFactory;
18 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceCommunicator;
19 import org.opendaylight.netconf.client.mdsal.api.RemoteDeviceServices.Actions;
20 import org.osgi.service.component.annotations.Component;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Implementation of the factory for creation of {@link DOMActionService} instances that are provided by device.
26  * {@link DOMActionService} is implemented using {@link ActionTransformer} that builds NETCONF RPCs and
27  * transforms replied NETCONF message  to action result, and using {@link RemoteDeviceCommunicator} that is responsible
28  * for sending of built RPCs to NETCONF client.
29  */
30 @Singleton
31 @Component(immediate = true, property = "type=default")
32 public class DeviceActionFactoryImpl implements DeviceActionFactory {
33     private static final Logger LOG = LoggerFactory.getLogger(DeviceActionFactoryImpl.class);
34
35     @Override
36     public Actions.Normalized createDeviceAction(final ActionTransformer messageTransformer,
37             final RemoteDeviceCommunicator listener) {
38         return (schemaPath, dataTreeIdentifier, input) -> {
39             requireNonNull(schemaPath);
40             requireNonNull(dataTreeIdentifier);
41             requireNonNull(input);
42
43             final var actionResultFuture = listener.sendRequest(
44                 messageTransformer.toActionRequest(schemaPath, dataTreeIdentifier, input), input.name().getNodeType());
45
46             return Futures.transform(actionResultFuture, netconfMessageRpcResult -> {
47                 if (netconfMessageRpcResult != null) {
48                     return messageTransformer.toActionResult(schemaPath, netconfMessageRpcResult.getResult());
49                 }
50
51                 final String message = "Missing action result of action on schema path: " + schemaPath;
52                 LOG.error(message);
53                 throw new IllegalStateException(message);
54             }, MoreExecutors.directExecutor());
55         };
56     }
57 }