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