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.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22 import org.w3c.dom.Attr;
23 import org.w3c.dom.Document;
24 import org.w3c.dom.Element;
25 import org.w3c.dom.NodeList;
27 import com.google.common.base.Optional;
29 public abstract class AbstractNetconfOperation implements NetconfOperation {
30 private final String netconfSessionIdForReporting;
31 private static final Logger logger = LoggerFactory.getLogger(AbstractNetconfOperation.class);
33 protected AbstractNetconfOperation(String netconfSessionIdForReporting) {
34 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
37 public final String getNetconfSessionIdForReporting() {
38 return netconfSessionIdForReporting;
42 public HandlingPriority canHandle(Document message) throws NetconfDocumentedException {
43 OperationNameAndNamespace operationNameAndNamespace = null;
44 operationNameAndNamespace = new OperationNameAndNamespace(message);
45 return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
48 public static final class OperationNameAndNamespace {
49 private final String operationName, namespace;
51 public OperationNameAndNamespace(Document message) throws NetconfDocumentedException {
52 XmlElement requestElement = null;
53 requestElement = getRequestElementWithCheck(message);
55 XmlElement operationElement = requestElement.getOnlyChildElement();
56 operationName = operationElement.getName();
57 namespace = operationElement.getNamespace();
60 public String getOperationName() {
64 public String getNamespace() {
69 protected static XmlElement getRequestElementWithCheck(Document message) throws NetconfDocumentedException {
70 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
71 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
74 protected HandlingPriority canHandle(String operationName, String operationNamespace) {
75 return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
76 ? getHandlingPriority()
77 : HandlingPriority.CANNOT_HANDLE;
80 protected HandlingPriority getHandlingPriority() {
81 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
84 protected String getOperationNamespace() {
85 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
88 protected abstract String getOperationName();
91 public Document handle(Document requestMessage,
92 NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
94 XmlElement requestElement = getRequestElementWithCheck(requestMessage);
96 Document document = XmlUtil.newDocument();
98 XmlElement operationElement = requestElement.getOnlyChildElement();
99 Map<String, Attr> attributes = requestElement.getAttributes();
101 Element response = handle(document, operationElement, subsequentOperation);
102 Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
104 if(XmlElement.fromDomElement(response).hasNamespace()) {
105 rpcReply.appendChild(response);
107 Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
108 NodeList list = response.getChildNodes();
109 while(list.getLength()!=0) {
110 responseNS.appendChild(list.item(0));
112 rpcReply.appendChild(responseNS);
115 for (String attrName : attributes.keySet()) {
116 rpcReply.setAttributeNode((Attr) document.importNode(attributes.get(attrName), true));
118 document.appendChild(rpcReply);
122 protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
123 throws NetconfDocumentedException;
126 public String toString() {
127 final StringBuffer sb = new StringBuffer(getClass().getName());
129 sb.append("{name=").append(getOperationName());
130 } catch(UnsupportedOperationException e) {
133 sb.append(", namespace=").append(getOperationNamespace());
134 sb.append(", session=").append(netconfSessionIdForReporting);
136 return sb.toString();