Fixed netty & checkstyle failures
[openflowjava.git] / openflow-protocol-impl / src / main / java / org / opendaylight / openflowjava / protocol / impl / core / IdleHandler.java
1 /*
2  * Copyright (c) 2013 Pantheon Technologies s.r.o. 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.openflowjava.protocol.impl.core;
10
11 import io.netty.channel.ChannelHandlerContext;
12 import io.netty.handler.timeout.ReadTimeoutHandler;
13
14 import java.util.concurrent.TimeUnit;
15
16 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.system.rev130927.SwitchIdleEventBuilder;
17 import org.slf4j.Logger;
18 import org.slf4j.LoggerFactory;
19
20 /**
21  * Detects idle state of switch and informs upper layers
22  * @author michal.polkorab
23  */
24 public class IdleHandler extends ReadTimeoutHandler {
25
26     private static final Logger LOG = LoggerFactory.getLogger(IdleHandler.class);
27     private boolean first = true;
28
29     /**
30      * @param readerIdleTime
31      * @param unit
32      */
33     public IdleHandler(final long readerIdleTime, final TimeUnit unit) {
34         super(readerIdleTime, unit);
35     }
36
37     @Override
38     public void channelRead(final ChannelHandlerContext ctx, final Object msg) throws Exception {
39         super.channelRead(ctx, msg);
40         first = true;
41     }
42
43     @Override
44     protected void readTimedOut(final ChannelHandlerContext ctx) throws Exception {
45         if (first) {
46             LOG.debug("Switch idle");
47             SwitchIdleEventBuilder builder = new SwitchIdleEventBuilder();
48             builder.setInfo("Switch idle");
49             ctx.fireChannelRead(builder.build());
50             first = false;
51         }
52     }
53 }