Merge "Bug 5916: He plugin: Wake up statistics collector thread if RPC fails."
[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 com.google.common.annotations.VisibleForTesting;
11 import java.util.Collection;
12 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
13 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
14 import org.opendaylight.controller.sal.binding.api.NotificationProviderService;
15 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
16 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.MessageCountDumper;
18 import org.opendaylight.openflowplugin.api.openflow.statistics.MessageObservatory;
19 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
20 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
21 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterManager;
22 import org.opendaylight.openflowplugin.openflow.md.core.MDController;
23 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
24 import org.opendaylight.openflowplugin.openflow.md.core.role.OfEntityManager;
25 import org.opendaylight.openflowplugin.openflow.md.core.session.OFRoleManager;
26 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
27 import org.opendaylight.openflowplugin.statistics.MessageSpyCounterImpl;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
29 import org.opendaylight.yangtools.yang.binding.DataContainer;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 /**
34  * OFPlugin provider implementation
35  */
36 public class OpenflowPluginProvider implements AutoCloseable, OpenFlowPluginExtensionRegistratorProvider {
37
38     private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
39
40     private static final boolean SKIP_TABLE_FEATURES = false;
41
42     private Collection<SwitchConnectionProvider> switchConnectionProviders;
43
44     private MDController mdController;
45
46     private MessageObservatory<DataContainer> messageCountProvider;
47
48     private SalRegistrationManager registrationManager;
49
50     private ExtensionConverterManager extensionConverterManager;
51
52     private OfpRole role;
53     private Boolean skipTableFeatures;
54
55     private OFRoleManager roleManager;
56     private OfEntityManager entManager;
57     private DataBroker dataBroker;
58     private NotificationProviderService notificationService;
59     private RpcProviderRegistry rpcRegistry;
60     private EntityOwnershipService entityOwnershipService;
61
62     private OpenflowPluginConfig openflowPluginConfig;
63
64
65
66     /**
67      * Initialization of services and msgSpy counter
68      */
69     public void initialization() {
70         messageCountProvider = new MessageSpyCounterImpl();
71         extensionConverterManager = new ExtensionConverterManagerImpl();
72         roleManager = new OFRoleManager(OFSessionUtil.getSessionManager());
73         openflowPluginConfig = readConfig(skipTableFeatures);
74         entManager = new OfEntityManager(entityOwnershipService,getOpenflowPluginConfig());
75         entManager.setDataBroker(dataBroker);
76         entManager.init();
77
78         LOG.debug("dependencies gathered..");
79         registrationManager = new SalRegistrationManager();
80         registrationManager.setDataService(dataBroker);
81         registrationManager.setPublishService(notificationService);
82         registrationManager.setRpcProviderRegistry(rpcRegistry);
83         registrationManager.setOfEntityManager(entManager);
84         registrationManager.init();
85
86         mdController = new MDController();
87         mdController.setSwitchConnectionProviders(switchConnectionProviders);
88         mdController.setMessageSpyCounter(messageCountProvider);
89         mdController.setExtensionConverterProvider(extensionConverterManager);
90         mdController.init();
91         mdController.start();
92     }
93
94     /**
95      * @param switchConnectionProvider switch connection provider
96      */
97     public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
98         this.switchConnectionProviders = switchConnectionProvider;
99     }
100
101     @Override
102     public void close() {
103         LOG.debug("close");
104
105         if(mdController != null) {
106             mdController.stop();
107             mdController = null;
108         }
109
110         if(registrationManager != null) {
111             registrationManager.close();
112             registrationManager = null;
113         }
114     }
115
116     public MessageCountDumper getMessageCountDumper() {
117         return messageCountProvider;
118     }
119
120     /**
121      * @return the extensionConverterRegistry
122      */
123     @Override
124     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
125         return extensionConverterManager;
126     }
127
128     /**
129      * @param role of instance
130      */
131     public void setRole(OfpRole role) {
132         this.role = role;
133     }
134
135     /**
136      * @param newRole new controller role
137      */
138     public void fireRoleChange(OfpRole newRole) {
139         if (!role.equals(newRole)) {
140             LOG.debug("Controller role was changed from {} to {}", role, newRole);
141             role = newRole;
142             switch (role) {
143                 case BECOMEMASTER:
144                     //TODO: implement appropriate action
145                     roleManager.manageRoleChange(role);
146                     break;
147                 case BECOMESLAVE:
148                     //TODO: implement appropriate action
149                     roleManager.manageRoleChange(role);
150                     break;
151                 case NOCHANGE:
152                     //TODO: implement appropriate action
153                     roleManager.manageRoleChange(role);
154                     break;
155                 default:
156                     LOG.warn("role not supported: {}", role);
157                     break;
158             }
159         }
160     }
161
162     private OpenflowPluginConfig readConfig(Boolean skipTableFeatures){
163
164         final OpenflowPluginConfig.OpenflowPluginConfigBuilder openflowCfgBuilder = OpenflowPluginConfig.builder();
165
166         if(skipTableFeatures !=null){
167             openflowCfgBuilder.setSkipTableFeatures(skipTableFeatures.booleanValue());
168         } else{
169             LOG.warn("Could not load XML configuration file via ConfigSubsystem! Fallback to default config value(s)");
170             openflowCfgBuilder.setSkipTableFeatures(SKIP_TABLE_FEATURES);
171         }
172
173         return openflowCfgBuilder.build();
174     }
175
176     public void setDataBroker(DataBroker dataBroker) {
177         this.dataBroker = dataBroker;
178     }
179
180     public void setNotificationService(NotificationProviderService notificationService) {
181         this.notificationService = notificationService;
182     }
183
184     public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
185         this.rpcRegistry = rpcRegistry;
186     }
187
188     public void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
189         this.entityOwnershipService = entityOwnershipService;
190     }
191
192     public void setSkipTableFeatures(Boolean skipTableFeatures) {
193         this.skipTableFeatures = skipTableFeatures;
194     }
195
196     @VisibleForTesting
197     public OpenflowPluginConfig getOpenflowPluginConfig() {
198         return openflowPluginConfig;
199     }
200
201     @VisibleForTesting
202     protected RpcProviderRegistry getRpcRegistry() {
203         return rpcRegistry;
204     }
205
206     @VisibleForTesting
207     protected NotificationProviderService getNotificationService() {
208         return notificationService;
209     }
210
211     @VisibleForTesting
212     protected DataBroker getDataBroker() {
213         return dataBroker;
214     }
215 }