f715cc8becf8cdac2c6bdcbb84e6671e84f6b2b7
[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 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<SchemaContextListener> listeners = ListenerRegistry.create();
40     private final Object lock = new Object();
41
42     public void tryToUpdateSchemaContext() {
43         synchronized (lock) {
44             final Optional<? extends SchemaContext> optSchema = contextResolver.getSchemaContext();
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 SchemaContext schemaContext) {
57         synchronized (lock) {
58             for (final ListenerRegistration<? extends SchemaContextListener> registration
59                     : listeners.getRegistrations()) {
60                 try {
61                     registration.getInstance().onGlobalContextUpdated(schemaContext);
62                 } catch (final Exception e) {
63                     LOG.error("Exception occured during invoking listener", e);
64                 }
65             }
66         }
67     }
68
69     public List<Registration> registerAvailableYangs(final List<URL> yangs) {
70         final List<Registration> sourceRegistrator = new ArrayList<>();
71         for (final URL url : yangs) {
72             try {
73                 sourceRegistrator.add(contextResolver.registerSource(url));
74             } catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
75                 LOG.warn("Failed to register {}, ignoring it", url, e);
76             }
77         }
78         return sourceRegistrator;
79     }
80
81     public void removeListener(final SchemaContextListener schemaContextListener) {
82         synchronized (lock) {
83             for (final ListenerRegistration<? extends SchemaContextListener> listenerRegistration
84                     : listeners.getRegistrations()) {
85                 if (listenerRegistration.getInstance().equals(schemaContextListener)) {
86                     listenerRegistration.close();
87                     break;
88                 }
89             }
90         }
91     }
92
93     public boolean hasListeners() {
94         synchronized (lock) {
95             return !Iterables.isEmpty(listeners.getRegistrations());
96         }
97     }
98
99     @Override
100     public SchemaContext getGlobalContext() {
101         return contextResolver.getSchemaContext().orElse(null);
102     }
103
104     @Override
105     public ListenerRegistration<SchemaContextListener> registerSchemaContextListener(
106             final SchemaContextListener listener) {
107         synchronized (lock) {
108             contextResolver.getSchemaContext().ifPresent(listener::onGlobalContextUpdated);
109             return listeners.register(listener);
110         }
111     }
112
113     @Override
114     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
115         return contextResolver.getSource(sourceIdentifier);
116     }
117
118     @Override
119     public void close() {
120         synchronized (lock) {
121             listeners.getRegistrations().forEach(ListenerRegistration::close);
122         }
123     }
124 }