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.ListenerRegistration;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
24 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
25 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
26 import org.osgi.service.component.ComponentFactory;
27 import org.osgi.service.component.ComponentInstance;
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 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 ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
60 final EffectiveModelContextListener listener) {
61 return registerListener(requireNonNull(listener));
65 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
66 return currentSnapshot.get().getSource(sourceIdentifier);
69 @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
70 void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
71 LOG.info("Updating context to generation {}", newContext.getGeneration());
72 final ModuleInfoSnapshot snapshot = newContext.getService();
73 final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
74 final ModuleInfoSnapshot previous = currentSnapshot.getAndSet(snapshot);
75 LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
77 listeners.forEach(listener -> notifyListener(ctx, listener));
80 void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) {
81 final ModuleInfoSnapshot snapshot = oldContext.getService();
82 if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) {
83 LOG.info("Lost final generation {}", oldContext.getGeneration());
87 @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
88 policyOption = ReferencePolicyOption.GREEDY)
89 void addListener(final EffectiveModelContextListener listener) {
90 LOG.trace("Adding listener {}", listener);
91 listeners.add(listener);
92 listener.onModelContextUpdated(getGlobalContext());
95 void removeListener(final EffectiveModelContextListener listener) {
96 LOG.trace("Removing listener {}", listener);
97 listeners.remove(listener);
101 @SuppressWarnings("static-method")
103 LOG.info("DOM Schema services activated");
108 LOG.info("DOM Schema services deactivated");
112 private @NonNull ListenerRegistration<EffectiveModelContextListener> registerListener(
113 final @NonNull EffectiveModelContextListener listener) {
114 final ComponentInstance reg = listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
115 return new ListenerRegistration<>() {
117 public EffectiveModelContextListener getInstance() {
122 public void close() {
128 @SuppressWarnings("checkstyle:illegalCatch")
129 private static void notifyListener(final @NonNull EffectiveModelContext context,
130 final EffectiveModelContextListener listener) {
132 listener.onModelContextUpdated(context);
133 } catch (RuntimeException e) {
134 LOG.warn("Failed to notify listener {}", listener, e);