2 * Copyright (c) 2016 Cisco Systems, Inc. and others. 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.client.mdsal.impl;
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
12 import static java.util.Objects.requireNonNull;
14 import java.io.IOException;
15 import javax.xml.stream.XMLStreamException;
16 import javax.xml.transform.dom.DOMResult;
17 import javax.xml.transform.dom.DOMSource;
18 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
19 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
20 import org.opendaylight.netconf.api.messages.NetconfMessage;
21 import org.opendaylight.netconf.api.xml.XmlElement;
22 import org.opendaylight.netconf.api.xml.XmlUtil;
23 import org.opendaylight.netconf.client.mdsal.api.BaseNetconfSchema;
24 import org.opendaylight.netconf.client.mdsal.api.RpcTransformer;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.common.RpcResult;
27 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
30 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
35 * Transforms base netconf RPCs.
37 public class BaseRpcSchemalessTransformer implements RpcTransformer<NormalizedNode, DOMRpcResult> {
38 private final BaseNetconfSchema baseSchema;
39 private final MessageCounter counter;
41 public BaseRpcSchemalessTransformer(final BaseNetconfSchema baseSchema, final MessageCounter counter) {
42 this.baseSchema = requireNonNull(baseSchema);
43 this.counter = requireNonNull(counter);
47 public NetconfMessage toRpcRequest(final QName rpc, final NormalizedNode payload) {
48 // In case no input for rpc is defined, we can simply construct the payload here
49 final var mappedRpcs = baseSchema.mappedRpcs();
50 final var mappedRpc = checkNotNull(mappedRpcs.get(rpc),
51 "Unknown rpc %s, available rpcs: %s", rpc, mappedRpcs.keySet());
52 final DOMResult domResult = NetconfMessageTransformUtil.prepareDomResultForRpcRequest(rpc, counter);
53 if (mappedRpc.getInput().getChildNodes().isEmpty()) {
54 return new NetconfMessage(domResult.getNode().getOwnerDocument());
57 checkNotNull(payload, "Transforming an rpc with input: %s, payload cannot be null", rpc);
58 checkArgument(payload instanceof ContainerNode,
59 "Transforming an rpc with input: %s, payload has to be a container, but was: %s", rpc, payload);
61 final DOMResult result = domResult;
63 NetconfMessageTransformUtil.writeNormalizedOperationInput((ContainerNode) payload, result, Absolute.of(rpc),
64 baseSchema.modelContext());
65 } catch (final XMLStreamException | IOException | IllegalStateException e) {
66 throw new IllegalStateException("Unable to serialize input of " + rpc, e);
69 final Document node = result.getNode().getOwnerDocument();
71 return new NetconfMessage(node);
75 public DOMRpcResult toRpcResult(final RpcResult<NetconfMessage> resultPayload, final QName rpc) {
76 if (!resultPayload.isSuccessful()) {
77 return new DefaultDOMRpcResult(resultPayload.getErrors());
80 final var message = resultPayload.getResult();
81 final ContainerNode normalizedNode;
82 if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpc)) {
83 final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument());
84 final Document data = XmlUtil.newDocument();
85 data.appendChild(data.importNode(xmlData, true));
87 normalizedNode = ImmutableNodes.newContainerBuilder()
88 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_NODEID)
89 .withChild(ImmutableNodes.newAnyxmlBuilder(DOMSource.class)
90 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_NODEID)
91 .withValue(new DOMSource(data))
95 //other base rpcs don't have any output, we can simply construct the payload here
96 checkArgument(isOkPresent(message.getDocument()),
97 "Unexpected content in response of rpc: %s, %s", rpc, message);
98 normalizedNode = null;
101 return new DefaultDOMRpcResult(normalizedNode);
104 // FIXME this should go to some util class
105 static boolean isOkPresent(final Document doc) {
106 return XmlElement.fromDomDocument(doc).getOnlyChildElementWithSameNamespaceOptionally("ok").isPresent();