/* * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved. * * This program and the accompanying materials are made available under the * terms of the Eclipse Public License v1.0 which accompanies this distribution, * and is available at http://www.eclipse.org/legal/epl-v10.html */ package org.opendaylight.controller.config.facade.xml.osgi; import com.google.common.collect.Sets; import java.lang.ref.SoftReference; import java.util.Collections; import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicReference; import org.opendaylight.controller.config.util.capability.ModuleListener; import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry; import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext; import org.opendaylight.yangtools.yang.common.QName; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier; import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class YangStoreService implements YangStoreContext { private static final Logger LOG = LoggerFactory.getLogger(YangStoreService.class); /** * This is a rather interesting locking model. We need to guard against both the * cache expiring from GC and being invalidated by schema context change. The * context can change while we are doing processing, so we do not want to block * it, so no synchronization can happen on the methods. * * So what we are doing is the following: * * We synchronize with GC as usual, using a SoftReference. * * The atomic reference is used to synchronize with {@link #refresh(org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext)}, e.g. when * refresh happens, it will push a SoftReference(null), e.g. simulate the GC. Now * that may happen while the getter is already busy acting on the old schema context, * so it needs to understand that a refresh has happened and retry. To do that, it * attempts a CAS operation -- if it fails, in knows that the SoftReference has * been replaced and thus it needs to retry. * * Note that {@link #getYangStoreSnapshot()} will still use synchronize() internally * to stop multiple threads doing the same work. */ private final AtomicReference> ref = new AtomicReference<>(new SoftReference(null)); private final AtomicReference> refBindingContext = new AtomicReference<>(new SoftReference(null)); private final SchemaContextProvider schemaContextProvider; private final ExecutorService notificationExecutor = Executors.newSingleThreadExecutor(new ThreadFactory() { @Override public Thread newThread(final Runnable r) { return new Thread(r, "yangstore-capability-notifications"); } }); private final Set listeners = Collections.synchronizedSet(new HashSet()); public YangStoreService(final SchemaContextProvider schemaContextProvider) { this.schemaContextProvider = schemaContextProvider; } private synchronized YangStoreContext getYangStoreSnapshot() { SoftReference r = ref.get(); YangStoreSnapshot ret = r.get(); while (ret == null) { // We need to be compute a new value ret = new YangStoreSnapshot(schemaContextProvider.getSchemaContext(), refBindingContext.get().get()); if (!ref.compareAndSet(r, new SoftReference<>(ret))) { LOG.debug("Concurrent refresh detected, recomputing snapshot"); r = ref.get(); ret = null; } } return ret; } public YangStoreContext getCurrentSnapshot() { return getYangStoreSnapshot(); } @Override public Map> getModuleMXBeanEntryMap() { return getYangStoreSnapshot().getModuleMXBeanEntryMap(); } @Override public Map> getQNamesToIdentitiesToModuleMXBeanEntries() { return getYangStoreSnapshot().getQNamesToIdentitiesToModuleMXBeanEntries(); } @Override public Set getModules() { return getYangStoreSnapshot().getModules(); } @Override public String getModuleSource(final ModuleIdentifier moduleIdentifier) { return getYangStoreSnapshot().getModuleSource(moduleIdentifier); } @Override public EnumResolver getEnumResolver() { return getYangStoreSnapshot().getEnumResolver(); } public void refresh(final BindingRuntimeContext runtimeContext) { final YangStoreSnapshot previous = ref.get().get(); ref.set(new SoftReference(null)); refBindingContext.set(new SoftReference<>(runtimeContext)); notificationExecutor.submit(new CapabilityChangeNotifier(previous)); } public AutoCloseable registerModuleListener(final ModuleListener listener) { YangStoreContext context = ref.get().get(); if (context == null) { context = getYangStoreSnapshot(); } this.listeners.add(listener); listener.onCapabilitiesChanged(context.getModules(), Collections.emptySet()); return new AutoCloseable() { @Override public void close() throws Exception { YangStoreService.this.listeners.remove(listener); } }; } private final class CapabilityChangeNotifier implements Runnable { private final YangStoreSnapshot previous; public CapabilityChangeNotifier(final YangStoreSnapshot previous) { this.previous = previous; } @Override public void run() { final YangStoreContext current = getYangStoreSnapshot(); if(!current.equals(previous)) { final Set removed = Sets.difference(previous.getModules(), current.getModules()); final Set added = Sets.difference(current.getModules(), previous.getModules()); for (final ModuleListener listener : listeners) { listener.onCapabilitiesChanged(added, removed); } } } } }