9d958660615c27c27e86fee68e669f7e17c6a1b1
[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 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;
28
29 import java.util.Set;
30
31 import static org.opendaylight.controller.netconf.mapping.api.NetconfOperationProvider.NetconfOperationProviderUtil.getNetconfSessionIdForReporting;
32
33 public class NetconfServerSessionNegotiatorFactory implements SessionNegotiatorFactory<NetconfHelloMessage, NetconfServerSession, NetconfServerSessionListener> {
34
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);
38
39     private final Timer timer;
40
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);
47
48     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
49                                                  SessionIdProvider idProvider, long connectionTimeoutMillis,
50                                                  DefaultCommitNotificationProducer commitNot, SessionMonitoringService monitoringService) {
51         this.timer = timer;
52         this.netconfOperationProvider = netconfOperationProvider;
53         this.idProvider = idProvider;
54         this.connectionTimeoutMillis = connectionTimeoutMillis;
55         this.commitNotificationProducer = commitNot;
56         this.monitoringService = monitoringService;
57     }
58
59     /**
60      *
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
67      */
68     @Override
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);
75
76         NetconfServerSessionPreferences proposal = null;
77         try {
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);
83         }
84
85         NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
86                 commitNotificationProducer, monitoringService,
87                 netconfOperationServiceSnapshot, capabilityProvider);
88
89         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
90                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
91     }
92
93     private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) throws NetconfDocumentedException {
94         return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), DEFAULT_CAPABILITIES), sessionId);
95     }
96
97 }