Bug 1544 - Explicit LLDP flow to punt whole LLDP packets to the controller
[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.md.sal.common.api.data.AsyncDataBroker;
14 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
17 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
18 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
19 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
20 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
21 import org.opendaylight.openflowplugin.openflow.md.core.MDController;
22 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
23 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManager;
24 import org.opendaylight.openflowplugin.openflow.md.lldp.LLDPPAcketPuntEnforcer;
25 import org.opendaylight.openflowplugin.statistics.MessageCountCommandProvider;
26 import org.opendaylight.openflowplugin.statistics.MessageCountDumper;
27 import org.opendaylight.openflowplugin.statistics.MessageObservatory;
28 import org.opendaylight.openflowplugin.statistics.MessageSpyCounterImpl;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.service.rev130819.SalFlowService;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.Nodes;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.Node;
32 import org.opendaylight.yangtools.yang.binding.DataContainer;
33 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
34 import org.opendaylight.yangtools.yang.binding.RpcService;
35 import org.osgi.framework.BundleContext;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 /**
40  * OFPlugin provider implementation
41  */
42 public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseable {
43
44     private static Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
45
46     private BindingAwareBroker broker;
47
48     private BundleContext context;
49
50     private Collection<SwitchConnectionProvider> switchConnectionProviders;
51
52     private MDController mdController;
53
54     private MessageCountCommandProvider messageCountCommandProvider;
55
56     private MessageObservatory<DataContainer> messageCountProvider;
57
58     private SalRegistrationManager registrationManager;
59     
60     private ExtensionConverterManager extensionConverterManager;  
61
62     /**
63      * Initialization of services and msgSpy counter
64      */
65     public void initialization() {
66         messageCountProvider = new MessageSpyCounterImpl();
67         extensionConverterManager = new ExtensionConverterManagerImpl();
68         this.registerProvider();
69     }
70
71     /**
72      * @param switchConnectionProvider
73      */
74     public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
75         this.switchConnectionProviders = switchConnectionProvider;
76     }
77
78     /**
79      * @return osgi context
80      */
81     public BundleContext getContext() {
82         return context;
83     }
84
85     /**
86      * dependencymanager requirement
87      * @param context
88      *
89      * @deprecated we should stop relying on osgi to provide cli interface for messageCounter
90      */
91     @Deprecated
92     public void setContext(BundleContext context) {
93         this.context = context;
94     }
95
96     @Override
97     public void onSessionInitiated(ProviderContext session) {
98         LOG.debug("onSessionInitiated");
99         registrationManager = new SalRegistrationManager();
100         registrationManager.onSessionInitiated(session);
101         //TODO : LLDPPAcketPuntEnforcer should be instantiated and registered in separate module driven by config subsystem
102         InstanceIdentifier path = InstanceIdentifier.create(Nodes.class).child(Node.class);
103         registrationManager.getSessionManager().getDataBroker().registerDataChangeListener(
104                 LogicalDatastoreType.OPERATIONAL,
105                 path,
106                 new LLDPPAcketPuntEnforcer(
107                         session.<SalFlowService>getRpcService(SalFlowService.class)),
108                 AsyncDataBroker.DataChangeScope.BASE);
109         mdController = new MDController();
110         mdController.setSwitchConnectionProviders(switchConnectionProviders);
111         mdController.setMessageSpyCounter(messageCountProvider);
112         mdController.setExtensionConverterProvider(extensionConverterManager);
113         mdController.init();
114         mdController.start();
115         messageCountCommandProvider = new MessageCountCommandProvider(context, messageCountProvider);
116         messageCountCommandProvider.onSessionInitiated(session);
117     }
118
119     @Override
120     public void close() {
121         LOG.debug("close");
122         mdController.stop();
123         mdController = null;
124         registrationManager.close();
125         registrationManager = null;
126         messageCountCommandProvider.close();
127         messageCountCommandProvider = null;
128     }
129
130     @Override
131     public void onSessionInitialized(ConsumerContext session) {
132         // NOOP
133     }
134
135     @Override
136     public Collection<? extends ProviderFunctionality> getFunctionality() {
137         return Collections.emptySet();
138     }
139
140     @Override
141     public java.util.Collection<? extends RpcService> getImplementations() {
142         return Collections.emptySet();
143     }
144
145     /**
146      * @return BA default broker
147      */
148     public BindingAwareBroker getBroker() {
149         return broker;
150     }
151
152     /**
153      * dependencymanager requirement
154      * @param broker
155      */
156     public void setBroker(BindingAwareBroker broker) {
157         this.broker = broker;
158     }
159
160     /**
161      * dependencymanager requirement
162      * @param brokerArg
163      */
164     public void unsetBroker(BindingAwareBroker brokerArg) {
165         this.broker = null;
166     }
167
168     private boolean hasAllDependencies(){
169         if(this.broker != null && this.switchConnectionProviders != null) {
170             return true;
171         }
172         return false;
173     }
174
175     /**
176      * register providers for md-sal
177      */
178     private void registerProvider() {
179         if(hasAllDependencies()) {
180             this.broker.registerProvider(this,context);
181         }
182     }
183
184     public MessageCountDumper getMessageCountDumper() {
185         return messageCountProvider;
186     }
187     
188     /**
189      * @return the extensionConverterRegistry
190      */
191     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
192         return extensionConverterManager;
193     }
194  }