Fixed as per comments group and meter
[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 MeterConsumerImpl meterImplRef;
33     private static DataProviderService dataProviderService;
34     private static IClusterContainerServices clusterContainerService = null;
35     private static IContainer container;
36     
37     @Override
38     public void onSessionInitiated(ProviderContext session) {
39         FRMConsumerImpl.p_session = session;
40
41         if (!getDependentModule()) {
42             logger.error("Unable to fetch handlers for dependent modules");
43             System.out.println("Unable to fetch handlers for dependent modules");
44             return;
45         }
46
47         if (null != session) {
48             notificationService = session.getSALService(NotificationService.class);
49
50             if (null != notificationService) {
51                 dataBrokerService = session.getSALService(DataBrokerService.class);
52
53                 if (null != dataBrokerService) {
54                     dataProviderService = session.getSALService(DataProviderService.class);
55
56                     if (null != dataProviderService) {
57                         flowImplRef = new FlowConsumerImpl();
58                         groupImplRef = new GroupConsumerImpl();
59                         meterImplRef = new MeterConsumerImpl();
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(
106                     IClusterContainerServices.class, this);
107             try {
108                 Thread.sleep(4);
109             } catch (InterruptedException e) {
110                 // TODO Auto-generated catch block
111                 e.printStackTrace();
112             }
113         } while (clusterContainerService == null);
114
115         do {
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         return true;
127     }
128
129     public static DataProviderService getDataProviderService() {
130         return dataProviderService;
131     }
132
133     public FlowConsumerImpl getFlowImplRef() {
134         return flowImplRef;
135     }
136
137     public GroupConsumerImpl getGroupImplRef() {
138         return groupImplRef;
139     }
140
141     public static ProviderContext getProviderSession() {
142         return p_session;
143     }
144
145     public static NotificationService getNotificationService() {
146         return notificationService;
147     }
148
149     public static DataBrokerService getDataBrokerService() {
150         return dataBrokerService;
151     }
152
153     /*
154      * OSGI COMMANDS
155      */
156     @Override
157     public String getHelp() {
158         StringBuffer help = new StringBuffer();
159         return help.toString();
160     }
161
162 }