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;
13 import java.util.ArrayList;
14 import java.util.Collections;
15 import java.util.Enumeration;
16 import java.util.List;
18 import org.opendaylight.controller.sal.core.api.model.SchemaService;
19 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider;
20 import org.opendaylight.yangtools.concepts.ListenerRegistration;
21 import org.opendaylight.yangtools.concepts.Registration;
22 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
23 import org.opendaylight.yangtools.yang.model.api.Module;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
26 import org.opendaylight.yangtools.yang.parser.impl.util.URLSchemaContextResolver;
27 import org.osgi.framework.Bundle;
28 import org.osgi.framework.BundleContext;
29 import org.osgi.framework.BundleEvent;
30 import org.osgi.framework.ServiceReference;
31 import org.osgi.util.tracker.BundleTracker;
32 import org.osgi.util.tracker.BundleTrackerCustomizer;
33 import org.osgi.util.tracker.ServiceTracker;
34 import org.osgi.util.tracker.ServiceTrackerCustomizer;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
38 import com.google.common.base.Optional;
39 import com.google.common.collect.ImmutableList;
41 public class GlobalBundleScanningSchemaServiceImpl implements //
42 SchemaContextProvider, //
44 ServiceTrackerCustomizer<SchemaServiceListener, SchemaServiceListener>, //
46 private static final Logger LOG = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class);
48 private final URLSchemaContextResolver contextResolver = new URLSchemaContextResolver();
49 private final BundleScanner scanner = new BundleScanner();
50 private ServiceTracker<SchemaServiceListener, SchemaServiceListener> listenerTracker;
51 private BundleTracker<Iterable<Registration<URL>>> bundleTracker;
52 private ListenerRegistry<SchemaServiceListener> listeners;
53 private boolean starting = true;
54 private BundleContext context;
56 public ListenerRegistry<SchemaServiceListener> getListeners() {
60 public void setListeners(final ListenerRegistry<SchemaServiceListener> listeners) {
61 this.listeners = listeners;
64 public BundleContext getContext() {
68 public void setContext(final BundleContext context) {
69 this.context = context;
73 checkState(context != null);
74 if (listeners == null) {
75 listeners = new ListenerRegistry<>();
78 listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, GlobalBundleScanningSchemaServiceImpl.this);
79 bundleTracker = new BundleTracker<Iterable<Registration<URL>>>(context, BundleEvent.RESOLVED
80 | BundleEvent.UNRESOLVED, scanner);
82 listenerTracker.open();
84 tryToUpdateSchemaContext();
88 public SchemaContext getSchemaContext() {
89 return getGlobalContext();
93 public SchemaContext getGlobalContext() {
94 return contextResolver.getSchemaContext().orNull();
98 public void addModule(final Module module) {
99 throw new UnsupportedOperationException();
103 public SchemaContext getSessionContext() {
104 throw new UnsupportedOperationException();
108 public void removeModule(final Module module) {
109 throw new UnsupportedOperationException();
113 public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(final SchemaServiceListener listener) {
114 Optional<SchemaContext> potentialCtx = contextResolver.getSchemaContext();
115 if(potentialCtx.isPresent()) {
116 listener.onGlobalContextUpdated(potentialCtx.get());
118 return listeners.register(listener);
122 public void close() throws Exception {
123 if (bundleTracker != null) {
124 bundleTracker.close();
126 if (listenerTracker != null) {
127 listenerTracker.close();
129 // FIXME: Add listeners.close();
133 private void updateContext(final SchemaContext snapshot) {
134 Object[] services = listenerTracker.getServices();
135 for (ListenerRegistration<SchemaServiceListener> listener : listeners) {
137 listener.getInstance().onGlobalContextUpdated(snapshot);
138 } catch (Exception e) {
139 LOG.error("Exception occured during invoking listener", e);
142 if (services != null) {
143 for (Object rawListener : services) {
144 SchemaServiceListener listener = (SchemaServiceListener) rawListener;
146 listener.onGlobalContextUpdated(snapshot);
147 } catch (Exception e) {
148 LOG.error("Exception occured during invoking listener {}", listener, e);
154 private class BundleScanner implements BundleTrackerCustomizer<Iterable<Registration<URL>>> {
156 public Iterable<Registration<URL>> addingBundle(final Bundle bundle, final BundleEvent event) {
158 if (bundle.getBundleId() == 0) {
159 return Collections.emptyList();
162 final Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
163 if (enumeration == null) {
164 return Collections.emptyList();
167 final List<Registration<URL>> urls = new ArrayList<>();
168 while (enumeration.hasMoreElements()) {
169 final URL u = enumeration.nextElement();
171 urls.add(contextResolver.registerSource(u));
172 LOG.debug("Registered {}", u);
173 } catch (Exception e) {
174 LOG.warn("Failed to register {}, ignoring it", e);
178 if (!urls.isEmpty()) {
179 LOG.debug("Loaded {} new URLs, rebuilding schema context", urls.size());
180 tryToUpdateSchemaContext();
183 return ImmutableList.copyOf(urls);
187 public void modifiedBundle(final Bundle bundle, final BundleEvent event, final Iterable<Registration<URL>> object) {
188 LOG.debug("Modified bundle {} {} {}", bundle, event, object);
192 * If removing YANG files makes yang store inconsistent, method
193 * {@link #getYangStoreSnapshot()} will throw exception. There is no
198 public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final Iterable<Registration<URL>> urls) {
199 for (Registration<URL> url : urls) {
202 } catch (Exception e) {
203 LOG.warn("Failed do unregister URL {}, proceeding", url, e);
206 tryToUpdateSchemaContext();
211 public SchemaServiceListener addingService(final ServiceReference<SchemaServiceListener> reference) {
213 SchemaServiceListener listener = context.getService(reference);
214 SchemaContext _ctxContext = getGlobalContext();
215 if (getContext() != null && _ctxContext != null) {
216 listener.onGlobalContextUpdated(_ctxContext);
221 public synchronized void tryToUpdateSchemaContext() {
225 Optional<SchemaContext> schema = contextResolver.tryToUpdateSchemaContext();
226 if(schema.isPresent()) {
227 updateContext(schema.get());
232 public void modifiedService(final ServiceReference<SchemaServiceListener> reference, final SchemaServiceListener service) {
237 public void removedService(final ServiceReference<SchemaServiceListener> reference, final SchemaServiceListener service) {
238 context.ungetService(reference);