a1ace089a0279b82e352dcfa8e9565ada4d1b4ae
[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 java.util.function.Consumer;
17 import org.eclipse.jdt.annotation.NonNull;
18 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
19 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
20 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
21 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
22 import org.opendaylight.yangtools.concepts.AbstractRegistration;
23 import org.opendaylight.yangtools.concepts.Registration;
24 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
25 import org.opendaylight.yangtools.yang.model.api.source.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
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 implements DOMSchemaService, DOMYangTextSourceProvider {
43     private static final Logger LOG = LoggerFactory.getLogger(OSGiDOMSchemaService.class);
44
45     private final List<Consumer<EffectiveModelContext>> listeners = new CopyOnWriteArrayList<>();
46     private final AtomicReference<ModuleInfoSnapshot> currentSnapshot = new AtomicReference<>();
47     private final ComponentFactory<ModelContextListener> listenerFactory;
48
49     private boolean deactivated;
50
51     @Activate
52     public OSGiDOMSchemaService(
53             @Reference(target = "(component.factory=" + ModelContextListener.FACTORY_NAME + ")")
54             final ComponentFactory<ModelContextListener> listenerFactory) {
55         this.listenerFactory = requireNonNull(listenerFactory);
56         LOG.info("DOM Schema services activated");
57     }
58
59     @Deactivate
60     void deactivate() {
61         LOG.info("DOM Schema services deactivated");
62         deactivated = true;
63     }
64
65     @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
66     void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
67         LOG.info("Updating context to generation {}", newContext.getGeneration());
68         final var snapshot = newContext.getService();
69         final var modelContext = snapshot.modelContext();
70         final var previous = currentSnapshot.getAndSet(snapshot);
71         LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
72
73         listeners.forEach(listener -> notifyListener(modelContext, listener));
74     }
75
76     void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) {
77         final var snapshot = oldContext.getService();
78         if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) {
79             LOG.info("Lost final generation {}", oldContext.getGeneration());
80         }
81     }
82
83     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
84             policyOption = ReferencePolicyOption.GREEDY)
85     void addListener(final Consumer<EffectiveModelContext> listener) {
86         LOG.trace("Adding listener {}", listener);
87         listeners.add(listener);
88         listener.accept(getGlobalContext());
89     }
90
91     void removeListener(final Consumer<EffectiveModelContext> listener) {
92         LOG.trace("Removing listener {}", listener);
93         listeners.remove(listener);
94     }
95
96     @Override
97     public @NonNull EffectiveModelContext getGlobalContext() {
98         return currentSnapshot.get().modelContext();
99     }
100
101     @Override
102     public Registration registerSchemaContextListener(final Consumer<EffectiveModelContext> listener) {
103         final var reg = listenerFactory.newInstance(ModelContextListener.props(listener));
104         return new AbstractRegistration() {
105             @Override
106             protected void removeRegistration() {
107                 reg.dispose();
108             }
109         };
110     }
111
112     @Override
113     public ListenableFuture<? extends YangTextSource> getSource(final SourceIdentifier sourceIdentifier) {
114         return currentSnapshot.get().getSource(sourceIdentifier);
115     }
116
117     @SuppressWarnings("checkstyle:illegalCatch")
118     private static void notifyListener(final @NonNull EffectiveModelContext modelContext,
119             final Consumer<EffectiveModelContext> listener) {
120         try {
121             listener.accept(modelContext);
122         } catch (RuntimeException e) {
123             LOG.warn("Failed to notify listener {}", listener, e);
124         }
125     }
126 }