2 * Copyright (c) 2013 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
9 package org.opendaylight.controller.netconf.util.mapping;
11 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
12 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
13 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
14 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
15 import org.opendaylight.controller.netconf.util.xml.XmlElement;
16 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
17 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
18 import org.w3c.dom.Attr;
19 import org.w3c.dom.Document;
20 import org.w3c.dom.Element;
21 import org.w3c.dom.NodeList;
25 public abstract class AbstractNetconfOperation implements NetconfOperation {
26 private final String netconfSessionIdForReporting;
28 protected AbstractNetconfOperation(String netconfSessionIdForReporting) {
29 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
32 public String getNetconfSessionIdForReporting() {
33 return netconfSessionIdForReporting;
37 public HandlingPriority canHandle(Document message) {
38 OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
39 return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
42 public static class OperationNameAndNamespace {
43 private final String operationName, namespace;
45 public OperationNameAndNamespace(Document message) {
46 XmlElement requestElement = getRequestElementWithCheck(message);
48 XmlElement operationElement = requestElement.getOnlyChildElement();
49 operationName = operationElement.getName();
50 namespace = operationElement.getNamespace();
53 public String getOperationName() {
57 public String getNamespace() {
62 protected static XmlElement getRequestElementWithCheck(Document message) {
63 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
64 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
67 protected abstract HandlingPriority canHandle(String operationName, String netconfOperationNamespace);
70 public Document handle(Document message, NetconfOperationRouter opRouter) throws NetconfDocumentedException {
72 XmlElement requestElement = getRequestElementWithCheck(message);
74 Document document = XmlUtil.newDocument();
76 XmlElement operationElement = requestElement.getOnlyChildElement();
77 Map<String, Attr> attributes = requestElement.getAttributes();
79 Element response = handle(document, operationElement, opRouter);
80 Element rpcReply = document.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
81 XmlNetconfConstants.RPC_REPLY_KEY);
83 if(XmlElement.fromDomElement(response).hasNamespace()) {
84 rpcReply.appendChild(response);
86 Element responseNS = document.createElementNS(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0, response.getNodeName());
87 NodeList list = response.getChildNodes();
88 while(list.getLength()!=0) {
89 responseNS.appendChild(list.item(0));
91 rpcReply.appendChild(responseNS);
94 for (String attrName : attributes.keySet()) {
95 rpcReply.setAttributeNode((Attr) document.importNode(attributes.get(attrName), true));
97 document.appendChild(rpcReply);
101 protected abstract Element handle(Document document, XmlElement operationElement, NetconfOperationRouter opRouter)
102 throws NetconfDocumentedException;
105 public String toString() {
106 return getClass() + "{" + netconfSessionIdForReporting + '}';