Simplify YangModuleInfoScanner
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / impl / RegularYangModuleInfoRegistry.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.mdsal.binding.runtime.api.ModuleInfoSnapshot;
17 import org.opendaylight.mdsal.binding.runtime.spi.ModuleInfoSnapshotResolver;
18 import org.opendaylight.yangtools.concepts.AbstractRegistration;
19 import org.opendaylight.yangtools.concepts.ObjectRegistration;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
22 import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
23 import org.osgi.service.component.ComponentFactory;
24 import org.osgi.service.component.ComponentInstance;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
30  */
31 final class RegularYangModuleInfoRegistry extends YangModuleInfoRegistry {
32     private static final Logger LOG = LoggerFactory.getLogger(RegularYangModuleInfoRegistry.class);
33
34     private final ComponentFactory<OSGiModuleInfoSnapshotImpl> contextFactory;
35     private final ModuleInfoSnapshotResolver resolver;
36
37     @GuardedBy("this")
38     private ComponentInstance<OSGiModuleInfoSnapshotImpl> currentInstance;
39     @GuardedBy("this")
40     private ModuleInfoSnapshot currentSnapshot;
41     @GuardedBy("this")
42     private int generation;
43
44     private volatile boolean ignoreScanner = true;
45
46     RegularYangModuleInfoRegistry(final ComponentFactory<OSGiModuleInfoSnapshotImpl> contextFactory,
47             final YangParserFactory factory) {
48         this.contextFactory = requireNonNull(contextFactory);
49         resolver = new ModuleInfoSnapshotResolver("binding-dom-codec", factory);
50     }
51
52     // Invocation from scanner, we may want to ignore this in order to not process partial updates
53     @Override
54     void scannerUpdate() {
55         if (!ignoreScanner) {
56             synchronized (this) {
57                 updateService();
58             }
59         }
60     }
61
62     @Override
63     synchronized void scannerShutdown() {
64         ignoreScanner = true;
65     }
66
67     @Override
68     synchronized void enableScannerAndUpdate() {
69         ignoreScanner = false;
70         updateService();
71     }
72
73     @Override
74     synchronized void close() {
75         ignoreScanner = true;
76         if (currentInstance != null) {
77             currentInstance.dispose();
78             currentInstance = null;
79         }
80     }
81
82     @Override
83     Registration registerInfos(final List<YangModuleInfo> infos) {
84         final var regs = resolver.registerModuleInfos(infos);
85         return new AbstractRegistration() {
86             @Override
87             protected void removeRegistration() {
88                 regs.forEach(ObjectRegistration::close);
89             }
90         };
91     }
92
93     @Holding("this")
94     private void updateService() {
95         final ModuleInfoSnapshot newSnapshot;
96         try {
97             newSnapshot = resolver.takeSnapshot();
98         } catch (NoSuchElementException e) {
99             LOG.debug("No snapshot available", e);
100             return;
101         }
102         if (newSnapshot.equals(currentSnapshot)) {
103             LOG.debug("No update to snapshot");
104             return;
105         }
106
107
108         final ComponentInstance<OSGiModuleInfoSnapshotImpl> newInstance = contextFactory.newInstance(
109             OSGiModuleInfoSnapshotImpl.props(nextGeneration(), newSnapshot));
110         if (currentInstance != null) {
111             currentInstance.dispose();
112         }
113         currentInstance = newInstance;
114         currentSnapshot = newSnapshot;
115     }
116
117     @Holding("this")
118     private long nextGeneration() {
119         return generation == -1 ? -1 : ++generation;
120     }
121 }