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);
45 @Reference(target = "(component.factory=" + EffectiveModelContextImpl.FACTORY_NAME + ")")
46 ComponentFactory<EffectiveModelContextImpl> listenerFactory = null;
48 private final List<EffectiveModelContextListener> listeners = new CopyOnWriteArrayList<>();
49 private final AtomicReference<ModuleInfoSnapshot> currentSnapshot = new AtomicReference<>();
51 private boolean deactivated;
54 public @NonNull EffectiveModelContext getGlobalContext() {
55 return currentSnapshot.get().getEffectiveModelContext();
59 public Registration registerSchemaContextListener(final EffectiveModelContextListener listener) {
60 return registerListener(requireNonNull(listener));
64 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
65 return currentSnapshot.get().getSource(sourceIdentifier);
68 @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
69 void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
70 LOG.info("Updating context to generation {}", newContext.getGeneration());
71 final ModuleInfoSnapshot snapshot = newContext.getService();
72 final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
73 final ModuleInfoSnapshot previous = currentSnapshot.getAndSet(snapshot);
74 LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
76 listeners.forEach(listener -> notifyListener(ctx, listener));
79 void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) {
80 final ModuleInfoSnapshot snapshot = oldContext.getService();
81 if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) {
82 LOG.info("Lost final generation {}", oldContext.getGeneration());
86 @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
87 policyOption = ReferencePolicyOption.GREEDY)
88 void addListener(final EffectiveModelContextListener listener) {
89 LOG.trace("Adding listener {}", listener);
90 listeners.add(listener);
91 listener.onModelContextUpdated(getGlobalContext());
94 void removeListener(final EffectiveModelContextListener listener) {
95 LOG.trace("Removing listener {}", listener);
96 listeners.remove(listener);
100 @SuppressWarnings("static-method")
102 LOG.info("DOM Schema services activated");
107 LOG.info("DOM Schema services deactivated");
111 private @NonNull Registration registerListener(final @NonNull EffectiveModelContextListener listener) {
112 final var reg = listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
113 return new AbstractRegistration() {
115 protected void removeRegistration() {
121 @SuppressWarnings("checkstyle:illegalCatch")
122 private static void notifyListener(final @NonNull EffectiveModelContext context,
123 final EffectiveModelContextListener listener) {
125 listener.onModelContextUpdated(context);
126 } catch (RuntimeException e) {
127 LOG.warn("Failed to notify listener {}", listener, e);