Fix raw type warnings
[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<EffectiveModelContextImpl> 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<EffectiveModelContextImpl> reg =
105             listenerFactory.newInstance(EffectiveModelContextImpl.props(listener));
106         return new ListenerRegistration<>() {
107             @Override
108             public EffectiveModelContextListener getInstance() {
109                 return listener;
110             }
111
112             @Override
113             public void close() {
114                 reg.dispose();
115             }
116         };
117     }
118
119     @SuppressWarnings("checkstyle:illegalCatch")
120     private static void notifyListener(final EffectiveModelContext context,
121             final EffectiveModelContextListener listener) {
122         try {
123             listener.onModelContextUpdated(context);
124         } catch (RuntimeException e) {
125             LOG.warn("Failed to notify listener {}", listener, e);
126         }
127     }
128 }