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.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.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.base.Preconditions;
30 import com.google.common.collect.Maps;
32 public class DefaultCommit extends AbstractNetconfOperation {
34 private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class);
36 private static final String NOTIFY_ATTR = "notify";
38 private final DefaultCommitNotificationProducer notificationProducer;
39 private final CapabilityProvider cap;
40 private final NetconfOperationRouter operationRouter;
42 public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap,
43 String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) {
44 super(netconfSessionIdForReporting);
45 this.notificationProducer = notifier;
47 this.operationRouter = netconfOperationRouter;
48 this.getConfigMessage = loadGetConfigMessage();
51 private final Document getConfigMessage;
52 public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
54 private static Document loadGetConfigMessage() {
55 try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
56 return XmlUtil.readXmlToDocument(asStream);
57 } catch (Exception e) {
58 throw new IllegalStateException("Unable to load getConfig message for notifications from "
59 + GET_CONFIG_CANDIDATE_XML_LOCATION);
64 protected String getOperationName() {
65 return XmlNetconfConstants.COMMIT;
69 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
70 Preconditions.checkArgument(subsequentOperation.isExecutionTermination() == false,
71 "Subsequent netconf operation expected by %s", this);
73 if (isCommitWithoutNotification(requestMessage)) {
74 logger.debug("Skipping commit notification");
76 // Send commit notification if commit was not issued by persister
77 requestMessage = removePersisterAttributes(requestMessage);
78 Element cfgSnapshot = getConfigSnapshot(operationRouter);
79 logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
80 notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities());
83 return subsequentOperation.execute(requestMessage);
87 protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
88 throw new UnsupportedOperationException("Never gets called");
92 protected HandlingPriority getHandlingPriority() {
93 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
96 private Document removePersisterAttributes(Document message) {
97 final Element documentElement = message.getDocumentElement();
98 documentElement.removeAttribute(NOTIFY_ATTR);
102 private boolean isCommitWithoutNotification(Document message) {
103 XmlElement xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
104 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
106 String attr = xmlElement.getAttribute(NOTIFY_ATTR);
108 if (attr == null || attr.equals(""))
110 else if (attr.equals(Boolean.toString(false))) {
111 logger.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;
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();