50476c4e1aab615c3fdf0663e864d40b990cb081
[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     @Reference(target = "(component.factory=" + EffectiveModelContextImpl.FACTORY_NAME + ")")
46     ComponentFactory<EffectiveModelContextImpl> listenerFactory = null;
47
48     private final List<EffectiveModelContextListener> listeners = new CopyOnWriteArrayList<>();
49     private final AtomicReference<ModuleInfoSnapshot> currentSnapshot = new AtomicReference<>();
50
51     private boolean deactivated;
52
53     @Override
54     public @NonNull EffectiveModelContext getGlobalContext() {
55         return currentSnapshot.get().getEffectiveModelContext();
56     }
57
58     @Override
59     public Registration registerSchemaContextListener(final EffectiveModelContextListener listener) {
60         return registerListener(requireNonNull(listener));
61     }
62
63     @Override
64     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
65         return currentSnapshot.get().getSource(sourceIdentifier);
66     }
67
68     @Reference(policy = ReferencePolicy.DYNAMIC, policyOption = ReferencePolicyOption.GREEDY)
69     void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
70         LOG.info("Updating context to generation {}", newContext.getGeneration());
71         final ModuleInfoSnapshot snapshot = newContext.getService();
72         final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
73         final ModuleInfoSnapshot previous = currentSnapshot.getAndSet(snapshot);
74         LOG.debug("Snapshot updated from {} to {}", previous, snapshot);
75
76         listeners.forEach(listener -> notifyListener(ctx, listener));
77     }
78
79     void unbindSnapshot(final OSGiModuleInfoSnapshot oldContext) {
80         final ModuleInfoSnapshot snapshot = oldContext.getService();
81         if (currentSnapshot.compareAndSet(snapshot, null) && !deactivated) {
82             LOG.info("Lost final generation {}", oldContext.getGeneration());
83         }
84     }
85
86     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
87             policyOption = ReferencePolicyOption.GREEDY)
88     void addListener(final EffectiveModelContextListener listener) {
89         LOG.trace("Adding listener {}", listener);
90         listeners.add(listener);
91         listener.onModelContextUpdated(getGlobalContext());
92     }
93
94     void removeListener(final EffectiveModelContextListener listener) {
95         LOG.trace("Removing listener {}", listener);
96         listeners.remove(listener);
97     }
98
99     @Activate
100     @SuppressWarnings("static-method")
101     void activate() {
102         LOG.info("DOM Schema services activated");
103     }
104
105     @Deactivate
106     void deactivate() {
107         LOG.info("DOM Schema services deactivated");
108         deactivated = true;
109     }
110
111     private @NonNull Registration registerListener(final @NonNull EffectiveModelContextListener listener) {
112         final var reg = listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
113         return new AbstractRegistration() {
114             @Override
115             protected void removeRegistration() {
116                 reg.dispose();
117             }
118         };
119     }
120
121     @SuppressWarnings("checkstyle:illegalCatch")
122     private static void notifyListener(final @NonNull EffectiveModelContext context,
123             final EffectiveModelContextListener listener) {
124         try {
125             listener.onModelContextUpdated(context);
126         } catch (RuntimeException e) {
127             LOG.warn("Failed to notify listener {}", listener, e);
128         }
129     }
130 }