9cfb27920b52b7f8a9a607b41ac495d158911da1
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / 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.sal.connect.netconf;
9
10 import static java.util.Objects.requireNonNull;
11
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 javax.inject.Singleton;
16 import org.opendaylight.mdsal.dom.api.DOMActionService;
17 import org.opendaylight.netconf.api.NetconfMessage;
18 import org.opendaylight.netconf.sal.connect.api.DeviceActionFactory;
19 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
20 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
21 import org.opendaylight.yangtools.yang.common.RpcResult;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23 import org.osgi.service.component.annotations.Component;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26
27 /**
28  * Implementation of the factory for creation of {@link DOMActionService} instances that are provided by device.
29  * {@link DOMActionService} is implemented using {@link MessageTransformer} that builds NETCONF RPCs and
30  * transforms replied NETCONF message  to action result, and using {@link RemoteDeviceCommunicator} that is responsible
31  * for sending of built RPCs to NETCONF client.
32  */
33 @Singleton
34 @Component(immediate = true, property = "type=default")
35 public class DeviceActionFactoryImpl implements DeviceActionFactory {
36     private static final Logger LOG = LoggerFactory.getLogger(DeviceActionFactoryImpl.class);
37
38     @Override
39     public DOMActionService createDeviceAction(final MessageTransformer messageTransformer,
40             final RemoteDeviceCommunicator listener, final SchemaContext schemaContext) {
41         return (schemaPath, dataTreeIdentifier, input) -> {
42             requireNonNull(schemaPath);
43             requireNonNull(dataTreeIdentifier);
44             requireNonNull(input);
45
46             final ListenableFuture<RpcResult<NetconfMessage>> actionResultFuture = listener.sendRequest(
47                 messageTransformer.toActionRequest(schemaPath, dataTreeIdentifier, input),
48                 input.getIdentifier().getNodeType());
49
50             return Futures.transform(actionResultFuture, netconfMessageRpcResult -> {
51                 if (netconfMessageRpcResult != null) {
52                     return messageTransformer.toActionResult(schemaPath, netconfMessageRpcResult.getResult());
53                 }
54
55                 final String message = "Missing action result of action on schema path: " + schemaPath;
56                 LOG.error(message);
57                 throw new IllegalStateException(message);
58             }, MoreExecutors.directExecutor());
59         };
60     }
61 }