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