Improve iteration over subnets map by using entrySet instead of keyset.
[controller.git] / opendaylight / netconf / netconf-client / src / main / java / org / opendaylight / controller / netconf / client / NetconfClientDispatcher.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.client;
10
11 import io.netty.channel.EventLoopGroup;
12 import io.netty.channel.socket.SocketChannel;
13 import io.netty.util.HashedWheelTimer;
14 import io.netty.util.concurrent.Future;
15 import io.netty.util.concurrent.Promise;
16 import java.io.Closeable;
17 import java.net.InetSocketAddress;
18 import org.opendaylight.controller.netconf.api.NetconfMessage;
19 import org.opendaylight.controller.netconf.api.NetconfSession;
20 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
21 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
22 import org.opendaylight.protocol.framework.AbstractDispatcher;
23 import org.opendaylight.protocol.framework.ReconnectStrategy;
24 import org.opendaylight.protocol.framework.SessionListener;
25 import org.opendaylight.protocol.framework.SessionListenerFactory;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
30
31     private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
32
33     private final NetconfClientSessionNegotiatorFactory negotatorFactory;
34     private final HashedWheelTimer timer;
35
36     public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
37         super(bossGroup, workerGroup);
38         timer = new HashedWheelTimer();
39         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer);
40     }
41
42     public Future<NetconfClientSession> createClient(InetSocketAddress address,
43             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
44
45         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
46
47             @Override
48             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
49                 initialize(ch, promise);
50             }
51
52             private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
53                 new ClientChannelInitializer( negotatorFactory, sessionListener).initialize(ch, promise);
54             }
55         });
56     }
57
58     private static class ClientChannelInitializer extends AbstractChannelInitializer {
59
60         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
61         private final NetconfClientSessionListener sessionListener;
62
63         private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
64                                             NetconfClientSessionListener sessionListener) {
65             this.negotiatorFactory = negotiatorFactory;
66             this.sessionListener = sessionListener;
67         }
68
69         @Override
70         public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
71                 super.initialize(ch,promise);
72         }
73
74         @Override
75         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
76             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
77                 @Override
78                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
79                     return sessionListener;
80                 }
81             }, ch, promise));
82         }
83
84     }
85     @Override
86     public void close() {
87         try {
88             timer.stop();
89         } catch (Exception e) {
90             logger.debug("Ignoring exception while closing {}", timer, e);
91         }
92     }
93 }