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.impl.mapping.operations;
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;
27 import java.io.InputStream;
30 public class DefaultCommit implements NetconfOperationFilter {
32 private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class);
34 private static final String NOTIFY_ATTR = "notify";
36 private final DefaultCommitNotificationProducer notificationProducer;
37 private final CapabilityProvider cap;
38 private final String netconfSessionIdForReporting;
40 public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
41 String netconfSessionIdForReporting) {
42 this.notificationProducer = notifier;
44 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
45 this.getConfigMessage = loadGetConfigMessage();
48 private final Document getConfigMessage;
49 public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
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);
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
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());
77 return filterChain.execute(message, operationRouter);
81 public int getSoringOrder() {
86 public int compareTo(NetconfOperationFilter o) {
87 return Integer.compare(getSoringOrder(), o.getSoringOrder());
90 private boolean canHandle(OperationNameAndNamespace operationNameAndNamespace) {
91 if (operationNameAndNamespace.getOperationName().equals("commit") == false)
93 return operationNameAndNamespace.getNamespace().equals(
94 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
97 private Document removePersisterAttributes(Document message) {
98 final Element documentElement = message.getDocumentElement();
99 documentElement.removeAttribute(NOTIFY_ATTR);
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);
107 String attr = xmlElement.getAttribute(NOTIFY_ATTR);
109 if (attr == null || attr.equals(""))
111 else if (attr.equals(Boolean.toString(false))) {
112 logger.debug("Commit operation received with notify=false attribute {}", message);
119 private Element getConfigSnapshot(NetconfOperationRouter opRouter) throws NetconfDocumentedException {
120 final Document responseDocument = opRouter.onNetconfMessage(getConfigMessage);
122 XmlElement dataElement;
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";
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);
137 return dataElement.getDomElement();
141 public String toString() {
142 return "DefaultCommit{" + netconfSessionIdForReporting + '}';