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