Merge "creating a default subnet"
[controller.git] / opendaylight / forwardingrulesmanager_mdsal / openflow / src / main / java / org / opendaylight / controller / forwardingrulesmanager_mdsal / consumer / impl / FRMConsumerImpl.java
1
2 /*
3  * Copyright (c) 2013 Ericsson , Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.forwardingrulesmanager_mdsal.consumer.impl;
11
12
13 import org.eclipse.osgi.framework.console.CommandProvider;
14 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
15 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
16 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
17 import org.opendaylight.controller.sal.binding.api.NotificationService;
18 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
19 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
20 import org.opendaylight.controller.sal.core.IContainer;
21 import org.opendaylight.controller.sal.utils.ServiceHelper;
22 import org.opendaylight.controller.switchmanager.ISwitchManager;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.FrameworkUtil;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28
29 public class FRMConsumerImpl extends AbstractBindingAwareProvider implements CommandProvider{
30         protected static final Logger logger = LoggerFactory.getLogger(FRMConsumerImpl.class);
31         private static ProviderContext p_session;
32     private static DataBrokerService dataBrokerService;          
33     private static NotificationService notificationService;     
34     private FlowConsumerImpl flowImplRef;
35         private GroupConsumerImpl groupImplRef;
36         private static DataProviderService dataProviderService;  
37
38         private static IClusterContainerServices clusterContainerService = null;
39         private static ISwitchManager switchManager;
40         private static IContainer container;
41         
42         @Override
43     public void onSessionInitiated(ProviderContext session) {
44         
45         FRMConsumerImpl.p_session = session;
46         
47         if (!getDependentModule()) {
48             logger.error("Unable to fetch handlers for dependent modules");
49             System.out.println("Unable to fetch handlers for dependent modules");
50             return;
51         }
52         
53         if (null != session) {
54                 notificationService = session.getSALService(NotificationService.class);
55                 
56                 if (null != notificationService) {
57                         dataBrokerService = session.getSALService(DataBrokerService.class);
58                         
59                         if (null != dataBrokerService) {
60                                 dataProviderService = session.getSALService(DataProviderService.class);
61                                 
62                                 if (null != dataProviderService) {
63                                         flowImplRef = new FlowConsumerImpl();
64                         //              groupImplRef = new GroupConsumerImpl();
65                                         registerWithOSGIConsole();
66                                 }
67                                 else {
68                                         logger.error("Data Provider Service is down or NULL. " +
69                                                         "Accessing data from configuration data store will not be possible");
70                         System.out.println("Data Broker Service is down or NULL.");
71                                 }
72                                         
73                         }
74                         else {
75                                 logger.error("Data Broker Service is down or NULL.");
76                         System.out.println("Data Broker Service is down or NULL.");
77                         }
78                 }
79                 else {
80                         logger.error("Notification Service is down or NULL.");
81                 System.out.println("Notification Service is down or NULL.");
82                 }                       
83         }
84         else {
85                 logger.error("Consumer session is NULL. Please check if provider is registered");
86                 System.out.println("Consumer session is NULL. Please check if provider is registered");
87         }
88   
89     }
90         
91         public static IClusterContainerServices getClusterContainerService() {
92         return clusterContainerService;
93     }
94
95     public static void setClusterContainerService(
96             IClusterContainerServices clusterContainerService) {
97         FRMConsumerImpl.clusterContainerService = clusterContainerService;
98     }
99
100     public static ISwitchManager getSwitchManager() {
101         return switchManager;
102     }
103
104     public static void setSwitchManager(ISwitchManager switchManager) {
105         FRMConsumerImpl.switchManager = switchManager;
106     }
107
108     public static IContainer getContainer() {
109         return container;
110     }
111
112     public static void setContainer(IContainer container) {
113         FRMConsumerImpl.container = container;
114     }
115
116     private void registerWithOSGIConsole() {
117         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
118         bundleContext.registerService(CommandProvider.class.getName(), this, null);
119     }
120         
121         private boolean getDependentModule() {
122             do {
123         clusterContainerService = (IClusterContainerServices) ServiceHelper.getGlobalInstance(IClusterContainerServices.class, this);
124         try {
125             Thread.sleep(4);
126         } catch (InterruptedException e) {
127             // TODO Auto-generated catch block
128             e.printStackTrace();
129         }
130             } while(clusterContainerService == null);
131             
132             do {
133                 
134             
135         container = (IContainer) ServiceHelper.getGlobalInstance(IContainer.class, this);
136         try {
137             Thread.sleep(5);
138         } catch (InterruptedException e) {
139             // TODO Auto-generated catch block
140             e.printStackTrace();
141         }
142             } while (container == null);
143             
144             do {
145                 switchManager = (ISwitchManager) ServiceHelper.getInstance(ISwitchManager.class, container.getName(), this);
146                 try {
147                     Thread.sleep(5);
148                 } catch (InterruptedException e) {
149                     // TODO Auto-generated catch block
150                     e.printStackTrace();
151                 }
152             } while(null == switchManager);
153         return true;
154         }
155
156         
157
158     public static DataProviderService getDataProviderService() {
159                 return dataProviderService;
160         }
161
162         public FlowConsumerImpl getFlowImplRef() {
163             return flowImplRef;
164         }
165
166         public GroupConsumerImpl getGroupImplRef() {
167             return groupImplRef;
168         }
169          
170         public static ProviderContext getProviderSession() {
171                 return p_session;
172         }       
173
174         public static NotificationService getNotificationService() {
175                 return notificationService;
176         }
177
178         public static DataBrokerService getDataBrokerService() {
179                 return dataBrokerService;
180         }
181         
182         /*
183      * OSGI COMMANDS
184      */
185     @Override
186     public String getHelp() {
187         StringBuffer help = new StringBuffer();
188         return help.toString();
189     }
190
191 }
192