Fixed netconf monitoring.
[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
17 import java.io.Closeable;
18 import java.net.InetSocketAddress;
19
20 import org.opendaylight.controller.netconf.api.NetconfMessage;
21 import org.opendaylight.controller.netconf.api.NetconfSession;
22 import org.opendaylight.controller.netconf.api.NetconfTerminationReason;
23 import org.opendaylight.controller.netconf.util.AbstractChannelInitializer;
24 import org.opendaylight.protocol.framework.AbstractDispatcher;
25 import org.opendaylight.protocol.framework.ReconnectStrategy;
26 import org.opendaylight.protocol.framework.SessionListener;
27 import org.opendaylight.protocol.framework.SessionListenerFactory;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 import com.google.common.base.Optional;
32
33 public class NetconfClientDispatcher extends AbstractDispatcher<NetconfClientSession, NetconfClientSessionListener> implements Closeable {
34
35     private static final Logger logger = LoggerFactory.getLogger(NetconfClient.class);
36
37     private final NetconfClientSessionNegotiatorFactory negotatorFactory;
38     private final HashedWheelTimer timer;
39
40     public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup) {
41         super(bossGroup, workerGroup);
42         timer = new HashedWheelTimer();
43         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.<String>absent());
44     }
45
46     public NetconfClientDispatcher(EventLoopGroup bossGroup, EventLoopGroup workerGroup, String additionalHeader) {
47         super(bossGroup, workerGroup);
48         timer = new HashedWheelTimer();
49         this.negotatorFactory = new NetconfClientSessionNegotiatorFactory(timer, Optional.of(additionalHeader));
50     }
51
52     public Future<NetconfClientSession> createClient(InetSocketAddress address,
53             final NetconfClientSessionListener sessionListener, ReconnectStrategy strat) {
54
55         return super.createClient(address, strat, new PipelineInitializer<NetconfClientSession>() {
56
57             @Override
58             public void initializeChannel(final SocketChannel ch, final Promise<NetconfClientSession> promise) {
59                 initialize(ch, promise);
60             }
61
62             private void initialize(SocketChannel ch, Promise<NetconfClientSession> promise) {
63                 new ClientChannelInitializer( negotatorFactory, sessionListener).initialize(ch, promise);
64             }
65         });
66     }
67
68     private static class ClientChannelInitializer extends AbstractChannelInitializer {
69
70         private final NetconfClientSessionNegotiatorFactory negotiatorFactory;
71         private final NetconfClientSessionListener sessionListener;
72
73         private ClientChannelInitializer(NetconfClientSessionNegotiatorFactory negotiatorFactory,
74                                             NetconfClientSessionListener sessionListener) {
75             this.negotiatorFactory = negotiatorFactory;
76             this.sessionListener = sessionListener;
77         }
78
79         @Override
80         public void initialize(SocketChannel ch, Promise<? extends NetconfSession> promise) {
81                 super.initialize(ch,promise);
82         }
83
84         @Override
85         protected void initializeAfterDecoder(SocketChannel ch, Promise<? extends NetconfSession> promise) {
86             ch.pipeline().addLast("negotiator", negotiatorFactory.getSessionNegotiator(new SessionListenerFactory() {
87                 @Override
88                 public SessionListener<NetconfMessage, NetconfClientSession, NetconfTerminationReason> getSessionListener() {
89                     return sessionListener;
90                 }
91             }, ch, promise));
92         }
93
94     }
95     @Override
96     public void close() {
97         try {
98             timer.stop();
99         } catch (Exception e) {
100             logger.debug("Ignoring exception while closing {}", timer, e);
101         }
102     }
103 }