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 final 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(final ListenerRegistry<SchemaServiceListener> listeners) {
64 this.listeners = listeners;
67 public BundleContext getContext() {
71 public void setContext(final 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();
96 public SchemaContext getGlobalContext() {
97 return contextResolver.getSchemaContext().orNull();
101 public void addModule(final Module module) {
102 throw new UnsupportedOperationException();
106 public SchemaContext getSessionContext() {
107 throw new UnsupportedOperationException();
111 public void removeModule(final Module module) {
112 throw new UnsupportedOperationException();
116 public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(final SchemaServiceListener listener) {
117 Optional<SchemaContext> potentialCtx = contextResolver.getSchemaContext();
118 if(potentialCtx.isPresent()) {
119 listener.onGlobalContextUpdated(potentialCtx.get());
121 return listeners.register(listener);
125 public void close() throws Exception {
126 if (bundleTracker != null) {
127 bundleTracker.close();
129 if (listenerTracker != null) {
130 listenerTracker.close();
132 // FIXME: Add listeners.close();
136 private void updateContext(final SchemaContext snapshot) {
137 Object[] services = listenerTracker.getServices();
138 for (ListenerRegistration<SchemaServiceListener> listener : listeners) {
140 listener.getInstance().onGlobalContextUpdated(snapshot);
141 } catch (Exception e) {
142 logger.error("Exception occured during invoking listener", e);
145 if (services != null) {
146 for (Object rawListener : services) {
147 SchemaServiceListener listener = (SchemaServiceListener) rawListener;
149 listener.onGlobalContextUpdated(snapshot);
150 } catch (Exception e) {
151 logger.error("Exception occured during invoking listener", e);
157 private class BundleScanner implements BundleTrackerCustomizer<ImmutableSet<Registration<URL>>> {
159 public ImmutableSet<Registration<URL>> addingBundle(final Bundle bundle, final BundleEvent event) {
161 if (bundle.getBundleId() == 0) {
162 return ImmutableSet.of();
165 Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
166 Builder<Registration<URL>> builder = ImmutableSet.<Registration<URL>> builder();
167 while (enumeration != null && enumeration.hasMoreElements()) {
168 Registration<URL> reg = contextResolver.registerSource(enumeration.nextElement());
171 ImmutableSet<Registration<URL>> urls = builder.build();
175 tryToUpdateSchemaContext();
180 public void modifiedBundle(final Bundle bundle, final BundleEvent event, final ImmutableSet<Registration<URL>> object) {
181 logger.debug("Modified bundle {} {} {}", bundle, event, object);
185 * If removing YANG files makes yang store inconsistent, method
186 * {@link #getYangStoreSnapshot()} will throw exception. There is no
191 public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final ImmutableSet<Registration<URL>> urls) {
192 for (Registration<URL> url : urls) {
195 } catch (Exception e) {
199 tryToUpdateSchemaContext();
204 public SchemaServiceListener addingService(final ServiceReference<SchemaServiceListener> reference) {
206 SchemaServiceListener listener = context.getService(reference);
207 SchemaContext _ctxContext = getGlobalContext();
208 if (getContext() != null && _ctxContext != null) {
209 listener.onGlobalContextUpdated(_ctxContext);
214 public synchronized void tryToUpdateSchemaContext() {
218 Optional<SchemaContext> schema = contextResolver.tryToUpdateSchemaContext();
219 if(schema.isPresent()) {
220 updateContext(schema.get());
225 public void modifiedService(final ServiceReference<SchemaServiceListener> reference, final SchemaServiceListener service) {
230 public void removedService(final ServiceReference<SchemaServiceListener> reference, final SchemaServiceListener service) {
231 context.ungetService(reference);