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.api.NetconfOperationRouter;
15 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
16 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
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;
24 public abstract class AbstractNetconfOperation implements NetconfOperation {
25 private final String netconfSessionIdForReporting;
27 protected AbstractNetconfOperation(String netconfSessionIdForReporting) {
28 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
31 public String getNetconfSessionIdForReporting() {
32 return netconfSessionIdForReporting;
36 public HandlingPriority canHandle(Document message) {
37 OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
38 return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
41 public static class OperationNameAndNamespace {
42 private final String operationName, namespace;
44 public OperationNameAndNamespace(Document message) {
45 XmlElement requestElement = getRequestElementWithCheck(message);
47 XmlElement operationElement = requestElement.getOnlyChildElement();
48 operationName = operationElement.getName();
49 namespace = operationElement.getNamespace();
52 public String getOperationName() {
56 public String getNamespace() {
61 protected static XmlElement getRequestElementWithCheck(Document message) {
62 return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
63 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
66 protected abstract HandlingPriority canHandle(String operationName, String netconfOperationNamespace);
69 public Document handle(Document message, NetconfOperationRouter opRouter) throws NetconfDocumentedException {
71 XmlElement requestElement = getRequestElementWithCheck(message);
73 Document document = XmlUtil.newDocument();
75 XmlElement operationElement = requestElement.getOnlyChildElement();
76 Map<String, Attr> attributes = requestElement.getAttributes();
78 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);
82 rpcReply.appendChild(response);
84 for (String attrName : attributes.keySet()) {
85 rpcReply.setAttribute(attrName, attributes.get(attrName).getNodeValue());
88 document.appendChild(rpcReply);
92 protected abstract Element handle(Document document, XmlElement operationElement, NetconfOperationRouter opRouter)
93 throws NetconfDocumentedException;
96 public String toString() {
97 return getClass() + "{" + netconfSessionIdForReporting + '}';