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;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
15 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
16 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
17 import org.opendaylight.controller.netconf.util.xml.XmlElement;
18 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
19 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
20 import org.w3c.dom.Attr;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.NodeList;
25 import com.google.common.base.Optional;
27 public abstract class AbstractNetconfOperation implements NetconfOperation {
28 private final String netconfSessionIdForReporting;
30 protected AbstractNetconfOperation(String netconfSessionIdForReporting) {
31 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
34 public final String getNetconfSessionIdForReporting() {
35 return netconfSessionIdForReporting;
39 public HandlingPriority canHandle(Document message) {
40 OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
41 return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
44 public static class OperationNameAndNamespace {
45 private final String operationName, namespace;
47 public OperationNameAndNamespace(Document message) {
48 XmlElement requestElement = getRequestElementWithCheck(message);
50 XmlElement operationElement = requestElement.getOnlyChildElement();
51 operationName = operationElement.getName();
52 namespace = operationElement.getNamespace();
55 public String getOperationName() {
59 public String getNamespace() {
64 protected static XmlElement getRequestElementWithCheck(Document message) {
65 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
66 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
69 protected HandlingPriority canHandle(String operationName, String operationNamespace) {
70 return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
71 ? getHandlingPriority()
72 : HandlingPriority.CANNOT_HANDLE;
75 protected HandlingPriority getHandlingPriority() {
76 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
79 protected String getOperationNamespace() {
80 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
83 protected abstract String getOperationName();
86 public Document handle(Document requestMessage,
87 NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
89 XmlElement requestElement = getRequestElementWithCheck(requestMessage);
91 Document document = XmlUtil.newDocument();
93 XmlElement operationElement = requestElement.getOnlyChildElement();
94 Map<String, Attr> attributes = requestElement.getAttributes();
96 Element response = handle(document, operationElement, subsequentOperation);
97 Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
99 if(XmlElement.fromDomElement(response).hasNamespace()) {
100 rpcReply.appendChild(response);
102 Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
103 NodeList list = response.getChildNodes();
104 while(list.getLength()!=0) {
105 responseNS.appendChild(list.item(0));
107 rpcReply.appendChild(responseNS);
110 for (String attrName : attributes.keySet()) {
111 rpcReply.setAttributeNode((Attr) document.importNode(attributes.get(attrName), true));
113 document.appendChild(rpcReply);
117 protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
118 throws NetconfDocumentedException;
121 public String toString() {
122 final StringBuffer sb = new StringBuffer(getClass().getName());
124 sb.append("{name=").append(getOperationName());
125 } catch(UnsupportedOperationException e) {
128 sb.append(", namespace=").append(getOperationNamespace());
129 sb.append(", session=").append(netconfSessionIdForReporting);
131 return sb.toString();