Remove BindingRuntimeContext{Listener,Service}
[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 java.util.Objects.requireNonNull;
11
12 import org.checkerframework.checker.lock.qual.GuardedBy;
13 import org.opendaylight.binding.runtime.spi.ModuleInfoBackedContext;
14 import org.opendaylight.binding.runtime.spi.ModuleInfoRegistry;
15 import org.opendaylight.yangtools.concepts.ObjectRegistration;
16 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
19 import org.osgi.framework.ServiceRegistration;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 /**
24  * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
25  */
26 final class OsgiModuleInfoRegistry implements ModuleInfoRegistry {
27     private static final Logger LOG = LoggerFactory.getLogger(OsgiModuleInfoRegistry.class);
28
29     private final SchemaContextProvider schemaContextProvider;
30     private final ModuleInfoBackedContext moduleInfoRegistry;
31
32     @GuardedBy("this")
33     private ServiceRegistration<?> registration;
34     @GuardedBy("this")
35     private long generation;
36
37     OsgiModuleInfoRegistry(final ModuleInfoBackedContext moduleInfoRegistry,
38         final SchemaContextProvider schemaContextProvider) {
39
40         this.moduleInfoRegistry = requireNonNull(moduleInfoRegistry);
41         this.schemaContextProvider = requireNonNull(schemaContextProvider);
42     }
43
44     @Override
45     public ObjectRegistration<YangModuleInfo> registerModuleInfo(final YangModuleInfo yangModuleInfo) {
46         return new ObjectRegistrationWrapper(registerInfo(yangModuleInfo));
47     }
48
49     @SuppressWarnings("checkstyle:illegalCatch")
50     synchronized void updateService() {
51         final SchemaContext context;
52         try {
53             context = schemaContextProvider.getSchemaContext();
54         } catch (final RuntimeException e) {
55             // The ModuleInfoBackedContext throws a RuntimeException if it can't create the schema context.
56             LOG.error("Error updating the schema context", e);
57             return;
58         }
59         LOG.trace("Assembled context {}", context);
60
61         //        // FIXME: MDSAL-392: UGH, this should be a snapshot
62         //        final BindingRuntimeContext next = DefaultBindingRuntimeContext.create(
63         //            new DefaultBindingRuntimeGenerator().generateTypeMapping(context), moduleInfoRegistry);
64
65         // FIXME: publish new the new context, remove the old one
66     }
67
68     ObjectRegistration<YangModuleInfo> registerInfo(final YangModuleInfo yangModuleInfo) {
69         return moduleInfoRegistry.registerModuleInfo(yangModuleInfo);
70     }
71
72     private class ObjectRegistrationWrapper implements ObjectRegistration<YangModuleInfo> {
73         private final ObjectRegistration<YangModuleInfo> inner;
74
75         ObjectRegistrationWrapper(final ObjectRegistration<YangModuleInfo> inner) {
76             this.inner = requireNonNull(inner);
77         }
78
79         @Override
80         public YangModuleInfo getInstance() {
81             return inner.getInstance();
82         }
83
84         @Override
85         @SuppressWarnings("checkstyle:illegalCatch")
86         public void close() {
87             try {
88                 inner.close();
89             } finally {
90                 // send modify event when a bundle disappears
91                 updateService();
92             }
93         }
94
95         @Override
96         public String toString() {
97             return inner.toString();
98         }
99     }
100 }