07ffff6029d6f4994633c4fda3658e8e77eca0b8
[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.*;
15 import org.opendaylight.controller.config.yang.store.api.YangStoreException;
16 import org.opendaylight.controller.config.yang.store.api.YangStoreListenerRegistration;
17 import org.opendaylight.controller.config.yang.store.api.YangStoreService;
18 import org.opendaylight.controller.config.yang.store.api.YangStoreSnapshot;
19 import org.opendaylight.controller.config.yang.store.spi.YangStoreListener;
20 import org.osgi.framework.Bundle;
21 import org.osgi.framework.BundleEvent;
22 import org.osgi.util.tracker.BundleTrackerCustomizer;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
29 import java.util.*;
30
31 /**
32  * Note on consistency:
33  * When this bundle is activated after other bundles containing yang files, the resolving order
34  * is not preserved. We thus maintain two maps, one containing consistent snapshot, other inconsistent. The
35  * container should eventually send all events and thus making the inconsistent map redundant.
36  */
37 public class ExtenderYangTrackerCustomizer implements BundleTrackerCustomizer<Object>, YangStoreService {
38
39     private static final Logger logger = LoggerFactory
40             .getLogger(ExtenderYangTrackerCustomizer.class);
41
42     private final Multimap<Bundle, URL> consistentBundlesToYangURLs = HashMultimap.create();
43
44     /*
45     Map of currently problematic yang files that should get fixed eventually after all events are received.
46      */
47     private final Multimap<Bundle, URL> inconsistentBundlesToYangURLs = HashMultimap.create();
48
49     private final YangStoreCache cache = new YangStoreCache();
50     private final MbeParser mbeParser;
51     private final List<YangStoreListener> listeners = new ArrayList<>();
52
53     public ExtenderYangTrackerCustomizer() {
54         this(new MbeParser());
55
56     }
57
58     @VisibleForTesting
59     ExtenderYangTrackerCustomizer(MbeParser mbeParser) {
60         this.mbeParser = mbeParser;
61     }
62
63     @Override
64     public Object addingBundle(Bundle bundle, BundleEvent event) {
65
66         // Ignore system bundle:
67         // system bundle might have config-api on classpath &&
68         // config-api contains yang files =>
69         // system bundle might contain yang files from that bundle
70         if (bundle.getBundleId() == 0)
71             return bundle;
72
73         Enumeration<URL> enumeration = bundle.findEntries("META-INF/yang", "*.yang", false);
74         if (enumeration != null && enumeration.hasMoreElements()) {
75             synchronized (this) {
76                 List<URL> addedURLs = new ArrayList<>();
77                 while (enumeration.hasMoreElements()) {
78                     URL url = enumeration.nextElement();
79                     addedURLs.add(url);
80                 }
81                 logger.trace("Bundle {} has event {}, bundle state {}, URLs {}", bundle, event, bundle.getState(), addedURLs);
82                 // test that yang store is consistent
83                 Multimap<Bundle, URL> proposedNewState = HashMultimap.create(consistentBundlesToYangURLs);
84                 proposedNewState.putAll(inconsistentBundlesToYangURLs);
85                 proposedNewState.putAll(bundle, addedURLs);
86                 boolean adding = true;
87                 if (tryToUpdateState(addedURLs, proposedNewState, adding) == false) {
88                     inconsistentBundlesToYangURLs.putAll(bundle, addedURLs);
89                 }
90             }
91         }
92         return bundle;
93     }
94
95     private synchronized boolean tryToUpdateState(Collection<URL> changedURLs, Multimap<Bundle, URL> proposedNewState, boolean adding) {
96         Preconditions.checkArgument(changedURLs.size() > 0, "No change can occur when no URLs are changed");
97         try(YangStoreSnapshot snapshot = createSnapshot(mbeParser, proposedNewState)) {
98             // consistent state
99             // merge into
100             consistentBundlesToYangURLs.clear();
101             consistentBundlesToYangURLs.putAll(proposedNewState);
102             inconsistentBundlesToYangURLs.clear();
103             // update cache
104             updateCache(snapshot);
105             logger.info("Yang store updated to new consistent state");
106             logger.trace("Yang store updated to new consistent state containing {}", consistentBundlesToYangURLs);
107
108             notifyListeners(changedURLs, adding);
109             return true;
110         } catch(YangStoreException e) {
111             // inconsistent state
112             logger.debug("Yang store is falling back on last consistent state containing {}, inconsistent yang files {}, reason {}",
113                     consistentBundlesToYangURLs, inconsistentBundlesToYangURLs, e.toString());
114             return false;
115         }
116     }
117
118     private void updateCache(YangStoreSnapshot snapshot) {
119         cache.cacheYangStore(consistentBundlesToYangURLs, snapshot);
120     }
121
122     @Override
123     public void modifiedBundle(Bundle bundle, BundleEvent event, Object object) {
124         logger.debug("Modified bundle {} {} {}", bundle, event, object);
125     }
126
127     /**
128      * Notifiers get only notified when consistent snapshot has changed.
129      */
130     private void notifyListeners(Collection<URL> changedURLs, boolean adding) {
131         Preconditions.checkArgument(changedURLs.size() > 0, "Cannot notify when no URLs changed");
132         if (changedURLs.size() > 0) {
133             RuntimeException potential = new RuntimeException("Error while notifying listeners");
134             for (YangStoreListener listener : listeners) {
135                 try {
136                     if (adding) {
137                         listener.onAddedYangURL(changedURLs);
138                     } else {
139                         listener.onRemovedYangURL(changedURLs);
140                     }
141                 } catch(RuntimeException e) {
142                     potential.addSuppressed(e);
143                 }
144             }
145             if (potential.getSuppressed().length > 0) {
146                 throw potential;
147             }
148         }
149     }
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         inconsistentBundlesToYangURLs.removeAll(bundle);
159         Collection<URL> consistentURLsToBeRemoved = consistentBundlesToYangURLs.removeAll(bundle);
160
161         if (consistentURLsToBeRemoved.isEmpty()){
162             return; // no change
163         }
164         boolean adding = false;
165         notifyListeners(consistentURLsToBeRemoved, adding);
166     }
167
168     @Override
169     public synchronized YangStoreSnapshot getYangStoreSnapshot()
170             throws YangStoreException {
171         Optional<YangStoreSnapshot> yangStoreOpt = cache.getCachedYangStore(consistentBundlesToYangURLs);
172         if (yangStoreOpt.isPresent()) {
173             logger.trace("Returning cached yang store {}", yangStoreOpt.get());
174             return yangStoreOpt.get();
175         }
176         YangStoreSnapshot snapshot = createSnapshot(mbeParser, consistentBundlesToYangURLs);
177         updateCache(snapshot);
178         return snapshot;
179     }
180
181     private static YangStoreSnapshot createSnapshot(MbeParser mbeParser, Multimap<Bundle, URL> multimap) throws YangStoreException {
182         try {
183             YangStoreSnapshot 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     @Override
210     public synchronized YangStoreListenerRegistration registerListener(final YangStoreListener listener) {
211         listeners.add(listener);
212         return new YangStoreListenerRegistration() {
213             @Override
214             public void close() {
215                 listeners.remove(listener);
216             }
217         };
218     }
219
220     private static final class YangStoreCache {
221
222         Set<URL> cachedUrls;
223         YangStoreSnapshot cachedYangStoreSnapshot;
224
225         Optional<YangStoreSnapshot> getCachedYangStore(
226                 Multimap<Bundle, URL> bundlesToYangURLs) {
227             Set<URL> urls = setFromMultimapValues(bundlesToYangURLs);
228             if (cachedUrls != null && cachedUrls.equals(urls)) {
229                 Preconditions.checkState(cachedYangStoreSnapshot != null);
230                 return Optional.of(cachedYangStoreSnapshot);
231             }
232             return Optional.absent();
233         }
234
235         private static Set<URL> setFromMultimapValues(
236                 Multimap<Bundle, URL> bundlesToYangURLs) {
237             Set<URL> urls = Sets.newHashSet(bundlesToYangURLs.values());
238             Preconditions.checkState(bundlesToYangURLs.size() == urls.size());
239             return urls;
240         }
241
242         void cacheYangStore(Multimap<Bundle, URL> urls,
243                 YangStoreSnapshot yangStoreSnapshot) {
244             this.cachedUrls = setFromMultimapValues(urls);
245             this.cachedYangStoreSnapshot = yangStoreSnapshot;
246         }
247
248     }
249 }