BUG-5636: making table features configurable for the He plugin.
[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.sal.binding.api.NotificationProviderService;
14 import org.opendaylight.controller.sal.binding.api.RpcProviderRegistry;
15 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
16 import org.opendaylight.openflowplugin.api.openflow.statistics.MessageCountDumper;
17 import org.opendaylight.openflowplugin.api.openflow.statistics.MessageObservatory;
18 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
19 import org.opendaylight.openflowplugin.extension.api.OpenFlowPluginExtensionRegistratorProvider;
20 import org.opendaylight.openflowplugin.extension.api.core.extension.ExtensionConverterManager;
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.session.OFRoleManager;
24 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
25 import org.opendaylight.openflowplugin.openflow.md.core.role.OfEntityManager;
26 import org.opendaylight.openflowplugin.statistics.MessageSpyCounterImpl;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
28 import org.opendaylight.yangtools.yang.binding.DataContainer;
29 import org.opendaylight.controller.md.sal.common.api.clustering.EntityOwnershipService;
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 Collection<SwitchConnectionProvider> switchConnectionProviders;
41
42     private MDController mdController;
43
44     private MessageObservatory<DataContainer> messageCountProvider;
45
46     private SalRegistrationManager registrationManager;
47
48     private ExtensionConverterManager extensionConverterManager;
49
50     private OfpRole role;
51
52     private OFRoleManager roleManager;
53     private OfEntityManager entManager;
54     private DataBroker dataBroker;
55     private NotificationProviderService notificationService;
56     private RpcProviderRegistry rpcRegistry;
57     private EntityOwnershipService entityOwnershipService;
58
59     private OpenflowPluginConfig openflowPluginConfig;
60
61     /**
62      * Initialization of services and msgSpy counter
63      */
64     public void initialization() {
65         messageCountProvider = new MessageSpyCounterImpl();
66         extensionConverterManager = new ExtensionConverterManagerImpl();
67         roleManager = new OFRoleManager(OFSessionUtil.getSessionManager());
68         entManager = new OfEntityManager(entityOwnershipService,getOpenflowPluginConfig());
69         entManager.setDataBroker(dataBroker);
70
71         LOG.debug("dependencies gathered..");
72         registrationManager = new SalRegistrationManager();
73         registrationManager.setDataService(dataBroker);
74         registrationManager.setPublishService(notificationService);
75         registrationManager.setRpcProviderRegistry(rpcRegistry);
76         registrationManager.setOfEntityManager(entManager);
77         registrationManager.init();
78
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     /**
88      * @param switchConnectionProvider switch connection provider
89      */
90     public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
91         this.switchConnectionProviders = switchConnectionProvider;
92     }
93
94     @Override
95     public void close() {
96         LOG.debug("close");
97         mdController.stop();
98         mdController = null;
99         registrationManager.close();
100         registrationManager = null;
101     }
102
103     public MessageCountDumper getMessageCountDumper() {
104         return messageCountProvider;
105     }
106
107     /**
108      * @return the extensionConverterRegistry
109      */
110     @Override
111     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
112         return extensionConverterManager;
113     }
114
115     /**
116      * @param role of instance
117      */
118     public void setRole(OfpRole role) {
119         this.role = role;
120     }
121
122     /**
123      * @param newRole new controller role
124      */
125     public void fireRoleChange(OfpRole newRole) {
126         if (!role.equals(newRole)) {
127             LOG.debug("Controller role was changed from {} to {}", role, newRole);
128             role = newRole;
129             switch (role) {
130                 case BECOMEMASTER:
131                     //TODO: implement appropriate action
132                     roleManager.manageRoleChange(role);
133                     break;
134                 case BECOMESLAVE:
135                     //TODO: implement appropriate action
136                     roleManager.manageRoleChange(role);
137                     break;
138                 case NOCHANGE:
139                     //TODO: implement appropriate action
140                     roleManager.manageRoleChange(role);
141                     break;
142                 default:
143                     LOG.warn("role not supported: {}", role);
144                     break;
145             }
146         }
147     }
148
149     public void setDataBroker(DataBroker dataBroker) {
150         this.dataBroker = dataBroker;
151     }
152
153     public void setNotificationService(NotificationProviderService notificationService) {
154         this.notificationService = notificationService;
155     }
156
157     public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
158         this.rpcRegistry = rpcRegistry;
159     }
160
161     public void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
162         this.entityOwnershipService = entityOwnershipService;
163     }
164
165     public void setOpenflowPluginConfig(OpenflowPluginConfig openflowPluginConfig) {
166         this.openflowPluginConfig = openflowPluginConfig;
167     }
168
169     @VisibleForTesting
170     public OpenflowPluginConfig getOpenflowPluginConfig() {
171         return openflowPluginConfig;
172     }
173
174     @VisibleForTesting
175     protected RpcProviderRegistry getRpcRegistry() {
176         return rpcRegistry;
177     }
178
179     @VisibleForTesting
180     protected NotificationProviderService getNotificationService() {
181         return notificationService;
182     }
183
184     @VisibleForTesting
185     protected DataBroker getDataBroker() {
186         return dataBroker;
187     }
188 }