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