Initial code drop of netconf protocol implementation
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / mapping / operations / DefaultCommit.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.impl.mapping.operations;
10
11 import com.google.common.collect.Maps;
12 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
14 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
15 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
16 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter;
17 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilterChain;
18 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation.OperationNameAndNamespace;
19 import org.opendaylight.controller.netconf.util.xml.XmlElement;
20 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
21 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24 import org.w3c.dom.Document;
25 import org.w3c.dom.Element;
26
27 import java.io.InputStream;
28 import java.util.Map;
29
30 public class DefaultCommit implements NetconfOperationFilter {
31
32     private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class);
33
34     private static final String NOTIFY_ATTR = "notify";
35
36     private final DefaultCommitNotificationProducer notificationProducer;
37     private final CapabilityProvider cap;
38     private final String netconfSessionIdForReporting;
39
40     public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
41             String netconfSessionIdForReporting) {
42         this.notificationProducer = notifier;
43         this.cap = cap;
44         this.netconfSessionIdForReporting = netconfSessionIdForReporting;
45         this.getConfigMessage = loadGetConfigMessage();
46     }
47
48     private final Document getConfigMessage;
49     public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
50
51     private static Document loadGetConfigMessage() {
52         try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
53             return XmlUtil.readXmlToDocument(asStream);
54         } catch (Exception e) {
55             throw new IllegalStateException("Unable to load getConfig message for notifications from "
56                     + GET_CONFIG_CANDIDATE_XML_LOCATION);
57         }
58     }
59
60     @Override
61     public Document doFilter(Document message, NetconfOperationRouter operationRouter,
62             NetconfOperationFilterChain filterChain) throws NetconfDocumentedException {
63         OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
64         if (canHandle(operationNameAndNamespace)) {
65             if (isCommitWithoutNotification(message)) {
66                 message = removePersisterAttributes(message);
67                 logger.debug("Skipping commit notification");
68                 // fall back to filter chain
69             } else {
70                 Document innerResult = filterChain.execute(message, operationRouter);
71                 Element cfgSnapshot = getConfigSnapshot(operationRouter);
72                 logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
73                 notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
74                 return innerResult;
75             }
76         }
77         return filterChain.execute(message, operationRouter);
78     }
79
80     @Override
81     public int getSoringOrder() {
82         return 0;
83     }
84
85     @Override
86     public int compareTo(NetconfOperationFilter o) {
87         return Integer.compare(getSoringOrder(), o.getSoringOrder());
88     }
89
90     private boolean canHandle(OperationNameAndNamespace operationNameAndNamespace) {
91         if (operationNameAndNamespace.getOperationName().equals("commit") == false)
92             return false;
93         return operationNameAndNamespace.getNamespace().equals(
94                 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
95     }
96
97     private Document removePersisterAttributes(Document message) {
98         final Element documentElement = message.getDocumentElement();
99         documentElement.removeAttribute(NOTIFY_ATTR);
100         return message;
101     }
102
103     private boolean isCommitWithoutNotification(Document message) {
104         XmlElement xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
105                 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
106
107         String attr = xmlElement.getAttribute(NOTIFY_ATTR);
108
109         if (attr == null || attr.equals(""))
110             return false;
111         else if (attr.equals(Boolean.toString(false))) {
112             logger.debug("Commit operation received with notify=false attribute {}", message);
113             return true;
114         } else {
115             return false;
116         }
117     }
118
119     private Element getConfigSnapshot(NetconfOperationRouter opRouter) throws NetconfDocumentedException {
120         final Document responseDocument = opRouter.onNetconfMessage(getConfigMessage);
121
122         XmlElement dataElement;
123         try {
124             XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
125                     XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
126             dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
127         } catch (IllegalArgumentException e) {
128             final String msg = "Unexpected response from get-config operation";
129             logger.warn(msg, e);
130             Map<String, String> info = Maps.newHashMap();
131             info.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage());
132             throw new NetconfDocumentedException(msg, e, NetconfDocumentedException.ErrorType.application,
133                     NetconfDocumentedException.ErrorTag.operation_failed,
134                     NetconfDocumentedException.ErrorSeverity.error, info);
135         }
136
137         return dataElement.getDomElement();
138     }
139
140     @Override
141     public String toString() {
142         return "DefaultCommit{" + netconfSessionIdForReporting + '}';
143     }
144 }