2 * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.broker.schema;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Optional;
12 import com.google.common.collect.ImmutableMap;
13 import com.google.common.collect.Iterables;
14 import com.google.common.util.concurrent.CheckedFuture;
15 import java.io.IOException;
17 import java.util.ArrayList;
18 import java.util.List;
20 import javax.annotation.concurrent.GuardedBy;
21 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
22 import org.opendaylight.mdsal.dom.api.DOMSchemaServiceExtension;
23 import org.opendaylight.mdsal.dom.api.DOMYangTextSourceProvider;
24 import org.opendaylight.yangtools.concepts.ListenerRegistration;
25 import org.opendaylight.yangtools.concepts.Registration;
26 import org.opendaylight.yangtools.util.ListenerRegistry;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
30 import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
31 import org.opendaylight.yangtools.yang.model.repo.api.SchemaSourceException;
32 import org.opendaylight.yangtools.yang.model.repo.api.SourceIdentifier;
33 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
34 import org.opendaylight.yangtools.yang.parser.repo.YangTextSchemaContextResolver;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 public class ScanningSchemaServiceProvider
39 implements DOMSchemaService, SchemaContextProvider, DOMYangTextSourceProvider, AutoCloseable {
40 private static final Logger LOG = LoggerFactory.getLogger(ScanningSchemaServiceProvider.class);
43 private final ListenerRegistry<SchemaContextListener> listeners = new ListenerRegistry<>();
44 private final Object lock = new Object();
45 private final YangTextSchemaContextResolver contextResolver = YangTextSchemaContextResolver.create("global-bundle");
47 public void tryToUpdateSchemaContext() {
49 final Optional<SchemaContext> schema = contextResolver.getSchemaContext();
50 if (schema.isPresent()) {
51 if (LOG.isDebugEnabled()) {
52 LOG.debug("Got new SchemaContext: # of modules {}", schema.get().getAllModuleIdentifiers().size());
54 notifyListeners(schema.get());
60 @SuppressWarnings("checkstyle:IllegalCatch")
61 public void notifyListeners(final SchemaContext schemaContext) {
63 for (final ListenerRegistration<SchemaContextListener> registration : listeners) {
65 registration.getInstance().onGlobalContextUpdated(schemaContext);
66 } catch (final Exception e) {
67 LOG.error("Exception occured during invoking listener", e);
73 public List<Registration> registerAvailableYangs(final List<URL> yangs) {
74 final List<Registration> sourceRegistrator = new ArrayList<>();
75 for (final URL url : yangs) {
77 sourceRegistrator.add(contextResolver.registerSource(url));
78 } catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
79 LOG.warn("Failed to register {}, ignoring it", url, e);
82 return sourceRegistrator;
85 public void removeListener(final SchemaContextListener schemaContextListener) {
87 for (final ListenerRegistration<SchemaContextListener> listenerRegistration : listeners.getListeners()) {
88 if (listenerRegistration.getInstance().equals(schemaContextListener)) {
89 listenerRegistration.close();
96 public boolean hasListeners() {
99 if (Iterables.size(listeners.getListeners()) > 0) {
102 hasListeners = false;
109 public SchemaContext getSessionContext() {
110 throw new UnsupportedOperationException();
114 public SchemaContext getGlobalContext() {
115 return contextResolver.getSchemaContext().orNull();
119 public ListenerRegistration<SchemaContextListener>
120 registerSchemaContextListener(final SchemaContextListener listener) {
121 synchronized (lock) {
122 final Optional<SchemaContext> potentialCtx = contextResolver.getSchemaContext();
123 if (potentialCtx.isPresent()) {
124 listener.onGlobalContextUpdated(potentialCtx.get());
126 return listeners.register(listener);
131 public SchemaContext getSchemaContext() {
132 return getGlobalContext();
136 public CheckedFuture<? extends YangTextSchemaSource, SchemaSourceException>
137 getSource(final SourceIdentifier sourceIdentifier) {
138 return contextResolver.getSource(sourceIdentifier);
142 public Map<Class<? extends DOMSchemaServiceExtension>, DOMSchemaServiceExtension> getSupportedExtensions() {
143 return ImmutableMap.of(DOMYangTextSourceProvider.class, this);
147 public void close() {
148 synchronized (lock) {
149 for (final ListenerRegistration<SchemaContextListener> registration : listeners) {
150 registration.close();