Added dumpMsgCount osgi command
[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.QueueKeeperLightImpl;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
21 import org.opendaylight.yangtools.yang.binding.DataObject;
22
23 /**
24  * basic interconnecting piece between plugin and library 
25  */
26 public class SwitchConnectionHandlerImpl implements SwitchConnectionHandler {
27     
28     private ScheduledThreadPoolExecutor spyPool; 
29
30     private QueueKeeperLightImpl queueKeeper;
31     private ErrorHandler errorHandler;
32     private MessageSpy<OfHeader, DataObject> messageSpy;
33     private int spyRate = 10;
34
35     /**
36      *
37      */
38     public SwitchConnectionHandlerImpl() {
39         queueKeeper = new QueueKeeperLightImpl();
40         
41         errorHandler = new ErrorHandlerQueueImpl();
42         //TODO: implement shutdown invocation upon service stop event
43         new Thread(errorHandler).start();
44         
45         //TODO: implement shutdown invocation upon service stop event
46         spyPool = new ScheduledThreadPoolExecutor(1);
47     }
48
49     /**
50      * wire all up
51      */
52     public void init() {
53         queueKeeper.setTranslatorMapping(OFSessionUtil.getTranslatorMap());
54         queueKeeper.setPopListenersMapping(OFSessionUtil.getPopListenerMapping());
55         queueKeeper.setMessageSpy(messageSpy);
56         
57         queueKeeper.init();
58         
59         spyPool.scheduleAtFixedRate(messageSpy, spyRate, spyRate, TimeUnit.SECONDS);
60     }
61
62     @Override
63     public boolean accept(InetAddress address) {
64         // TODO:: add policy derived rules
65         return true;
66     }
67
68     @Override
69     public void onSwitchConnected(ConnectionAdapter connectionAdapter) {
70         ConnectionConductor conductor = ConnectionConductorFactory.createConductor(
71                 connectionAdapter, queueKeeper);
72         conductor.setErrorHandler(errorHandler);
73     }
74     
75     /**
76      * @param messageSpy the messageSpy to set
77      */
78     public void setMessageSpy(MessageSpy<OfHeader, DataObject> messageSpy) {
79         this.messageSpy = messageSpy;
80     }
81
82 }