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