Rework binding component instantiation
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / schema / ScanningSchemaServiceProvider.java
1 /*
2  * Copyright (c) 2017 Pantheon Technologies 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.broker.schema;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.collect.Iterables;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import java.io.IOException;
14 import java.net.URL;
15 import java.util.ArrayList;
16 import java.util.List;
17 import java.util.Optional;
18 import org.checkerframework.checker.lock.qual.GuardedBy;
19 import org.opendaylight.mdsal.dom.spi.AbstractDOMSchemaService;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.util.ListenerRegistry;
23 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
25 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
26 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
27 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
28 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
29 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33 @Deprecated
34 public class ScanningSchemaServiceProvider extends AbstractDOMSchemaService.WithYangTextSources
35         implements AutoCloseable {
36     private static final Logger LOG = LoggerFactory.getLogger(ScanningSchemaServiceProvider.class);
37
38     private final YangTextSchemaContextResolver contextResolver = YangTextSchemaContextResolver.create("global-bundle");
39     @GuardedBy("lock")
40     private final ListenerRegistry<SchemaContextListener> listeners = ListenerRegistry.create();
41     private final Object lock = new Object();
42
43     public void tryToUpdateSchemaContext() {
44         synchronized (lock) {
45             final Optional<? extends SchemaContext> optSchema = contextResolver.getSchemaContext();
46             optSchema.ifPresent(schema -> {
47                 if (LOG.isDebugEnabled()) {
48                     LOG.debug("Got new SchemaContext: # of modules {}", schema.getModules().size());
49                 }
50                 notifyListeners(schema);
51             });
52         }
53     }
54
55     @VisibleForTesting
56     @SuppressWarnings("checkstyle:IllegalCatch")
57     public void notifyListeners(final SchemaContext schemaContext) {
58         synchronized (lock) {
59             for (final ListenerRegistration<? extends SchemaContextListener> registration
60                     : listeners.getRegistrations()) {
61                 try {
62                     registration.getInstance().onGlobalContextUpdated(schemaContext);
63                 } catch (final Exception e) {
64                     LOG.error("Exception occured during invoking listener", e);
65                 }
66             }
67         }
68     }
69
70     public List<Registration> registerAvailableYangs(final List<URL> yangs) {
71         final List<Registration> sourceRegistrator = new ArrayList<>();
72         for (final URL url : yangs) {
73             try {
74                 sourceRegistrator.add(contextResolver.registerSource(url));
75             } catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
76                 LOG.warn("Failed to register {}, ignoring it", url, e);
77             }
78         }
79         return sourceRegistrator;
80     }
81
82     public void removeListener(final SchemaContextListener schemaContextListener) {
83         synchronized (lock) {
84             for (final ListenerRegistration<? extends SchemaContextListener> listenerRegistration
85                     : listeners.getRegistrations()) {
86                 if (listenerRegistration.getInstance().equals(schemaContextListener)) {
87                     listenerRegistration.close();
88                     break;
89                 }
90             }
91         }
92     }
93
94     public boolean hasListeners() {
95         synchronized (lock) {
96             return !Iterables.isEmpty(listeners.getRegistrations());
97         }
98     }
99
100     @Override
101     public SchemaContext getGlobalContext() {
102         return contextResolver.getSchemaContext().orElse(null);
103     }
104
105     @Override
106     public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
107             final SchemaContextListener listener) {
108         synchronized (lock) {
109             contextResolver.getSchemaContext().ifPresent(listener::onGlobalContextUpdated);
110             return listeners.register(listener);
111         }
112     }
113
114     @Override
115     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
116         return contextResolver.getSource(sourceIdentifier);
117     }
118
119     @Override
120     public void close() {
121         synchronized (lock) {
122             listeners.getRegistrations().forEach(ListenerRegistration::close);
123         }
124     }
125 }