package org.opendaylight.controller.netconf.impl;
-import com.google.common.base.Preconditions;
-import io.netty.channel.Channel;
-import io.netty.util.Timer;
-import io.netty.util.concurrent.Promise;
-import org.opendaylight.controller.netconf.api.NetconfMessage;
+import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
+
+import com.google.common.collect.ImmutableSet;
+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.util.xml.XMLNetconfUtil;
+import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
+import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
+import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
+import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
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 com.google.common.collect.Sets;
-public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory {
+import io.netty.channel.Channel;
+import io.netty.util.Timer;
+import io.netty.util.concurrent.Promise;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
- public static final String SERVER_HELLO_XML_LOCATION = "/server_hello.xml";
+ private static final Set<String> DEFAULT_BASE_CAPABILITIES = ImmutableSet.of(
+ XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0
+ // FIXME, Chunk framing causes ConcurrentClientsTest to fail, investigate
+// XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1,
+ // FIXME, EXI causing issues with sal-netconf-connector, investigate
+// 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 NetconfOperationProvider netconfOperationProvider;
private final long connectionTimeoutMillis;
-
- public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationServiceFactoryListener factoriesListener,
- SessionIdProvider idProvider, long connectionTimeoutMillis) {
+ private final DefaultCommitNotificationProducer commitNotificationProducer;
+ private final SessionMonitoringService monitoringService;
+ private static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiatorFactory.class);
+
+ // TODO too many params, refactor
+ public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
+ SessionIdProvider idProvider, long connectionTimeoutMillis,
+ DefaultCommitNotificationProducer commitNot,
+ SessionMonitoringService monitoringService) {
this.timer = timer;
- this.factoriesListener = factoriesListener;
+ this.netconfOperationProvider = netconfOperationProvider;
this.idProvider = idProvider;
this.connectionTimeoutMillis = connectionTimeoutMillis;
+ this.commitNotificationProducer = commitNot;
+ this.monitoringService = monitoringService;
}
- 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();
- }
-
+ /**
+ *
+ * @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) {
+ public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> defunctSessionListenerFactory,
+ Channel channel, Promise<NetconfServerSession> promise) {
long sessionId = idProvider.getNextSessionId();
+ NetconfOperationServiceSnapshot netconfOperationServiceSnapshot = netconfOperationProvider.openSnapshot(
+ getNetconfSessionIdForReporting(sessionId));
+ CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationServiceSnapshot);
+
+ NetconfServerSessionPreferences proposal = null;
+ try {
+ proposal = new NetconfServerSessionPreferences(
+ createHelloMessage(sessionId, capabilityProvider), sessionId);
+ } catch (NetconfDocumentedException e) {
+ logger.error("Unable to create hello mesage for session {} with capability provider {}", sessionId,capabilityProvider);
+ throw new IllegalStateException(e);
+ }
+
+ NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
+ commitNotificationProducer, monitoringService,
+ netconfOperationServiceSnapshot, capabilityProvider);
- NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(createHelloMessage(sessionId),
- sessionId);
return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
sessionListenerFactory.getSessionListener(), 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 NetconfMessage createHelloMessage(long sessionId) {
- Document helloMessageTemplate = getHelloTemplateClone();
-
- // change session ID
- final Node sessionIdNode = (Node) XmlUtil.evaluateXPath(sessionIdXPath, helloMessageTemplate,
- XPathConstants.NODE);
- sessionIdNode.setTextContent(String.valueOf(sessionId));
-
- // 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 NetconfMessage(helloMessageTemplate);
+ private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) throws NetconfDocumentedException {
+ return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), DEFAULT_BASE_CAPABILITIES), sessionId);
}
- private synchronized Document getHelloTemplateClone() {
- return (Document) this.helloMessageTemplate.cloneNode(true);
- }
}