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 com.google.common.base.Optional;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
16 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
17 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
18 import org.opendaylight.controller.netconf.util.xml.XmlElement;
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 public abstract class AbstractNetconfOperation implements NetconfOperation {
26 private final String netconfSessionIdForReporting;
28 protected AbstractNetconfOperation(final String netconfSessionIdForReporting) {
29 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
32 public final String getNetconfSessionIdForReporting() {
33 return netconfSessionIdForReporting;
37 public HandlingPriority canHandle(final Document message) throws NetconfDocumentedException {
38 OperationNameAndNamespace operationNameAndNamespace = null;
39 operationNameAndNamespace = new OperationNameAndNamespace(message);
40 return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
43 public static final class OperationNameAndNamespace {
44 private final String operationName, namespace;
45 private final XmlElement operationElement;
47 public OperationNameAndNamespace(final Document message) throws NetconfDocumentedException {
48 XmlElement requestElement = null;
49 requestElement = getRequestElementWithCheck(message);
50 operationElement = requestElement.getOnlyChildElement();
51 operationName = operationElement.getName();
52 namespace = operationElement.getNamespace();
55 public String getOperationName() {
59 public String getNamespace() {
63 public XmlElement getOperationElement() {
64 return operationElement;
68 protected static XmlElement getRequestElementWithCheck(final Document message) throws NetconfDocumentedException {
69 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
70 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
73 protected HandlingPriority canHandle(final String operationName, final String operationNamespace) {
74 return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
75 ? getHandlingPriority()
76 : HandlingPriority.CANNOT_HANDLE;
79 protected HandlingPriority getHandlingPriority() {
80 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
83 protected String getOperationNamespace() {
84 return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
87 protected abstract String getOperationName();
90 public Document handle(final Document requestMessage,
91 final NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
93 XmlElement requestElement = getRequestElementWithCheck(requestMessage);
95 Document document = XmlUtil.newDocument();
97 XmlElement operationElement = requestElement.getOnlyChildElement();
98 Map<String, Attr> attributes = requestElement.getAttributes();
100 Element response = handle(document, operationElement, subsequentOperation);
101 Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
103 if(XmlElement.fromDomElement(response).hasNamespace()) {
104 rpcReply.appendChild(response);
106 Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
107 NodeList list = response.getChildNodes();
108 while(list.getLength()!=0) {
109 responseNS.appendChild(list.item(0));
111 rpcReply.appendChild(responseNS);
114 for (Attr attribute : attributes.values()) {
115 rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
117 document.appendChild(rpcReply);
121 protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
122 throws NetconfDocumentedException;
125 public String toString() {
126 final StringBuffer sb = new StringBuffer(getClass().getName());
128 sb.append("{name=").append(getOperationName());
129 } catch(UnsupportedOperationException e) {
132 sb.append(", namespace=").append(getOperationNamespace());
133 sb.append(", session=").append(netconfSessionIdForReporting);
135 return sb.toString();