Resolve Bug:448 - Remove yang-store api and impl.
[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 org.opendaylight.yangtools.concepts.ObjectRegistration;
12 import org.opendaylight.yangtools.sal.binding.generator.api.ModuleInfoRegistry;
13 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
14 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
15 import org.osgi.framework.BundleContext;
16 import org.osgi.framework.ServiceRegistration;
17
18 import java.util.Hashtable;
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 ServiceRegistration<SchemaContextProvider> osgiReg;
27
28     public RefreshingSCPModuleInfoRegistry(ModuleInfoRegistry moduleInfoRegistry,
29                                            SchemaContextProvider schemaContextProvider, BundleContext bundleContext) {
30         this.moduleInfoRegistry = moduleInfoRegistry;
31         osgiReg = bundleContext.registerService(SchemaContextProvider.class, schemaContextProvider, new Hashtable<String, String>());
32     }
33
34     private void updateService() {
35         osgiReg.setProperties(null); // send modifiedService event
36     }
37
38     @Override
39     public ObjectRegistration<YangModuleInfo> registerModuleInfo(YangModuleInfo yangModuleInfo) {
40         ObjectRegistration<YangModuleInfo> yangModuleInfoObjectRegistration = moduleInfoRegistry.registerModuleInfo(yangModuleInfo);
41         ObjectRegistrationWrapper wrapper = new ObjectRegistrationWrapper(yangModuleInfoObjectRegistration);
42         updateService();
43         return wrapper;
44     }
45
46
47     @Override
48     public void close() {
49         osgiReg.unregister();
50     }
51
52     private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {
53         private final ObjectRegistration<YangModuleInfo> inner;
54
55         private ObjectRegistrationWrapper(ObjectRegistration<YangModuleInfo> inner) {
56             this.inner = inner;
57         }
58
59         @Override
60         public YangModuleInfo getInstance() {
61             return inner.getInstance();
62         }
63
64         @Override
65         public void close() throws Exception {
66             inner.close();
67             updateService();// send modify event when a bundle disappears
68         }
69
70
71         @Override
72         public String toString() {
73             return inner.toString();
74         }
75     }
76 }