5321dded8f160639e2736bda8da044c9a037dc7e
[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.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
19 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
20 import org.osgi.framework.BundleContext;
21 import org.osgi.framework.ServiceRegistration;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
27  */
28 public class RefreshingSCPModuleInfoRegistry implements ModuleInfoRegistry, AutoCloseable {
29     private static final Logger LOG = LoggerFactory.getLogger(RefreshingSCPModuleInfoRegistry.class);
30
31     private final ModuleInfoRegistry moduleInfoRegistry;
32     private final SchemaContextProvider schemaContextProvider;
33     private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
34     private final BindingContextProvider bindingContextProvider;
35     private final ClassLoadingStrategy classLoadingStrat;
36
37     private volatile ServiceRegistration<SchemaContextProvider> osgiReg;
38
39     public RefreshingSCPModuleInfoRegistry(final ModuleInfoRegistry moduleInfoRegistry,
40         final SchemaContextProvider schemaContextProvider, final ClassLoadingStrategy classLoadingStrat,
41         final SchemaSourceProvider<YangTextSchemaSource> sourceProvider, final BindingContextProvider bindingContextProvider,
42         final BundleContext bundleContext) {
43
44         this.moduleInfoRegistry = moduleInfoRegistry;
45         this.schemaContextProvider = schemaContextProvider;
46         this.classLoadingStrat = classLoadingStrat;
47         this.sourceProvider = sourceProvider;
48         this.bindingContextProvider = bindingContextProvider;
49         osgiReg = bundleContext
50             .registerService(SchemaContextProvider.class, schemaContextProvider, new Hashtable<String, String>());
51     }
52
53     public void updateService() {
54         if(osgiReg != null) {
55             try {
56                 bindingContextProvider.update(classLoadingStrat, schemaContextProvider);
57                 osgiReg.setProperties(new Hashtable<String, Object>() {{
58                         put(BindingRuntimeContext.class.getName(), bindingContextProvider.getBindingContext());
59                         put(SchemaSourceProvider.class.getName(), sourceProvider);
60                     }}); // send modifiedService event
61             } catch (RuntimeException e) {
62                 // The ModuleInfoBackedContext throws a RuntimeException if it can't create the schema context.
63                 LOG.warn("Error updating the BindingContextProvider", e);
64             }
65         }
66     }
67
68     @Override
69     public ObjectRegistration<YangModuleInfo> registerModuleInfo(YangModuleInfo yangModuleInfo) {
70         ObjectRegistration<YangModuleInfo> yangModuleInfoObjectRegistration = moduleInfoRegistry.registerModuleInfo(yangModuleInfo);
71         ObjectRegistrationWrapper wrapper = new ObjectRegistrationWrapper(yangModuleInfoObjectRegistration);
72         return wrapper;
73     }
74
75     @Override
76     public void close() throws Exception {
77         if(osgiReg != null) {
78             osgiReg.unregister();
79         }
80
81         osgiReg = null;
82     }
83
84     private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {
85         private final ObjectRegistration<YangModuleInfo> inner;
86
87         private ObjectRegistrationWrapper(ObjectRegistration<YangModuleInfo> inner) {
88             this.inner = inner;
89         }
90
91         @Override
92         public YangModuleInfo getInstance() {
93             return inner.getInstance();
94         }
95
96         @Override
97         public void close() throws Exception {
98             inner.close();
99             updateService();// send modify event when a bundle disappears
100         }
101
102         @Override
103         public String toString() {
104             return inner.toString();
105         }
106     }
107 }