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.netconf.util.mapping;
11 import com.google.common.base.Optional;
13 import org.opendaylight.controller.config.util.xml.DocumentedException;
14 import org.opendaylight.controller.config.util.xml.XmlElement;
15 import org.opendaylight.controller.config.util.xml.XmlMappingConstants;
16 import org.opendaylight.controller.config.util.xml.XmlUtil;
17 import org.opendaylight.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.netconf.mapping.api.HandlingPriority;
19 import org.opendaylight.netconf.mapping.api.NetconfOperation;
20 import org.opendaylight.netconf.mapping.api.NetconfOperationChainedExecution;
21 import org.w3c.dom.Attr;
22 import org.w3c.dom.Document;
23 import org.w3c.dom.Element;
24 import org.w3c.dom.NodeList;
26 public abstract class AbstractNetconfOperation implements NetconfOperation {
27 private final String netconfSessionIdForReporting;
29 protected AbstractNetconfOperation(final String netconfSessionIdForReporting) {
30 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
33 public final String getNetconfSessionIdForReporting() {
34 return netconfSessionIdForReporting;
38 public HandlingPriority canHandle(final Document message) throws DocumentedException {
39 OperationNameAndNamespace operationNameAndNamespace = null;
40 operationNameAndNamespace = new OperationNameAndNamespace(message);
41 return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
44 public static final class OperationNameAndNamespace {
45 private final String operationName, namespace;
46 private final XmlElement operationElement;
48 public OperationNameAndNamespace(final Document message) throws DocumentedException {
49 XmlElement requestElement = null;
50 requestElement = getRequestElementWithCheck(message);
51 operationElement = requestElement.getOnlyChildElement();
52 operationName = operationElement.getName();
53 namespace = operationElement.getNamespace();
56 public String getOperationName() {
60 public String getNamespace() {
64 public XmlElement getOperationElement() {
65 return operationElement;
69 protected static XmlElement getRequestElementWithCheck(final Document message) throws DocumentedException {
70 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
71 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
74 protected HandlingPriority canHandle(final String operationName, final 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(final Document requestMessage,
92 final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
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, XmlMappingConstants.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 (Attr attribute : attributes.values()) {
116 rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
118 document.appendChild(rpcReply);
122 protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
123 throws DocumentedException;
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();