2 * Copyright (c) 2013, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.manager.impl.osgi.mapping;
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;
25 * Update SchemaContext service in Service Registry each time new YangModuleInfo
26 * is added or removed.
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;
35 private volatile ServiceRegistration<SchemaContextProvider> osgiReg;
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) {
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>());
51 public synchronized void updateService() {
52 if (this.osgiReg != null) {
53 this.bindingContextProvider.update(this.classLoadingStrat, this.schemaContextProvider);
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);
64 public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
65 final ObjectRegistration<YangModuleInfo> yangModuleInfoObjectRegistration = this.moduleInfoRegistry
66 .registerModuleInfo(yangModuleInfo);
67 return new ObjectRegistrationWrapper(yangModuleInfoObjectRegistration);
71 public synchronized void close() throws Exception {
72 if (this.osgiReg != null) {
73 this.osgiReg.unregister();
79 private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {
80 private final ObjectRegistration<YangModuleInfo> inner;
82 ObjectRegistrationWrapper(final ObjectRegistration<YangModuleInfo> inner) {
87 public YangModuleInfo getInstance() {
88 return this.inner.getInstance();
92 public void close() throws Exception {
94 // send modify event when a bundle disappears
99 public String toString() {
100 return this.inner.toString();