2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.config.facade.xml.osgi;
11 import com.google.common.collect.Sets;
12 import java.lang.ref.SoftReference;
13 import java.util.Collections;
14 import java.util.HashSet;
17 import java.util.concurrent.ExecutorService;
18 import java.util.concurrent.Executors;
19 import java.util.concurrent.ThreadFactory;
20 import java.util.concurrent.atomic.AtomicReference;
21 import org.opendaylight.controller.config.util.capability.ModuleListener;
22 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
23 import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.model.api.Module;
26 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
27 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 public class YangStoreService implements YangStoreContext {
33 private static final Logger LOG = LoggerFactory.getLogger(YangStoreService.class);
36 * This is a rather interesting locking model. We need to guard against both the
37 * cache expiring from GC and being invalidated by schema context change. The
38 * context can change while we are doing processing, so we do not want to block
39 * it, so no synchronization can happen on the methods.
41 * So what we are doing is the following:
43 * We synchronize with GC as usual, using a SoftReference.
45 * The atomic reference is used to synchronize with {@link #refresh(org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext)}, e.g. when
46 * refresh happens, it will push a SoftReference(null), e.g. simulate the GC. Now
47 * that may happen while the getter is already busy acting on the old schema context,
48 * so it needs to understand that a refresh has happened and retry. To do that, it
49 * attempts a CAS operation -- if it fails, in knows that the SoftReference has
50 * been replaced and thus it needs to retry.
52 * Note that {@link #getYangStoreSnapshot()} will still use synchronize() internally
53 * to stop multiple threads doing the same work.
55 private final AtomicReference<SoftReference<YangStoreSnapshot>> ref =
56 new AtomicReference<>(new SoftReference<YangStoreSnapshot>(null));
58 private final AtomicReference<SoftReference<BindingRuntimeContext>> refBindingContext =
59 new AtomicReference<>(new SoftReference<BindingRuntimeContext>(null));
61 private final SchemaContextProvider schemaContextProvider;
63 private final ExecutorService notificationExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() {
65 public Thread newThread(final Runnable r) {
66 return new Thread(r, "yangstore-capability-notifications");
70 private final Set<ModuleListener> listeners = Collections.synchronizedSet(new HashSet<ModuleListener>());
72 public YangStoreService(final SchemaContextProvider schemaContextProvider) {
73 this.schemaContextProvider = schemaContextProvider;
76 private synchronized YangStoreContext getYangStoreSnapshot() {
77 SoftReference<YangStoreSnapshot> r = ref.get();
78 YangStoreSnapshot ret = r.get();
81 // We need to be compute a new value
82 ret = new YangStoreSnapshot(schemaContextProvider.getSchemaContext(), refBindingContext.get().get());
84 if (!ref.compareAndSet(r, new SoftReference<>(ret))) {
85 LOG.debug("Concurrent refresh detected, recomputing snapshot");
94 public YangStoreContext getCurrentSnapshot() {
95 return getYangStoreSnapshot();
99 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
100 return getYangStoreSnapshot().getModuleMXBeanEntryMap();
104 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
105 return getYangStoreSnapshot().getQNamesToIdentitiesToModuleMXBeanEntries();
109 public Set<Module> getModules() {
110 return getYangStoreSnapshot().getModules();
114 public String getModuleSource(final ModuleIdentifier moduleIdentifier) {
115 return getYangStoreSnapshot().getModuleSource(moduleIdentifier);
119 public EnumResolver getEnumResolver() {
120 return getYangStoreSnapshot().getEnumResolver();
123 public void refresh(final BindingRuntimeContext runtimeContext) {
124 final YangStoreSnapshot previous = ref.get().get();
125 ref.set(new SoftReference<YangStoreSnapshot>(null));
126 refBindingContext.set(new SoftReference<>(runtimeContext));
127 notificationExecutor.submit(new CapabilityChangeNotifier(previous));
130 public AutoCloseable registerModuleListener(final ModuleListener listener) {
131 YangStoreContext context = ref.get().get();
133 if (context == null) {
134 context = getYangStoreSnapshot();
137 this.listeners.add(listener);
138 listener.onCapabilitiesChanged(context.getModules(), Collections.<Module>emptySet());
140 return new AutoCloseable() {
142 public void close() throws Exception {
143 YangStoreService.this.listeners.remove(listener);
148 private final class CapabilityChangeNotifier implements Runnable {
150 private final YangStoreSnapshot previous;
152 public CapabilityChangeNotifier(final YangStoreSnapshot previous) {
153 this.previous = previous;
158 final YangStoreContext current = getYangStoreSnapshot();
160 if(!current.equals(previous)) {
161 final Set<Module> removed = Sets.difference(previous.getModules(), current.getModules());
162 final Set<Module> added = Sets.difference(current.getModules(), previous.getModules());
164 for (final ModuleListener listener : listeners) {
165 listener.onCapabilitiesChanged(added, removed);