Bug 977: Return RpcError result on neconf failure
[controller.git] / opendaylight / netconf / netconf-util / src / main / java / org / opendaylight / controller / netconf / util / mapping / AbstractNetconfOperation.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.util.mapping;
10
11 import java.util.Map;
12
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.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;
26
27 import com.google.common.base.Optional;
28
29 public abstract class AbstractNetconfOperation implements NetconfOperation {
30     private final String netconfSessionIdForReporting;
31     private static final Logger logger = LoggerFactory.getLogger(AbstractNetconfOperation.class);
32
33     protected AbstractNetconfOperation(String netconfSessionIdForReporting) {
34         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
35     }
36
37     public final String getNetconfSessionIdForReporting() {
38         return netconfSessionIdForReporting;
39     }
40
41     @Override
42     public HandlingPriority canHandle(Document message) throws NetconfDocumentedException {
43         OperationNameAndNamespace operationNameAndNamespace = null;
44         operationNameAndNamespace = new OperationNameAndNamespace(message);
45         return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
46     }
47
48     public static final class OperationNameAndNamespace {
49         private final String operationName, namespace;
50         private final XmlElement operationElement;
51
52         public OperationNameAndNamespace(Document message) throws NetconfDocumentedException {
53             XmlElement requestElement = null;
54             requestElement = getRequestElementWithCheck(message);
55             operationElement = requestElement.getOnlyChildElement();
56             operationName = operationElement.getName();
57             namespace = operationElement.getNamespace();
58         }
59
60         public String getOperationName() {
61             return operationName;
62         }
63
64         public String getNamespace() {
65             return namespace;
66         }
67
68         public XmlElement getOperationElement() {
69             return operationElement;
70         }
71     }
72
73     protected static XmlElement getRequestElementWithCheck(Document message) throws NetconfDocumentedException {
74         return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
75                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
76     }
77
78     protected HandlingPriority canHandle(String operationName, String operationNamespace) {
79         return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
80                 ? getHandlingPriority()
81                 : HandlingPriority.CANNOT_HANDLE;
82     }
83
84     protected HandlingPriority getHandlingPriority() {
85         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
86     }
87
88     protected String getOperationNamespace() {
89         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
90     }
91
92     protected abstract String getOperationName();
93
94     @Override
95     public Document handle(Document requestMessage,
96             NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
97
98         XmlElement requestElement = getRequestElementWithCheck(requestMessage);
99
100         Document document = XmlUtil.newDocument();
101
102         XmlElement operationElement = requestElement.getOnlyChildElement();
103         Map<String, Attr> attributes = requestElement.getAttributes();
104
105         Element response = handle(document, operationElement, subsequentOperation);
106         Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
107
108         if(XmlElement.fromDomElement(response).hasNamespace()) {
109             rpcReply.appendChild(response);
110         } else {
111             Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
112             NodeList list = response.getChildNodes();
113             while(list.getLength()!=0) {
114                 responseNS.appendChild(list.item(0));
115             }
116             rpcReply.appendChild(responseNS);
117         }
118
119         for (String attrName : attributes.keySet()) {
120             rpcReply.setAttributeNode((Attr) document.importNode(attributes.get(attrName), true));
121         }
122         document.appendChild(rpcReply);
123         return document;
124     }
125
126     protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
127             throws NetconfDocumentedException;
128
129     @Override
130     public String toString() {
131         final StringBuffer sb = new StringBuffer(getClass().getName());
132         try {
133             sb.append("{name=").append(getOperationName());
134         } catch(UnsupportedOperationException e) {
135             // no problem
136         }
137         sb.append(", namespace=").append(getOperationNamespace());
138         sb.append(", session=").append(netconfSessionIdForReporting);
139         sb.append('}');
140         return sb.toString();
141     }
142 }