2 * Copyright © 2019 FRINX s.r.o. All rights reserved.
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
8 package org.opendaylight.netconf.sal.connect.netconf;
10 import static java.util.Objects.requireNonNull;
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;
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.
34 @Component(immediate = true, property = "type=default")
35 public class DeviceActionFactoryImpl implements DeviceActionFactory {
36 private static final Logger LOG = LoggerFactory.getLogger(DeviceActionFactoryImpl.class);
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);
46 final ListenableFuture<RpcResult<NetconfMessage>> actionResultFuture = listener.sendRequest(
47 messageTransformer.toActionRequest(schemaPath, dataTreeIdentifier, input),
48 input.getIdentifier().getNodeType());
50 return Futures.transform(actionResultFuture, netconfMessageRpcResult -> {
51 if (netconfMessageRpcResult != null) {
52 return messageTransformer.toActionResult(schemaPath, netconfMessageRpcResult.getResult());
55 final String message = "Missing action result of action on schema path: " + schemaPath;
57 throw new IllegalStateException(message);
58 }, MoreExecutors.directExecutor());