2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.binding.impl;
10 import org.opendaylight.controller.sal.binding.api.BindingAwareBroker.ConsumerContext;
11 import org.opendaylight.controller.sal.binding.api.BindingAwareService;
12 import org.opendaylight.yangtools.yang.binding.RpcService;
13 import org.osgi.framework.BundleContext;
14 import org.osgi.framework.InvalidSyntaxException;
15 import org.osgi.framework.ServiceReference;
16 import org.slf4j.LoggerFactory
17 import static org.opendaylight.controller.sal.binding.impl.osgi.Constants.*
19 class OsgiConsumerContext implements ConsumerContext {
21 static val log = LoggerFactory.getLogger(OsgiConsumerContext)
22 protected val BundleContext bundleContext;
23 protected val BindingAwareBrokerImpl broker;
25 new(BundleContext ctx, BindingAwareBrokerImpl broker) {
26 this.bundleContext = ctx;
30 override def <T extends BindingAwareService> getSALService(Class<T> service) {
32 // SAL Services are global
33 var ref = bundleContext.getServiceReference(service);
34 return bundleContext.getService(ref) as T;
37 override def <T extends RpcService> T getRpcService(Class<T> module) {
40 val services = bundleContext.getServiceReferences(module, getProxyFilter());
42 // Proxy service found / using first implementation
43 // FIXME: Add advanced logic to retrieve service with right set of models
44 if (false == services.empty) {
45 val ref = services.iterator().next() as ServiceReference<T>;
46 return bundleContext.getService(ref) as T;
48 return broker.getRpcService(module);
50 } catch (InvalidSyntaxException e) {
51 log.error("Created filter was invalid:", e.message, e)
57 private def getProxyFilter() {
58 return '''(«SAL_SERVICE_TYPE»=«SAL_SERVICE_TYPE_CONSUMER_PROXY»)'''