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