c5f8e99f2271baaa84b9a4c58657a69f50c92369
[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 com.google.common.collect.ImmutableSet;
14 import java.util.Set;
15
16 import org.opendaylight.controller.netconf.api.NetconfDocumentedException;
17 import org.opendaylight.controller.netconf.api.NetconfServerSessionPreferences;
18 import org.opendaylight.controller.netconf.impl.mapping.CapabilityProvider;
19 import org.opendaylight.controller.netconf.impl.osgi.SessionMonitoringService;
20 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider;
21 import org.opendaylight.controller.netconf.mapping.api.NetconfOperationServiceSnapshot;
22 import org.opendaylight.controller.netconf.util.messages.NetconfHelloMessage;
23 import org.opendaylight.controller.netconf.util.xml.XmlNetconfConstants;
24 import org.opendaylight.protocol.framework.SessionListenerFactory;
25 import org.opendaylight.protocol.framework.SessionNegotiator;
26 import org.opendaylight.protocol.framework.SessionNegotiatorFactory;
27
28 import com.google.common.collect.Sets;
29
30 import io.netty.channel.Channel;
31 import io.netty.util.Timer;
32 import io.netty.util.concurrent.Promise;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
35
36 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
37
38     private static final Set<String> DEFAULT_BASE_CAPABILITIES = ImmutableSet.of(
39             XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_0
40             // FIXME, Chunk framing causes ConcurrentClientsTest to fail, investigate
41 //            XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_BASE_1_1,
42             // FIXME, EXI causing issues with sal-netconf-connector, investigate
43 //            XmlNetconfConstants.URN_IETF_PARAMS_NETCONF_CAPABILITY_EXI_1_0
44     );
45
46     private final Timer timer;
47
48     private final SessionIdProvider idProvider;
49     private final NetconfOperationProvider netconfOperationProvider;
50     private final long connectionTimeoutMillis;
51     private final DefaultCommitNotificationProducer commitNotificationProducer;
52     private final SessionMonitoringService monitoringService;
53     private static final Logger logger = LoggerFactory.getLogger(NetconfServerSessionNegotiatorFactory.class);
54
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 = timer;
61         this.netconfOperationProvider = netconfOperationProvider;
62         this.idProvider = idProvider;
63         this.connectionTimeoutMillis = connectionTimeoutMillis;
64         this.commitNotificationProducer = commitNot;
65         this.monitoringService = monitoringService;
66     }
67
68     /**
69      *
70      * @param defunctSessionListenerFactory will not be taken into account as session listener factory can
71      *                                      only be created after snapshot is opened, thus this method constructs
72      *                                      proper session listener factory.
73      * @param channel Underlying channel
74      * @param promise Promise to be notified
75      * @return session negotiator
76      */
77     @Override
78     public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> defunctSessionListenerFactory,
79                                                                         Channel channel, Promise<NetconfServerSession> promise) {
80         long sessionId = idProvider.getNextSessionId();
81         NetconfOperationServiceSnapshot netconfOperationServiceSnapshot = netconfOperationProvider.openSnapshot(
82                 getNetconfSessionIdForReporting(sessionId));
83         CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationServiceSnapshot);
84
85         NetconfServerSessionPreferences proposal = null;
86         try {
87             proposal = new NetconfServerSessionPreferences(
88                     createHelloMessage(sessionId, capabilityProvider), sessionId);
89         } catch (NetconfDocumentedException e) {
90             logger.error("Unable to create hello mesage for session {} with capability provider {}", sessionId,capabilityProvider);
91             throw new IllegalStateException(e);
92         }
93
94         NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
95                 commitNotificationProducer, monitoringService,
96                 netconfOperationServiceSnapshot, capabilityProvider);
97
98         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
99                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
100     }
101
102     private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) throws NetconfDocumentedException {
103         return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), DEFAULT_BASE_CAPABILITIES), sessionId);
104     }
105
106 }