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 org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
17 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
18 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
19 import org.opendaylight.mdsal.dom.spi.AbstractDOMSchemaService;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 import org.osgi.service.component.ComponentFactory;
26 import org.osgi.service.component.ComponentInstance;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Deactivate;
30 import org.osgi.service.component.annotations.FieldOption;
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<>();
50 private volatile ModuleInfoSnapshot currentSnapshot;
53 public EffectiveModelContext getGlobalContext() {
54 return currentSnapshot.getEffectiveModelContext();
58 public ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
59 final EffectiveModelContextListener listener) {
60 return registerListener(requireNonNull(listener));
64 public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
65 return currentSnapshot.getSource(sourceIdentifier);
68 @Reference(fieldOption = FieldOption.REPLACE)
69 void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
70 LOG.trace("Updating context to generation {}", newContext.getGeneration());
71 final ModuleInfoSnapshot snapshot = newContext.getService();
72 final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
73 currentSnapshot = snapshot;
74 listeners.forEach(listener -> notifyListener(ctx, listener));
77 @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
78 policyOption = ReferencePolicyOption.GREEDY)
79 void addListener(final EffectiveModelContextListener listener) {
80 LOG.trace("Adding listener {}", listener);
81 listeners.add(listener);
82 listener.onModelContextUpdated(getGlobalContext());
85 void removeListener(final EffectiveModelContextListener listener) {
86 LOG.trace("Removing listener {}", listener);
87 listeners.remove(listener);
91 @SuppressWarnings("static-method")
93 LOG.info("DOM Schema services activated");
97 @SuppressWarnings("static-method")
99 LOG.info("DOM Schema services deactivated");
102 private @NonNull ListenerRegistration<EffectiveModelContextListener> registerListener(
103 final @NonNull EffectiveModelContextListener listener) {
104 final ComponentInstance<EffectiveModelContextImpl> reg =
105 listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
106 return new ListenerRegistration<>() {
108 public EffectiveModelContextListener getInstance() {
113 public void close() {
119 @SuppressWarnings("checkstyle:illegalCatch")
120 private static void notifyListener(final EffectiveModelContext context,
121 final EffectiveModelContextListener listener) {
123 listener.onModelContextUpdated(context);
124 } catch (RuntimeException e) {
125 LOG.warn("Failed to notify listener {}", listener, e);