Improve ModuleInfoSnapshotBuilder API
[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.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 RegularYangModuleInfoRegistry extends YangModuleInfoRegistry {
30     private static final Logger LOG = LoggerFactory.getLogger(RegularYangModuleInfoRegistry.class);
31
32     private final ComponentFactory contextFactory;
33     private final ModuleInfoSnapshotResolver 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     RegularYangModuleInfoRegistry(final ComponentFactory contextFactory, final YangParserFactory factory) {
45         this.contextFactory = requireNonNull(contextFactory);
46         moduleInfoRegistry = new ModuleInfoSnapshotResolver("binding-dom-codec", factory);
47     }
48
49     // Invocation from scanner, we may want to ignore this in order to not process partial updates
50     @Override
51     void scannerUpdate() {
52         if (!ignoreScanner) {
53             synchronized (this) {
54                 updateService();
55             }
56         }
57     }
58
59     @Override
60     synchronized void scannerShutdown() {
61         ignoreScanner = true;
62     }
63
64     @Override
65     synchronized void enableScannerAndUpdate() {
66         ignoreScanner = false;
67         updateService();
68     }
69
70     @Override
71     synchronized void close() {
72         ignoreScanner = true;
73         if (currentInstance != null) {
74             currentInstance.dispose();
75             currentInstance = null;
76         }
77     }
78
79     @Override
80     List<ObjectRegistration<YangModuleInfo>> registerInfos(final List<YangModuleInfo> infos) {
81         return moduleInfoRegistry.registerModuleInfos(infos);
82     }
83
84     @Holding("this")
85     private void updateService() {
86         final ModuleInfoSnapshot newSnapshot;
87         try {
88             newSnapshot = moduleInfoRegistry.takeSnapshot();
89         } catch (NoSuchElementException e) {
90             LOG.debug("No snapshot available", e);
91             return;
92         }
93         if (newSnapshot.equals(currentSnapshot)) {
94             LOG.debug("No update to snapshot");
95             return;
96         }
97
98
99         final ComponentInstance newInstance = contextFactory.newInstance(
100             OSGiModuleInfoSnapshotImpl.props(nextGeneration(), newSnapshot));
101         if (currentInstance != null) {
102             currentInstance.dispose();
103         }
104         currentInstance = newInstance;
105         currentSnapshot = newSnapshot;
106     }
107
108     @Holding("this")
109     private long nextGeneration() {
110         return generation == -1 ? -1 : ++generation;
111     }
112 }