Bump mdsal to 4.0.0
[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
9 package org.opendaylight.netconf.sal.connect.netconf;
10
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ClassToInstanceMap;
13 import com.google.common.collect.ImmutableClassToInstanceMap;
14 import com.google.common.util.concurrent.Futures;
15 import com.google.common.util.concurrent.ListenableFuture;
16 import com.google.common.util.concurrent.MoreExecutors;
17 import org.opendaylight.mdsal.dom.api.DOMActionResult;
18 import org.opendaylight.mdsal.dom.api.DOMActionService;
19 import org.opendaylight.mdsal.dom.api.DOMActionServiceExtension;
20 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
21 import org.opendaylight.netconf.api.NetconfMessage;
22 import org.opendaylight.netconf.sal.connect.api.DeviceActionFactory;
23 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
24 import org.opendaylight.netconf.sal.connect.api.RemoteDeviceCommunicator;
25 import org.opendaylight.yangtools.yang.common.RpcResult;
26 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Implementation of the factory for creation of {@link DOMActionService} instances that are provided by device.
34  * {@link DOMActionService} is implemented using {@link MessageTransformer} that builds NETCONF RPCs and
35  * transforms replied NETCONF message  to action result, and using {@link RemoteDeviceCommunicator} that is responsible
36  * for sending of built RPCs to NETCONF client.
37  */
38 public class DeviceActionFactoryImpl implements DeviceActionFactory {
39
40     private static final Logger LOG = LoggerFactory.getLogger(DeviceActionFactoryImpl.class);
41
42     @Override
43     public DOMActionService createDeviceAction(final MessageTransformer<NetconfMessage> messageTransformer,
44             final RemoteDeviceCommunicator<NetconfMessage> listener, final SchemaContext schemaContext) {
45
46         return new DOMActionService() {
47             @Override
48             public ListenableFuture<? extends DOMActionResult> invokeAction(final SchemaPath schemaPath,
49                     final DOMDataTreeIdentifier dataTreeIdentifier, final ContainerNode input) {
50                 Preconditions.checkNotNull(schemaPath);
51                 Preconditions.checkNotNull(dataTreeIdentifier);
52                 Preconditions.checkNotNull(input);
53
54                 final ListenableFuture<RpcResult<NetconfMessage>> actionResultFuture = listener.sendRequest(
55                         messageTransformer.toActionRequest(schemaPath, dataTreeIdentifier, input), input.getNodeType());
56
57                 return Futures.transform(actionResultFuture, netconfMessageRpcResult -> {
58                     if (netconfMessageRpcResult != null) {
59                         return messageTransformer.toActionResult(schemaPath, netconfMessageRpcResult.getResult());
60                     } else {
61                         final String message = "Missing action result of action on schema path: " + schemaPath;
62                         LOG.error(message);
63                         throw new IllegalStateException(message);
64                     }
65                 }, MoreExecutors.directExecutor());
66             }
67
68             @Override
69             public ClassToInstanceMap<DOMActionServiceExtension> getExtensions() {
70                 return ImmutableClassToInstanceMap.of();
71             }
72         };
73     }
74 }