Merge "Neutron API v2.0 bindings and APIs for security groups/rules."
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / GlobalBundleScanningSchemaServiceImpl.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.sal.dom.broker;
9
10 import static com.google.common.base.Preconditions.checkState;
11
12 import java.net.URL;
13 import java.util.Enumeration;
14
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;
34
35 import com.google.common.base.Optional;
36 import com.google.common.collect.ImmutableSet;
37 import com.google.common.collect.ImmutableSet.Builder;
38
39 public class GlobalBundleScanningSchemaServiceImpl implements //
40         SchemaContextProvider, //
41         SchemaService, //
42         ServiceTrackerCustomizer<SchemaServiceListener, SchemaServiceListener>, //
43         AutoCloseable {
44     private static final Logger logger = LoggerFactory.getLogger(GlobalBundleScanningSchemaServiceImpl.class);
45
46     private ListenerRegistry<SchemaServiceListener> listeners;
47
48     private BundleContext context;
49     private final BundleScanner scanner = new BundleScanner();
50
51     private BundleTracker<ImmutableSet<Registration<URL>>> bundleTracker;
52
53     private final URLSchemaContextResolver contextResolver = new URLSchemaContextResolver();
54
55     private ServiceTracker<SchemaServiceListener, SchemaServiceListener> listenerTracker;
56
57     private boolean starting = true;
58
59     public ListenerRegistry<SchemaServiceListener> getListeners() {
60         return listeners;
61     }
62
63     public void setListeners(final ListenerRegistry<SchemaServiceListener> listeners) {
64         this.listeners = listeners;
65     }
66
67     public BundleContext getContext() {
68         return context;
69     }
70
71     public void setContext(final BundleContext context) {
72         this.context = context;
73     }
74
75     public void start() {
76         checkState(context != null);
77         if (listeners == null) {
78             listeners = new ListenerRegistry<>();
79         }
80
81         listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, GlobalBundleScanningSchemaServiceImpl.this);
82         bundleTracker = new BundleTracker<ImmutableSet<Registration<URL>>>(context, BundleEvent.RESOLVED
83                         | BundleEvent.UNRESOLVED, scanner);
84         bundleTracker.open();
85         listenerTracker.open();
86         starting = false;
87         tryToUpdateSchemaContext();
88     }
89
90     @Override
91     public SchemaContext getSchemaContext() {
92         return getGlobalContext();
93     }
94
95     @Override
96     public SchemaContext getGlobalContext() {
97         return contextResolver.getSchemaContext().orNull();
98     }
99
100     @Override
101     public void addModule(final Module module) {
102         throw new UnsupportedOperationException();
103     }
104
105     @Override
106     public SchemaContext getSessionContext() {
107         throw new UnsupportedOperationException();
108     }
109
110     @Override
111     public void removeModule(final Module module) {
112         throw new UnsupportedOperationException();
113     }
114
115     @Override
116     public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(final SchemaServiceListener listener) {
117         Optional<SchemaContext> potentialCtx = contextResolver.getSchemaContext();
118         if(potentialCtx.isPresent()) {
119             listener.onGlobalContextUpdated(potentialCtx.get());
120         }
121         return listeners.register(listener);
122     }
123
124     @Override
125     public void close() throws Exception {
126         if (bundleTracker != null) {
127             bundleTracker.close();
128         }
129         if (listenerTracker != null) {
130             listenerTracker.close();
131         }
132         // FIXME: Add listeners.close();
133     }
134
135
136     private void updateContext(final SchemaContext snapshot) {
137         Object[] services = listenerTracker.getServices();
138         for (ListenerRegistration<SchemaServiceListener> listener : listeners) {
139             try {
140                 listener.getInstance().onGlobalContextUpdated(snapshot);
141             } catch (Exception e) {
142                 logger.error("Exception occured during invoking listener", e);
143             }
144         }
145         if (services != null) {
146             for (Object rawListener : services) {
147                 SchemaServiceListener listener = (SchemaServiceListener) rawListener;
148                 try {
149                     listener.onGlobalContextUpdated(snapshot);
150                 } catch (Exception e) {
151                     logger.error("Exception occured during invoking listener", e);
152                 }
153             }
154         }
155     }
156
157     private class BundleScanner implements BundleTrackerCustomizer<ImmutableSet<Registration<URL>>> {
158         @Override
159         public ImmutableSet<Registration<URL>> addingBundle(final Bundle bundle, final BundleEvent event) {
160
161             if (bundle.getBundleId() == 0) {
162                 return ImmutableSet.of();
163             }
164
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());
169                 builder.add(reg);
170             }
171             ImmutableSet<Registration<URL>> urls = builder.build();
172             if(urls.isEmpty()) {
173                 return urls;
174             }
175             tryToUpdateSchemaContext();
176             return urls;
177         }
178
179         @Override
180         public void modifiedBundle(final Bundle bundle, final BundleEvent event, final ImmutableSet<Registration<URL>> object) {
181             logger.debug("Modified bundle {} {} {}", bundle, event, object);
182         }
183
184         /**
185          * If removing YANG files makes yang store inconsistent, method
186          * {@link #getYangStoreSnapshot()} will throw exception. There is no
187          * rollback.
188          */
189
190         @Override
191         public synchronized void removedBundle(final Bundle bundle, final BundleEvent event, final ImmutableSet<Registration<URL>> urls) {
192             for (Registration<URL> url : urls) {
193                 try {
194                     url.close();
195                 } catch (Exception e) {
196                     e.printStackTrace();
197                 }
198             }
199             tryToUpdateSchemaContext();
200         }
201     }
202
203     @Override
204     public SchemaServiceListener addingService(final ServiceReference<SchemaServiceListener> reference) {
205
206         SchemaServiceListener listener = context.getService(reference);
207         SchemaContext _ctxContext = getGlobalContext();
208         if (getContext() != null && _ctxContext != null) {
209             listener.onGlobalContextUpdated(_ctxContext);
210         }
211         return listener;
212     }
213
214     public synchronized void tryToUpdateSchemaContext() {
215         if(starting ) {
216             return;
217         }
218         Optional<SchemaContext> schema = contextResolver.tryToUpdateSchemaContext();
219         if(schema.isPresent()) {
220             updateContext(schema.get());
221         }
222     }
223
224     @Override
225     public void modifiedService(final ServiceReference<SchemaServiceListener> reference, final SchemaServiceListener service) {
226         // NOOP
227     }
228
229     @Override
230     public void removedService(final ServiceReference<SchemaServiceListener> reference, final SchemaServiceListener service) {
231         context.ungetService(reference);
232     }
233 }