Merge "BUG 2799: Migration of Message Bus from deprecated Helium MD-SAL APIs to Lithi...
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / mapping / RefreshingSCPModuleInfoRegistry.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
9 package org.opendaylight.controller.config.manager.impl.osgi.mapping;
10
11 import java.util.Hashtable;
12 import org.opendaylight.yangtools.concepts.ObjectRegistration;
13 import org.opendaylight.yangtools.sal.binding.generator.api.ClassLoadingStrategy;
14 import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
15 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
17 import org.osgi.framework.BundleContext;
18 import org.osgi.framework.ServiceRegistration;
19
20 /**
21  * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
22  */
23 public class RefreshingSCPModuleInfoRegistry implements ModuleInfoRegistry, AutoCloseable {
24
25     private final ModuleInfoRegistry moduleInfoRegistry;
26     private final SchemaContextProvider schemaContextProvider;
27     private final BindingContextProvider bindingContextProvider;
28     private final ClassLoadingStrategy classLoadingStrat;
29
30     private final ServiceRegistration<SchemaContextProvider> osgiReg;
31
32     public RefreshingSCPModuleInfoRegistry(final ModuleInfoRegistry moduleInfoRegistry,
33                                            final SchemaContextProvider schemaContextProvider, final ClassLoadingStrategy classLoadingStrat, final BindingContextProvider bindingContextProvider, final BundleContext bundleContext) {
34         this.moduleInfoRegistry = moduleInfoRegistry;
35         this.schemaContextProvider = schemaContextProvider;
36         this.classLoadingStrat = classLoadingStrat;
37         this.bindingContextProvider = bindingContextProvider;
38         osgiReg = bundleContext.registerService(SchemaContextProvider.class, schemaContextProvider, new Hashtable<String, String>());
39     }
40
41     private void updateService() {
42         bindingContextProvider.update(classLoadingStrat, schemaContextProvider);
43         osgiReg.setProperties(null); // send modifiedService event
44     }
45
46     @Override
47     public ObjectRegistration<YangModuleInfo> registerModuleInfo(YangModuleInfo yangModuleInfo) {
48         ObjectRegistration<YangModuleInfo> yangModuleInfoObjectRegistration = moduleInfoRegistry.registerModuleInfo(yangModuleInfo);
49         ObjectRegistrationWrapper wrapper = new ObjectRegistrationWrapper(yangModuleInfoObjectRegistration);
50         updateService();
51         return wrapper;
52     }
53
54     @Override
55     public void close() throws Exception {
56         osgiReg.unregister();
57     }
58
59
60     private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {
61         private final ObjectRegistration<YangModuleInfo> inner;
62
63         private ObjectRegistrationWrapper(ObjectRegistration<YangModuleInfo> inner) {
64             this.inner = inner;
65         }
66
67         @Override
68         public YangModuleInfo getInstance() {
69             return inner.getInstance();
70         }
71
72         @Override
73         public void close() throws Exception {
74             inner.close();
75             updateService();// send modify event when a bundle disappears
76         }
77
78
79         @Override
80         public String toString() {
81             return inner.toString();
82         }
83     }
84 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.