rewrite ofp-He to direct access md-sal services
[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.openflow.md.core.MDController;
21 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManager;
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.statistics.MessageSpyCounterImpl;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
27 import org.opendaylight.yangtools.yang.binding.DataContainer;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 /**
32  * OFPlugin provider implementation
33  */
34 public class OpenflowPluginProvider implements AutoCloseable, OpenFlowPluginExtensionRegistratorProvider {
35
36     private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
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     private OfpRole role;
49
50     private OFRoleManager roleManager;
51     private DataBroker dataBroker;
52     private NotificationProviderService notificationService;
53     private RpcProviderRegistry rpcRegistry;
54
55     /**
56      * Initialization of services and msgSpy counter
57      */
58     public void initialization() {
59         messageCountProvider = new MessageSpyCounterImpl();
60         extensionConverterManager = new ExtensionConverterManagerImpl();
61         roleManager = new OFRoleManager(OFSessionUtil.getSessionManager());
62
63         LOG.debug("dependencies gathered..");
64         registrationManager = new SalRegistrationManager();
65         registrationManager.setDataService(dataBroker);
66         registrationManager.setPublishService(notificationService);
67         registrationManager.setRpcProviderRegistry(rpcRegistry);
68         registrationManager.init();
69
70         mdController = new MDController();
71         mdController.setSwitchConnectionProviders(switchConnectionProviders);
72         mdController.setMessageSpyCounter(messageCountProvider);
73         mdController.setExtensionConverterProvider(extensionConverterManager);
74         mdController.init();
75         mdController.start();
76     }
77
78     /**
79      * @param switchConnectionProvider
80      */
81     public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
82         this.switchConnectionProviders = switchConnectionProvider;
83     }
84
85     @Override
86     public void close() {
87         LOG.debug("close");
88         mdController.stop();
89         mdController = null;
90         registrationManager.close();
91         registrationManager = null;
92     }
93
94     public MessageCountDumper getMessageCountDumper() {
95         return messageCountProvider;
96     }
97
98     /**
99      * @return the extensionConverterRegistry
100      */
101     @Override
102     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
103         return extensionConverterManager;
104     }
105
106     /**
107      * @param role of instance
108      */
109     public void setRole(OfpRole role) {
110         this.role = role;
111     }
112
113     /**
114      * @param newRole
115      */
116     public void fireRoleChange(OfpRole newRole) {
117         if (!role.equals(newRole)) {
118             LOG.debug("my role was chaged from {} to {}", role, newRole);
119             role = newRole;
120             switch (role) {
121             case BECOMEMASTER:
122                 //TODO: implement appropriate action
123                 roleManager.manageRoleChange(role);
124                 break;
125             case BECOMESLAVE:
126                 //TODO: implement appropriate action
127                 roleManager.manageRoleChange(role);
128                 break;
129             case NOCHANGE:
130                 //TODO: implement appropriate action
131                 roleManager.manageRoleChange(role);
132                 break;
133             default:
134                 LOG.warn("role not supported: {}", role);
135                 break;
136             }
137         }
138     }
139
140     public void setDataBroker(DataBroker dataBroker) {
141         this.dataBroker = dataBroker;
142     }
143
144     public void setNotificationService(NotificationProviderService notificationService) {
145         this.notificationService = notificationService;
146     }
147
148     public void setRpcRegistry(RpcProviderRegistry rpcRegistry) {
149         this.rpcRegistry = rpcRegistry;
150     }
151
152     @VisibleForTesting
153     protected RpcProviderRegistry getRpcRegistry() {
154         return rpcRegistry;
155     }
156
157     @VisibleForTesting
158     protected NotificationProviderService getNotificationService() {
159         return notificationService;
160     }
161
162     @VisibleForTesting
163     protected DataBroker getDataBroker() {
164         return dataBroker;
165     }
166 }