7cbbc262e16670dbca9b806ef5da244de3afd27f
[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 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
16 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
17 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
18 import org.opendaylight.openflowplugin.openflow.md.core.MDController;
19 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManager;
20 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
21 import org.opendaylight.openflowplugin.api.statistics.MessageCountDumper;
22 import org.opendaylight.openflowplugin.api.statistics.MessageObservatory;
23 import org.opendaylight.openflowplugin.statistics.MessageSpyCounterImpl;
24 import org.opendaylight.yangtools.yang.binding.DataContainer;
25 import org.opendaylight.yangtools.yang.binding.RpcService;
26 import org.osgi.framework.BundleContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * OFPlugin provider implementation
32  */
33 public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseable {
34
35     private static Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
36
37     private BindingAwareBroker broker;
38
39     private BundleContext context;
40
41     private Collection<SwitchConnectionProvider> switchConnectionProviders;
42
43     private MDController mdController;
44
45     private MessageObservatory<DataContainer> messageCountProvider;
46
47     private SalRegistrationManager registrationManager;
48
49     private ExtensionConverterManager extensionConverterManager;
50
51     /**
52      * Initialization of services and msgSpy counter
53      */
54     public void initialization() {
55         messageCountProvider = new MessageSpyCounterImpl();
56         extensionConverterManager = new ExtensionConverterManagerImpl();
57         this.registerProvider();
58     }
59
60     /**
61      * @param switchConnectionProvider
62      */
63     public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
64         this.switchConnectionProviders = switchConnectionProvider;
65     }
66
67     /**
68      * @return osgi context
69      */
70     public BundleContext getContext() {
71         return context;
72     }
73
74     @Override
75     public void onSessionInitiated(ProviderContext session) {
76         LOG.debug("onSessionInitiated");
77         registrationManager = new SalRegistrationManager();
78         registrationManager.onSessionInitiated(session);
79         mdController = new MDController();
80         mdController.setSwitchConnectionProviders(switchConnectionProviders);
81         mdController.setMessageSpyCounter(messageCountProvider);
82         mdController.setExtensionConverterProvider(extensionConverterManager);
83         mdController.init();
84         mdController.start();
85     }
86
87     @Override
88     public void close() {
89         LOG.debug("close");
90         mdController.stop();
91         mdController = null;
92         registrationManager.close();
93         registrationManager = null;
94     }
95
96     /**
97      * @return BA default broker
98      */
99     public BindingAwareBroker getBroker() {
100         return broker;
101     }
102
103     /**
104      * dependencymanager requirement
105      *
106      * @param broker
107      */
108     public void setBroker(BindingAwareBroker broker) {
109         this.broker = broker;
110     }
111
112     /**
113      * dependencymanager requirement
114      *
115      * @param brokerArg
116      */
117     public void unsetBroker(BindingAwareBroker brokerArg) {
118         this.broker = null;
119     }
120
121     private boolean hasAllDependencies() {
122         if (this.broker != null && this.switchConnectionProviders != null) {
123             return true;
124         }
125         return false;
126     }
127
128     /**
129      * register providers for md-sal
130      */
131     private void registerProvider() {
132         if (hasAllDependencies()) {
133             this.broker.registerProvider(this, context);
134         }
135     }
136
137     public MessageCountDumper getMessageCountDumper() {
138         return messageCountProvider;
139     }
140
141     /**
142      * @return the extensionConverterRegistry
143      */
144     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
145         return extensionConverterManager;
146     }
147 }