Bug-2827: role switch proposal
[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 java.util.Collection;
11 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
12 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
13 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
14 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
15 import org.opendaylight.openflowplugin.extension.api.ExtensionConverterRegistrator;
16 import org.opendaylight.openflowplugin.openflow.md.core.MDController;
17 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManager;
18 import org.opendaylight.openflowplugin.openflow.md.core.extension.ExtensionConverterManagerImpl;
19 import org.opendaylight.openflowplugin.openflow.md.core.session.OFRoleManager;
20 import org.opendaylight.openflowplugin.openflow.md.core.session.OFSessionUtil;
21 import org.opendaylight.openflowplugin.api.statistics.MessageCountDumper;
22 import org.opendaylight.openflowplugin.api.statistics.MessageObservatory;
23 import org.opendaylight.openflowplugin.statistics.MessageSpyCounterImpl;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.openflow.common.config.impl.rev140326.OfpRole;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.osgi.framework.BundleContext;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 /**
31  * OFPlugin provider implementation
32  */
33 public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseable {
34
35     private static final Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
36
37     private BindingAwareBroker broker;
38
39     private BundleContext context;
40
41     private Collection<SwitchConnectionProvider> switchConnectionProviders;
42
43     private MDController mdController;
44
45     private MessageObservatory<DataContainer> messageCountProvider;
46
47     private SalRegistrationManager registrationManager;
48
49     private ExtensionConverterManager extensionConverterManager;
50
51     private OfpRole role;
52
53     private OFRoleManager roleManager;
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         this.registerProvider();
63     }
64
65     /**
66      * @param switchConnectionProvider
67      */
68     public void setSwitchConnectionProviders(Collection<SwitchConnectionProvider> switchConnectionProvider) {
69         this.switchConnectionProviders = switchConnectionProvider;
70     }
71
72     /**
73      * @return osgi context
74      */
75     public BundleContext getContext() {
76         return context;
77     }
78
79     @Override
80     public void onSessionInitiated(ProviderContext session) {
81         LOG.debug("onSessionInitiated");
82         registrationManager = new SalRegistrationManager();
83         registrationManager.onSessionInitiated(session);
84         mdController = new MDController();
85         mdController.setSwitchConnectionProviders(switchConnectionProviders);
86         mdController.setMessageSpyCounter(messageCountProvider);
87         mdController.setExtensionConverterProvider(extensionConverterManager);
88         mdController.init();
89         mdController.start();
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     /**
102      * @return BA default broker
103      */
104     public BindingAwareBroker getBroker() {
105         return broker;
106     }
107
108     /**
109      * dependencymanager requirement
110      *
111      * @param broker
112      */
113     public void setBroker(BindingAwareBroker broker) {
114         this.broker = broker;
115     }
116
117     /**
118      * dependencymanager requirement
119      *
120      * @param brokerArg
121      */
122     public void unsetBroker(BindingAwareBroker brokerArg) {
123         this.broker = null;
124     }
125
126     private boolean hasAllDependencies() {
127         if (this.broker != null && this.switchConnectionProviders != null) {
128             return true;
129         }
130         return false;
131     }
132
133     /**
134      * register providers for md-sal
135      */
136     private void registerProvider() {
137         if (hasAllDependencies()) {
138             this.broker.registerProvider(this, context);
139         }
140     }
141
142     public MessageCountDumper getMessageCountDumper() {
143         return messageCountProvider;
144     }
145
146     /**
147      * @return the extensionConverterRegistry
148      */
149     public ExtensionConverterRegistrator getExtensionConverterRegistrator() {
150         return extensionConverterManager;
151     }
152
153     /**
154      * @param role of instance
155      */
156     public void setRole(OfpRole role) {
157         this.role = role;
158     }
159
160     /**
161      * @param newRole
162      */
163     public void fireRoleChange(OfpRole newRole) {
164         if (!role.equals(newRole)) {
165             LOG.debug("my role was chaged from {} to {}", role, newRole);
166             role = newRole;
167             switch (role) {
168             case BECOMEMASTER:
169                 //TODO: implement appropriate action
170                 roleManager.manageRoleChange(role);
171                 break;
172             case BECOMESLAVE:
173                 //TODO: implement appropriate action
174                 roleManager.manageRoleChange(role);
175                 break;
176             case NOCHANGE:
177                 //TODO: implement appropriate action
178                 roleManager.manageRoleChange(role);
179                 break;
180             default:
181                 LOG.warn("role not supported: {}", role);
182                 break;
183             }
184         }
185     }
186 }