BUG-1075: ingress back pressure
[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.QueueProcessorLightImpl;
20 import org.opendaylight.yangtools.yang.binding.DataContainer;
21
22 /**
23  * basic interconnecting piece between plugin and library 
24  */
25 public class SwitchConnectionHandlerImpl implements SwitchConnectionHandler {
26     
27     private ScheduledThreadPoolExecutor spyPool; 
28
29     private QueueProcessorLightImpl queueProcessor;
30     private ErrorHandler errorHandler;
31     private MessageSpy<DataContainer> messageSpy;
32     private int spyRate = 10;
33
34     /**
35      *
36      */
37     public SwitchConnectionHandlerImpl() {
38         queueProcessor = new QueueProcessorLightImpl();
39         
40         //TODO: implement shutdown invocation upon service stop event
41         spyPool = new ScheduledThreadPoolExecutor(1);
42     }
43
44     /**
45      * wire all up
46      */
47     public void init() {
48         queueProcessor.setTranslatorMapping(OFSessionUtil.getTranslatorMap());
49         queueProcessor.setPopListenersMapping(OFSessionUtil.getPopListenerMapping());
50         queueProcessor.setMessageSpy(messageSpy);
51         
52         queueProcessor.init();
53         
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, queueProcessor);
67         conductor.setErrorHandler(errorHandler);
68     }
69     
70     /**
71      * @param messageSpy the messageSpy to set
72      */
73     public void setMessageSpy(MessageSpy<DataContainer> messageSpy) {
74         this.messageSpy = messageSpy;
75     }
76     
77     /**
78      * @param errorHandler the errorHandler to set
79      */
80     public void setErrorHandler(ErrorHandler errorHandler) {
81         this.errorHandler = errorHandler;
82     }
83
84 }