config-manager: final parameters
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / BeanToOsgiServiceManager.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, 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 package org.opendaylight.controller.config.manager.impl.osgi;
9
10 import com.google.common.base.Preconditions;
11 import java.util.Dictionary;
12 import java.util.HashSet;
13 import java.util.Hashtable;
14 import java.util.Map;
15 import java.util.Map.Entry;
16 import java.util.Set;
17 import javax.annotation.concurrent.GuardedBy;
18 import org.opendaylight.controller.config.api.ModuleIdentifier;
19 import org.opendaylight.controller.config.api.annotations.ServiceInterfaceAnnotation;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceRegistration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Registers instantiated beans as OSGi services and unregisters these services
27  * if beans are destroyed.
28  */
29 public class BeanToOsgiServiceManager {
30     private static final String SERVICE_NAME_OSGI_PROP = "name";
31
32     /**
33      * To be called for every created, reconfigured and recreated config bean.
34      * It is expected that before using this method OSGi service registry will
35      * be cleaned from previous registrations.
36      */
37     public OsgiRegistration registerToOsgi(final AutoCloseable instance, final ModuleIdentifier moduleIdentifier,
38                                            final BundleContext bundleContext,
39                                            final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
40         return new OsgiRegistration(instance, moduleIdentifier, bundleContext, serviceNamesToAnnotations);
41     }
42
43     public static class OsgiRegistration implements AutoCloseable {
44         private static final Logger LOG = LoggerFactory.getLogger(OsgiRegistration.class);
45
46         @GuardedBy("this")
47         private AutoCloseable instance;
48         private final ModuleIdentifier moduleIdentifier;
49         @GuardedBy("this")
50         private final Set<ServiceRegistration<?>> serviceRegistrations;
51         @GuardedBy("this")
52         private final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations;
53
54         public OsgiRegistration(final AutoCloseable instance, final ModuleIdentifier moduleIdentifier,
55                                 final BundleContext bundleContext,
56                                 final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
57             this.instance = instance;
58             this.moduleIdentifier = moduleIdentifier;
59             this.serviceNamesToAnnotations = serviceNamesToAnnotations;
60             this.serviceRegistrations = registerToSR(instance, bundleContext, serviceNamesToAnnotations);
61         }
62
63         private static Set<ServiceRegistration<?>> registerToSR(final AutoCloseable instance, final BundleContext bundleContext,
64                                                                 final Map<ServiceInterfaceAnnotation, String /* service ref name */> serviceNamesToAnnotations) {
65             Set<ServiceRegistration<?>> serviceRegistrations = new HashSet<>();
66             for (Entry<ServiceInterfaceAnnotation, String /* service ref name */> entry : serviceNamesToAnnotations.entrySet()) {
67                 ServiceInterfaceAnnotation annotation = entry.getKey();
68                 Class<?> requiredInterface = annotation.osgiRegistrationType();
69
70                 if(!annotation.registerToOsgi()) {
71                     LOG.debug("registerToOsgi for service interface {} is false - not registering", requiredInterface);
72                     continue;
73                 }
74
75                 Preconditions.checkState(requiredInterface.isInstance(instance), instance.getClass().getName() +
76                         " instance should implement " + requiredInterface.getName());
77                 Dictionary<String, String> propertiesForOsgi = createProps(entry.getValue());
78                 ServiceRegistration<?> serviceRegistration = bundleContext
79                         .registerService(requiredInterface.getName(), instance, propertiesForOsgi);
80                 serviceRegistrations.add(serviceRegistration);
81             }
82             return serviceRegistrations;
83         }
84
85         @Override
86         public synchronized void close() {
87             for (ServiceRegistration<?> serviceRegistration : serviceRegistrations) {
88                 try {
89                     serviceRegistration.unregister();
90                 } catch(final IllegalStateException e) {
91                     LOG.trace("Cannot unregister {}", serviceRegistration, e);
92                 }
93             }
94             serviceRegistrations.clear();
95         }
96
97         public synchronized void updateRegistrations(final Map<ServiceInterfaceAnnotation, String /* service ref name */> newAnnotationMapping,
98                                                      final BundleContext bundleContext, final AutoCloseable newInstance) {
99             boolean notEquals = !this.instance.equals(newInstance);
100             notEquals |= !newAnnotationMapping.equals(serviceNamesToAnnotations);
101             if (notEquals) {
102                 // FIXME: changing from old state to new state can be improved by computing the diff
103                 LOG.debug("Detected change in service registrations for {}: old: {}, new: {}", moduleIdentifier,
104                         serviceNamesToAnnotations, newAnnotationMapping);
105                 close();
106                 this.instance = newInstance;
107                 Set<ServiceRegistration<?>> newRegs = registerToSR(instance, bundleContext, newAnnotationMapping);
108                 serviceRegistrations.clear();
109                 serviceRegistrations.addAll(newRegs);
110             }
111         }
112
113         private static Dictionary<String, String> createProps(final String serviceName) {
114             Hashtable<String, String> result = new Hashtable<>();
115             result.put(SERVICE_NAME_OSGI_PROP, serviceName);
116             return result;
117         }
118     }
119 }