Clean up netconf-client-mdsal warnings
[netconf.git] / plugins / netconf-client-mdsal / src / main / java / org / opendaylight / netconf / client / mdsal / impl / SchemalessMessageTransformer.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 javax.xml.transform.dom.DOMSource;
11 import org.opendaylight.mdsal.dom.api.DOMNotification;
12 import org.opendaylight.netconf.api.messages.NetconfMessage;
13 import org.opendaylight.netconf.api.xml.MissingNameSpaceException;
14 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.netconf.client.mdsal.api.NotificationTransformer;
16 import org.opendaylight.netconf.client.mdsal.api.RpcTransformer;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.RpcResult;
19 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
20 import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23
24 /**
25  * Transforms anyxml rpcs for schemaless netconf devices.
26  */
27 public class SchemalessMessageTransformer implements NotificationTransformer, RpcTransformer<DOMSource, DOMSource> {
28     // TODO maybe we should move this somewhere else as this
29     // might be used in applications using schemaless mountpoints
30     public static final NodeIdentifier SCHEMALESS_NOTIFICATION_PAYLOAD =
31             // FIXME: assign proper namespace
32             new NodeIdentifier(QName.create("", "schemaless-notification-payload"));
33
34     private final MessageCounter counter;
35
36     public SchemalessMessageTransformer(final MessageCounter counter) {
37         this.counter = counter;
38     }
39
40     @Override
41     public DOMNotification toNotification(final NetconfMessage message) {
42         final var stripped = NetconfMessageTransformUtil.stripNotification(message);
43         final QName notificationNoRev;
44         try {
45             notificationNoRev =
46                     QName.create(stripped.getValue().getNamespace(), stripped.getValue().getName()).withoutRevision();
47         } catch (final MissingNameSpaceException e) {
48             throw new IllegalArgumentException("Unable to parse notification "
49                     + message + ", cannot find namespace", e);
50         }
51
52         return new NetconfMessageTransformer.NetconfDeviceNotification(ImmutableNodes.newContainerBuilder()
53             .withNodeIdentifier(SCHEMALESS_NOTIFICATION_PAYLOAD)
54             .withChild(ImmutableNodes.newAnyxmlBuilder(DOMSource.class)
55                 .withNodeIdentifier(new NodeIdentifier(notificationNoRev))
56                 .withValue(new DOMSource(stripped.getValue().getDomElement()))
57                 .build())
58             .build(), stripped.getKey());
59     }
60
61     @Override
62     public NetconfMessage toRpcRequest(final QName rpc, final DOMSource payload) {
63         wrapPayload((Document) payload.getNode());
64         return new NetconfMessage((Document) payload.getNode());
65     }
66
67     /**
68      * Transforms reply message to anyXml node.
69      * In case, that rpc-reply doesn't contain data and contains only &lt;ok/&gt; element, returns null.
70      * @param resultPayload reply message
71      * @return anyxml
72      */
73     @Override
74     public DOMSource toRpcResult(final RpcResult<NetconfMessage> resultPayload, final QName rpc) {
75         final var document = resultPayload.getResult().getDocument();
76         return BaseRpcSchemalessTransformer.isOkPresent(document) ? null : new DOMSource(document);
77     }
78
79     private void wrapPayload(final Document doc) {
80         final Element payload = doc.getDocumentElement();
81         doc.removeChild(payload);
82         final Element rpcNS =
83                 doc.createElementNS(NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getNamespace().toString(),
84                 NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getLocalName());
85         // set msg id
86         rpcNS.setAttribute(XmlNetconfConstants.MESSAGE_ID,
87                 counter.getNewMessageId(NetconfMessageTransformUtil.MESSAGE_ID_PREFIX));
88         rpcNS.appendChild(payload);
89         doc.appendChild(rpcNS);
90     }
91 }