Merge "Bug 509: Fixed incorrect merging of Data Store Writes / Events"
[controller.git] / opendaylight / netconf / netconf-impl / src / main / java / org / opendaylight / controller / netconf / impl / NetconfServerSessionNegotiatorFactory.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.netconf.impl;
10
11 import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
12
13 import java.util.Set;
14
15 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
16 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
17 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
18 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
19 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
20 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
21 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
22 import org.opendaylight.protocol.framework.SessionListenerFactory;
23 import org.opendaylight.protocol.framework.SessionNegotiator;
24 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
25
26 import com.google.common.collect.Sets;
27
28 import io.netty.channel.Channel;
29 import io.netty.util.Timer;
30 import io.netty.util.concurrent.Promise;
31
32 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
33
34     private static final Set<String> DEFAULT_CAPABILITIES = Sets.newHashSet(
35             XmlNetconfConstants.URN_IETF_PARAMS_XML_NS_NETCONF_BASE_1_0,
36             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0);
37
38     private final Timer timer;
39
40     private final SessionIdProvider idProvider;
41     private final NetconfOperationProvider netconfOperationProvider;
42     private final long connectionTimeoutMillis;
43     private final DefaultCommitNotificationProducer commitNotificationProducer;
44     private final SessionMonitoringService monitoringService;
45
46     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
47                                                  SessionIdProvider idProvider, long connectionTimeoutMillis,
48                                                  DefaultCommitNotificationProducer commitNot, SessionMonitoringService monitoringService) {
49         this.timer = timer;
50         this.netconfOperationProvider = netconfOperationProvider;
51         this.idProvider = idProvider;
52         this.connectionTimeoutMillis = connectionTimeoutMillis;
53         this.commitNotificationProducer = commitNot;
54         this.monitoringService = monitoringService;
55     }
56
57     /**
58      *
59      * @param defunctSessionListenerFactory will not be taken into account as session listener factory can
60      *                                      only be created after snapshot is opened, thus this method constructs
61      *                                      proper session listener factory.
62      * @param channel Underlying channel
63      * @param promise Promise to be notified
64      * @return session negotiator
65      */
66     @Override
67     public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> defunctSessionListenerFactory,
68                                                                         Channel channel, Promise<NetconfServerSession> promise) {
69         long sessionId = idProvider.getNextSessionId();
70         NetconfOperationServiceSnapshot netconfOperationServiceSnapshot = netconfOperationProvider.openSnapshot(
71                 getNetconfSessionIdForReporting(sessionId));
72         CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationServiceSnapshot);
73
74         NetconfServerSessionPreferences proposal = new NetconfServerSessionPreferences(
75                 createHelloMessage(sessionId, capabilityProvider), sessionId);
76
77         NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
78                 commitNotificationProducer, monitoringService,
79                 netconfOperationServiceSnapshot, capabilityProvider);
80
81         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
82                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
83     }
84
85     private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) {
86         return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), DEFAULT_CAPABILITIES), sessionId);
87     }
88
89 }