91fe5271ded349f6231d8fd8c095936cfc8f5a32
[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 java.util.Collections;
12
13 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
15 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareProvider;
17 import org.opendaylight.openflowjava.protocol.spi.connection.SwitchConnectionProvider;
18 import org.opendaylight.openflowplugin.openflow.md.core.MDController;
19 import org.opendaylight.openflowplugin.openflow.md.core.cmd.MessageCountCommandProvider;
20 import org.opendaylight.openflowplugin.openflow.md.queue.MessageObservatory;
21 import org.opendaylight.openflowplugin.openflow.md.queue.MessageSpyCounterImpl;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflow.protocol.rev130731.OfHeader;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.RpcService;
25 import org.osgi.framework.BundleContext;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
29 /**
30  * OFPlugin provider implementation
31  */
32 public class OpenflowPluginProvider implements BindingAwareProvider, AutoCloseable {
33     
34     private static Logger LOG = LoggerFactory.getLogger(OpenflowPluginProvider.class);
35     
36     private BindingAwareBroker broker;
37
38     private BundleContext context;
39
40     private SwitchConnectionProvider switchConnectionProvider;
41
42     private MDController mdController;
43     
44     private MessageCountCommandProvider messageCountCommandProvider;
45
46     private MessageObservatory<OfHeader, DataObject> messageCountProvider;
47     
48     private SalRegistrationManager registrationManager;
49     
50     public void unsetSwitchConnectionProvider() {
51         switchConnectionProvider = null;
52     }
53
54     public void setSwitchConnectionProvider(
55             SwitchConnectionProvider switchConnectionProvider) {
56         this.switchConnectionProvider = switchConnectionProvider;
57         registerProvider();
58     }
59
60     public BundleContext getContext() {
61         return context;
62     }
63
64     public void setContext(BundleContext context) {
65         this.context = context;
66     }
67
68     @Override
69     public void onSessionInitiated(ProviderContext session) {
70         LOG.debug("onSessionInitiated");
71         messageCountProvider = new MessageSpyCounterImpl();
72         registrationManager = new SalRegistrationManager();
73         registrationManager.onSessionInitiated(session);
74         mdController = new MDController();
75         mdController.setSwitchConnectionProvider(switchConnectionProvider);
76         mdController.setMessageSpyCounter(messageCountProvider);
77         mdController.init();
78         mdController.start();
79         messageCountCommandProvider = new MessageCountCommandProvider(context, messageCountProvider);
80         messageCountCommandProvider.onSessionInitiated(session);
81     }
82     
83     @Override
84     public void close() {
85         LOG.debug("close");
86         mdController.stop();
87         mdController = null;
88         registrationManager.close();
89         registrationManager = null;
90         messageCountCommandProvider.close();
91         messageCountCommandProvider = null;
92     }
93
94     @Override
95     public void onSessionInitialized(ConsumerContext session) {
96         // NOOP
97     }
98
99     @Override
100     public Collection<? extends ProviderFunctionality> getFunctionality() {
101         return Collections.emptySet();
102     }
103
104     @Override
105     public java.util.Collection<? extends RpcService> getImplementations() {
106         return Collections.emptySet();
107     }
108
109     public BindingAwareBroker getBroker() {
110         return broker;
111     }
112
113     public void setBroker(BindingAwareBroker broker) {
114         this.broker = broker;
115         registerProvider();
116     }
117
118     public void unsetBroker(BindingAwareBroker broker) {
119         this.broker = null;
120     }
121
122     private boolean hasAllDependencies(){
123         if(this.broker != null && this.switchConnectionProvider != null) {
124             return true;
125         }
126         return false;
127     }
128     private void registerProvider() {
129         if(hasAllDependencies()) {
130             this.broker.registerProvider(this,context);
131         }
132     }
133  }