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 static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableSet;
17 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
18 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
19 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
20 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
21 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
22 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
23 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
24 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
25 import org.opendaylight.protocol.framework.SessionListenerFactory;
26 import org.opendaylight.protocol.framework.SessionNegotiator;
27 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
29 import com.google.common.collect.Sets;
31 import io.netty.channel.Channel;
32 import io.netty.util.Timer;
33 import io.netty.util.concurrent.Promise;
34 import org.slf4j.Logger;
35 import org.slf4j.LoggerFactory;
37 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
39 public static final Set<String> DEFAULT_BASE_CAPABILITIES = ImmutableSet.of(
40 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0,
41 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1,
42 XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0
45 private final Timer timer;
47 private final SessionIdProvider idProvider;
48 private final NetconfOperationProvider netconfOperationProvider;
49 private final long connectionTimeoutMillis;
50 private final DefaultCommitNotificationProducer commitNotificationProducer;
51 private final SessionMonitoringService monitoringService;
52 private static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiatorFactory.class);
53 private final Set<String> baseCapabilities;
55 // TODO too many params, refactor
56 public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
57 SessionIdProvider idProvider, long connectionTimeoutMillis,
58 DefaultCommitNotificationProducer commitNot,
59 SessionMonitoringService monitoringService) {
60 this(timer, netconfOperationProvider, idProvider, connectionTimeoutMillis, commitNot, monitoringService, DEFAULT_BASE_CAPABILITIES);
63 // TODO too many params, refactor
64 public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
65 SessionIdProvider idProvider, long connectionTimeoutMillis,
66 DefaultCommitNotificationProducer commitNot,
67 SessionMonitoringService monitoringService, Set<String> baseCapabilities) {
69 this.netconfOperationProvider = netconfOperationProvider;
70 this.idProvider = idProvider;
71 this.connectionTimeoutMillis = connectionTimeoutMillis;
72 this.commitNotificationProducer = commitNot;
73 this.monitoringService = monitoringService;
74 this.baseCapabilities = validateBaseCapabilities(baseCapabilities);
77 private ImmutableSet<String> validateBaseCapabilities(final Set<String> baseCapabilities) {
78 // Check base capabilities to be supported by the server
79 Sets.SetView<String> unknownBaseCaps = Sets.difference(baseCapabilities, DEFAULT_BASE_CAPABILITIES);
80 Preconditions.checkArgument(unknownBaseCaps.isEmpty(),
81 "Base capabilities that will be supported by netconf server have to be subset of %s, unknown base capabilities: %s",
82 DEFAULT_BASE_CAPABILITIES, unknownBaseCaps);
84 ImmutableSet.Builder<String> b = ImmutableSet.builder();
85 b.addAll(baseCapabilities);
86 // Base 1.0 capability is supported by default
87 b.add(XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0);
93 * @param defunctSessionListenerFactory will not be taken into account as session listener factory can
94 * only be created after snapshot is opened, thus this method constructs
95 * proper session listener factory.
96 * @param channel Underlying channel
97 * @param promise Promise to be notified
98 * @return session negotiator
101 public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> defunctSessionListenerFactory,
102 Channel channel, Promise<NetconfServerSession> promise) {
103 long sessionId = idProvider.getNextSessionId();
104 NetconfOperationServiceSnapshot netconfOperationServiceSnapshot = netconfOperationProvider.openSnapshot(
105 getNetconfSessionIdForReporting(sessionId));
106 CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationServiceSnapshot);
108 NetconfServerSessionPreferences proposal = null;
110 proposal = new NetconfServerSessionPreferences(
111 createHelloMessage(sessionId, capabilityProvider), sessionId);
112 } catch (NetconfDocumentedException e) {
113 logger.error("Unable to create hello mesage for session {} with capability provider {}", sessionId,capabilityProvider);
114 throw new IllegalStateException(e);
117 NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
118 commitNotificationProducer, monitoringService,
119 netconfOperationServiceSnapshot, capabilityProvider);
121 return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
122 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
125 private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) throws NetconfDocumentedException {
126 return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), baseCapabilities), sessionId);