2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.netconf.client.mdsal.impl;
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.impl.schema.Builders;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
25 * Transforms anyxml rpcs for schemaless netconf devices.
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"));
34 private final MessageCounter counter;
36 public SchemalessMessageTransformer(final MessageCounter counter) {
37 this.counter = counter;
41 public DOMNotification toNotification(final NetconfMessage message) {
42 final var stripped = NetconfMessageTransformUtil.stripNotification(message);
43 final QName 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);
52 return new NetconfMessageTransformer.NetconfDeviceNotification(Builders.containerBuilder()
53 .withNodeIdentifier(SCHEMALESS_NOTIFICATION_PAYLOAD)
54 .withChild(Builders.anyXmlBuilder()
55 .withNodeIdentifier(new NodeIdentifier(notificationNoRev))
56 .withValue(new DOMSource(stripped.getValue().getDomElement()))
58 .build(), stripped.getKey());
62 public NetconfMessage toRpcRequest(final QName rpc, final DOMSource payload) {
63 wrapPayload((Document) payload.getNode());
64 return new NetconfMessage((Document) payload.getNode());
68 * Transforms reply message to anyXml node.
69 * In case, that rpc-reply doesn't contain data and contains only <ok/> element, returns null.
70 * @param resultPayload reply message
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);
79 private void wrapPayload(final Document doc) {
80 final Element payload = doc.getDocumentElement();
81 doc.removeChild(payload);
83 doc.createElementNS(NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getNamespace().toString(),
84 NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getLocalName());
86 rpcNS.setAttribute(XmlNetconfConstants.MESSAGE_ID,
87 counter.getNewMessageId(NetconfMessageTransformUtil.MESSAGE_ID_PREFIX));
88 rpcNS.appendChild(payload);
89 doc.appendChild(rpcNS);