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