2 * Copyright (c) 2017, 2020 PANTHEON.tech, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.schema.osgi.impl;
10 import static java.util.Objects.requireNonNull;
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;
29 * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
31 final class RegularYangModuleInfoRegistry extends YangModuleInfoRegistry {
32 private static final Logger LOG = LoggerFactory.getLogger(RegularYangModuleInfoRegistry.class);
34 private final ComponentFactory<OSGiModuleInfoSnapshotImpl> contextFactory;
35 private final ModuleInfoSnapshotResolver resolver;
38 private ComponentInstance<OSGiModuleInfoSnapshotImpl> currentInstance;
40 private ModuleInfoSnapshot currentSnapshot;
42 private int generation;
44 private volatile boolean ignoreScanner = true;
46 RegularYangModuleInfoRegistry(final ComponentFactory<OSGiModuleInfoSnapshotImpl> contextFactory,
47 final YangParserFactory factory) {
48 this.contextFactory = requireNonNull(contextFactory);
49 resolver = new ModuleInfoSnapshotResolver("binding-dom-codec", factory);
52 // Invocation from scanner, we may want to ignore this in order to not process partial updates
54 void scannerUpdate() {
63 synchronized void scannerShutdown() {
68 synchronized void enableScannerAndUpdate() {
69 ignoreScanner = false;
74 synchronized void close() {
76 if (currentInstance != null) {
77 currentInstance.dispose();
78 currentInstance = null;
83 Registration registerInfos(final List<YangModuleInfo> infos) {
84 final var regs = resolver.registerModuleInfos(infos);
85 return new AbstractRegistration() {
87 protected void removeRegistration() {
88 regs.forEach(ObjectRegistration::close);
94 private void updateService() {
95 final ModuleInfoSnapshot newSnapshot;
97 newSnapshot = resolver.takeSnapshot();
98 } catch (NoSuchElementException e) {
99 LOG.debug("No snapshot available", e);
102 if (newSnapshot.equals(currentSnapshot)) {
103 LOG.debug("No update to snapshot");
108 final ComponentInstance<OSGiModuleInfoSnapshotImpl> newInstance = contextFactory.newInstance(
109 OSGiModuleInfoSnapshotImpl.props(nextGeneration(), newSnapshot));
110 if (currentInstance != null) {
111 currentInstance.dispose();
113 currentInstance = newInstance;
114 currentSnapshot = newSnapshot;
118 private long nextGeneration() {
119 return generation == -1 ? -1 : ++generation;