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