Bump upstreams
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / impl / BaseRpcSchemalessTransformer.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  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.client.mdsal.impl;
9
10 import static com.google.common.base.Preconditions.checkArgument;
11 import static com.google.common.base.Preconditions.checkNotNull;
12
13 import com.google.common.collect.ImmutableMap;
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.BaseNetconfSchemas;
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.EffectiveModelContext;
31 import org.opendaylight.yangtools.yang.model.api.RpcDefinition;
32 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35
36 /**
37  * Transforms base netconf RPCs.
38  */
39 public class BaseRpcSchemalessTransformer implements RpcTransformer<NormalizedNode, DOMRpcResult> {
40     private final ImmutableMap<QName, ? extends RpcDefinition> mappedRpcs;
41     private final EffectiveModelContext modelContext;
42     private final MessageCounter counter;
43
44     public BaseRpcSchemalessTransformer(final BaseNetconfSchemas baseSchemas, final MessageCounter counter) {
45         final var baseSchema = baseSchemas.baseSchema();
46         mappedRpcs = baseSchema.getMappedRpcs();
47         modelContext = baseSchema.modelContext();
48         this.counter = counter;
49     }
50
51     @Override
52     public NetconfMessage toRpcRequest(final QName rpc, final NormalizedNode payload) {
53         // In case no input for rpc is defined, we can simply construct the payload here
54
55         final var mappedRpc = checkNotNull(mappedRpcs.get(rpc),
56             "Unknown rpc %s, available rpcs: %s", rpc, mappedRpcs.keySet());
57         final DOMResult domResult = NetconfMessageTransformUtil.prepareDomResultForRpcRequest(rpc, counter);
58         if (mappedRpc.getInput().getChildNodes().isEmpty()) {
59             return new NetconfMessage(domResult.getNode().getOwnerDocument());
60         }
61
62         checkNotNull(payload, "Transforming an rpc with input: %s, payload cannot be null", rpc);
63         checkArgument(payload instanceof ContainerNode,
64                 "Transforming an rpc with input: %s, payload has to be a container, but was: %s", rpc, payload);
65
66         final DOMResult result = domResult;
67         try {
68             NetconfMessageTransformUtil.writeNormalizedOperationInput((ContainerNode) payload, result, Absolute.of(rpc),
69                 modelContext);
70         } catch (final XMLStreamException | IOException | IllegalStateException e) {
71             throw new IllegalStateException("Unable to serialize input of " + rpc, e);
72         }
73
74         final Document node = result.getNode().getOwnerDocument();
75
76         return new NetconfMessage(node);
77     }
78
79     @Override
80     public DOMRpcResult toRpcResult(final RpcResult<NetconfMessage> resultPayload, final QName rpc) {
81         if (!resultPayload.isSuccessful()) {
82             return new DefaultDOMRpcResult(resultPayload.getErrors());
83         }
84
85         final var message = resultPayload.getResult();
86         final ContainerNode normalizedNode;
87         if (NetconfMessageTransformUtil.isDataRetrievalOperation(rpc)) {
88             final Element xmlData = NetconfMessageTransformUtil.getDataSubtree(message.getDocument());
89             final Document data = XmlUtil.newDocument();
90             data.appendChild(data.importNode(xmlData, true));
91
92             normalizedNode = ImmutableNodes.newContainerBuilder()
93                 .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_NODEID)
94                 .withChild(ImmutableNodes.newAnyxmlBuilder(DOMSource.class)
95                     .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_DATA_NODEID)
96                     .withValue(new DOMSource(data))
97                     .build())
98                 .build();
99         } else {
100             //other base rpcs don't have any output, we can simply construct the payload here
101             checkArgument(isOkPresent(message.getDocument()),
102                 "Unexpected content in response of rpc: %s, %s", rpc, message);
103             normalizedNode = null;
104
105         }
106         return new DefaultDOMRpcResult(normalizedNode);
107     }
108
109     // FIXME this should go to some util class
110     static boolean isOkPresent(final Document doc) {
111         return XmlElement.fromDomDocument(doc).getOnlyChildElementWithSameNamespaceOptionally("ok").isPresent();
112     }
113 }