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