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;
16 import java.util.ArrayList;
17 import java.util.Collections;
18 import java.util.Enumeration;
19 import java.util.List;
20 import org.opendaylight.controller.sal.core.api.model.SchemaService;
21 import org.opendaylight.yangtools.concepts.ListenerRegistration;
22 import org.opendaylight.yangtools.concepts.Registration;
23 import org.opendaylight.yangtools.util.ListenerRegistry;
24 import org.opendaylight.yangtools.yang.model.api.Module;
25 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
26 import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
28 import org.opendaylight.yangtools.yang.parser.repo.URLSchemaContextResolver;
29 import org.osgi.framework.Bundle;
30 import org.osgi.framework.BundleContext;
31 import org.osgi.framework.BundleEvent;
32 import org.osgi.framework.ServiceReference;
33 import org.osgi.util.tracker.BundleTracker;
34 import org.osgi.util.tracker.BundleTrackerCustomizer;
35 import org.osgi.util.tracker.ServiceTracker;
36 import org.osgi.util.tracker.ServiceTrackerCustomizer;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
40 public class GlobalBundleScanningSchemaServiceImpl implements SchemaContextProvider, SchemaService, ServiceTrackerCustomizer<SchemaContextListener, SchemaContextListener>, AutoCloseable {
41 private static final Logger LOG = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class);
43 private final ListenerRegistry<SchemaContextListener> listeners = new ListenerRegistry<>();
44 private final URLSchemaContextResolver contextResolver = URLSchemaContextResolver.create("global-bundle");
45 private final BundleScanner scanner = new BundleScanner();
46 private final BundleContext context;
48 private ServiceTracker<SchemaContextListener, SchemaContextListener> listenerTracker;
49 private BundleTracker<Iterable<Registration>> bundleTracker;
50 private boolean starting = true;
51 private static GlobalBundleScanningSchemaServiceImpl instance;
53 private GlobalBundleScanningSchemaServiceImpl(final BundleContext context) {
54 this.context = Preconditions.checkNotNull(context);
57 public synchronized static GlobalBundleScanningSchemaServiceImpl createInstance(final BundleContext ctx) {
58 Preconditions.checkState(instance == null);
59 instance = new GlobalBundleScanningSchemaServiceImpl(ctx);
64 public synchronized static GlobalBundleScanningSchemaServiceImpl getInstance() {
65 Preconditions.checkState(instance != null, "Global Instance was not instantiated");
70 public static synchronized void destroyInstance() {
78 public BundleContext getContext() {
83 checkState(context != null);
84 LOG.debug("start() starting");
86 listenerTracker = new ServiceTracker<>(context, SchemaContextListener.class, GlobalBundleScanningSchemaServiceImpl.this);
87 bundleTracker = new BundleTracker<>(context, Bundle.RESOLVED | Bundle.STARTING |
88 Bundle.STOPPING | Bundle.ACTIVE, scanner);
91 LOG.debug("BundleTracker.open() complete");
93 listenerTracker.open();
95 tryToUpdateSchemaContext();
97 LOG.debug("start() complete");
101 public SchemaContext getSchemaContext() {
102 return getGlobalContext();
106 public SchemaContext getGlobalContext() {
107 return contextResolver.getSchemaContext().orNull();
111 public void addModule(final Module module) {
112 throw new UnsupportedOperationException();
116 public SchemaContext getSessionContext() {
117 throw new UnsupportedOperationException();
121 public void removeModule(final Module module) {
122 throw new UnsupportedOperationException();
126 public synchronized ListenerRegistration<SchemaContextListener> registerSchemaContextListener(final SchemaContextListener listener) {
127 Optional<SchemaContext> potentialCtx = contextResolver.getSchemaContext();
128 if(potentialCtx.isPresent()) {
129 listener.onGlobalContextUpdated(potentialCtx.get());
131 return listeners.register(listener);
135 public void close() {
136 if (bundleTracker != null) {
137 bundleTracker.close();
139 if (listenerTracker != null) {
140 listenerTracker.close();
143 for (ListenerRegistration<SchemaContextListener> l : listeners.getListeners()) {
148 private synchronized void updateContext(final SchemaContext snapshot) {
149 Object[] services = listenerTracker.getServices();
150 for (ListenerRegistration<SchemaContextListener> listener : listeners) {
152 listener.getInstance().onGlobalContextUpdated(snapshot);
153 } catch (Exception e) {
154 LOG.error("Exception occured during invoking listener", e);
157 if (services != null) {
158 for (Object rawListener : services) {
159 final SchemaContextListener listener = (SchemaContextListener) rawListener;
161 listener.onGlobalContextUpdated(snapshot);
162 } catch (Exception e) {
163 LOG.error("Exception occured during invoking listener {}", listener, e);
169 private class BundleScanner implements BundleTrackerCustomizer<Iterable<Registration>> {
171 public Iterable<Registration> addingBundle(final Bundle bundle, final BundleEvent event) {
173 if (bundle.getBundleId() == 0) {
174 return Collections.emptyList();
177 final Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
178 if (enumeration == null) {
179 return Collections.emptyList();
182 final List<Registration> urls = new ArrayList<>();
183 while (enumeration.hasMoreElements()) {
184 final URL u = enumeration.nextElement();
186 urls.add(contextResolver.registerSource(u));
187 LOG.debug("Registered {}", u);
188 } catch (Exception e) {
189 LOG.warn("Failed to register {}, ignoring it", e);
193 if (!urls.isEmpty()) {
194 LOG.debug("Loaded {} new URLs from bundle {}, attempting to rebuild schema context",
195 urls.size(), bundle.getSymbolicName());
196 tryToUpdateSchemaContext();
199 return ImmutableList.copyOf(urls);
203 public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Iterable<Registration> object) {
207 * If removing YANG files makes yang store inconsistent, method
208 * {@link #getYangStoreSnapshot()} will throw exception. There is no
213 public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final Iterable<Registration> urls) {
214 for (Registration url : urls) {
217 } catch (Exception e) {
218 LOG.warn("Failed do unregister URL {}, proceeding", url, e);
221 tryToUpdateSchemaContext();
226 public synchronized SchemaContextListener addingService(final ServiceReference<SchemaContextListener> reference) {
228 SchemaContextListener listener = context.getService(reference);
229 SchemaContext _ctxContext = getGlobalContext();
230 if (getContext() != null && _ctxContext != null) {
231 listener.onGlobalContextUpdated(_ctxContext);
236 public synchronized void tryToUpdateSchemaContext() {
240 Optional<SchemaContext> schema = contextResolver.getSchemaContext();
241 if(schema.isPresent()) {
242 if(LOG.isDebugEnabled()) {
243 LOG.debug("Got new SchemaContext: # of modules {}", schema.get().getAllModuleIdentifiers().size());
246 updateContext(schema.get());
251 public void modifiedService(final ServiceReference<SchemaContextListener> reference, final SchemaContextListener service) {
256 public void removedService(final ServiceReference<SchemaContextListener> reference, final SchemaContextListener service) {
257 context.ungetService(reference);