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