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.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
23 import org.osgi.service.component.ComponentFactory;
24 import org.osgi.service.component.ComponentInstance;
25 import org.osgi.service.component.annotations.Activate;
26 import org.osgi.service.component.annotations.Component;
27 import org.osgi.service.component.annotations.Deactivate;
28 import org.osgi.service.component.annotations.FieldOption;
29 import org.osgi.service.component.annotations.Reference;
30 import org.osgi.service.component.annotations.ReferenceCardinality;
31 import org.osgi.service.component.annotations.ReferencePolicy;
32 import org.osgi.service.component.annotations.ReferencePolicyOption;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 * OSGi Service Registry-backed implementation of {@link DOMSchemaService}.
39 @Component(service = DOMSchemaService.class, immediate = true)
40 public final class OSGiDOMSchemaService extends AbstractDOMSchemaService {
41 private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMSchemaService.class);
43 @Reference(target = "(component.factory=" + SchemaSchemaContextListenerImpl.FACTORY_NAME + ")")
44 ComponentFactory listenerFactory = null;
46 private final List<SchemaContextListener> listeners = new CopyOnWriteArrayList<>();
48 private volatile ModuleInfoSnapshot currentSnapshot;
51 public EffectiveModelContext getGlobalContext() {
52 return currentSnapshot.getEffectiveModelContext();
56 public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
57 final SchemaContextListener listener) {
58 return registerListener(requireNonNull(listener));
61 @Reference(fieldOption = FieldOption.REPLACE)
62 void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
63 LOG.trace("Updating context to generation {}", newContext.getGeneration());
64 final ModuleInfoSnapshot snapshot = newContext.getService();
65 final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
66 currentSnapshot = snapshot;
67 listeners.forEach(listener -> notifyListener(ctx, listener));
70 @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
71 policyOption = ReferencePolicyOption.GREEDY)
72 void addListener(final SchemaContextListener listener) {
73 LOG.trace("Adding listener {}", listener);
74 listeners.add(listener);
75 listener.onGlobalContextUpdated(getGlobalContext());
78 void removeListener(final SchemaContextListener listener) {
79 LOG.trace("Removing listener {}", listener);
80 listeners.remove(listener);
84 @SuppressWarnings("static-method")
86 LOG.info("DOM Schema services activated");
90 @SuppressWarnings("static-method")
92 LOG.info("DOM Schema services deactivated");
95 private @NonNull ListenerRegistration<SchemaContextListener> registerListener(
96 final @NonNull SchemaContextListener listener) {
97 final ComponentInstance reg = listenerFactory.newInstance(SchemaSchemaContextListenerImpl.props(listener));
98 return new ListenerRegistration<>() {
100 public SchemaContextListener getInstance() {
105 public void close() {
111 @SuppressWarnings("checkstyle:illegalCatch")
112 private static void notifyListener(final SchemaContext context, final SchemaContextListener listener) {
114 listener.onGlobalContextUpdated(context);
115 } catch (RuntimeException e) {
116 LOG.warn("Failed to notify listener {}", listener, e);