Merge "BUG-731: do not catch Throwable"
[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.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.w3c.dom.Attr;
21 import org.w3c.dom.Document;
22 import org.w3c.dom.Element;
23 import org.w3c.dom.NodeList;
24
25 import com.google.common.base.Optional;
26
27 public abstract class AbstractNetconfOperation implements NetconfOperation {
28     private final String netconfSessionIdForReporting;
29
30     protected AbstractNetconfOperation(String netconfSessionIdForReporting) {
31         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
32     }
33
34     public final String getNetconfSessionIdForReporting() {
35         return netconfSessionIdForReporting;
36     }
37
38     @Override
39     public HandlingPriority canHandle(Document message) {
40         OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
41         return canHandle(operationNameAndNamespace.getOperationName(), operationNameAndNamespace.getNamespace());
42     }
43
44     public static final class OperationNameAndNamespace {
45         private final String operationName, namespace;
46
47         public OperationNameAndNamespace(Document message) {
48             XmlElement requestElement = getRequestElementWithCheck(message);
49
50             XmlElement operationElement = requestElement.getOnlyChildElement();
51             operationName = operationElement.getName();
52             namespace = operationElement.getNamespace();
53         }
54
55         public String getOperationName() {
56             return operationName;
57         }
58
59         public String getNamespace() {
60             return namespace;
61         }
62     }
63
64     protected static XmlElement getRequestElementWithCheck(Document message) {
65         return XmlElement.fromDomElementWithExpected(message.getDocumentElement(), XmlNetconfConstants.RPC_KEY,
66                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
67     }
68
69     protected HandlingPriority canHandle(String operationName, String operationNamespace) {
70         return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
71                 ? getHandlingPriority()
72                 : HandlingPriority.CANNOT_HANDLE;
73     }
74
75     protected HandlingPriority getHandlingPriority() {
76         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
77     }
78
79     protected String getOperationNamespace() {
80         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
81     }
82
83     protected abstract String getOperationName();
84
85     @Override
86     public Document handle(Document requestMessage,
87             NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
88
89         XmlElement requestElement = getRequestElementWithCheck(requestMessage);
90
91         Document document = XmlUtil.newDocument();
92
93         XmlElement operationElement = requestElement.getOnlyChildElement();
94         Map<String, Attr> attributes = requestElement.getAttributes();
95
96         Element response = handle(document, operationElement, subsequentOperation);
97         Element rpcReply = XmlUtil.createElement(document, XmlNetconfConstants.RPC_REPLY_KEY, Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
98
99         if(XmlElement.fromDomElement(response).hasNamespace()) {
100             rpcReply.appendChild(response);
101         } else {
102             Element responseNS = XmlUtil.createElement(document, response.getNodeName(), Optional.of(XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0));
103             NodeList list = response.getChildNodes();
104             while(list.getLength()!=0) {
105                 responseNS.appendChild(list.item(0));
106             }
107             rpcReply.appendChild(responseNS);
108         }
109
110         for (String attrName : attributes.keySet()) {
111             rpcReply.setAttributeNode((Attr) document.importNode(attributes.get(attrName), true));
112         }
113         document.appendChild(rpcReply);
114         return document;
115     }
116
117     protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
118             throws NetconfDocumentedException;
119
120     @Override
121     public String toString() {
122         final StringBuffer sb = new StringBuffer(getClass().getName());
123         try {
124             sb.append("{name=").append(getOperationName());
125         } catch(UnsupportedOperationException e) {
126             // no problem
127         }
128         sb.append(", namespace=").append(getOperationNamespace());
129         sb.append(", session=").append(netconfSessionIdForReporting);
130         sb.append('}');
131         return sb.toString();
132     }
133 }