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 java.io.InputStream;
14 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
15 import org.opendaylight.controller.netconf.api.NetconfOperationRouter;
16 import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer;
17 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
18 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilterChain;
20 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation.OperationNameAndNamespace;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
23 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
24 import org.slf4j.Logger;
25 import org.slf4j.LoggerFactory;
26 import org.w3c.dom.Document;
27 import org.w3c.dom.Element;
29 import com.google.common.collect.Maps;
31 public class DefaultCommit implements NetconfOperationFilter {
33 private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class);
35 private static final String NOTIFY_ATTR = "notify";
37 private final DefaultCommitNotificationProducer notificationProducer;
38 private final CapabilityProvider cap;
39 private final String netconfSessionIdForReporting;
41 public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
42 String netconfSessionIdForReporting) {
43 this.notificationProducer = notifier;
45 this.netconfSessionIdForReporting = netconfSessionIdForReporting;
46 this.getConfigMessage = loadGetConfigMessage();
49 private final Document getConfigMessage;
50 public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
52 private static Document loadGetConfigMessage() {
53 try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
54 return XmlUtil.readXmlToDocument(asStream);
55 } catch (Exception e) {
56 throw new IllegalStateException("Unable to load getConfig message for notifications from "
57 + GET_CONFIG_CANDIDATE_XML_LOCATION);
62 public Document doFilter(Document message, NetconfOperationRouter operationRouter,
63 NetconfOperationFilterChain filterChain) throws NetconfDocumentedException {
64 OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message);
65 if (canHandle(operationNameAndNamespace)) {
66 if (isCommitWithoutNotification(message)) {
67 message = removePersisterAttributes(message);
68 logger.debug("Skipping commit notification");
69 // fall back to filter chain
71 Document innerResult = filterChain.execute(message, operationRouter);
72 Element cfgSnapshot = getConfigSnapshot(operationRouter);
73 logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
74 notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
78 return filterChain.execute(message, operationRouter);
82 public int getSortingOrder() {
87 public int compareTo(NetconfOperationFilter o) {
88 return Integer.compare(getSortingOrder(), o.getSortingOrder());
91 private boolean canHandle(OperationNameAndNamespace operationNameAndNamespace) {
92 if (operationNameAndNamespace.getOperationName().equals("commit") == false)
94 return operationNameAndNamespace.getNamespace().equals(
95 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
98 private Document removePersisterAttributes(Document message) {
99 final Element documentElement = message.getDocumentElement();
100 documentElement.removeAttribute(NOTIFY_ATTR);
104 private boolean isCommitWithoutNotification(Document message) {
105 XmlElement xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
106 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
108 String attr = xmlElement.getAttribute(NOTIFY_ATTR);
110 if (attr == null || attr.equals(""))
112 else if (attr.equals(Boolean.toString(false))) {
113 logger.debug("Commit operation received with notify=false attribute {}", message);
120 private Element getConfigSnapshot(NetconfOperationRouter opRouter) throws NetconfDocumentedException {
121 final Document responseDocument = opRouter.onNetconfMessage(
122 getConfigMessage, null);
124 XmlElement dataElement;
126 XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
127 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
128 dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
129 } catch (IllegalArgumentException e) {
130 final String msg = "Unexpected response from get-config operation";
132 Map<String, String> info = Maps.newHashMap();
133 info.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage());
134 throw new NetconfDocumentedException(msg, e, NetconfDocumentedException.ErrorType.application,
135 NetconfDocumentedException.ErrorTag.operation_failed,
136 NetconfDocumentedException.ErrorSeverity.error, info);
139 return dataElement.getDomElement();
143 public String toString() {
144 return "DefaultCommit{" + netconfSessionIdForReporting + '}';