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