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;
12 import java.io.InputStream;
13 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
14 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
15 import org.opendaylight.controller.netconf.impl.CommitNotifier;
16 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
17 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
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 public class DefaultCommit extends AbstractNetconfOperation {
30 private static final Logger LOG = LoggerFactory.getLogger(DefaultCommit.class);
32 private static final String NOTIFY_ATTR = "notify";
34 private final CommitNotifier notificationProducer;
35 private final CapabilityProvider cap;
36 private final NetconfOperationRouter operationRouter;
38 public DefaultCommit(CommitNotifier notifier, CapabilityProvider cap,
39 String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) {
40 super(netconfSessionIdForReporting);
41 this.notificationProducer = notifier;
43 this.operationRouter = netconfOperationRouter;
44 this.getConfigMessage = loadGetConfigMessage();
47 private final Document getConfigMessage;
48 public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
50 private static Document loadGetConfigMessage() {
51 try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
52 return XmlUtil.readXmlToDocument(asStream);
53 } catch (Exception e) {
54 throw new IllegalStateException("Unable to load getConfig message for notifications from "
55 + GET_CONFIG_CANDIDATE_XML_LOCATION);
60 protected String getOperationName() {
61 return XmlNetconfConstants.COMMIT;
65 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
66 Preconditions.checkArgument(!subsequentOperation.isExecutionTermination(),
67 "Subsequent netconf operation expected by %s", this);
69 if (isCommitWithoutNotification(requestMessage)) {
70 LOG.debug("Skipping commit notification");
72 // Send commit notification if commit was not issued by persister
73 removePersisterAttributes(requestMessage);
74 Element cfgSnapshot = getConfigSnapshot(operationRouter);
75 LOG.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
76 notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
79 return subsequentOperation.execute(requestMessage);
83 protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
84 throw new UnsupportedOperationException("Never gets called");
88 protected HandlingPriority getHandlingPriority() {
89 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
92 private void removePersisterAttributes(Document message) {
93 message.getDocumentElement().removeAttribute(NOTIFY_ATTR);
96 private boolean isCommitWithoutNotification(Document message) {
97 XmlElement xmlElement = null;
99 xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
100 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
101 } catch (NetconfDocumentedException e) {
102 LOG.trace("Commit operation is not valid due to ",e);
106 String attr = xmlElement.getAttribute(NOTIFY_ATTR);
108 if (attr == null || attr.equals("")){
110 } else if (attr.equals(Boolean.toString(false))) {
111 LOG.debug("Commit operation received with notify=false attribute {}", message);
118 private Element getConfigSnapshot(NetconfOperationRouter opRouter) throws NetconfDocumentedException {
119 final Document responseDocument = opRouter.onNetconfMessage(
120 getConfigMessage, null);
122 XmlElement dataElement;
123 XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
124 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
125 dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
126 return dataElement.getDomElement();