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