d59232cf08de0904b55f75009756124d2117d5c0
[controller.git] / opendaylight / config / config-manager / src / main / java / org / opendaylight / controller / config / manager / impl / osgi / mapping / RefreshingSCPModuleInfoRegistry.java
1 /*
2  * Copyright (c) 2013, 2017 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.Dictionary;
12 import java.util.Hashtable;
13 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
14 import org.opendaylight.mdsal.binding.generator.api.ModuleInfoRegistry;
15 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
16 import org.opendaylight.yangtools.concepts.ObjectRegistration;
17 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
19 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
20 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
21 import org.osgi.framework.BundleContext;
22 import org.osgi.framework.ServiceRegistration;
23
24 /**
25  * Update SchemaContext service in Service Registry each time new YangModuleInfo
26  * is added or removed.
27  */
28 public class RefreshingSCPModuleInfoRegistry implements ModuleInfoRegistry, AutoCloseable {
29     private final ModuleInfoRegistry moduleInfoRegistry;
30     private final SchemaContextProvider schemaContextProvider;
31     private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
32     private final BindingContextProvider bindingContextProvider;
33     private final ClassLoadingStrategy classLoadingStrat;
34
35     private volatile ServiceRegistration<SchemaContextProvider> osgiReg;
36
37     public RefreshingSCPModuleInfoRegistry(final ModuleInfoRegistry moduleInfoRegistry,
38             final SchemaContextProvider schemaContextProvider, final ClassLoadingStrategy classLoadingStrat,
39             final SchemaSourceProvider<YangTextSchemaSource> sourceProvider,
40             final BindingContextProvider bindingContextProvider, final BundleContext bundleContext) {
41
42         this.moduleInfoRegistry = moduleInfoRegistry;
43         this.schemaContextProvider = schemaContextProvider;
44         this.classLoadingStrat = classLoadingStrat;
45         this.sourceProvider = sourceProvider;
46         this.bindingContextProvider = bindingContextProvider;
47         this.osgiReg = bundleContext.registerService(SchemaContextProvider.class, schemaContextProvider,
48                 new Hashtable<String, String>());
49     }
50
51     public synchronized void updateService() {
52         if (this.osgiReg != null) {
53             this.bindingContextProvider.update(this.classLoadingStrat, this.schemaContextProvider);
54
55             final Dictionary<String, Object> props = new Hashtable<>();
56             props.put(BindingRuntimeContext.class.getName(), this.bindingContextProvider.getBindingContext());
57             props.put(SchemaSourceProvider.class.getName(), this.sourceProvider);
58             // send modifiedService event
59             this.osgiReg.setProperties(props);
60         }
61     }
62
63     @Override
64     public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
65         final ObjectRegistration<YangModuleInfo> yangModuleInfoObjectRegistration = this.moduleInfoRegistry
66                 .registerModuleInfo(yangModuleInfo);
67         return new ObjectRegistrationWrapper(yangModuleInfoObjectRegistration);
68     }
69
70     @Override
71     public synchronized void close() throws Exception {
72         if (this.osgiReg != null) {
73             this.osgiReg.unregister();
74         }
75
76         this.osgiReg = null;
77     }
78
79     private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {
80         private final ObjectRegistration<YangModuleInfo> inner;
81
82         ObjectRegistrationWrapper(final ObjectRegistration<YangModuleInfo> inner) {
83             this.inner = inner;
84         }
85
86         @Override
87         public YangModuleInfo getInstance() {
88             return this.inner.getInstance();
89         }
90
91         @Override
92         public void close() {
93             this.inner.close();
94             // send modify event when a bundle disappears
95             updateService();
96         }
97
98         @Override
99         public String toString() {
100             return this.inner.toString();
101         }
102     }
103 }