Merge "Topology Manager changes for cross checking Production switches"
[controller.git] / opendaylight / md-sal / forwardingrules-manager / src / main / java / org / opendaylight / controller / forwardingrulesmanager / consumer / impl / FRMConsumerImpl.java
1 /*
2  * Copyright (c) 2013 Ericsson , 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
9 package org.opendaylight.controller.forwardingrulesmanager.consumer.impl;
10
11 import org.eclipse.osgi.framework.console.CommandProvider;
12 import org.opendaylight.controller.clustering.services.IClusterContainerServices;
13 import org.opendaylight.controller.sal.binding.api.AbstractBindingAwareProvider;
14 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ProviderContext;
15 import org.opendaylight.controller.sal.binding.api.NotificationService;
16 import org.opendaylight.controller.sal.binding.api.data.DataBrokerService;
17 import org.opendaylight.controller.sal.binding.api.data.DataProviderService;
18 import org.opendaylight.controller.sal.core.IContainer;
19 import org.opendaylight.controller.sal.utils.ServiceHelper;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.FrameworkUtil;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 public class FRMConsumerImpl extends AbstractBindingAwareProvider implements CommandProvider {
26     protected static final Logger logger = LoggerFactory.getLogger(FRMConsumerImpl.class);
27     private static ProviderContext p_session;
28     private static DataBrokerService dataBrokerService;
29     private static NotificationService notificationService;
30     private FlowConsumerImpl flowImplRef;
31     private GroupConsumerImpl groupImplRef;
32     private static DataProviderService dataProviderService;
33
34         private static IClusterContainerServices clusterContainerService = null;
35         private static IContainer container;
36         
37         @Override
38     public void onSessionInitiated(ProviderContext session) {
39
40         FRMConsumerImpl.p_session = session;
41
42         if (!getDependentModule()) {
43             logger.error("Unable to fetch handlers for dependent modules");
44             System.out.println("Unable to fetch handlers for dependent modules");
45             return;
46         }
47
48         if (null != session) {
49             notificationService = session.getSALService(NotificationService.class);
50
51             if (null != notificationService) {
52                 dataBrokerService = session.getSALService(DataBrokerService.class);
53
54                 if (null != dataBrokerService) {
55                     dataProviderService = session.getSALService(DataProviderService.class);
56
57                     if (null != dataProviderService) {
58                         flowImplRef = new FlowConsumerImpl();
59                         // groupImplRef = new GroupConsumerImpl();
60                         registerWithOSGIConsole();
61                     } else {
62                         logger.error("Data Provider Service is down or NULL. "
63                                 + "Accessing data from configuration data store will not be possible");
64                         System.out.println("Data Broker Service is down or NULL.");
65                     }
66
67                 } else {
68                     logger.error("Data Broker Service is down or NULL.");
69                     System.out.println("Data Broker Service is down or NULL.");
70                 }
71             } else {
72                 logger.error("Notification Service is down or NULL.");
73                 System.out.println("Notification Service is down or NULL.");
74             }
75         } else {
76             logger.error("Consumer session is NULL. Please check if provider is registered");
77             System.out.println("Consumer session is NULL. Please check if provider is registered");
78         }
79
80     }
81
82     public static IClusterContainerServices getClusterContainerService() {
83         return clusterContainerService;
84     }
85
86     public static void setClusterContainerService(IClusterContainerServices clusterContainerService) {
87         FRMConsumerImpl.clusterContainerService = clusterContainerService;
88     }
89
90     public static IContainer getContainer() {
91         return container;
92     }
93
94     public static void setContainer(IContainer container) {
95         FRMConsumerImpl.container = container;
96     }
97
98     private void registerWithOSGIConsole() {
99         BundleContext bundleContext = FrameworkUtil.getBundle(this.getClass()).getBundleContext();
100         bundleContext.registerService(CommandProvider.class.getName(), this, null);
101     }
102         
103         private boolean getDependentModule() {
104             do {
105         clusterContainerService = (IClusterContainerServices) ServiceHelper.getGlobalInstance(IClusterContainerServices.class, this);
106         try {
107             Thread.sleep(4);
108         } catch (InterruptedException e) {
109             // TODO Auto-generated catch block
110             e.printStackTrace();
111         }
112             } while(clusterContainerService == null);
113             
114             do {
115                 
116             
117         container = (IContainer) ServiceHelper.getGlobalInstance(IContainer.class, this);
118         try {
119             Thread.sleep(5);
120         } catch (InterruptedException e) {
121             // TODO Auto-generated catch block
122             e.printStackTrace();
123         }
124             } while (container == null);
125             
126            
127         return true;
128         }
129
130         
131
132     public static DataProviderService getDataProviderService() {
133         return dataProviderService;
134     }
135
136     public FlowConsumerImpl getFlowImplRef() {
137         return flowImplRef;
138     }
139
140     public GroupConsumerImpl getGroupImplRef() {
141         return groupImplRef;
142     }
143                 
144
145     public static ProviderContext getProviderSession() {
146         return p_session;
147     }
148
149     public static NotificationService getNotificationService() {
150         return notificationService;
151     }
152
153     public static DataBrokerService getDataBrokerService() {
154         return dataBrokerService;
155     }
156
157     /*
158      * OSGI COMMANDS
159      */
160     @Override
161     public String getHelp() {
162         StringBuffer help = new StringBuffer();
163         return help.toString();
164     }
165
166 }