Switch DOMExtensibleService to require getExtensions()
[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.ClassToInstanceMap;
12 import com.google.common.collect.ImmutableClassToInstanceMap;
13 import com.google.common.collect.Iterables;
14 import com.google.common.util.concurrent.ListenableFuture;
15 import java.io.IOException;
16 import java.net.URL;
17 import java.util.ArrayList;
18 import java.util.List;
19 import java.util.Optional;
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;
37
38 public class ScanningSchemaServiceProvider
39         implements DOMSchemaService, SchemaContextProvider, DOMYangTextSourceProvider, AutoCloseable {
40     private static final Logger LOG = LoggerFactory.getLogger(ScanningSchemaServiceProvider.class);
41
42     @GuardedBy("lock")
43     private final ListenerRegistry<SchemaContextListener> listeners = new ListenerRegistry<>();
44     private final Object lock = new Object();
45     private final YangTextSchemaContextResolver contextResolver = YangTextSchemaContextResolver.create("global-bundle");
46
47     public void tryToUpdateSchemaContext() {
48         synchronized (lock) {
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().getModules().size());
53                 }
54                 notifyListeners(schema.get());
55             }
56         }
57     }
58
59     @VisibleForTesting
60     @SuppressWarnings("checkstyle:IllegalCatch")
61     public void notifyListeners(final SchemaContext schemaContext) {
62         synchronized (lock) {
63             for (final ListenerRegistration<SchemaContextListener> registration : listeners) {
64                 try {
65                     registration.getInstance().onGlobalContextUpdated(schemaContext);
66                 } catch (final Exception e) {
67                     LOG.error("Exception occured during invoking listener", e);
68                 }
69             }
70         }
71     }
72
73     public List<Registration> registerAvailableYangs(final List<URL> yangs) {
74         final List<Registration> sourceRegistrator = new ArrayList<>();
75         for (final URL url : yangs) {
76             try {
77                 sourceRegistrator.add(contextResolver.registerSource(url));
78             } catch (SchemaSourceException | IOException | YangSyntaxErrorException e) {
79                 LOG.warn("Failed to register {}, ignoring it", url, e);
80             }
81         }
82         return sourceRegistrator;
83     }
84
85     public void removeListener(final SchemaContextListener schemaContextListener) {
86         synchronized (lock) {
87             for (final ListenerRegistration<SchemaContextListener> listenerRegistration : listeners.getListeners()) {
88                 if (listenerRegistration.getInstance().equals(schemaContextListener)) {
89                     listenerRegistration.close();
90                     break;
91                 }
92             }
93         }
94     }
95
96     public boolean hasListeners() {
97         boolean hasListeners;
98         synchronized (lock) {
99             if (Iterables.size(listeners.getListeners()) > 0) {
100                 hasListeners = true;
101             } else {
102                 hasListeners = false;
103             }
104         }
105         return hasListeners;
106     }
107
108     @Override
109     public SchemaContext getSessionContext() {
110         throw new UnsupportedOperationException();
111     }
112
113     @Override
114     public SchemaContext getGlobalContext() {
115         return contextResolver.getSchemaContext().orElse(null);
116     }
117
118     @Override
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());
125             }
126             return listeners.register(listener);
127         }
128     }
129
130     @Override
131     public SchemaContext getSchemaContext() {
132         return getGlobalContext();
133     }
134
135     @Override
136     public ListenableFuture<? extends YangTextSchemaSource> getSource(final SourceIdentifier sourceIdentifier) {
137         return contextResolver.getSource(sourceIdentifier);
138     }
139
140     @Override
141     public ClassToInstanceMap<DOMSchemaServiceExtension> getExtensions() {
142         return ImmutableClassToInstanceMap.of(DOMYangTextSourceProvider.class, this);
143     }
144
145     @Override
146     public void close() {
147         synchronized (lock) {
148             for (final ListenerRegistration<SchemaContextListener> registration : listeners) {
149                 registration.close();
150             }
151         }
152     }
153 }