1eeebadcf12e87b7e4b2f5fb32f47f9ddf423ced
[openflowplugin.git] / openflowplugin / src / main / java / org / opendaylight / openflowplugin / openflow / md / core / sal / OpenflowPluginProvider.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 package org.opendaylight.openflowplugin.openflow.md.core.sal;
9
10 import java.util.Collection;
11 import java.util.Collections;
12
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
17 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
18 import org.opendaylight.openflowplugin.openflow.md.core.MDController;
19 import org.opendaylight.openflowplugin.openflow.md.core.cmd.MessageCountCommandProvider;
20 import org.opendaylight.openflowplugin.openflow.md.queue.MessageObservatory;
21 import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpyCounterImpl;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.RpcService;
25 import org.osgi.framework.BundleContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * OFPlugin provider implementation
31  */
32 public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseable {
33     
34     private static Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
35     
36     private BindingAwareBroker broker;
37
38     private BundleContext context;
39
40     private SwitchConnectionProvider switchConnectionProvider;
41
42     private MDController mdController;
43     
44     private MessageCountCommandProvider messageCountCommandProvider;
45
46     private MessageObservatory<OfHeader, DataObject> messageCountProvider;
47     
48     private SalRegistrationManager registrationManager;
49     
50     /**
51      * dependencymanager requirement 
52      * @param switchConnectionProviderArg
53      */
54     public void unsetSwitchConnectionProvider(SwitchConnectionProvider switchConnectionProviderArg) {
55         switchConnectionProvider = null;
56     }
57
58     /**
59      * dependencymanager requirement 
60      * @param switchConnectionProvider
61      */
62     public void setSwitchConnectionProvider(
63             SwitchConnectionProvider switchConnectionProvider) {
64         this.switchConnectionProvider = switchConnectionProvider;
65         registerProvider();
66     }
67
68     /**
69      * @return osgi context
70      */
71     public BundleContext getContext() {
72         return context;
73     }
74
75     /**
76      * dependencymanager requirement 
77      * @param context
78      */
79     public void setContext(BundleContext context) {
80         this.context = context;
81     }
82
83     @Override
84     public void onSessionInitiated(ProviderContext session) {
85         LOG.debug("onSessionInitiated");
86         messageCountProvider = new MessageSpyCounterImpl();
87         registrationManager = new SalRegistrationManager();
88         registrationManager.onSessionInitiated(session);
89         mdController = new MDController();
90         mdController.setSwitchConnectionProvider(switchConnectionProvider);
91         mdController.setMessageSpyCounter(messageCountProvider);
92         mdController.init();
93         mdController.start();
94         messageCountCommandProvider = new MessageCountCommandProvider(context, messageCountProvider);
95         messageCountCommandProvider.onSessionInitiated(session);
96     }
97     
98     @Override
99     public void close() {
100         LOG.debug("close");
101         mdController.stop();
102         mdController = null;
103         registrationManager.close();
104         registrationManager = null;
105         messageCountCommandProvider.close();
106         messageCountCommandProvider = null;
107     }
108
109     @Override
110     public void onSessionInitialized(ConsumerContext session) {
111         // NOOP
112     }
113
114     @Override
115     public Collection<? extends ProviderFunctionality> getFunctionality() {
116         return Collections.emptySet();
117     }
118
119     @Override
120     public java.util.Collection<? extends RpcService> getImplementations() {
121         return Collections.emptySet();
122     }
123
124     /**
125      * @return BA default broker
126      */
127     public BindingAwareBroker getBroker() {
128         return broker;
129     }
130
131     /**
132      * dependencymanager requirement 
133      * @param broker
134      */
135     public void setBroker(BindingAwareBroker broker) {
136         this.broker = broker;
137         registerProvider();
138     }
139
140     /**
141      * dependencymanager requirement 
142      * @param brokerArg
143      */
144     public void unsetBroker(BindingAwareBroker brokerArg) {
145         this.broker = null;
146     }
147
148     private boolean hasAllDependencies(){
149         if(this.broker != null && this.switchConnectionProvider != null) {
150             return true;
151         }
152         return false;
153     }
154     private void registerProvider() {
155         if(hasAllDependencies()) {
156             this.broker.registerProvider(this,context);
157         }
158     }
159  }