90b94ef120c8d2e05473e76db4db7e7dd325c46a
[controller.git] / opendaylight / config / yang-store-impl / src / main / java / org / opendaylight / controller / config / yang / store / impl / ExtenderYangTracker.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.config.yang.store.impl;
9
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Function;
12 import com.google.common.base.Optional;
13 import com.google.common.base.Preconditions;
14 import com.google.common.collect.Collections2;
15 import com.google.common.collect.HashMultimap;
16 import com.google.common.collect.Multimap;
17 import com.google.common.collect.Sets;
18 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
19 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
20 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
21 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
22 import org.opendaylight.yangtools.yang.model.api.Module;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleContext;
25 import org.osgi.framework.BundleEvent;
26 import org.osgi.util.tracker.BundleTracker;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
30 import javax.annotation.concurrent.GuardedBy;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.net.URL;
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.Collections;
37 import java.util.Enumeration;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Set;
41 import java.util.regex.Matcher;
42 import java.util.regex.Pattern;
43
44 /**
45  * Note on consistency:
46  * When this bundle is activated after other bundles containing yang files, the resolving order
47  * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
48  * container should eventually send all events and thus making the inconsistent map redundant.
49  */
50 public class ExtenderYangTracker extends BundleTracker<Object> implements YangStoreService, AutoCloseable {
51
52     private static final Logger logger = LoggerFactory.getLogger(ExtenderYangTracker.class);
53
54     private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
55
56     /*
57     Map of currently problematic yang files that should get fixed eventually after all events are received.
58      */
59     private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
60
61     private final YangStoreCache cache = new YangStoreCache();
62     private final MbeParser mbeParser;
63
64
65     public ExtenderYangTracker(Optional<Pattern> maybeBlacklist, BundleContext bundleContext) {
66         this(new MbeParser(), maybeBlacklist, bundleContext);
67     }
68
69     @GuardedBy("this")
70     private Optional<Pattern> maybeBlacklist;
71
72     @VisibleForTesting
73     ExtenderYangTracker(MbeParser mbeParser, Optional<Pattern> maybeBlacklist, BundleContext bundleContext) {
74         super(bundleContext, BundleEvent.RESOLVED | BundleEvent.UNRESOLVED, null);
75         this.mbeParser = mbeParser;
76         this.maybeBlacklist = maybeBlacklist;
77         open();
78     }
79
80     @Override
81     public Object addingBundle(Bundle bundle, BundleEvent event) {
82
83         // Ignore system bundle:
84         // system bundle might have config-api on classpath &&
85         // config-api contains yang files =>
86         // system bundle might contain yang files from that bundle
87         if (bundle.getBundleId() == 0)
88             return bundle;
89
90         if (maybeBlacklist.isPresent()) {
91             Matcher m = maybeBlacklist.get().matcher(bundle.getSymbolicName());
92             if (m.matches()) {
93                 logger.debug("Ignoring {} because it is in blacklist {}", bundle, maybeBlacklist);
94                 return bundle;
95             }
96         }
97
98         Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
99         if (enumeration != null && enumeration.hasMoreElements()) {
100             synchronized (this) {
101                 List<URL> addedURLs = new ArrayList<>();
102                 while (enumeration.hasMoreElements()) {
103                     URL url = enumeration.nextElement();
104                     addedURLs.add(url);
105                 }
106                 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
107                 // test that yang store is consistent
108                 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
109                 proposedNewState.putAll(inconsistentBundlesToYangURLs);
110                 proposedNewState.putAll(bundle, addedURLs);
111
112                 Preconditions.checkArgument(addedURLs.size() > 0, "No change can occur when no URLs are changed");
113
114                 try(YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, proposedNewState)) {
115                     onSnapshotSuccess(proposedNewState, snapshot);
116                 } catch(YangStoreException e) {
117                     onSnapshotFailure(bundle, addedURLs, e);
118                 }
119             }
120         }
121         return bundle;
122     }
123
124     private void onSnapshotFailure(Bundle bundle, List<URL> addedURLs, Exception failureReason) {
125         // inconsistent state
126         inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
127
128         logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
129                 consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, failureReason);
130         logger.warn("Yang store is falling back on last consistent state containing {} files, inconsistent yang files size is {}, reason {}",
131                 consistentBundlesToYangURLs.size(), inconsistentBundlesToYangURLs.size(), failureReason.toString());
132     }
133
134     private void onSnapshotSuccess(Multimap<Bundle, URL> proposedNewState, YangStoreSnapshotImpl snapshot) {
135         // consistent state
136         // merge into
137         consistentBundlesToYangURLs.clear();
138         consistentBundlesToYangURLs.putAll(proposedNewState);
139         inconsistentBundlesToYangURLs.clear();
140
141         updateCache(snapshot);
142
143         logger.info("Yang store updated to new consistent state containing {} yang files", consistentBundlesToYangURLs.size());
144         logger.debug("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
145     }
146
147     private void updateCache(YangStoreSnapshotImpl snapshot) {
148         cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
149     }
150
151     @Override
152     public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
153         logger.debug("Modified bundle {} {} {}", bundle, event, object);
154     }
155
156     /**
157      * If removing YANG files makes yang store inconsistent, method {@link #getYangStoreSnapshot()}
158      * will throw exception. There is no rollback.
159      */
160     @Override
161     public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
162         logger.debug("Removed bundle {} {} {}", bundle, event, object);
163         inconsistentBundlesToYangURLs.removeAll(bundle);
164         consistentBundlesToYangURLs.removeAll(bundle);
165     }
166
167     @Override
168     public synchronized YangStoreSnapshot getYangStoreSnapshot()
169             throws YangStoreException {
170         Optional<YangStoreSnapshot> yangStoreOpt = cache.getSnapshotIfPossible(consistentBundlesToYangURLs);
171         if (yangStoreOpt.isPresent()) {
172             logger.debug("Returning cached yang store {}", yangStoreOpt.get());
173             return yangStoreOpt.get();
174         }
175
176         YangStoreSnapshotImpl snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
177         updateCache(snapshot);
178         return snapshot;
179     }
180
181     private static YangStoreSnapshotImpl createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
182         try {
183             YangStoreSnapshotImpl yangStoreSnapshot = mbeParser.parseYangFiles(fromUrlsToInputStreams(multimap));
184             logger.trace("{} module entries parsed successfully from {} yang files",
185                     yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size());
186             return yangStoreSnapshot;
187         } catch (RuntimeException e) {
188             throw new YangStoreException("Unable to parse yang files from following URLs: " + multimap, e);
189         }
190     }
191
192     private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
193         return Collections2.transform(multimap.values(),
194                 new Function<URL, InputStream>() {
195
196                     @Override
197                     public InputStream apply(URL url) {
198                         try {
199                             return url.openStream();
200                         } catch (IOException e) {
201                             logger.warn("Unable to open stream from {}", url);
202                             throw new IllegalStateException(
203                                     "Unable to open stream from " + url, e);
204                         }
205                     }
206                 });
207     }
208
209     public synchronized void setMaybeBlacklist(Optional<Pattern> maybeBlacklistPattern) {
210         maybeBlacklist = maybeBlacklistPattern;
211         cache.invalidate();
212     }
213 }
214
215 class YangStoreCache {
216
217     @GuardedBy("this")
218     private Set<URL> cachedUrls = null;
219     @GuardedBy("this")
220     private Optional<YangStoreSnapshot> cachedYangStoreSnapshot = getInitialSnapshot();
221
222     synchronized Optional<YangStoreSnapshot> getSnapshotIfPossible(Multimap<Bundle, URL> bundlesToYangURLs) {
223         Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
224
225         if (cachedUrls==null || cachedUrls.equals(urls)) {
226             Preconditions.checkState(cachedYangStoreSnapshot.isPresent());
227             YangStoreSnapshot freshSnapshot = new YangStoreSnapshotImpl(cachedYangStoreSnapshot.get());
228             return Optional.of(freshSnapshot);
229         }
230
231         return Optional.absent();
232     }
233
234     private static Set<URL> setFromMultimapValues(
235             Multimap<Bundle, URL> bundlesToYangURLs) {
236         Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
237         Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
238         return urls;
239     }
240
241     synchronized void cacheYangStore(Multimap<Bundle, URL> urls,
242                         YangStoreSnapshot yangStoreSnapshot) {
243         this.cachedUrls = setFromMultimapValues(urls);
244         this.cachedYangStoreSnapshot = Optional.of(yangStoreSnapshot);
245     }
246
247     synchronized void invalidate() {
248         cachedUrls.clear();
249         if (cachedYangStoreSnapshot.isPresent()){
250             cachedYangStoreSnapshot.get().close();
251             cachedYangStoreSnapshot = Optional.absent();
252         }
253     }
254
255     private Optional<YangStoreSnapshot> getInitialSnapshot() {
256         YangStoreSnapshot initialSnapshot = new YangStoreSnapshot() {
257             @Override
258             public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
259                 return Collections.emptyMap();
260             }
261
262             @Override
263             public Map<String, Map.Entry<Module, String>> getModuleMap() {
264                 return Collections.emptyMap();
265             }
266
267             @Override
268             public int countModuleMXBeanEntries() {
269                 return 0;
270             }
271
272             @Override
273             public void close() {
274             }
275         };
276         return Optional.of(initialSnapshot);
277     }
278 }