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