8afa1eeb5f0e475cfe65fb43936967a6f35bb0f3
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / sal / dom / broker / SchemaServiceImpl.java
1 package org.opendaylight.controller.sal.dom.broker;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.net.URL;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Enumeration;
9 import java.util.List;
10 import java.util.Set;
11 import java.util.zip.Checksum;
12
13 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
14 import org.osgi.util.tracker.BundleTracker;
15 import org.osgi.util.tracker.BundleTrackerCustomizer;
16 import org.osgi.util.tracker.ServiceTracker;
17 import org.osgi.util.tracker.ServiceTrackerCustomizer;
18 import org.opendaylight.yangtools.yang.model.api.Module;
19 import org.opendaylight.yangtools.yang.model.parser.api.YangModelParser;
20 import org.opendaylight.yangtools.yang.parser.impl.YangParserImpl;
21 import org.osgi.framework.Bundle;
22 import org.osgi.framework.BundleActivator;
23 import org.osgi.framework.BundleContext;
24 import org.osgi.framework.BundleEvent;
25 import org.osgi.framework.ServiceReference;
26 import org.opendaylight.yangtools.concepts.ListenerRegistration;
27 import org.opendaylight.yangtools.concepts.util.ListenerRegistry;
28 import org.opendaylight.controller.sal.core.api.model.SchemaService;
29 import org.opendaylight.controller.sal.core.api.model.SchemaServiceListener;
30 import org.opendaylight.controller.sal.dom.broker.impl.SchemaContextProvider;
31 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import com.google.common.base.Function;
35 import com.google.common.base.Optional;
36 import com.google.common.base.Preconditions;
37 import com.google.common.collect.Collections2;
38 import com.google.common.collect.HashMultimap;
39 import com.google.common.collect.Multimap;
40 import com.google.common.collect.Sets;
41
42 import static com.google.common.base.Preconditions.*;
43
44 public class SchemaServiceImpl implements //
45         SchemaContextProvider, //
46         SchemaService, //
47         ServiceTrackerCustomizer<SchemaServiceListener, SchemaServiceListener>, //
48         AutoCloseable {
49     private static final Logger logger = LoggerFactory.getLogger(SchemaServiceImpl.class);
50
51     private ListenerRegistry<SchemaServiceListener> listeners;
52     private YangModelParser parser;
53
54     private BundleContext context;
55     private BundleScanner scanner = new BundleScanner();
56
57     /**
58      * Map of currently problematic yang files that should get fixed eventually
59      * after all events are received.
60      */
61     private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
62     private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
63     private BundleTracker<Object> bundleTracker;
64     private final YangStoreCache cache = new YangStoreCache();
65
66     private ServiceTracker<SchemaServiceListener, SchemaServiceListener> listenerTracker;
67
68     public ListenerRegistry<SchemaServiceListener> getListeners() {
69         return listeners;
70     }
71
72     public void setListeners(ListenerRegistry<SchemaServiceListener> listeners) {
73         this.listeners = listeners;
74     }
75
76     public YangModelParser getParser() {
77         return parser;
78     }
79
80     public void setParser(YangModelParser parser) {
81         this.parser = parser;
82     }
83
84     public BundleContext getContext() {
85         return context;
86     }
87
88     public void setContext(BundleContext context) {
89         this.context = context;
90     }
91
92     public void start() {
93         checkState(parser != null);
94         checkState(context != null);
95         if (listeners == null) {
96             listeners = new ListenerRegistry<>();
97         }
98
99         listenerTracker = new ServiceTracker<>(context, SchemaServiceListener.class, this);
100         bundleTracker = new BundleTracker<Object>(context, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, scanner);
101         bundleTracker.open();
102         listenerTracker.open();
103     }
104
105     
106     @Override
107     public SchemaContext getSchemaContext() {
108         return getGlobalContext();
109     }
110     
111     public SchemaContext getGlobalContext() {
112         return getSchemaContextSnapshot();
113     }
114
115     public synchronized SchemaContext getSchemaContextSnapshot() {
116         Optional<SchemaContext> yangStoreOpt = cache.getCachedSchemaContext(consistentBundlesToYangURLs);
117         if (yangStoreOpt.isPresent()) {
118             return yangStoreOpt.get();
119         }
120         SchemaContext snapshot = createSnapshot(parser, consistentBundlesToYangURLs);
121         updateCache(snapshot);
122         return snapshot;
123     }
124
125     @Override
126     public void addModule(Module module) {
127         // TODO Auto-generated method stub
128         throw new UnsupportedOperationException();
129     }
130
131     @Override
132     public SchemaContext getSessionContext() {
133         // TODO Auto-generated method stub
134         throw new UnsupportedOperationException();
135     }
136
137     @Override
138     public void removeModule(Module module) {
139         // TODO Auto-generated method stub
140         throw new UnsupportedOperationException();
141     }
142
143     @Override
144     public ListenerRegistration<SchemaServiceListener> registerSchemaServiceListener(SchemaServiceListener listener) {
145         return listeners.register(listener);
146     }
147
148     @Override
149     public void close() throws Exception {
150         bundleTracker.close();
151         // FIXME: Add listeners.close();
152
153     }
154
155     private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState,
156             boolean adding) {
157         Preconditions.checkArgument(!changedURLs.isEmpty(), "No change can occur when no URLs are changed");
158
159         try {
160             // consistent state
161             // merge into
162             SchemaContext snapshot = createSnapshot(parser, proposedNewState);
163             consistentBundlesToYangURLs.clear();
164             consistentBundlesToYangURLs.putAll(proposedNewState);
165             inconsistentBundlesToYangURLs.clear();
166             // update cache
167             updateCache(snapshot);
168             logger.info("SchemaService updated to new consistent state");
169             logger.trace("SchemaService  updated to new consistent state containing {}", consistentBundlesToYangURLs);
170
171             // notifyListeners(changedURLs, adding);
172             return true;
173         } catch (Exception e) {
174             // inconsistent state
175             logger.debug(
176                     "SchemaService is falling back on last consistent state containing {}, inconsistent yang files {}",
177                     consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e);
178             return false;
179         }
180     }
181
182     private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
183         return Collections2.transform(multimap.values(), new Function<URL, InputStream>() {
184
185             @Override
186             public InputStream apply(URL url) {
187                 try {
188                     return url.openStream();
189                 } catch (IOException e) {
190                     logger.warn("Unable to open stream from {}", url);
191                     throw new IllegalStateException("Unable to open stream from " + url, e);
192                 }
193             }
194         });
195     }
196
197     private static SchemaContext createSnapshot(YangModelParser parser, Multimap<Bundle, URL> multimap) {
198         List<InputStream> models = new ArrayList<>(fromUrlsToInputStreams(multimap));
199         Set<Module> modules = parser.parseYangModelsFromStreams(models);
200         SchemaContext yangStoreSnapshot = parser.resolveSchemaContext(modules);
201         return yangStoreSnapshot;
202     }
203
204     private void updateCache(SchemaContext snapshot) {
205         cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
206
207         Object[] services = listenerTracker.getServices();
208         if (services != null) {
209             for (Object rawListener : services) {
210                 SchemaServiceListener listener = (SchemaServiceListener) rawListener;
211                 try {
212                     listener.onGlobalContextUpdated(snapshot);
213                 } catch (Exception e) {
214                     logger.error("Exception occured during invoking listener", e);
215                 }
216             }
217         }
218         for (ListenerRegistration<SchemaServiceListener> listener : listeners) {
219             try {
220                 listener.getInstance().onGlobalContextUpdated(snapshot);
221             } catch (Exception e) {
222                 logger.error("Exception occured during invoking listener", e);
223             }
224         }
225     }
226
227     private class BundleScanner implements BundleTrackerCustomizer<Object> {
228         @Override
229         public Object addingBundle(Bundle bundle, BundleEvent event) {
230
231             // Ignore system bundle:
232             // system bundle might have config-api on classpath &&
233             // config-api contains yang files =>
234             // system bundle might contain yang files from that bundle
235             if (bundle.getBundleId() == 0) {
236                 return bundle;
237             }
238
239             Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
240             if (enumeration != null && enumeration.hasMoreElements()) {
241                 synchronized (this) {
242                     List<URL> addedURLs = new ArrayList<>();
243                     while (enumeration.hasMoreElements()) {
244                         URL url = enumeration.nextElement();
245                         addedURLs.add(url);
246                     }
247                     logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(),
248                             addedURLs);
249                     // test that yang store is consistent
250                     Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
251                     proposedNewState.putAll(inconsistentBundlesToYangURLs);
252                     proposedNewState.putAll(bundle, addedURLs);
253                     boolean adding = true;
254
255                     if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
256                         inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
257                     }
258                 }
259             }
260             return bundle;
261         }
262
263         @Override
264         public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
265             logger.debug("Modified bundle {} {} {}", bundle, event, object);
266         }
267
268         /**
269          * If removing YANG files makes yang store inconsistent, method
270          * {@link #getYangStoreSnapshot()} will throw exception. There is no
271          * rollback.
272          */
273
274         @Override
275         public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
276             inconsistentBundlesToYangURLs.removeAll(bundle);
277             Collection<URL> consistentURLsToBeRemoved = consistentBundlesToYangURLs.removeAll(bundle);
278
279             if (consistentURLsToBeRemoved.isEmpty()) {
280                 return; // no change
281             }
282             boolean adding = false;
283             // notifyListeners(consistentURLsToBeRemoved, adding);
284         }
285     }
286
287     private static final class YangStoreCache {
288
289         Set<URL> cachedUrls;
290         SchemaContext cachedContextSnapshot;
291
292         Optional<SchemaContext> getCachedSchemaContext(Multimap<Bundle, URL> bundlesToYangURLs) {
293             Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
294             if (cachedUrls != null && cachedUrls.equals(urls)) {
295                 Preconditions.checkState(cachedContextSnapshot != null);
296                 return Optional.of(cachedContextSnapshot);
297             }
298             return Optional.absent();
299         }
300
301         private static Set<URL> setFromMultimapValues(Multimap<Bundle, URL> bundlesToYangURLs) {
302             Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
303             Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
304             return urls;
305         }
306
307         void cacheYangStore(Multimap<Bundle, URL> urls, SchemaContext ctx) {
308             this.cachedUrls = setFromMultimapValues(urls);
309             this.cachedContextSnapshot = ctx;
310         }
311     }
312
313     @Override
314     public SchemaServiceListener addingService(ServiceReference<SchemaServiceListener> reference) {
315
316         SchemaServiceListener listener = context.getService(reference);
317         SchemaContext _ctxContext = getGlobalContext();
318         if (getContext() != null) {
319             listener.onGlobalContextUpdated(_ctxContext);
320         }
321         return listener;
322     }
323
324     @Override
325     public void modifiedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
326         // NOOP
327     }
328
329     @Override
330     public void removedService(ServiceReference<SchemaServiceListener> reference, SchemaServiceListener service) {
331         context.ungetService(reference);
332     }
333 }