X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fnetconf%2Fnetconf-impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fnetconf%2Fimpl%2Fmapping%2Foperations%2FDefaultCommit.java;h=8b2c02bcd42cd916c2597b109762b686f68ea6c8;hp=9069e87a935784419e0c573f8721d8e31c1b3af8;hb=a8efbf6623ba1f9ffbb0278672f32e9cfe73a014;hpb=caee336f062eba4909ba53cbaccdde0714236134 diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCommit.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCommit.java index 9069e87a93..8b2c02bcd4 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCommit.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/mapping/operations/DefaultCommit.java @@ -8,41 +8,45 @@ package org.opendaylight.controller.netconf.impl.mapping.operations; +import com.google.common.base.Function; +import com.google.common.base.Preconditions; +import com.google.common.collect.Collections2; +import com.google.common.collect.Sets; import java.io.InputStream; -import java.util.Map; - +import java.util.Set; import org.opendaylight.controller.netconf.api.NetconfDocumentedException; -import org.opendaylight.controller.netconf.api.NetconfOperationRouter; -import org.opendaylight.controller.netconf.impl.DefaultCommitNotificationProducer; -import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider; -import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilter; -import org.opendaylight.controller.netconf.mapping.api.NetconfOperationFilterChain; -import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation.OperationNameAndNamespace; +import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService; +import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants; +import org.opendaylight.controller.netconf.impl.CommitNotifier; +import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter; +import org.opendaylight.controller.netconf.mapping.api.HandlingPriority; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationChainedExecution; +import org.opendaylight.controller.netconf.util.mapping.AbstractNetconfOperation; import org.opendaylight.controller.netconf.util.xml.XmlElement; -import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants; import org.opendaylight.controller.netconf.util.xml.XmlUtil; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev100924.Uri; +import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.netconf.monitoring.rev101004.netconf.state.Capabilities; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; -import com.google.common.collect.Maps; - -public class DefaultCommit implements NetconfOperationFilter { +public class DefaultCommit extends AbstractNetconfOperation { - private static final Logger logger = LoggerFactory.getLogger(DefaultCommit.class); + private static final Logger LOG = LoggerFactory.getLogger(DefaultCommit.class); private static final String NOTIFY_ATTR = "notify"; - private final DefaultCommitNotificationProducer notificationProducer; - private final CapabilityProvider cap; - private final String netconfSessionIdForReporting; + private final CommitNotifier notificationProducer; + private final NetconfMonitoringService cap; + private final NetconfOperationRouter operationRouter; - public DefaultCommit(DefaultCommitNotificationProducer notifier, CapabilityProvider cap, - String netconfSessionIdForReporting) { + public DefaultCommit(CommitNotifier notifier, NetconfMonitoringService cap, + String netconfSessionIdForReporting, NetconfOperationRouter netconfOperationRouter) { + super(netconfSessionIdForReporting); this.notificationProducer = notifier; this.cap = cap; - this.netconfSessionIdForReporting = netconfSessionIdForReporting; + this.operationRouter = netconfOperationRouter; this.getConfigMessage = loadGetConfigMessage(); } @@ -59,58 +63,68 @@ public class DefaultCommit implements NetconfOperationFilter { } @Override - public Document doFilter(Document message, NetconfOperationRouter operationRouter, - NetconfOperationFilterChain filterChain) throws NetconfDocumentedException { - OperationNameAndNamespace operationNameAndNamespace = new OperationNameAndNamespace(message); - if (canHandle(operationNameAndNamespace)) { - if (isCommitWithoutNotification(message)) { - message = removePersisterAttributes(message); - logger.debug("Skipping commit notification"); - // fall back to filter chain - } else { - Document innerResult = filterChain.execute(message, operationRouter); - Element cfgSnapshot = getConfigSnapshot(operationRouter); - logger.debug("Config snapshot retrieved successfully {}", cfgSnapshot); - notificationProducer.sendCommitNotification("ok", cfgSnapshot, cap.getCapabilities()); - return innerResult; - } - } - return filterChain.execute(message, operationRouter); + protected String getOperationName() { + return XmlNetconfConstants.COMMIT; } @Override - public int getSortingOrder() { - return 0; + public Document handle(Document requestMessage, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException { + Preconditions.checkArgument(!subsequentOperation.isExecutionTermination(), + "Subsequent netconf operation expected by %s", this); + + if (isCommitWithoutNotification(requestMessage)) { + LOG.debug("Skipping commit notification"); + } else { + // Send commit notification if commit was not issued by persister + removePersisterAttributes(requestMessage); + Element cfgSnapshot = getConfigSnapshot(operationRouter); + LOG.debug("Config snapshot retrieved successfully {}", cfgSnapshot); + notificationProducer.sendCommitNotification("ok", cfgSnapshot, transformCapabilities(cap.getCapabilities())); + } + + return subsequentOperation.execute(requestMessage); + } + + // FIXME move somewhere to util since this is required also by negotiatiorFactory + public static Set transformCapabilities(final Capabilities capabilities) { + return Sets.newHashSet(Collections2.transform(capabilities.getCapability(), new Function() { + @Override + public String apply(final Uri uri) { + return uri.getValue(); + } + })); } @Override - public int compareTo(NetconfOperationFilter o) { - return Integer.compare(getSortingOrder(), o.getSortingOrder()); + protected Element handle(Document document, XmlElement message, NetconfOperationChainedExecution subsequentOperation) throws NetconfDocumentedException { + throw new UnsupportedOperationException("Never gets called"); } - private boolean canHandle(OperationNameAndNamespace operationNameAndNamespace) { - if (operationNameAndNamespace.getOperationName().equals("commit") == false) - return false; - return operationNameAndNamespace.getNamespace().equals( - XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); + @Override + protected HandlingPriority getHandlingPriority() { + return HandlingPriority.HANDLE_WITH_DEFAULT_PRIORITY.increasePriority(1); } - private Document removePersisterAttributes(Document message) { - final Element documentElement = message.getDocumentElement(); - documentElement.removeAttribute(NOTIFY_ATTR); - return message; + private void removePersisterAttributes(Document message) { + message.getDocumentElement().removeAttribute(NOTIFY_ATTR); } private boolean isCommitWithoutNotification(Document message) { - XmlElement xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(), - XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); + XmlElement xmlElement = null; + try { + xmlElement = XmlElement.fromDomElementWithExpected(message.getDocumentElement(), + XmlNetconfConstants.RPC_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); + } catch (NetconfDocumentedException e) { + LOG.trace("Commit operation is not valid due to ",e); + return false; + } String attr = xmlElement.getAttribute(NOTIFY_ATTR); - if (attr == null || attr.equals("")) + if (attr == null || attr.equals("")){ return false; - else if (attr.equals(Boolean.toString(false))) { - logger.debug("Commit operation received with notify=false attribute {}", message); + } else if (attr.equals(Boolean.toString(false))) { + LOG.debug("Commit operation received with notify=false attribute {}", message); return true; } else { return false; @@ -122,25 +136,10 @@ public class DefaultCommit implements NetconfOperationFilter { getConfigMessage, null); XmlElement dataElement; - try { - XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(), - XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); - dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY); - } catch (IllegalArgumentException e) { - final String msg = "Unexpected response from get-config operation"; - logger.warn(msg, e); - Map info = Maps.newHashMap(); - info.put(NetconfDocumentedException.ErrorTag.operation_failed.toString(), e.getMessage()); - throw new NetconfDocumentedException(msg, e, NetconfDocumentedException.ErrorType.application, - NetconfDocumentedException.ErrorTag.operation_failed, - NetconfDocumentedException.ErrorSeverity.error, info); - } - + XmlElement xmlElement = XmlElement.fromDomElementWithExpected(responseDocument.getDocumentElement(), + XmlNetconfConstants.RPC_REPLY_KEY, XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0); + dataElement = xmlElement.getOnlyChildElement(XmlNetconfConstants.DATA_KEY); return dataElement.getDomElement(); } - @Override - public String toString() { - return "DefaultCommit{" + netconfSessionIdForReporting + '}'; - } }