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