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