0512f65cbe890b50a17cf4972574d6d9b7c1cdf9
[mdsal.git] / dom / mdsal-dom-schema-osgi / src / main / java / org / opendaylight / mdsal / dom / schema / osgi / impl / OSGiDOMSchemaService.java
1 /*
2  * Copyright (c) 2017, 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.schema.osgi.impl;
9
10 import static java.util.Objects.requireNonNull;
11
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;
37
38 /**
39  * OSGi Service Registry-backed implementation of {@link DOMSchemaService}.
40  */
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);
44
45
46     private final List<EffectiveModelContextListener> listeners = new CopyOnWriteArrayList<>();
47     private final AtomicReference<ModuleInfoSnapshot> currentSnapshot = new AtomicReference<>();
48     private final ComponentFactory<EffectiveModelContextImpl> listenerFactory;
49
50     private boolean deactivated;
51
52     @Activate
53     public OSGiDOMSchemaService(
54             @Reference(target = "(component.factory=" + EffectiveModelContextImpl.FACTORY_NAME + ")")
55             final ComponentFactory<EffectiveModelContextImpl> listenerFactory) {
56         this.listenerFactory = requireNonNull(listenerFactory);
57         LOG.info("DOM Schema services activated");
58     }
59
60     @Deactivate
61     void deactivate() {
62         LOG.info("DOM Schema services deactivated");
63         deactivated = true;
64     }
65
66     @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
67     void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
68         LOG.info("Updating context to generation {}", newContext.getGeneration());
69         final var snapshot = newContext.getService();
70         final var modelContext = snapshot.getEffectiveModelContext();
71         final var previous = currentSnapshot.getAndSet(snapshot);
72         LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
73
74         listeners.forEach(listener -> notifyListener(modelContext, listener));
75     }
76
77     void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) {
78         final var snapshot = oldContext.getService();
79         if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) {
80             LOG.info("Lost final generation {}", oldContext.getGeneration());
81         }
82     }
83
84     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
85             policyOption = ReferencePolicyOption.GREEDY)
86     void addListener(final EffectiveModelContextListener listener) {
87         LOG.trace("Adding listener {}", listener);
88         listeners.add(listener);
89         listener.onModelContextUpdated(getGlobalContext());
90     }
91
92     void removeListener(final EffectiveModelContextListener listener) {
93         LOG.trace("Removing listener {}", listener);
94         listeners.remove(listener);
95     }
96
97     @Override
98     public @NonNull EffectiveModelContext getGlobalContext() {
99         return currentSnapshot.get().getEffectiveModelContext();
100     }
101
102     @Override
103     public Registration registerSchemaContextListener(final EffectiveModelContextListener listener) {
104         final var reg = listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
105         return new AbstractRegistration() {
106             @Override
107             protected void removeRegistration() {
108                 reg.dispose();
109             }
110         };
111     }
112
113     @Override
114     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
115         return currentSnapshot.get().getSource(sourceIdentifier);
116     }
117
118     @SuppressWarnings("checkstyle:illegalCatch")
119     private static void notifyListener(final @NonNull EffectiveModelContext modelContext,
120             final EffectiveModelContextListener listener) {
121         try {
122             listener.onModelContextUpdated(modelContext);
123         } catch (RuntimeException e) {
124             LOG.warn("Failed to notify listener {}", listener, e);
125         }
126     }
127 }