7845fd238e501445e4589d5fbfb4c11f2c7233e7
[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),
45                 input.getIdentifier().getNodeType());
46
47             return Futures.transform(actionResultFuture, netconfMessageRpcResult -> {
48                 if (netconfMessageRpcResult != null) {
49                     return messageTransformer.toActionResult(schemaPath, netconfMessageRpcResult.getResult());
50                 }
51
52                 final String message = "Missing action result of action on schema path: " + schemaPath;
53                 LOG.error(message);
54                 throw new IllegalStateException(message);
55             }, MoreExecutors.directExecutor());
56         };
57     }
58 }