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;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.Iterables;
17 import java.util.ArrayList;
18 import java.util.Collections;
19 import java.util.Enumeration;
20 import java.util.List;
21 import org.opendaylight.controller.sal.core.api.model.SchemaService;
22 import org.opendaylight.yangtools.concepts.ListenerRegistration;
23 import org.opendaylight.yangtools.concepts.Registration;
24 import org.opendaylight.yangtools.util.ListenerRegistry;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
28 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
29 import org.opendaylight.yangtools.yang.parser.repo.URLSchemaContextResolver;
30 import org.osgi.framework.Bundle;
31 import org.osgi.framework.BundleContext;
32 import org.osgi.framework.BundleEvent;
33 import org.osgi.framework.ServiceReference;
34 import org.osgi.util.tracker.BundleTracker;
35 import org.osgi.util.tracker.BundleTrackerCustomizer;
36 import org.osgi.util.tracker.ServiceTracker;
37 import org.osgi.util.tracker.ServiceTrackerCustomizer;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
41 public class GlobalBundleScanningSchemaServiceImpl implements SchemaContextProvider, SchemaService, ServiceTrackerCustomizer<SchemaContextListener, SchemaContextListener>, AutoCloseable {
42 private static final Logger LOG = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class);
44 private final ListenerRegistry<SchemaContextListener> listeners = new ListenerRegistry<>();
45 private final URLSchemaContextResolver contextResolver = URLSchemaContextResolver.create("global-bundle");
46 private final BundleScanner scanner = new BundleScanner();
47 private final BundleContext context;
49 private ServiceTracker<SchemaContextListener, SchemaContextListener> listenerTracker;
50 private BundleTracker<Iterable<Registration>> bundleTracker;
51 private boolean starting = true;
52 private volatile boolean stopping;
53 private static GlobalBundleScanningSchemaServiceImpl instance;
55 private GlobalBundleScanningSchemaServiceImpl(final BundleContext context) {
56 this.context = Preconditions.checkNotNull(context);
59 public synchronized static GlobalBundleScanningSchemaServiceImpl createInstance(final BundleContext ctx) {
60 Preconditions.checkState(instance == null);
61 instance = new GlobalBundleScanningSchemaServiceImpl(ctx);
66 public synchronized static GlobalBundleScanningSchemaServiceImpl getInstance() {
67 Preconditions.checkState(instance != null, "Global Instance was not instantiated");
72 public static synchronized void destroyInstance() {
80 public BundleContext getContext() {
85 checkState(context != null);
86 LOG.debug("start() starting");
88 listenerTracker = new ServiceTracker<>(context, SchemaContextListener.class, GlobalBundleScanningSchemaServiceImpl.this);
89 bundleTracker = new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING |
90 Bundle.STOPPING | Bundle.ACTIVE, scanner);
93 LOG.debug("BundleTracker.open() complete");
95 listenerTracker.open();
97 tryToUpdateSchemaContext();
99 LOG.debug("start() complete");
103 public SchemaContext getSchemaContext() {
104 return getGlobalContext();
108 public SchemaContext getGlobalContext() {
109 return contextResolver.getSchemaContext().orNull();
113 public void addModule(final Module module) {
114 throw new UnsupportedOperationException();
118 public SchemaContext getSessionContext() {
119 throw new UnsupportedOperationException();
123 public void removeModule(final Module module) {
124 throw new UnsupportedOperationException();
128 public synchronized ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
129 Optional<SchemaContext> potentialCtx = contextResolver.getSchemaContext();
130 if(potentialCtx.isPresent()) {
131 listener.onGlobalContextUpdated(potentialCtx.get());
133 return listeners.register(listener);
137 public void close() {
139 if (bundleTracker != null) {
140 bundleTracker.close();
142 if (listenerTracker != null) {
143 listenerTracker.close();
146 for (ListenerRegistration<SchemaContextListener> l : listeners.getListeners()) {
151 private synchronized void updateContext(final SchemaContext snapshot) {
152 Object[] services = listenerTracker.getServices();
153 for (ListenerRegistration<SchemaContextListener> listener : listeners) {
155 listener.getInstance().onGlobalContextUpdated(snapshot);
156 } catch (Exception e) {
157 LOG.error("Exception occured during invoking listener", e);
160 if (services != null) {
161 for (Object rawListener : services) {
162 final SchemaContextListener listener = (SchemaContextListener) rawListener;
164 listener.onGlobalContextUpdated(snapshot);
165 } catch (Exception e) {
166 LOG.error("Exception occured during invoking listener {}", listener, e);
172 private class BundleScanner implements BundleTrackerCustomizer<Iterable<Registration>> {
174 public Iterable<Registration> addingBundle(final Bundle bundle, final BundleEvent event) {
176 if (bundle.getBundleId() == 0) {
177 return Collections.emptyList();
180 final Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
181 if (enumeration == null) {
182 return Collections.emptyList();
185 final List<Registration> urls = new ArrayList<>();
186 while (enumeration.hasMoreElements()) {
187 final URL u = enumeration.nextElement();
189 urls.add(contextResolver.registerSource(u));
190 LOG.debug("Registered {}", u);
191 } catch (Exception e) {
192 LOG.warn("Failed to register {}, ignoring it", e);
196 if (!urls.isEmpty()) {
197 LOG.debug("Loaded {} new URLs from bundle {}, attempting to rebuild schema context",
198 urls.size(), bundle.getSymbolicName());
199 tryToUpdateSchemaContext();
202 return ImmutableList.copyOf(urls);
206 public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Iterable<Registration> object) {
210 * If removing YANG files makes yang store inconsistent, method
211 * {@link #getYangStoreSnapshot()} will throw exception. There is no
216 public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final Iterable<Registration> urls) {
217 for (Registration url : urls) {
220 } catch (Exception e) {
221 LOG.warn("Failed do unregister URL {}, proceeding", url, e);
225 int numUrls = Iterables.size(urls);
227 if(LOG.isDebugEnabled()) {
228 LOG.debug("removedBundle: {}, state: {}, # urls: {}", bundle.getSymbolicName(), bundle.getState(), numUrls);
231 tryToUpdateSchemaContext();
237 public synchronized SchemaContextListener addingService(final ServiceReference<SchemaContextListener> reference) {
239 SchemaContextListener listener = context.getService(reference);
240 SchemaContext _ctxContext = getGlobalContext();
241 if (getContext() != null && _ctxContext != null) {
242 listener.onGlobalContextUpdated(_ctxContext);
247 public synchronized void tryToUpdateSchemaContext() {
248 if (starting || stopping) {
251 Optional<SchemaContext> schema = contextResolver.getSchemaContext();
252 if(schema.isPresent()) {
253 if(LOG.isDebugEnabled()) {
254 LOG.debug("Got new SchemaContext: # of modules {}", schema.get().getAllModuleIdentifiers().size());
257 updateContext(schema.get());
262 public void modifiedService(final ServiceReference<SchemaContextListener> reference, final SchemaContextListener service) {
267 public void removedService(final ServiceReference<SchemaContextListener> reference, final SchemaContextListener service) {
268 context.ungetService(reference);