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.Enumeration;
15 import org.opendaylight.controller.sal.core.api.model.SchemaService;
16 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider;
17 import org.opendaylight.yangtools.concepts.ListenerRegistration;
18 import org.opendaylight.yangtools.concepts.Registration;
19 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
20 import org.opendaylight.yangtools.yang.model.api.Module;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaServiceListener;
23 import org.opendaylight.yangtools.yang.parser.impl.util.URLSchemaContextResolver;
24 import org.osgi.framework.Bundle;
25 import org.osgi.framework.BundleContext;
26 import org.osgi.framework.BundleEvent;
27 import org.osgi.framework.ServiceReference;
28 import org.osgi.util.tracker.BundleTracker;
29 import org.osgi.util.tracker.BundleTrackerCustomizer;
30 import org.osgi.util.tracker.ServiceTracker;
31 import org.osgi.util.tracker.ServiceTrackerCustomizer;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
35 import com.google.common.base.Optional;
36 import com.google.common.collect.ImmutableSet;
37 import com.google.common.collect.ImmutableSet.Builder;
39 public class GlobalBundleScanningSchemaServiceImpl implements //
40 SchemaContextProvider, //
42 ServiceTrackerCustomizer<SchemaServiceListener, SchemaServiceListener>, //
44 private static final Logger logger = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class);
46 private ListenerRegistry<SchemaServiceListener> listeners;
48 private BundleContext context;
49 private BundleScanner scanner = new BundleScanner();
51 private BundleTracker<ImmutableSet<Registration<URL>>> bundleTracker;
53 private final URLSchemaContextResolver contextResolver = new URLSchemaContextResolver();
55 private ServiceTracker<SchemaServiceListener, SchemaServiceListener> listenerTracker;
57 private boolean starting = true;
59 public ListenerRegistry<SchemaServiceListener> getListeners() {
63 public void setListeners(ListenerRegistry<SchemaServiceListener> listeners) {
64 this.listeners = listeners;
67 public BundleContext getContext() {
71 public void setContext(BundleContext context) {
72 this.context = context;
76 checkState(context != null);
77 if (listeners == null) {
78 listeners = new ListenerRegistry<>();
81 listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, GlobalBundleScanningSchemaServiceImpl.this);
82 bundleTracker = new BundleTracker<ImmutableSet<Registration<URL>>>(context, BundleEvent.RESOLVED
83 | BundleEvent.UNRESOLVED, scanner);
85 listenerTracker.open();
87 tryToUpdateSchemaContext();
91 public SchemaContext getSchemaContext() {
92 return getGlobalContext();
95 public SchemaContext getGlobalContext() {
96 return contextResolver.getSchemaContext().orNull();
100 public void addModule(Module module) {
101 throw new UnsupportedOperationException();
105 public SchemaContext getSessionContext() {
106 throw new UnsupportedOperationException();
110 public void removeModule(Module module) {
111 throw new UnsupportedOperationException();
115 public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(SchemaServiceListener listener) {
116 return listeners.register(listener);
120 public void close() throws Exception {
121 if (bundleTracker != null) {
122 bundleTracker.close();
124 if (listenerTracker != null) {
125 listenerTracker.close();
127 // FIXME: Add listeners.close();
131 private void updateContext(SchemaContext snapshot) {
132 Object[] services = listenerTracker.getServices();
133 if (services != null) {
134 for (Object rawListener : services) {
135 SchemaServiceListener listener = (SchemaServiceListener) rawListener;
137 listener.onGlobalContextUpdated(snapshot);
138 } catch (Exception e) {
139 logger.error("Exception occured during invoking listener", e);
143 for (ListenerRegistration<SchemaServiceListener> listener : listeners) {
145 listener.getInstance().onGlobalContextUpdated(snapshot);
146 } catch (Exception e) {
147 logger.error("Exception occured during invoking listener", e);
152 private class BundleScanner implements BundleTrackerCustomizer<ImmutableSet<Registration<URL>>> {
154 public ImmutableSet<Registration<URL>> addingBundle(Bundle bundle, BundleEvent event) {
156 if (bundle.getBundleId() == 0) {
157 return ImmutableSet.of();
160 Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
161 Builder<Registration<URL>> builder = ImmutableSet.<Registration<URL>> builder();
162 while (enumeration != null && enumeration.hasMoreElements()) {
163 Registration<URL> reg = contextResolver.registerSource(enumeration.nextElement());
166 ImmutableSet<Registration<URL>> urls = builder.build();
170 tryToUpdateSchemaContext();
175 public void modifiedBundle(Bundle bundle, BundleEvent event, ImmutableSet<Registration<URL>> object) {
176 logger.debug("Modified bundle {} {} {}", bundle, event, object);
180 * If removing YANG files makes yang store inconsistent, method
181 * {@link #getYangStoreSnapshot()} will throw exception. There is no
186 public synchronized void removedBundle(Bundle bundle, BundleEvent event, ImmutableSet<Registration<URL>> urls) {
187 for (Registration<URL> url : urls) {
190 } catch (Exception e) {
194 tryToUpdateSchemaContext();
199 public SchemaServiceListener addingService(ServiceReference<SchemaServiceListener> reference) {
201 SchemaServiceListener listener = context.getService(reference);
202 SchemaContext _ctxContext = getGlobalContext();
203 if (getContext() != null && _ctxContext != null) {
204 listener.onGlobalContextUpdated(_ctxContext);
209 public synchronized void tryToUpdateSchemaContext() {
213 Optional<SchemaContext> schema = contextResolver.tryToUpdateSchemaContext();
214 if(schema.isPresent()) {
215 updateContext(schema.get());
220 public void modifiedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
225 public void removedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
226 context.ungetService(reference);