Merge "Remove unused isMaster variable in ClusteredNetconfDevice"
[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 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;
25
26 /**
27  * Transforms anyxml rpcs for schemaless netconf devices.
28  */
29 public class SchemalessMessageTransformer implements MessageTransformer<NetconfMessage> {
30
31     private static final YangInstanceIdentifier.NodeIdentifier REPLY_ID =
32             new YangInstanceIdentifier.NodeIdentifier(NetconfMessageTransformUtil.NETCONF_RPC_REPLY_QNAME);
33
34     private final MessageCounter counter;
35
36     public SchemalessMessageTransformer(final MessageCounter counter) {
37         this.counter = counter;
38     }
39
40     @Override
41     public DOMNotification toNotification(final NetconfMessage message) {
42         //TODO add support for notifications
43         throw new UnsupportedOperationException("Notifications not supported.");
44     }
45
46     @Override
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()));
51     }
52
53     /**
54      * Transforms reply message to anyXml node. In case, that rpc-reply doesn't contain data and contains only &lt;ok/&gt;
55      * element, returns null.
56      * @param rpcReply reply message
57      * @return anyxml
58      */
59     @Override
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)) {
64             result =  null;
65         } else {
66             result = Builders.anyXmlBuilder()
67                     .withNodeIdentifier(REPLY_ID)
68                     .withValue(new DOMSource(rpcReply.getDocument()))
69                     .build();
70         }
71         return new DefaultDOMRpcResult(result);
72     }
73
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());
79         // set msg id
80         rpcNS.setAttribute(NetconfMessageTransformUtil.MESSAGE_ID_ATTR, counter.getNewMessageId(NetconfMessageTransformUtil.MESSAGE_ID_PREFIX));
81         rpcNS.appendChild(payload);
82         doc.appendChild(rpcNS);
83     }
84 }