Decouple config and netconf subsystems.
[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 com.google.common.base.Optional;
12 import java.util.Map;
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.controller.netconf.api.xml.XmlNetconfConstants;
18 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperation;
20 import org.opendaylight.controller.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;
25
26 public abstract class AbstractNetconfOperation implements NetconfOperation {
27     private final String netconfSessionIdForReporting;
28
29     protected AbstractNetconfOperation(final String netconfSessionIdForReporting) {
30         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
31     }
32
33     public final String getNetconfSessionIdForReporting() {
34         return netconfSessionIdForReporting;
35     }
36
37     @Override
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());
42     }
43
44     public static final class OperationNameAndNamespace {
45         private final String operationName, namespace;
46         private final XmlElement operationElement;
47
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();
54         }
55
56         public String getOperationName() {
57             return operationName;
58         }
59
60         public String getNamespace() {
61             return namespace;
62         }
63
64         public XmlElement getOperationElement() {
65             return operationElement;
66         }
67     }
68
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);
72     }
73
74     protected HandlingPriority canHandle(final String operationName, final String operationNamespace) {
75         return operationName.equals(getOperationName()) && operationNamespace.equals(getOperationNamespace())
76                 ? getHandlingPriority()
77                 : HandlingPriority.CANNOT_HANDLE;
78     }
79
80     protected HandlingPriority getHandlingPriority() {
81         return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY;
82     }
83
84     protected String getOperationNamespace() {
85         return XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0;
86     }
87
88     protected abstract String getOperationName();
89
90     @Override
91     public Document handle(final Document requestMessage,
92             final NetconfOperationChainedExecution subsequentOperation) throws DocumentedException {
93
94         XmlElement requestElement = getRequestElementWithCheck(requestMessage);
95
96         Document document = XmlUtil.newDocument();
97
98         XmlElement operationElement = requestElement.getOnlyChildElement();
99         Map<String, Attr> attributes = requestElement.getAttributes();
100
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));
103
104         if(XmlElement.fromDomElement(response).hasNamespace()) {
105             rpcReply.appendChild(response);
106         } else {
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));
111             }
112             rpcReply.appendChild(responseNS);
113         }
114
115         for (Attr attribute : attributes.values()) {
116             rpcReply.setAttributeNode((Attr) document.importNode(attribute, true));
117         }
118         document.appendChild(rpcReply);
119         return document;
120     }
121
122     protected abstract Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation)
123             throws DocumentedException;
124
125     @Override
126     public String toString() {
127         final StringBuffer sb = new StringBuffer(getClass().getName());
128         try {
129             sb.append("{name=").append(getOperationName());
130         } catch(UnsupportedOperationException e) {
131             // no problem
132         }
133         sb.append(", namespace=").append(getOperationNamespace());
134         sb.append(", session=").append(netconfSessionIdForReporting);
135         sb.append('}');
136         return sb.toString();
137     }
138 }