2 * Copyright (c) 2015, 2017 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.Collections2;
12 import com.google.common.collect.ImmutableSet;
13 import com.google.common.collect.Sets;
14 import com.google.common.util.concurrent.ThreadFactoryBuilder;
15 import java.util.Collections;
16 import java.util.HashSet;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import javax.annotation.concurrent.GuardedBy;
22 import org.opendaylight.controller.config.util.capability.Capability;
23 import org.opendaylight.controller.config.util.capability.ModuleListener;
24 import org.opendaylight.controller.config.util.capability.YangModuleCapability;
25 import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
26 import org.opendaylight.mdsal.binding.generator.util.BindingRuntimeContext;
27 import org.opendaylight.yangtools.yang.common.QName;
28 import org.opendaylight.yangtools.yang.model.api.Module;
29 import org.opendaylight.yangtools.yang.model.api.ModuleIdentifier;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContextProvider;
31 import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
32 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceProvider;
34 public class YangStoreService implements YangStoreContext {
36 private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
37 private final ExecutorService notificationExecutor = Executors.newSingleThreadExecutor(
38 new ThreadFactoryBuilder().setDaemon(true).setNameFormat("yangstore-capability-notifications").build());
41 * Guarded by explicit lock to allow for properly synchronizing the initial
42 * notification and modification of the listener set.
44 @GuardedBy("listeners")
45 private final Set<ModuleListener> listeners = new HashSet<>();
48 * This is the latest snapshot. Some of its state is always initialized, but the
49 * MXBean maps potentially cause recomputation. Accessing those two specific
50 * methods needs to re-check whether the snapshot has changed asynchronously and
53 private volatile YangStoreSnapshot snap;
55 public YangStoreService(final SchemaContextProvider schemaContextProvider,
56 final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
57 this.sourceProvider = sourceProvider;
60 public YangStoreContext getCurrentSnapshot() {
66 public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
67 Map<String, Map<String, ModuleMXBeanEntry>> ret;
68 YangStoreSnapshot snapshot;
72 ret = snapshot.getModuleMXBeanEntryMap();
73 } while (!snapshot.equals(this.snap));
79 public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
80 Map<QName, Map<String, ModuleMXBeanEntry>> ret;
81 YangStoreSnapshot snapshot;
85 ret = snapshot.getQNamesToIdentitiesToModuleMXBeanEntries();
86 } while (!snapshot.equals(this.snap));
92 public Set<Module> getModules() {
93 return this.snap.getModules();
97 public String getModuleSource(final ModuleIdentifier moduleIdentifier) {
98 return this.snap.getModuleSource(moduleIdentifier);
102 public EnumResolver getEnumResolver() {
103 return this.snap.getEnumResolver();
106 public void refresh(final BindingRuntimeContext runtimeContext) {
107 final YangStoreSnapshot next = new YangStoreSnapshot(runtimeContext, this.sourceProvider);
108 final YangStoreSnapshot previous = this.snap;
110 this.notificationExecutor.submit(() -> notifyListeners(previous, next));
113 public AutoCloseable registerModuleListener(final ModuleListener listener) {
114 final YangStoreContext context = this.snap;
116 synchronized (this.listeners) {
117 if (context != null) {
118 listener.onCapabilitiesChanged(toCapabilities(context.getModules(), context),
119 Collections.<Capability>emptySet());
121 this.listeners.add(listener);
125 synchronized (YangStoreService.this.listeners) {
126 YangStoreService.this.listeners.remove(listener);
131 void notifyListeners(final YangStoreSnapshot previous, final YangStoreSnapshot current) {
132 final Set<Module> prevModules = previous.getModules();
133 final Set<Module> currModules = current.getModules();
134 final Set<Module> removed = Sets.difference(prevModules, currModules);
135 final Set<Module> added = Sets.difference(currModules, prevModules);
137 final Set<Capability> addedCaps = toCapabilities(added, current);
138 final Set<Capability> removedCaps = toCapabilities(removed, current);
140 synchronized (this.listeners) {
141 for (final ModuleListener listener : this.listeners) {
142 listener.onCapabilitiesChanged(addedCaps, removedCaps);
147 private static Set<Capability> toCapabilities(final Set<Module> modules, final YangStoreContext current) {
148 return ImmutableSet.copyOf(Collections2.transform(modules,
149 input -> new YangModuleCapability(input, current.getModuleSource(input))));