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 org.opendaylight.controller.netconf.api.NetconfDocumentedException;
13 import org.opendaylight.controller.netconf.impl.osgi.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.HandlingPriority;
17 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
18 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
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;
29 public class DefaultCommit extends AbstractNetconfOperation {
31 private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class);
33 private static final String NOTIFY_ATTR = "notify";
35 private final DefaultCommitNotificationProducer notificationProducer;
36 private final CapabilityProvider cap;
37 private final NetconfOperationRouter operationRouter;
39 public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
40 String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) {
41 super(netconfSessionIdForReporting);
42 this.notificationProducer = notifier;
44 this.operationRouter = netconfOperationRouter;
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 protected String getOperationName() {
62 return XmlNetconfConstants.COMMIT;
66 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
67 Preconditions.checkArgument(!subsequentOperation.isExecutionTermination(),
68 "Subsequent netconf operation expected by %s", this);
70 if (isCommitWithoutNotification(requestMessage)) {
71 logger.debug("Skipping commit notification");
73 // Send commit notification if commit was not issued by persister
74 removePersisterAttributes(requestMessage);
75 Element cfgSnapshot = getConfigSnapshot(operationRouter);
76 logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
77 notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
80 return subsequentOperation.execute(requestMessage);
84 protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
85 throw new UnsupportedOperationException("Never gets called");
89 protected HandlingPriority getHandlingPriority() {
90 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
93 private void removePersisterAttributes(Document message) {
94 message.getDocumentElement().removeAttribute(NOTIFY_ATTR);
97 private boolean isCommitWithoutNotification(Document message) {
98 XmlElement xmlElement = null;
100 xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
101 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
102 } catch (NetconfDocumentedException e) {
103 logger.trace("Commit operation is not valid due to {}",e);
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(
121 getConfigMessage, null);
123 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 return dataElement.getDomElement();