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;
11 import com.google.common.collect.Sets;
12 import io.netty.channel.Channel;
13 import io.netty.util.Timer;
14 import io.netty.util.concurrent.Promise;
15 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
16 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
17 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
18 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
20 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
21 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
22 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
23 import org.opendaylight.protocol.framework.SessionListenerFactory;
24 import org.opendaylight.protocol.framework.SessionNegotiator;
25 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
31 import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
33 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
35 private static final Set<String> DEFAULT_CAPABILITIES = Sets.newHashSet(
36 XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
37 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0);
39 private final Timer timer;
41 private final SessionIdProvider idProvider;
42 private final NetconfOperationProvider netconfOperationProvider;
43 private final long connectionTimeoutMillis;
44 private final DefaultCommitNotificationProducer commitNotificationProducer;
45 private final SessionMonitoringService monitoringService;
46 private static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiatorFactory.class);
48 public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
49 SessionIdProvider idProvider, long connectionTimeoutMillis,
50 DefaultCommitNotificationProducer commitNot, SessionMonitoringService monitoringService) {
52 this.netconfOperationProvider = netconfOperationProvider;
53 this.idProvider = idProvider;
54 this.connectionTimeoutMillis = connectionTimeoutMillis;
55 this.commitNotificationProducer = commitNot;
56 this.monitoringService = monitoringService;
61 * @param defunctSessionListenerFactory will not be taken into account as session listener factory can
62 * only be created after snapshot is opened, thus this method constructs
63 * proper session listener factory.
64 * @param channel Underlying channel
65 * @param promise Promise to be notified
66 * @return session negotiator
69 public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> defunctSessionListenerFactory,
70 Channel channel, Promise<NetconfServerSession> promise) {
71 long sessionId = idProvider.getNextSessionId();
72 NetconfOperationServiceSnapshot netconfOperationServiceSnapshot = netconfOperationProvider.openSnapshot(
73 getNetconfSessionIdForReporting(sessionId));
74 CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationServiceSnapshot);
76 NetconfServerSessionPreferences proposal = null;
78 proposal = new NetconfServerSessionPreferences(
79 createHelloMessage(sessionId, capabilityProvider), sessionId);
80 } catch (NetconfDocumentedException e) {
81 logger.error("Unable to create hello mesage for session {} with capability provider {}", sessionId,capabilityProvider);
82 throw new IllegalStateException(e);
85 NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
86 commitNotificationProducer, monitoringService,
87 netconfOperationServiceSnapshot, capabilityProvider);
89 return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
90 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
93 private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) throws NetconfDocumentedException {
94 return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), DEFAULT_CAPABILITIES), sessionId);