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 com.google.common.util.concurrent.ListenableFuture;
13 import java.util.List;
14 import java.util.concurrent.CopyOnWriteArrayList;
15 import java.util.concurrent.atomic.AtomicReference;
16 import java.util.function.Consumer;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
19 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
20 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
21 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
22 import org.opendaylight.yangtools.concepts.AbstractRegistration;
23 import org.opendaylight.yangtools.concepts.Registration;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
27 import org.osgi.service.component.ComponentFactory;
28 import org.osgi.service.component.annotations.Activate;
29 import org.osgi.service.component.annotations.Component;
30 import org.osgi.service.component.annotations.Deactivate;
31 import org.osgi.service.component.annotations.Reference;
32 import org.osgi.service.component.annotations.ReferenceCardinality;
33 import org.osgi.service.component.annotations.ReferencePolicy;
34 import org.osgi.service.component.annotations.ReferencePolicyOption;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * OSGi Service Registry-backed implementation of {@link DOMSchemaService}.
41 @Component(service = DOMSchemaService.class, immediate = true)
42 public final class OSGiDOMSchemaService implements DOMSchemaService, DOMYangTextSourceProvider {
43 private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMSchemaService.class);
45 private final List<Consumer<EffectiveModelContext>> listeners = new CopyOnWriteArrayList<>();
46 private final AtomicReference<ModuleInfoSnapshot> currentSnapshot = new AtomicReference<>();
47 private final ComponentFactory<ModelContextListener> listenerFactory;
49 private boolean deactivated;
52 public OSGiDOMSchemaService(
53 @Reference(target = "(component.factory=" + ModelContextListener.FACTORY_NAME + ")")
54 final ComponentFactory<ModelContextListener> listenerFactory) {
55 this.listenerFactory = requireNonNull(listenerFactory);
56 LOG.info("DOM Schema services activated");
61 LOG.info("DOM Schema services deactivated");
65 @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
66 void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
67 LOG.info("Updating context to generation {}", newContext.getGeneration());
68 final var snapshot = newContext.getService();
69 final var modelContext = snapshot.modelContext();
70 final var previous = currentSnapshot.getAndSet(snapshot);
71 LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
73 listeners.forEach(listener -> notifyListener(modelContext, listener));
76 void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) {
77 final var snapshot = oldContext.getService();
78 if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) {
79 LOG.info("Lost final generation {}", oldContext.getGeneration());
83 @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
84 policyOption = ReferencePolicyOption.GREEDY)
85 void addListener(final Consumer<EffectiveModelContext> listener) {
86 LOG.trace("Adding listener {}", listener);
87 listeners.add(listener);
88 listener.accept(getGlobalContext());
91 void removeListener(final Consumer<EffectiveModelContext> listener) {
92 LOG.trace("Removing listener {}", listener);
93 listeners.remove(listener);
97 public @NonNull EffectiveModelContext getGlobalContext() {
98 return currentSnapshot.get().modelContext();
102 public Registration registerSchemaContextListener(final Consumer<EffectiveModelContext> listener) {
103 final var reg = listenerFactory.newInstance(ModelContextListener.props(listener));
104 return new AbstractRegistration() {
106 protected void removeRegistration() {
113 public ListenableFuture<? extends YangTextSource> getSource(final SourceIdentifier sourceIdentifier) {
114 return currentSnapshot.get().getSource(sourceIdentifier);
117 @SuppressWarnings("checkstyle:illegalCatch")
118 private static void notifyListener(final @NonNull EffectiveModelContext modelContext,
119 final Consumer<EffectiveModelContext> listener) {
121 listener.accept(modelContext);
122 } catch (RuntimeException e) {
123 LOG.warn("Failed to notify listener {}", listener, e);