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.sal.connect.netconf.schema.mapping;
10 import javax.xml.transform.dom.DOMSource;
11 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
12 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
13 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
14 import org.opendaylight.netconf.api.NetconfMessage;
15 import org.opendaylight.netconf.sal.connect.api.MessageTransformer;
16 import org.opendaylight.netconf.sal.connect.netconf.util.NetconfMessageTransformUtil;
17 import org.opendaylight.netconf.sal.connect.util.MessageCounter;
18 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
19 import org.opendaylight.yangtools.yang.data.api.schema.AnyXmlNode;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
22 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
27 * Transforms anyxml rpcs for schemaless netconf devices.
29 public class SchemalessMessageTransformer implements MessageTransformer<NetconfMessage> {
31 private static final YangInstanceIdentifier.NodeIdentifier REPLY_ID =
32 new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME);
34 private final MessageCounter counter;
36 public SchemalessMessageTransformer(final MessageCounter counter) {
37 this.counter = counter;
41 public DOMNotification toNotification(final NetconfMessage message) {
42 //TODO add support for notifications
43 throw new UnsupportedOperationException("Notifications not supported.");
47 public NetconfMessage toRpcRequest(final SchemaPath rpc, final NormalizedNode<?, ?> input) {
48 final DOMSource payload = (DOMSource) input.getValue();
49 wrapPayload((Document) payload.getNode());
50 return new NetconfMessage(((Document) ((AnyXmlNode) input).getValue().getNode()));
54 * Transforms reply message to anyXml node. In case, that rpc-reply doesn't contain data and contains only <ok/>
55 * element, returns null.
56 * @param rpcReply reply message
60 public DOMRpcResult toRpcResult(final NetconfMessage rpcReply, final SchemaPath rpc) {
61 final Document document = rpcReply.getDocument();
62 final AnyXmlNode result;
63 if(BaseRpcSchemalessTransformer.isOkPresent(document)) {
66 result = Builders.anyXmlBuilder()
67 .withNodeIdentifier(REPLY_ID)
68 .withValue(new DOMSource(rpcReply.getDocument()))
71 return new DefaultDOMRpcResult(result);
74 private void wrapPayload(final Document doc) {
75 final Element payload = doc.getDocumentElement();
76 doc.removeChild(payload);
77 final Element rpcNS = doc.createElementNS(NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getNamespace().toString(),
78 NetconfMessageTransformUtil.NETCONF_RPC_QNAME.getLocalName());
80 rpcNS.setAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR, counter.getNewMessageId(NetconfMessageTransformUtil.MESSAGE_ID_PREFIX));
81 rpcNS.appendChild(payload);
82 doc.appendChild(rpcNS);