6c7de9d5adff50eaaef1029c6eb7765f399ad386
[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 Collection<SwitchConnectionProvider> switchConnectionProviders;
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      * @param switchConnectionProvider
52      */
53     public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
54         this.switchConnectionProviders = switchConnectionProvider;
55     }
56
57     /**
58      * @return osgi context
59      */
60     public BundleContext getContext() {
61         return context;
62     }
63
64     /**
65      * dependencymanager requirement 
66      * @param context
67      * 
68      * @deprecated we should stop relying on osgi to provide cli interface for messageCounter 
69      */
70     @Deprecated
71     public void setContext(BundleContext context) {
72         this.context = context;
73     }
74
75     @Override
76     public void onSessionInitiated(ProviderContext session) {
77         LOG.debug("onSessionInitiated");
78         messageCountProvider = new MessageSpyCounterImpl();
79         registrationManager = new SalRegistrationManager();
80         registrationManager.onSessionInitiated(session);
81         mdController = new MDController();
82         mdController.setSwitchConnectionProviders(switchConnectionProviders);
83         mdController.setMessageSpyCounter(messageCountProvider);
84         mdController.init();
85         mdController.start();
86         messageCountCommandProvider = new MessageCountCommandProvider(context, messageCountProvider);
87         messageCountCommandProvider.onSessionInitiated(session);
88     }
89     
90     @Override
91     public void close() {
92         LOG.debug("close");
93         mdController.stop();
94         mdController = null;
95         registrationManager.close();
96         registrationManager = null;
97         messageCountCommandProvider.close();
98         messageCountCommandProvider = null;
99     }
100
101     @Override
102     public void onSessionInitialized(ConsumerContext session) {
103         // NOOP
104     }
105
106     @Override
107     public Collection<? extends ProviderFunctionality> getFunctionality() {
108         return Collections.emptySet();
109     }
110
111     @Override
112     public java.util.Collection<? extends RpcService> getImplementations() {
113         return Collections.emptySet();
114     }
115
116     /**
117      * @return BA default broker
118      */
119     public BindingAwareBroker getBroker() {
120         return broker;
121     }
122
123     /**
124      * dependencymanager requirement 
125      * @param broker
126      */
127     public void setBroker(BindingAwareBroker broker) {
128         this.broker = broker;
129     }
130
131     /**
132      * dependencymanager requirement 
133      * @param brokerArg
134      */
135     public void unsetBroker(BindingAwareBroker brokerArg) {
136         this.broker = null;
137     }
138
139     private boolean hasAllDependencies(){
140         if(this.broker != null && this.switchConnectionProviders != null) {
141             return true;
142         }
143         return false;
144     }
145     
146     /**
147      * register providers for md-sal
148      */
149     public void registerProvider() {
150         //FIXME: is it needed
151         if(hasAllDependencies()) {
152             this.broker.registerProvider(this,context);
153         }
154     }
155  }