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%2FNetconfServerSessionNegotiatorFactory.java;h=cf489608cab7c2a5ff36e2253c7fcc9516a296e4;hp=e052f61cc97dd7dce5a1e048f8f7f9938667df09;hb=c31509c7a6630e54a9f9749a643fed5e1a1ad380;hpb=082d7ba433b85d5810c50f624d2691088336e66a diff --git a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorFactory.java b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorFactory.java index e052f61cc9..cf489608ca 100644 --- a/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorFactory.java +++ b/opendaylight/netconf/netconf-impl/src/main/java/org/opendaylight/controller/netconf/impl/NetconfServerSessionNegotiatorFactory.java @@ -9,94 +9,119 @@ package org.opendaylight.controller.netconf.impl; import com.google.common.base.Preconditions; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; import io.netty.channel.Channel; import io.netty.util.Timer; import io.netty.util.concurrent.Promise; +import java.util.Set; +import org.opendaylight.controller.netconf.api.NetconfDocumentedException; import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences; -import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider; -import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationServiceFactoryListener; -import org.opendaylight.controller.netconf.util.NetconfUtil; +import org.opendaylight.controller.netconf.api.monitoring.NetconfMonitoringService; +import org.opendaylight.controller.netconf.api.xml.XmlNetconfConstants; +import org.opendaylight.controller.netconf.impl.mapping.operations.DefaultCommit; +import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouter; +import org.opendaylight.controller.netconf.impl.osgi.NetconfOperationRouterImpl; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationService; +import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceFactory; import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage; -import org.opendaylight.controller.netconf.util.xml.XMLNetconfUtil; -import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants; -import org.opendaylight.controller.netconf.util.xml.XmlUtil; import org.opendaylight.protocol.framework.SessionListenerFactory; import org.opendaylight.protocol.framework.SessionNegotiator; import org.opendaylight.protocol.framework.SessionNegotiatorFactory; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.Node; - -import javax.xml.xpath.XPathConstants; -import javax.xml.xpath.XPathExpression; -import java.io.InputStream; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory { - public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml"; + public static final Set DEFAULT_BASE_CAPABILITIES = ImmutableSet.of( + XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0, + XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1, + XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0 + ); private final Timer timer; - private static final Document helloMessageTemplate = loadHelloMessageTemplate(); private final SessionIdProvider idProvider; - private final NetconfOperationServiceFactoryListener factoriesListener; + private final NetconfOperationServiceFactory aggregatedOpService; private final long connectionTimeoutMillis; + private final CommitNotifier commitNotificationProducer; + private final NetconfMonitoringService monitoringService; + private static final Logger LOG = LoggerFactory.getLogger(NetconfServerSessionNegotiatorFactory.class); + private final Set baseCapabilities; + + // TODO too many params, refactor + public NetconfServerSessionNegotiatorFactory(final Timer timer, final NetconfOperationServiceFactory netconfOperationProvider, + final SessionIdProvider idProvider, final long connectionTimeoutMillis, + final CommitNotifier commitNot, + final NetconfMonitoringService monitoringService) { + this(timer, netconfOperationProvider, idProvider, connectionTimeoutMillis, commitNot, monitoringService, DEFAULT_BASE_CAPABILITIES); + } - public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener, - SessionIdProvider idProvider, long connectionTimeoutMillis) { + // TODO too many params, refactor + public NetconfServerSessionNegotiatorFactory(final Timer timer, final NetconfOperationServiceFactory netconfOperationProvider, + final SessionIdProvider idProvider, final long connectionTimeoutMillis, + final CommitNotifier commitNot, + final NetconfMonitoringService monitoringService, final Set baseCapabilities) { this.timer = timer; - this.factoriesListener = factoriesListener; + this.aggregatedOpService = netconfOperationProvider; this.idProvider = idProvider; this.connectionTimeoutMillis = connectionTimeoutMillis; + this.commitNotificationProducer = commitNot; + this.monitoringService = monitoringService; + this.baseCapabilities = validateBaseCapabilities(baseCapabilities); } - private static Document loadHelloMessageTemplate() { - InputStream resourceAsStream = NetconfServerSessionNegotiatorFactory.class - .getResourceAsStream(SERVER_HELLO_XML_LOCATION); - Preconditions.checkNotNull(resourceAsStream, "Unable to load server hello message blueprint from %s", - SERVER_HELLO_XML_LOCATION); - return NetconfUtil.createMessage(resourceAsStream).getDocument(); + private ImmutableSet validateBaseCapabilities(final Set baseCapabilities) { + // Check base capabilities to be supported by the server + final Sets.SetView unknownBaseCaps = Sets.difference(baseCapabilities, DEFAULT_BASE_CAPABILITIES); + Preconditions.checkArgument(unknownBaseCaps.isEmpty(), + "Base capabilities that will be supported by netconf server have to be subset of %s, unknown base capabilities: %s", + DEFAULT_BASE_CAPABILITIES, unknownBaseCaps); + + final ImmutableSet.Builder b = ImmutableSet.builder(); + b.addAll(baseCapabilities); + // Base 1.0 capability is supported by default + b.add(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0); + return b.build(); } + /** + * + * @param defunctSessionListenerFactory will not be taken into account as session listener factory can + * only be created after snapshot is opened, thus this method constructs + * proper session listener factory. + * @param channel Underlying channel + * @param promise Promise to be notified + * @return session negotiator + */ @Override - public SessionNegotiator getSessionNegotiator(SessionListenerFactory sessionListenerFactory, Channel channel, - Promise promise) { - long sessionId = idProvider.getNextSessionId(); + public SessionNegotiator getSessionNegotiator(final SessionListenerFactory defunctSessionListenerFactory, + final Channel channel, final Promise promise) { + final long sessionId = idProvider.getNextSessionId(); + + NetconfServerSessionPreferences proposal; + try { + proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId, monitoringService), sessionId); + } catch (final NetconfDocumentedException e) { + LOG.error("Unable to create hello message for session {} with {}", sessionId, monitoringService); + throw new IllegalStateException(e); + } - NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId), - sessionId); return new NetconfServerSessionNegotiator(proposal, promise, channel, timer, - sessionListenerFactory.getSessionListener(), connectionTimeoutMillis); + getListener(Long.toString(sessionId)), connectionTimeoutMillis); } - private static final XPathExpression sessionIdXPath = XMLNetconfUtil - .compileXPath("/netconf:hello/netconf:session-id"); - private static final XPathExpression capabilitiesXPath = XMLNetconfUtil - .compileXPath("/netconf:hello/netconf:capabilities"); - - private NetconfHelloMessage createHelloMessage(long sessionId) { - Document helloMessageTemplate = getHelloTemplateClone(); - - // change session ID - final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate, - XPathConstants.NODE); - sessionIdNode.setTextContent(String.valueOf(sessionId)); + private NetconfServerSessionListener getListener(final String netconfSessionIdForReporting) { + final NetconfOperationService service = + this.aggregatedOpService.createService(netconfSessionIdForReporting); + final NetconfOperationRouter operationRouter = + new NetconfOperationRouterImpl(service, commitNotificationProducer, monitoringService, netconfSessionIdForReporting); + return new NetconfServerSessionListener(operationRouter, monitoringService, service); - // add capabilities from yang store - final Element capabilitiesElement = (Element) XmlUtil.evaluateXPath(capabilitiesXPath, helloMessageTemplate, - XPathConstants.NODE); - - CapabilityProvider capabilityProvider = new CapabilityProviderImpl(factoriesListener.getSnapshot(sessionId)); - - for (String capability : capabilityProvider.getCapabilities()) { - final Element capabilityElement = helloMessageTemplate.createElement(XmlNetconfConstants.CAPABILITY); - capabilityElement.setTextContent(capability); - capabilitiesElement.appendChild(capabilityElement); - } - return new NetconfHelloMessage(helloMessageTemplate); } - private synchronized Document getHelloTemplateClone() { - return (Document) helloMessageTemplate.cloneNode(true); + private NetconfHelloMessage createHelloMessage(final long sessionId, final NetconfMonitoringService capabilityProvider) throws NetconfDocumentedException { + return NetconfHelloMessage.createServerHello(Sets.union(DefaultCommit.transformCapabilities(capabilityProvider.getCapabilities()), baseCapabilities), sessionId); } + }