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 //TODO make commit notification optional
41 private final CommitNotifier notificationProducer;
42 private final NetconfMonitoringService cap;
43 private final NetconfOperationRouter operationRouter;
45 public DefaultCommit(CommitNotifier notifier, NetconfMonitoringService cap,
46 String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) {
47 super(netconfSessionIdForReporting);
48 this.notificationProducer = notifier;
50 this.operationRouter = netconfOperationRouter;
51 this.getConfigMessage = loadGetConfigMessage();
54 private final Document getConfigMessage;
55 public static final String GET_CONFIG_CANDIDATE_XML_LOCATION = "/getConfig_candidate.xml";
57 private static Document loadGetConfigMessage() {
58 try (InputStream asStream = DefaultCommit.class.getResourceAsStream(GET_CONFIG_CANDIDATE_XML_LOCATION)) {
59 return XmlUtil.readXmlToDocument(asStream);
60 } catch (Exception e) {
61 throw new IllegalStateException("Unable to load getConfig message for notifications from "
62 + GET_CONFIG_CANDIDATE_XML_LOCATION);
67 protected String getOperationName() {
68 return XmlNetconfConstants.COMMIT;
72 public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
73 Preconditions.checkArgument(!subsequentOperation.isExecutionTermination(),
74 "Subsequent netconf operation expected by %s", this);
76 if (isCommitWithoutNotification(requestMessage)) {
77 LOG.debug("Skipping commit notification");
79 // Send commit notification if commit was not issued by persister
80 removePersisterAttributes(requestMessage);
81 Element cfgSnapshot = getConfigSnapshot(operationRouter);
82 LOG.debug("Config snapshot retrieved successfully {}", cfgSnapshot);
83 notificationProducer.sendCommitNotification("ok", cfgSnapshot, transformCapabilities(cap.getCapabilities()));
86 return subsequentOperation.execute(requestMessage);
89 // FIXME move somewhere to util since this is required also by negotiatiorFactory
90 public static Set<String> transformCapabilities(final Capabilities capabilities) {
91 return Sets.newHashSet(Collections2.transform(capabilities.getCapability(), new Function<Uri, String>() {
93 public String apply(final Uri uri) {
94 return uri.getValue();
100 protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException {
101 throw new UnsupportedOperationException("Never gets called");
105 protected HandlingPriority getHandlingPriority() {
106 return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1);
109 private void removePersisterAttributes(Document message) {
110 message.getDocumentElement().removeAttribute(NOTIFY_ATTR);
113 private boolean isCommitWithoutNotification(Document message) {
114 XmlElement xmlElement = null;
116 xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(),
117 XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
118 } catch (NetconfDocumentedException e) {
119 LOG.trace("Commit operation is not valid due to ",e);
123 String attr = xmlElement.getAttribute(NOTIFY_ATTR);
125 if (attr == null || attr.equals("")){
127 } else if (attr.equals(Boolean.toString(false))) {
128 LOG.debug("Commit operation received with notify=false attribute {}", message);
135 private Element getConfigSnapshot(NetconfOperationRouter opRouter) throws NetconfDocumentedException {
136 final Document responseDocument = opRouter.onNetconfMessage(
137 getConfigMessage, null);
139 XmlElement dataElement;
140 XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(),
141 XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0);
142 dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY);
143 return dataElement.getDomElement();