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