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.ObjectRegistration;
19 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
20 import org.opendaylight.yangtools.yang.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;
27 * Update SchemaContext service in Service Registry each time new YangModuleInfo is added or removed.
29 final class RegularYangModuleInfoRegistry extends YangModuleInfoRegistry {
30 private static final Logger LOG = LoggerFactory.getLogger(RegularYangModuleInfoRegistry.class);
32 private final ComponentFactory<OSGiModuleInfoSnapshotImpl> contextFactory;
33 private final ModuleInfoSnapshotResolver moduleInfoRegistry;
36 private ComponentInstance<OSGiModuleInfoSnapshotImpl> currentInstance;
38 private ModuleInfoSnapshot currentSnapshot;
40 private int generation;
42 private volatile boolean ignoreScanner = true;
44 RegularYangModuleInfoRegistry(final ComponentFactory<OSGiModuleInfoSnapshotImpl> contextFactory,
45 final YangParserFactory factory) {
46 this.contextFactory = requireNonNull(contextFactory);
47 moduleInfoRegistry = new ModuleInfoSnapshotResolver("binding-dom-codec", factory);
50 // Invocation from scanner, we may want to ignore this in order to not process partial updates
52 void scannerUpdate() {
61 synchronized void scannerShutdown() {
66 synchronized void enableScannerAndUpdate() {
67 ignoreScanner = false;
72 synchronized void close() {
74 if (currentInstance != null) {
75 currentInstance.dispose();
76 currentInstance = null;
81 List<ObjectRegistration<YangModuleInfo>> registerInfos(final List<YangModuleInfo> infos) {
82 return moduleInfoRegistry.registerModuleInfos(infos);
86 private void updateService() {
87 final ModuleInfoSnapshot newSnapshot;
89 newSnapshot = moduleInfoRegistry.takeSnapshot();
90 } catch (NoSuchElementException e) {
91 LOG.debug("No snapshot available", e);
94 if (newSnapshot.equals(currentSnapshot)) {
95 LOG.debug("No update to snapshot");
100 final ComponentInstance<OSGiModuleInfoSnapshotImpl> newInstance = contextFactory.newInstance(
101 OSGiModuleInfoSnapshotImpl.props(nextGeneration(), newSnapshot));
102 if (currentInstance != null) {
103 currentInstance.dispose();
105 currentInstance = newInstance;
106 currentSnapshot = newSnapshot;
110 private long nextGeneration() {
111 return generation == -1 ? -1 : ++generation;