Bump MRI upstreams
[netconf.git] / netconf / sal-netconf-connector / src / main / java / org / opendaylight / netconf / sal / connect / netconf / schema / mapping / 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.sal.connect.netconf.schema.mapping;
9
10 import java.time.Instant;
11 import java.util.Map;
12 import javax.xml.transform.dom.DOMSource;
13 import org.opendaylight.mdsal.dom.api.DOMNotification;
14 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
15 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
16 import org.opendaylight.netconf.api.NetconfMessage;
17 import org.opendaylight.netconf.api.xml.MissingNameSpaceException;
18 import org.opendaylight.netconf.api.xml.XmlElement;
19 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
20 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
21 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.DOMSourceAnyxmlNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
28 import org.w3c.dom.Document;
29 import org.w3c.dom.Element;
30
31 /**
32  * Transforms anyxml rpcs for schemaless netconf devices.
33  */
34 public class SchemalessMessageTransformer implements MessageTransformer<NetconfMessage> {
35     // TODO maybe we should move this somewhere else as this
36     // might be used in applications using schemaless mountpoints
37     public static final NodeIdentifier SCHEMALESS_NOTIFICATION_PAYLOAD =
38             // FIXME: assign proper namespace
39             new NodeIdentifier(QName.create("", "schemaless-notification-payload"));
40
41     private final MessageCounter counter;
42
43     public SchemalessMessageTransformer(final MessageCounter counter) {
44         this.counter = counter;
45     }
46
47     @Override
48     public DOMNotification toNotification(final NetconfMessage message) {
49         final Map.Entry<Instant, XmlElement> stripped = NetconfMessageTransformUtil.stripNotification(message);
50         final QName notificationNoRev;
51         try {
52             notificationNoRev =
53                     QName.create(stripped.getValue().getNamespace(), stripped.getValue().getName()).withoutRevision();
54         } catch (final MissingNameSpaceException e) {
55             throw new IllegalArgumentException("Unable to parse notification "
56                     + message + ", cannot find namespace", e);
57         }
58
59         final DOMSourceAnyxmlNode notificationPayload = Builders.anyXmlBuilder()
60                 .withNodeIdentifier(new NodeIdentifier(notificationNoRev))
61                 .withValue(new DOMSource(stripped.getValue().getDomElement()))
62                 .build();
63
64         final ContainerNode notificationBody = Builders.containerBuilder()
65                 .withNodeIdentifier(SCHEMALESS_NOTIFICATION_PAYLOAD)
66                 .withChild(notificationPayload)
67                 .build();
68
69         return new NetconfMessageTransformer.NetconfDeviceNotification(notificationBody, stripped.getKey());
70     }
71
72     @Override
73     public NetconfMessage toRpcRequest(final QName rpc, final NormalizedNode input) {
74         final DOMSource payload = (DOMSource) input.body();
75         wrapPayload((Document) payload.getNode());
76         return new NetconfMessage((Document) ((DOMSourceAnyxmlNode) input).body().getNode());
77     }
78
79     /**
80      * Transforms reply message to anyXml node.
81      * In case, that rpc-reply doesn't contain data and contains only &lt;ok/&gt; element, returns null.
82      * @param rpcReply reply message
83      * @return anyxml
84      */
85     @Override
86     public DOMRpcResult toRpcResult(final NetconfMessage rpcReply, final QName rpc) {
87         final Document document = rpcReply.getDocument();
88         final DOMSourceAnyxmlNode result;
89         if (BaseRpcSchemalessTransformer.isOkPresent(document)) {
90             result =  null;
91         } else {
92             result = Builders.anyXmlBuilder()
93                     .withNodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_NODEID)
94                     .withValue(new DOMSource(rpcReply.getDocument()))
95                     .build();
96         }
97         return new DefaultDOMRpcResult(result);
98     }
99
100     private void wrapPayload(final Document doc) {
101         final Element payload = doc.getDocumentElement();
102         doc.removeChild(payload);
103         final Element rpcNS =
104                 doc.createElementNS(NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getNamespace().toString(),
105                 NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getLocalName());
106         // set msg id
107         rpcNS.setAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR,
108                 counter.getNewMessageId(NetconfMessageTransformUtil.MESSAGE_ID_PREFIX));
109         rpcNS.appendChild(payload);
110         doc.appendChild(rpcNS);
111     }
112 }