Bump to odlparent-9.0.0/yangtools-7.0.1-SNAPSHOT
[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.util.concurrent.ListenableFuture;
12 import java.io.IOException;
13 import java.net.URL;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.Optional;
17 import org.checkerframework.checker.lock.qual.GuardedBy;
18 import org.opendaylight.mdsal.dom.spi.AbstractDOMSchemaService;
19 import org.opendaylight.yangtools.concepts.ListenerRegistration;
20 import org.opendaylight.yangtools.concepts.Registration;
21 import org.opendaylight.yangtools.util.ListenerRegistry;
22 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
23 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
24 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
25 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
26 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
27 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
28 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 @Deprecated
33 public class ScanningSchemaServiceProvider extends AbstractDOMSchemaService.WithYangTextSources
34         implements AutoCloseable {
35     private static final Logger LOG = LoggerFactory.getLogger(ScanningSchemaServiceProvider.class);
36
37     private final YangTextSchemaContextResolver contextResolver = YangTextSchemaContextResolver.create("global-bundle");
38     @GuardedBy("lock")
39     private final ListenerRegistry<EffectiveModelContextListener> listeners = ListenerRegistry.create();
40     private final Object lock = new Object();
41
42     public void tryToUpdateSchemaContext() {
43         synchronized (lock) {
44             final Optional<? extends EffectiveModelContext> optSchema = contextResolver.getEffectiveModelContext();
45             optSchema.ifPresent(schema -> {
46                 if (LOG.isDebugEnabled()) {
47                     LOG.debug("Got new SchemaContext: # of modules {}", schema.getModules().size());
48                 }
49                 notifyListeners(schema);
50             });
51         }
52     }
53
54     @VisibleForTesting
55     @SuppressWarnings("checkstyle:IllegalCatch")
56     public void notifyListeners(final EffectiveModelContext schemaContext) {
57         synchronized (lock) {
58             listeners.streamListeners().forEach(listener -> {
59                 try {
60                     listener.onModelContextUpdated(schemaContext);
61                 } catch (final Exception e) {
62                     LOG.error("Exception occured during invoking listener {}", listener, e);
63                 }
64             });
65         }
66     }
67
68     public List<Registration> registerAvailableYangs(final List<URL> yangs) {
69         final List<Registration> sourceRegistrator = new ArrayList<>();
70         for (final URL url : yangs) {
71             try {
72                 sourceRegistrator.add(contextResolver.registerSource(url));
73             } catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
74                 LOG.warn("Failed to register {}, ignoring it", url, e);
75             }
76         }
77         return sourceRegistrator;
78     }
79
80     public boolean hasListeners() {
81         synchronized (lock) {
82             return !listeners.isEmpty();
83         }
84     }
85
86     @Override
87     public EffectiveModelContext getGlobalContext() {
88         return contextResolver.getEffectiveModelContext().orElse(null);
89     }
90
91     @Override
92     public ListenerRegistration<EffectiveModelContextListener> registerSchemaContextListener(
93             final EffectiveModelContextListener listener) {
94         synchronized (lock) {
95             contextResolver.getEffectiveModelContext().ifPresent(listener::onModelContextUpdated);
96             return listeners.register(listener);
97         }
98     }
99
100     @Override
101     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
102         return contextResolver.getSource(sourceIdentifier);
103     }
104
105     @Override
106     public void close() {
107         synchronized (lock) {
108             listeners.clear();
109         }
110     }
111 }