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