Fix checkstyle in mdsal-binding-dom-codec
[mdsal.git] / binding / mdsal-binding-dom-codec-osgi / src / main / java / org / opendaylight / mdsal / binding / dom / codec / osgi / impl / OsgiModuleInfoRegistry.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies, s.r.o. 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 package org.opendaylight.mdsal.binding.dom.codec.osgi.impl;
9
10 import static com.google.common.base.Preconditions.checkNotNull;
11
12 import javax.annotation.concurrent.GuardedBy;
13 import org.opendaylight.mdsal.binding.generator.api.ModuleInfoRegistry;
14 import org.opendaylight.yangtools.concepts.ObjectRegistration;
15 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
18 import org.osgi.framework.ServiceRegistration;
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 /**
23  * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
24  */
25 final class OsgiModuleInfoRegistry implements ModuleInfoRegistry {
26     private static final Logger LOG = LoggerFactory.getLogger(OsgiModuleInfoRegistry.class);
27
28     private final SimpleBindingRuntimeContextService runtimeContext;
29     private final SchemaContextProvider schemaContextProvider;
30     private final ModuleInfoRegistry moduleInfoRegistry;
31
32     @GuardedBy("this")
33     private ServiceRegistration<?> registration;
34     @GuardedBy("this")
35     private long generation;
36
37     OsgiModuleInfoRegistry(final ModuleInfoRegistry moduleInfoRegistry,
38         final SchemaContextProvider schemaContextProvider, final SimpleBindingRuntimeContextService runtimeContext) {
39
40         this.moduleInfoRegistry = checkNotNull(moduleInfoRegistry);
41         this.schemaContextProvider = checkNotNull(schemaContextProvider);
42         this.runtimeContext = checkNotNull(runtimeContext);
43     }
44
45     @SuppressWarnings("checkstyle:illegalCatch")
46     synchronized void updateService() {
47         final SchemaContext context;
48         try {
49             context = schemaContextProvider.getSchemaContext();
50         } catch (final RuntimeException e) {
51             // The ModuleInfoBackedContext throws a RuntimeException if it can't create the schema context.
52             LOG.error("Error updating the schema context", e);
53             return;
54         }
55
56         try {
57             runtimeContext.updateBindingRuntimeContext(context);
58         } catch (final RuntimeException e) {
59             LOG.error("Error updating binding runtime context", e);
60             return;
61         }
62     }
63
64     @Override
65     public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
66         return new ObjectRegistrationWrapper(moduleInfoRegistry.registerModuleInfo(yangModuleInfo));
67     }
68
69     private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {
70         private final ObjectRegistration<YangModuleInfo> inner;
71
72         ObjectRegistrationWrapper(final ObjectRegistration<YangModuleInfo> inner) {
73             this.inner = checkNotNull(inner);
74         }
75
76         @Override
77         public YangModuleInfo getInstance() {
78             return inner.getInstance();
79         }
80
81         @Override
82         @SuppressWarnings("checkstyle:illegalCatch")
83         public void close() throws Exception {
84             try {
85                 inner.close();
86             } finally {
87                 // send modify event when a bundle disappears
88                 updateService();
89             }
90         }
91
92         @Override
93         public String toString() {
94             return inner.toString();
95         }
96     }
97 }