Rework binding component instantiation
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / impl / YangModuleInfoRegistry.java
1 /*
2  * Copyright (c) 2017, 2020 PANTHEON.tech, 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.dom.schema.osgi.impl;
9
10 import static java.util.Objects.requireNonNull;
11
12 import java.util.List;
13 import java.util.NoSuchElementException;
14 import org.checkerframework.checker.lock.qual.GuardedBy;
15 import org.checkerframework.checker.lock.qual.Holding;
16 import org.opendaylight.binding.runtime.api.ModuleInfoSnapshot;
17 import org.opendaylight.binding.runtime.spi.ModuleInfoSnapshotBuilder;
18 import org.opendaylight.yangtools.concepts.ObjectRegistration;
19 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
20 import org.opendaylight.yangtools.yang.model.parser.api.YangParserFactory;
21 import org.osgi.service.component.ComponentFactory;
22 import org.osgi.service.component.ComponentInstance;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 /**
27  * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
28  */
29 final class YangModuleInfoRegistry {
30     private static final Logger LOG = LoggerFactory.getLogger(YangModuleInfoRegistry.class);
31
32     private final ComponentFactory contextFactory;
33     private final ModuleInfoSnapshotBuilder moduleInfoRegistry;
34
35     @GuardedBy("this")
36     private ComponentInstance currentInstance;
37     @GuardedBy("this")
38     private ModuleInfoSnapshot currentSnapshot;
39     @GuardedBy("this")
40     private int generation;
41
42     private volatile boolean ignoreScanner = true;
43
44     YangModuleInfoRegistry(final ComponentFactory contextFactory, final YangParserFactory factory) {
45         this.contextFactory = requireNonNull(contextFactory);
46         moduleInfoRegistry = new ModuleInfoSnapshotBuilder("binding-dom-codec", factory);
47     }
48
49     // Invocation from scanner, we may want to ignore this in order to not process partial updates
50     void scannerUpdate() {
51         if (!ignoreScanner) {
52             synchronized (this) {
53                 updateService();
54             }
55         }
56     }
57
58     synchronized void scannerShutdown() {
59         ignoreScanner = true;
60     }
61
62     synchronized void enableScannerAndUpdate() {
63         ignoreScanner = false;
64         updateService();
65     }
66
67     synchronized void close() {
68         ignoreScanner = true;
69         if (currentInstance != null) {
70             currentInstance.dispose();
71             currentInstance = null;
72         }
73     }
74
75     List<ObjectRegistration<YangModuleInfo>> registerInfos(final List<YangModuleInfo> infos) {
76         return moduleInfoRegistry.registerModuleInfos(infos);
77     }
78
79     @Holding("this")
80     private void updateService() {
81         final ModuleInfoSnapshot newSnapshot;
82         try {
83             newSnapshot = moduleInfoRegistry.build();
84         } catch (NoSuchElementException e) {
85             LOG.debug("No snapshot available", e);
86             return;
87         }
88         if (newSnapshot.equals(currentSnapshot)) {
89             LOG.debug("No update to snapshot");
90             return;
91         }
92
93
94         final ComponentInstance newInstance = contextFactory.newInstance(
95             OSGiEffectiveModelImpl.props(nextGeneration(), newSnapshot));
96         if (currentInstance != null) {
97             currentInstance.dispose();
98         }
99         currentInstance = newInstance;
100         currentSnapshot = newSnapshot;
101     }
102
103     @Holding("this")
104     private long nextGeneration() {
105         return generation == -1 ? -1 : ++generation;
106     }
107 }