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.Function;
12 import com.google.common.base.Preconditions;
13 import com.google.common.collect.Collections2;
14 import com.google.common.collect.Sets;
15 import java.io.InputStream;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService;
19 import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants;
20 import org.opendaylight.controller.netconf.impl.CommitNotifier;
21 import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter;
22 import org.opendaylight.controller.netconf.mapping.api.HandlingPriority;
23 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution;
24 import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation;
25 import org.opendaylight.controller.netconf.util.xml.XmlElement;
26 import org.opendaylight.controller.netconf.util.xml.XmlUtil;
27 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri;
28 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 import org.w3c.dom.Document;
32 import org.w3c.dom.Element;
34 public class DefaultCommit extends AbstractNetconfOperation {
36 private static final Logger LOG = LoggerFactory.getLogger(DefaultCommit.class);
38 private static final String NOTIFY_ATTR = "notify";
40 private final CommitNotifier notificationProducer;
41 private final NetconfMonitoringService cap;
42 private final NetconfOperationRouter operationRouter;
44 public DefaultCommit(CommitNotifier notifier, NetconfMonitoringService cap,
45 String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) {
46 super(netconfSessionIdForReporting);
47 this.notificationProducer = notifier;
49 this.operationRouter = netconfOperationRouter;
50 this.getConfigMessage = loadGetConfigMessage();
53 private final Document getConfigMessage;
54 public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
56 private static Document loadGetConfigMessage() {
57 try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
58 return XmlUtil.readXmlToDocument(asStream);
59 } catch (Exception e) {
60 throw new IllegalStateException("Unable to load getConfig message for notifications from "
61 + GET_CONFIG_CANDIDATE_XML_LOCATION);
66 protected String getOperationName() {
67 return XmlNetconfConstants.COMMIT;
71 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
72 Preconditions.checkArgument(!subsequentOperation.isExecutionTermination(),
73 "Subsequent netconf operation expected by %s", this);
75 if (isCommitWithoutNotification(requestMessage)) {
76 LOG.debug("Skipping commit notification");
78 // Send commit notification if commit was not issued by persister
79 removePersisterAttributes(requestMessage);
80 Element cfgSnapshot = getConfigSnapshot(operationRouter);
81 LOG.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
82 notificationProducer.sendCommitNotification("ok", cfgSnapshot, transformCapabilities(cap.getCapabilities()));
85 return subsequentOperation.execute(requestMessage);
88 // FIXME move somewhere to util since this is required also by negotiatiorFactory
89 public static Set<String> transformCapabilities(final Capabilities capabilities) {
90 return Sets.newHashSet(Collections2.transform(capabilities.getCapability(), new Function<Uri, String>() {
92 public String apply(final Uri uri) {
93 return uri.getValue();
99 protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
100 throw new UnsupportedOperationException("Never gets called");
104 protected HandlingPriority getHandlingPriority() {
105 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
108 private void removePersisterAttributes(Document message) {
109 message.getDocumentElement().removeAttribute(NOTIFY_ATTR);
112 private boolean isCommitWithoutNotification(Document message) {
113 XmlElement xmlElement = null;
115 xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
116 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
117 } catch (NetconfDocumentedException e) {
118 LOG.trace("Commit operation is not valid due to ",e);
122 String attr = xmlElement.getAttribute(NOTIFY_ATTR);
124 if (attr == null || attr.equals("")){
126 } else if (attr.equals(Boolean.toString(false))) {
127 LOG.debug("Commit operation received with notify=false attribute {}", message);
134 private Element getConfigSnapshot(NetconfOperationRouter opRouter) throws NetconfDocumentedException {
135 final Document responseDocument = opRouter.onNetconfMessage(
136 getConfigMessage, null);
138 XmlElement dataElement;
139 XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
140 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
141 dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
142 return dataElement.getDomElement();