2 * Copyright (c) 2013 Cisco Systems, Inc. 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.controller.sal.dom.broker;
10 import static com.google.common.base.Preconditions.checkState;
12 import com.google.common.annotations.VisibleForTesting;
13 import com.google.common.base.Optional;
14 import com.google.common.base.Preconditions;
15 import com.google.common.collect.ImmutableList;
18 import java.util.ArrayList;
19 import java.util.Collections;
20 import java.util.Enumeration;
21 import java.util.List;
23 import org.opendaylight.controller.sal.core.api.model.SchemaService;
24 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider;
25 import org.opendaylight.yangtools.concepts.ListenerRegistration;
26 import org.opendaylight.yangtools.concepts.Registration;
27 import org.opendaylight.yangtools.util.ListenerRegistry;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
31 import org.opendaylight.yangtools.yang.parser.repo.URLSchemaContextResolver;
32 import org.osgi.framework.Bundle;
33 import org.osgi.framework.BundleContext;
34 import org.osgi.framework.BundleEvent;
35 import org.osgi.framework.ServiceReference;
36 import org.osgi.util.tracker.BundleTracker;
37 import org.osgi.util.tracker.BundleTrackerCustomizer;
38 import org.osgi.util.tracker.ServiceTracker;
39 import org.osgi.util.tracker.ServiceTrackerCustomizer;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
43 public class GlobalBundleScanningSchemaServiceImpl implements SchemaContextProvider, SchemaService, ServiceTrackerCustomizer<SchemaContextListener, SchemaContextListener>, AutoCloseable {
44 private static final Logger LOG = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class);
46 private final ListenerRegistry<SchemaContextListener> listeners = new ListenerRegistry<>();
47 private final URLSchemaContextResolver contextResolver = URLSchemaContextResolver.create("global-bundle");
48 private final BundleScanner scanner = new BundleScanner();
49 private final BundleContext context;
51 private ServiceTracker<SchemaContextListener, SchemaContextListener> listenerTracker;
52 private BundleTracker<Iterable<Registration>> bundleTracker;
53 private boolean starting = true;
54 private static GlobalBundleScanningSchemaServiceImpl instance;
56 private GlobalBundleScanningSchemaServiceImpl(final BundleContext context) {
57 this.context = Preconditions.checkNotNull(context);
60 public synchronized static GlobalBundleScanningSchemaServiceImpl createInstance(final BundleContext ctx) {
61 Preconditions.checkState(instance == null);
62 instance = new GlobalBundleScanningSchemaServiceImpl(ctx);
67 public synchronized static GlobalBundleScanningSchemaServiceImpl getInstance() {
68 Preconditions.checkState(instance != null, "Global Instance was not instantiated");
73 public static synchronized void destroyInstance() {
81 public BundleContext getContext() {
86 checkState(context != null);
88 listenerTracker = new ServiceTracker<>(context, SchemaContextListener.class, GlobalBundleScanningSchemaServiceImpl.this);
89 bundleTracker = new BundleTracker<>(context, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, scanner);
91 listenerTracker.open();
93 tryToUpdateSchemaContext();
97 public SchemaContext getSchemaContext() {
98 return getGlobalContext();
102 public SchemaContext getGlobalContext() {
103 return contextResolver.getSchemaContext().orNull();
107 public void addModule(final Module module) {
108 throw new UnsupportedOperationException();
112 public SchemaContext getSessionContext() {
113 throw new UnsupportedOperationException();
117 public void removeModule(final Module module) {
118 throw new UnsupportedOperationException();
122 public synchronized ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
123 Optional<SchemaContext> potentialCtx = contextResolver.getSchemaContext();
124 if(potentialCtx.isPresent()) {
125 listener.onGlobalContextUpdated(potentialCtx.get());
127 return listeners.register(listener);
131 public void close() {
132 if (bundleTracker != null) {
133 bundleTracker.close();
135 if (listenerTracker != null) {
136 listenerTracker.close();
139 for (ListenerRegistration<SchemaContextListener> l : listeners.getListeners()) {
144 private synchronized void updateContext(final SchemaContext snapshot) {
145 Object[] services = listenerTracker.getServices();
146 for (ListenerRegistration<SchemaContextListener> listener : listeners) {
148 listener.getInstance().onGlobalContextUpdated(snapshot);
149 } catch (Exception e) {
150 LOG.error("Exception occured during invoking listener", e);
153 if (services != null) {
154 for (Object rawListener : services) {
155 final SchemaContextListener listener = (SchemaContextListener) rawListener;
157 listener.onGlobalContextUpdated(snapshot);
158 } catch (Exception e) {
159 LOG.error("Exception occured during invoking listener {}", listener, e);
165 private class BundleScanner implements BundleTrackerCustomizer<Iterable<Registration>> {
167 public Iterable<Registration> addingBundle(final Bundle bundle, final BundleEvent event) {
169 if (bundle.getBundleId() == 0) {
170 return Collections.emptyList();
173 final Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
174 if (enumeration == null) {
175 return Collections.emptyList();
178 final List<Registration> urls = new ArrayList<>();
179 while (enumeration.hasMoreElements()) {
180 final URL u = enumeration.nextElement();
182 urls.add(contextResolver.registerSource(u));
183 LOG.debug("Registered {}", u);
184 } catch (Exception e) {
185 LOG.warn("Failed to register {}, ignoring it", e);
189 if (!urls.isEmpty()) {
190 LOG.debug("Loaded {} new URLs, rebuilding schema context", urls.size());
191 tryToUpdateSchemaContext();
194 return ImmutableList.copyOf(urls);
198 public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Iterable<Registration> object) {
199 LOG.debug("Modified bundle {} {} {}", bundle, event, object);
203 * If removing YANG files makes yang store inconsistent, method
204 * {@link #getYangStoreSnapshot()} will throw exception. There is no
209 public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final Iterable<Registration> urls) {
210 for (Registration url : urls) {
213 } catch (Exception e) {
214 LOG.warn("Failed do unregister URL {}, proceeding", url, e);
217 tryToUpdateSchemaContext();
222 public synchronized SchemaContextListener addingService(final ServiceReference<SchemaContextListener> reference) {
224 SchemaContextListener listener = context.getService(reference);
225 SchemaContext _ctxContext = getGlobalContext();
226 if (getContext() != null && _ctxContext != null) {
227 listener.onGlobalContextUpdated(_ctxContext);
232 public synchronized void tryToUpdateSchemaContext() {
236 Optional<SchemaContext> schema = contextResolver.getSchemaContext();
237 if(schema.isPresent()) {
238 updateContext(schema.get());
243 public void modifiedService(final ServiceReference<SchemaContextListener> reference, final SchemaContextListener service) {
248 public void removedService(final ServiceReference<SchemaContextListener> reference, final SchemaContextListener service) {
249 context.ungetService(reference);