567f10a8621fec4a83f8fa400102138aac4b8fd2
[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 org.eclipse.jdt.annotation.NonNull;
16 import org.opendaylight.mdsal.binding.runtime.api.ModuleInfoSnapshot;
17 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
18 import org.opendaylight.mdsal.dom.schema.osgi.OSGiModuleInfoSnapshot;
19 import org.opendaylight.mdsal.dom.spi.AbstractDOMSchemaService;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
23 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
24 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
25 import org.osgi.service.component.ComponentFactory;
26 import org.osgi.service.component.ComponentInstance;
27 import org.osgi.service.component.annotations.Activate;
28 import org.osgi.service.component.annotations.Component;
29 import org.osgi.service.component.annotations.Deactivate;
30 import org.osgi.service.component.annotations.FieldOption;
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 listenerFactory = null;
47
48     private final List<EffectiveModelContextListener> listeners = new CopyOnWriteArrayList<>();
49
50     private volatile ModuleInfoSnapshot currentSnapshot;
51
52     @Override
53     public EffectiveModelContext getGlobalContext() {
54         return currentSnapshot.getEffectiveModelContext();
55     }
56
57     @Override
58     public ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
59             final EffectiveModelContextListener listener) {
60         return registerListener(requireNonNull(listener));
61     }
62
63     @Override
64     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
65         return currentSnapshot.getSource(sourceIdentifier);
66     }
67
68     @Reference(fieldOption = FieldOption.REPLACE)
69     void bindSnapshot(final OSGiModuleInfoSnapshot newContext) {
70         LOG.trace("Updating context to generation {}", newContext.getGeneration());
71         final ModuleInfoSnapshot snapshot = newContext.getService();
72         final EffectiveModelContext ctx = snapshot.getEffectiveModelContext();
73         currentSnapshot = snapshot;
74         listeners.forEach(listener -> notifyListener(ctx, listener));
75     }
76
77     @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC,
78             policyOption = ReferencePolicyOption.GREEDY)
79     void addListener(final EffectiveModelContextListener listener) {
80         LOG.trace("Adding listener {}", listener);
81         listeners.add(listener);
82         listener.onModelContextUpdated(getGlobalContext());
83     }
84
85     void removeListener(final EffectiveModelContextListener listener) {
86         LOG.trace("Removing listener {}", listener);
87         listeners.remove(listener);
88     }
89
90     @Activate
91     @SuppressWarnings("static-method")
92     void activate() {
93         LOG.info("DOM Schema services activated");
94     }
95
96     @Deactivate
97     @SuppressWarnings("static-method")
98     void deactivate() {
99         LOG.info("DOM Schema services deactivated");
100     }
101
102     private @NonNull ListenerRegistration<EffectiveModelContextListener> registerListener(
103             final @NonNull EffectiveModelContextListener listener) {
104         final ComponentInstance reg = listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
105         return new ListenerRegistration<>() {
106             @Override
107             public EffectiveModelContextListener getInstance() {
108                 return listener;
109             }
110
111             @Override
112             public void close() {
113                 reg.dispose();
114             }
115         };
116     }
117
118     @SuppressWarnings("checkstyle:illegalCatch")
119     private static void notifyListener(final EffectiveModelContext context,
120             final EffectiveModelContextListener listener) {
121         try {
122             listener.onModelContextUpdated(context);
123         } catch (RuntimeException e) {
124             LOG.warn("Failed to notify listener {}", listener, e);
125         }
126     }
127 }