b62e60382c09210c6e24f6fc7be7db95b1c73944
[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.sal.binding.generator.util.BindingRuntimeContext;
16 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
18 import org.osgi.framework.BundleContext;
19 import org.osgi.framework.ServiceRegistration;
20
21 /**
22  * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
23  */
24 public class RefreshingSCPModuleInfoRegistry implements ModuleInfoRegistry, AutoCloseable {
25
26     private final ModuleInfoRegistry moduleInfoRegistry;
27     private final SchemaContextProvider schemaContextProvider;
28     private final BindingContextProvider bindingContextProvider;
29     private final ClassLoadingStrategy classLoadingStrat;
30
31     private final ServiceRegistration<SchemaContextProvider> osgiReg;
32
33     public RefreshingSCPModuleInfoRegistry(final ModuleInfoRegistry moduleInfoRegistry,
34                                            final SchemaContextProvider schemaContextProvider, final ClassLoadingStrategy classLoadingStrat, final BindingContextProvider bindingContextProvider, final BundleContext bundleContext) {
35         this.moduleInfoRegistry = moduleInfoRegistry;
36         this.schemaContextProvider = schemaContextProvider;
37         this.classLoadingStrat = classLoadingStrat;
38         this.bindingContextProvider = bindingContextProvider;
39         osgiReg = bundleContext.registerService(SchemaContextProvider.class, schemaContextProvider, new Hashtable<String, String>());
40     }
41
42     private void updateService() {
43         bindingContextProvider.update(classLoadingStrat, schemaContextProvider);
44         osgiReg.setProperties(new Hashtable<String, Object>() {{
45                 put(BindingRuntimeContext.class.getName(), bindingContextProvider.getBindingContext());
46             }
47         }); // send modifiedService event
48     }
49
50     @Override
51     public ObjectRegistration<YangModuleInfo> registerModuleInfo(YangModuleInfo yangModuleInfo) {
52         ObjectRegistration<YangModuleInfo> yangModuleInfoObjectRegistration = moduleInfoRegistry.registerModuleInfo(yangModuleInfo);
53         ObjectRegistrationWrapper wrapper = new ObjectRegistrationWrapper(yangModuleInfoObjectRegistration);
54         updateService();
55         return wrapper;
56     }
57
58     @Override
59     public void close() throws Exception {
60         osgiReg.unregister();
61     }
62
63     private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {
64         private final ObjectRegistration<YangModuleInfo> inner;
65
66         private ObjectRegistrationWrapper(ObjectRegistration<YangModuleInfo> inner) {
67             this.inner = inner;
68         }
69
70         @Override
71         public YangModuleInfo getInstance() {
72             return inner.getInstance();
73         }
74
75         @Override
76         public void close() throws Exception {
77             inner.close();
78             updateService();// send modify event when a bundle disappears
79         }
80
81         @Override
82         public String toString() {
83             return inner.toString();
84         }
85     }
86 }

©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.