BUG-848 Fix netconf communication while using CHUNK encoding
[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     // TODO make this configurable
39     private 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
43     );
44
45     private final Timer timer;
46
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
54     // TODO too many params, refactor
55     public NetconfServerSessionNegotiatorFactory(Timer timer, NetconfOperationProvider netconfOperationProvider,
56                                                  SessionIdProvider idProvider, long connectionTimeoutMillis,
57                                                  DefaultCommitNotificationProducer commitNot,
58                                                  SessionMonitoringService monitoringService) {
59         this.timer = timer;
60         this.netconfOperationProvider = netconfOperationProvider;
61         this.idProvider = idProvider;
62         this.connectionTimeoutMillis = connectionTimeoutMillis;
63         this.commitNotificationProducer = commitNot;
64         this.monitoringService = monitoringService;
65     }
66
67     /**
68      *
69      * @param defunctSessionListenerFactory will not be taken into account as session listener factory can
70      *                                      only be created after snapshot is opened, thus this method constructs
71      *                                      proper session listener factory.
72      * @param channel Underlying channel
73      * @param promise Promise to be notified
74      * @return session negotiator
75      */
76     @Override
77     public SessionNegotiator<NetconfServerSession> getSessionNegotiator(SessionListenerFactory<NetconfServerSessionListener> defunctSessionListenerFactory,
78                                                                         Channel channel, Promise<NetconfServerSession> promise) {
79         long sessionId = idProvider.getNextSessionId();
80         NetconfOperationServiceSnapshot netconfOperationServiceSnapshot = netconfOperationProvider.openSnapshot(
81                 getNetconfSessionIdForReporting(sessionId));
82         CapabilityProvider capabilityProvider = new CapabilityProviderImpl(netconfOperationServiceSnapshot);
83
84         NetconfServerSessionPreferences proposal = null;
85         try {
86             proposal = new NetconfServerSessionPreferences(
87                     createHelloMessage(sessionId, capabilityProvider), sessionId);
88         } catch (NetconfDocumentedException e) {
89             logger.error("Unable to create hello mesage for session {} with capability provider {}", sessionId,capabilityProvider);
90             throw new IllegalStateException(e);
91         }
92
93         NetconfServerSessionListenerFactory sessionListenerFactory = new NetconfServerSessionListenerFactory(
94                 commitNotificationProducer, monitoringService,
95                 netconfOperationServiceSnapshot, capabilityProvider);
96
97         return new NetconfServerSessionNegotiator(proposal, promise, channel, timer,
98                 sessionListenerFactory.getSessionListener(), connectionTimeoutMillis);
99     }
100
101     private NetconfHelloMessage createHelloMessage(long sessionId, CapabilityProvider capabilityProvider) throws NetconfDocumentedException {
102         return NetconfHelloMessage.createServerHello(Sets.union(capabilityProvider.getCapabilities(), DEFAULT_BASE_CAPABILITIES), sessionId);
103     }
104
105 }