Merge "Table update rpc added as provider"
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / SwitchConnectionHandlerImpl.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.openflowplugin.openflow.md.core;
10
11 import java.net.InetAddress;
12 import java.util.concurrent.ScheduledThreadPoolExecutor;
13 import java.util.concurrent.TimeUnit;
14
15 import org.opendaylight.openflowjava.protocol.api.connection.ConnectionAdapter;
16 import org.opendaylight.openflowjava.protocol.api.connection.SwitchConnectionHandler;
17 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
18 import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpy;
19 import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpyCounterImpl;
20 import org.opendaylight.openflowplugin.openflow.md.queue.QueueKeeperLightImpl;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
22 import org.opendaylight.yangtools.yang.binding.DataObject;
23
24 /**
25  * @author mirehak
26  *
27  */
28 public class SwitchConnectionHandlerImpl implements SwitchConnectionHandler {
29     
30     private ScheduledThreadPoolExecutor spyPool; 
31
32     private QueueKeeperLightImpl queueKeeper;
33     private ErrorHandler errorHandler;
34     private MessageSpy<OfHeader, DataObject> messageSpy;
35     private int spyRate = 10;
36
37     /**
38      *
39      */
40     public SwitchConnectionHandlerImpl() {
41         messageSpy = new MessageSpyCounterImpl();
42         queueKeeper = new QueueKeeperLightImpl();
43         queueKeeper.setTranslatorMapping(OFSessionUtil.getTranslatorMap());
44         queueKeeper.setPopListenersMapping(OFSessionUtil.getPopListenerMapping());
45         queueKeeper.setMessageSpy(messageSpy);
46         
47         queueKeeper.init();
48
49         errorHandler = new ErrorHandlerQueueImpl();
50         new Thread(errorHandler).start();
51         
52         //TODO: implement shutdown invocation upon service stop event
53         spyPool = new ScheduledThreadPoolExecutor(1);
54         spyPool.scheduleAtFixedRate(messageSpy, spyRate, spyRate, TimeUnit.SECONDS);
55     }
56
57     @Override
58     public boolean accept(InetAddress address) {
59         // TODO:: add policy derived rules
60         return true;
61     }
62
63     @Override
64     public void onSwitchConnected(ConnectionAdapter connectionAdapter) {
65         ConnectionConductor conductor = ConnectionConductorFactory.createConductor(
66                 connectionAdapter, queueKeeper);
67         conductor.setErrorHandler(errorHandler);
68     }
69
70 }