c151c04faa30ad41317fe1e2a9386d8145a56169
[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     /**
60      * Initialization of services and msgSpy counter
61      */
62     public void initialization() {
63         messageCountProvider = new MessageSpyCounterImpl();
64         extensionConverterManager = new ExtensionConverterManagerImpl();
65         roleManager = new OFRoleManager(OFSessionUtil.getSessionManager());
66         entManager = new OfEntityManager(entityOwnershipService);
67         entManager.setDataBroker(dataBroker);
68
69         LOG.debug("dependencies gathered..");
70         registrationManager = new SalRegistrationManager();
71         registrationManager.setDataService(dataBroker);
72         registrationManager.setPublishService(notificationService);
73         registrationManager.setRpcProviderRegistry(rpcRegistry);
74         registrationManager.setOfEntityManager(entManager);
75         registrationManager.init();
76
77         mdController = new MDController();
78         mdController.setSwitchConnectionProviders(switchConnectionProviders);
79         mdController.setMessageSpyCounter(messageCountProvider);
80         mdController.setExtensionConverterProvider(extensionConverterManager);
81         mdController.init();
82         mdController.start();
83     }
84
85     /**
86      * @param switchConnectionProvider switch connection provider
87      */
88     public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
89         this.switchConnectionProviders = switchConnectionProvider;
90     }
91
92     @Override
93     public void close() {
94         LOG.debug("close");
95         mdController.stop();
96         mdController = null;
97         registrationManager.close();
98         registrationManager = null;
99     }
100
101     public MessageCountDumper getMessageCountDumper() {
102         return messageCountProvider;
103     }
104
105     /**
106      * @return the extensionConverterRegistry
107      */
108     @Override
109     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
110         return extensionConverterManager;
111     }
112
113     /**
114      * @param role of instance
115      */
116     public void setRole(OfpRole role) {
117         this.role = role;
118     }
119
120     /**
121      * @param newRole new controller role
122      */
123     public void fireRoleChange(OfpRole newRole) {
124         if (!role.equals(newRole)) {
125             LOG.debug("Controller role was changed from {} to {}", role, newRole);
126             role = newRole;
127             switch (role) {
128                 case BECOMEMASTER:
129                     //TODO: implement appropriate action
130                     roleManager.manageRoleChange(role);
131                     break;
132                 case BECOMESLAVE:
133                     //TODO: implement appropriate action
134                     roleManager.manageRoleChange(role);
135                     break;
136                 case NOCHANGE:
137                     //TODO: implement appropriate action
138                     roleManager.manageRoleChange(role);
139                     break;
140                 default:
141                     LOG.warn("role not supported: {}", role);
142                     break;
143             }
144         }
145     }
146
147     public void setDataBroker(DataBroker dataBroker) {
148         this.dataBroker = dataBroker;
149     }
150
151     public void setNotificationService(NotificationProviderService notificationService) {
152         this.notificationService = notificationService;
153     }
154
155     public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
156         this.rpcRegistry = rpcRegistry;
157     }
158
159     public void setEntityOwnershipService(EntityOwnershipService entityOwnershipService) {
160         this.entityOwnershipService = entityOwnershipService;
161     }
162
163     @VisibleForTesting
164     protected RpcProviderRegistry getRpcRegistry() {
165         return rpcRegistry;
166     }
167
168     @VisibleForTesting
169     protected NotificationProviderService getNotificationService() {
170         return notificationService;
171     }
172
173     @VisibleForTesting
174     protected DataBroker getDataBroker() {
175         return dataBroker;
176     }
177 }