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 java.util.List;
13 import java.util.concurrent.CopyOnWriteArrayList;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.opendaylight.binding.runtime.api.ModuleInfoSnapshot;
16 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
17 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
18 import org.opendaylight.mdsal.dom.spi.AbstractDOMSchemaService;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
22 import org.osgi.service.component.ComponentFactory;
23 import org.osgi.service.component.ComponentInstance;
24 import org.osgi.service.component.annotations.Activate;
25 import org.osgi.service.component.annotations.Component;
26 import org.osgi.service.component.annotations.Deactivate;
27 import org.osgi.service.component.annotations.FieldOption;
28 import org.osgi.service.component.annotations.Reference;
29 import org.osgi.service.component.annotations.ReferenceCardinality;
30 import org.osgi.service.component.annotations.ReferencePolicy;
31 import org.osgi.service.component.annotations.ReferencePolicyOption;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
36 * OSGi Service Registry-backed implementation of {@link DOMSchemaService}.
38 @Component(service = DOMSchemaService.class, immediate = true)
39 public final class OSGiDOMSchemaService extends AbstractDOMSchemaService {
40 private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMSchemaService.class);
42 @Reference(target = "(component.factory=" + EffectiveModelContextImpl.FACTORY_NAME + ")")
43 ComponentFactory listenerFactory = null;
45 private final List<EffectiveModelContextListener> listeners = new CopyOnWriteArrayList<>();
47 private volatile ModuleInfoSnapshot currentSnapshot;
50 public EffectiveModelContext getGlobalContext() {
51 return currentSnapshot.getEffectiveModelContext();
55 public ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
56 final EffectiveModelContextListener listener) {
57 return registerListener(requireNonNull(listener));
60 @Reference(fieldOption = FieldOption.REPLACE)
61 void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
62 LOG.trace("Updating context to generation {}", newContext.getGeneration());
63 final ModuleInfoSnapshot snapshot = newContext.getService();
64 final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
65 currentSnapshot = snapshot;
66 listeners.forEach(listener -> notifyListener(ctx, listener));
69 @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
70 policyOption = ReferencePolicyOption.GREEDY)
71 void addListener(final EffectiveModelContextListener listener) {
72 LOG.trace("Adding listener {}", listener);
73 listeners.add(listener);
74 listener.onModelContextUpdated(getGlobalContext());
77 void removeListener(final EffectiveModelContextListener listener) {
78 LOG.trace("Removing listener {}", listener);
79 listeners.remove(listener);
83 @SuppressWarnings("static-method")
85 LOG.info("DOM Schema services activated");
89 @SuppressWarnings("static-method")
91 LOG.info("DOM Schema services deactivated");
94 private @NonNull ListenerRegistration<EffectiveModelContextListener> registerListener(
95 final @NonNull EffectiveModelContextListener listener) {
96 final ComponentInstance reg = listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
97 return new ListenerRegistration<>() {
99 public EffectiveModelContextListener getInstance() {
104 public void close() {
110 @SuppressWarnings("checkstyle:illegalCatch")
111 private static void notifyListener(final EffectiveModelContext context,
112 final EffectiveModelContextListener listener) {
114 listener.onModelContextUpdated(context);
115 } catch (RuntimeException e) {
116 LOG.warn("Failed to notify listener {}", listener, e);