Fix star import and enable checkstyle rule to prevent it.
[controller.git] / opendaylight / config / yang-store-impl / src / main / java / org / opendaylight / controller / config / yang / store / impl / ExtenderYangTrackerCustomizer.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.YangStoreListenerRegistration;
20 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
21 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
22 import org.opendaylight.controller.config.yang.store.spi.YangStoreListener;
23 import org.osgi.framework.Bundle;
24 import org.osgi.framework.BundleEvent;
25 import org.osgi.util.tracker.BundleTrackerCustomizer;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28
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.Enumeration;
35 import java.util.List;
36 import java.util.Set;
37
38 /**
39  * Note on consistency:
40  * When this bundle is activated after other bundles containing yang files, the resolving order
41  * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
42  * container should eventually send all events and thus making the inconsistent map redundant.
43  */
44 public class ExtenderYangTrackerCustomizer implements BundleTrackerCustomizer<Object>, YangStoreService {
45
46     private static final Logger logger = LoggerFactory
47             .getLogger(ExtenderYangTrackerCustomizer.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     private final List<YangStoreListener> listeners = new ArrayList<>();
59
60     public ExtenderYangTrackerCustomizer() {
61         this(new MbeParser());
62
63     }
64
65     @VisibleForTesting
66     ExtenderYangTrackerCustomizer(MbeParser mbeParser) {
67         this.mbeParser = mbeParser;
68     }
69
70     @Override
71     public Object addingBundle(Bundle bundle, BundleEvent event) {
72
73         // Ignore system bundle:
74         // system bundle might have config-api on classpath &&
75         // config-api contains yang files =>
76         // system bundle might contain yang files from that bundle
77         if (bundle.getBundleId() == 0)
78             return bundle;
79
80         Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
81         if (enumeration != null && enumeration.hasMoreElements()) {
82             synchronized (this) {
83                 List<URL> addedURLs = new ArrayList<>();
84                 while (enumeration.hasMoreElements()) {
85                     URL url = enumeration.nextElement();
86                     addedURLs.add(url);
87                 }
88                 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
89                 // test that yang store is consistent
90                 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
91                 proposedNewState.putAll(inconsistentBundlesToYangURLs);
92                 proposedNewState.putAll(bundle, addedURLs);
93                 boolean adding = true;
94                 if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
95                     inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
96                 }
97             }
98         }
99         return bundle;
100     }
101
102     private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState, boolean adding) {
103         Preconditions.checkArgument(changedURLs.size() > 0, "No change can occur when no URLs are changed");
104         try(YangStoreSnapshot snapshot = createSnapshot(mbeParser, proposedNewState)) {
105             // consistent state
106             // merge into
107             consistentBundlesToYangURLs.clear();
108             consistentBundlesToYangURLs.putAll(proposedNewState);
109             inconsistentBundlesToYangURLs.clear();
110             // update cache
111             updateCache(snapshot);
112             logger.info("Yang store updated to new consistent state");
113             logger.trace("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
114
115             notifyListeners(changedURLs, adding);
116             return true;
117         } catch(YangStoreException e) {
118             // inconsistent state
119             logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
120                     consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e.toString());
121             return false;
122         }
123     }
124
125     private void updateCache(YangStoreSnapshot snapshot) {
126         cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
127     }
128
129     @Override
130     public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
131         logger.debug("Modified bundle {} {} {}", bundle, event, object);
132     }
133
134     /**
135      * Notifiers get only notified when consistent snapshot has changed.
136      */
137     private void notifyListeners(Collection<URL> changedURLs, boolean adding) {
138         Preconditions.checkArgument(changedURLs.size() > 0, "Cannot notify when no URLs changed");
139         if (changedURLs.size() > 0) {
140             RuntimeException potential = new RuntimeException("Error while notifying listeners");
141             for (YangStoreListener listener : listeners) {
142                 try {
143                     if (adding) {
144                         listener.onAddedYangURL(changedURLs);
145                     } else {
146                         listener.onRemovedYangURL(changedURLs);
147                     }
148                 } catch(RuntimeException e) {
149                     potential.addSuppressed(e);
150                 }
151             }
152             if (potential.getSuppressed().length > 0) {
153                 throw potential;
154             }
155         }
156     }
157
158
159     /**
160      * If removing YANG files makes yang store inconsistent, method {@link #getYangStoreSnapshot()}
161      * will throw exception. There is no rollback.
162      */
163     @Override
164     public synchronized void removedBundle(Bundle bundle, BundleEvent event, Object object) {
165         inconsistentBundlesToYangURLs.removeAll(bundle);
166         Collection<URL> consistentURLsToBeRemoved = consistentBundlesToYangURLs.removeAll(bundle);
167
168         if (consistentURLsToBeRemoved.isEmpty()){
169             return; // no change
170         }
171         boolean adding = false;
172         notifyListeners(consistentURLsToBeRemoved, adding);
173     }
174
175     @Override
176     public synchronized YangStoreSnapshot getYangStoreSnapshot()
177             throws YangStoreException {
178         Optional<YangStoreSnapshot> yangStoreOpt = cache.getCachedYangStore(consistentBundlesToYangURLs);
179         if (yangStoreOpt.isPresent()) {
180             logger.trace("Returning cached yang store {}", yangStoreOpt.get());
181             return yangStoreOpt.get();
182         }
183         YangStoreSnapshot snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
184         updateCache(snapshot);
185         return snapshot;
186     }
187
188     private static YangStoreSnapshot createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
189         try {
190             YangStoreSnapshot yangStoreSnapshot = mbeParser.parseYangFiles(fromUrlsToInputStreams(multimap));
191             logger.trace("{} module entries parsed successfully from {} yang files",
192                     yangStoreSnapshot.countModuleMXBeanEntries(), multimap.values().size());
193             return yangStoreSnapshot;
194         } catch (RuntimeException e) {
195             throw new YangStoreException("Unable to parse yang files from following URLs: " + multimap, e);
196         }
197     }
198
199     private static Collection<InputStream> fromUrlsToInputStreams(Multimap<Bundle, URL> multimap) {
200         return Collections2.transform(multimap.values(),
201                 new Function<URL, InputStream>() {
202
203                     @Override
204                     public InputStream apply(URL url) {
205                         try {
206                             return url.openStream();
207                         } catch (IOException e) {
208                             logger.warn("Unable to open stream from {}", url);
209                             throw new IllegalStateException(
210                                     "Unable to open stream from " + url, e);
211                         }
212                     }
213                 });
214     }
215
216     @Override
217     public synchronized YangStoreListenerRegistration registerListener(final YangStoreListener listener) {
218         listeners.add(listener);
219         return new YangStoreListenerRegistration() {
220             @Override
221             public void close() {
222                 listeners.remove(listener);
223             }
224         };
225     }
226
227     private static final class YangStoreCache {
228
229         Set<URL> cachedUrls;
230         YangStoreSnapshot cachedYangStoreSnapshot;
231
232         Optional<YangStoreSnapshot> getCachedYangStore(
233                 Multimap<Bundle, URL> bundlesToYangURLs) {
234             Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
235             if (cachedUrls != null && cachedUrls.equals(urls)) {
236                 Preconditions.checkState(cachedYangStoreSnapshot != null);
237                 return Optional.of(cachedYangStoreSnapshot);
238             }
239             return Optional.absent();
240         }
241
242         private static Set<URL> setFromMultimapValues(
243                 Multimap<Bundle, URL> bundlesToYangURLs) {
244             Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
245             Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
246             return urls;
247         }
248
249         void cacheYangStore(Multimap<Bundle, URL> urls,
250                 YangStoreSnapshot yangStoreSnapshot) {
251             this.cachedUrls = setFromMultimapValues(urls);
252             this.cachedYangStoreSnapshot = yangStoreSnapshot;
253         }
254
255     }
256 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.