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.base.Preconditions;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.controller.netconf.impl.osgi.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.HandlingPriority;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
20 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
21 import org.opendaylight.controller.netconf.util.xml.XmlElement;
22 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
28 import java.io.InputStream;
30 public class DefaultCommit extends AbstractNetconfOperation {
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 NetconfOperationRouter operationRouter;
40 public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
41 String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) {
42 super(netconfSessionIdForReporting);
43 this.notificationProducer = notifier;
45 this.operationRouter = netconfOperationRouter;
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 protected String getOperationName() {
63 return XmlNetconfConstants.COMMIT;
67 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
68 Preconditions.checkArgument(!subsequentOperation.isExecutionTermination(),
69 "Subsequent netconf operation expected by %s", this);
71 if (isCommitWithoutNotification(requestMessage)) {
72 logger.debug("Skipping commit notification");
74 // Send commit notification if commit was not issued by persister
75 removePersisterAttributes(requestMessage);
76 Element cfgSnapshot = getConfigSnapshot(operationRouter);
77 logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
78 notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
81 return subsequentOperation.execute(requestMessage);
85 protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
86 throw new UnsupportedOperationException("Never gets called");
90 protected HandlingPriority getHandlingPriority() {
91 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
94 private void removePersisterAttributes(Document message) {
95 message.getDocumentElement().removeAttribute(NOTIFY_ATTR);
98 private boolean isCommitWithoutNotification(Document message) {
99 XmlElement xmlElement = null;
101 xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
102 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
103 } catch (NetconfDocumentedException e) {
104 logger.trace("Commit operation is not valid due to {}",e);
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;
125 XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
126 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
127 dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
128 return dataElement.getDomElement();