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 org.eclipse.jdt.annotation.NonNull;
17 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
18 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
19 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
20 import org.opendaylight.mdsal.dom.spi.AbstractDOMSchemaService;
21 import org.opendaylight.yangtools.concepts.AbstractRegistration;
22 import org.opendaylight.yangtools.concepts.Registration;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
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 extends AbstractDOMSchemaService.WithYangTextSources {
43 private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMSchemaService.class);
46 private final List<EffectiveModelContextListener> listeners = new CopyOnWriteArrayList<>();
47 private final AtomicReference<ModuleInfoSnapshot> currentSnapshot = new AtomicReference<>();
48 private final ComponentFactory<EffectiveModelContextImpl> listenerFactory;
50 private boolean deactivated;
53 public OSGiDOMSchemaService(
54 @Reference(target = "(component.factory=" + EffectiveModelContextImpl.FACTORY_NAME + ")")
55 final ComponentFactory<EffectiveModelContextImpl> listenerFactory) {
56 this.listenerFactory = requireNonNull(listenerFactory);
57 LOG.info("DOM Schema services activated");
62 LOG.info("DOM Schema services deactivated");
66 @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
67 void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
68 LOG.info("Updating context to generation {}", newContext.getGeneration());
69 final var snapshot = newContext.getService();
70 final var modelContext = snapshot.getEffectiveModelContext();
71 final var previous = currentSnapshot.getAndSet(snapshot);
72 LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
74 listeners.forEach(listener -> notifyListener(modelContext, listener));
77 void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) {
78 final var snapshot = oldContext.getService();
79 if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) {
80 LOG.info("Lost final generation {}", oldContext.getGeneration());
84 @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
85 policyOption = ReferencePolicyOption.GREEDY)
86 void addListener(final EffectiveModelContextListener listener) {
87 LOG.trace("Adding listener {}", listener);
88 listeners.add(listener);
89 listener.onModelContextUpdated(getGlobalContext());
92 void removeListener(final EffectiveModelContextListener listener) {
93 LOG.trace("Removing listener {}", listener);
94 listeners.remove(listener);
98 public @NonNull EffectiveModelContext getGlobalContext() {
99 return currentSnapshot.get().getEffectiveModelContext();
103 public Registration registerSchemaContextListener(final EffectiveModelContextListener listener) {
104 final var reg = listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
105 return new AbstractRegistration() {
107 protected void removeRegistration() {
114 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
115 return currentSnapshot.get().getSource(sourceIdentifier);
118 @SuppressWarnings("checkstyle:illegalCatch")
119 private static void notifyListener(final @NonNull EffectiveModelContext modelContext,
120 final EffectiveModelContextListener listener) {
122 listener.onModelContextUpdated(modelContext);
123 } catch (RuntimeException e) {
124 LOG.warn("Failed to notify listener {}", listener, e);