import com.google.common.io.ByteStreams;
import com.google.common.util.concurrent.CheckedFuture;
import java.io.IOException;
+import java.lang.ref.SoftReference;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
+import java.util.Objects;
import java.util.Set;
import org.opendaylight.controller.config.yangjmxgenerator.ModuleMXBeanEntry;
import org.opendaylight.controller.config.yangjmxgenerator.PackageTranslator;
import org.slf4j.LoggerFactory;
final class YangStoreSnapshot implements YangStoreContext, EnumResolver {
- private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
+ private static final class MXBeans {
+ private final Map<String /* Namespace from yang file */,
+ Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
+ private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
+
+ MXBeans(final SchemaContext schemaContext) {
+ LOG.trace("Resolved modules:{}", schemaContext.getModules());
+
+ // JMX generator
+ Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
+ PackageTranslator packageTranslator = new PackageTranslator(namespaceToPackageMapping);
+ Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
+ Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
+ // create SIE structure qNamesToSIEs
+ for (Module module : schemaContext.getModules()) {
+ String packageName = packageTranslator.getPackageName(module);
+ Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
+ .create(module, packageName, knownSEITracker);
+ for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
+ // merge value into qNamesToSIEs
+ if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
+ qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
+ } else {
+ throw new IllegalStateException("Cannot add two SIE with same qname "
+ + sieEntry.getValue());
+ }
+ }
+ }
+ Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
- private final Map<String /* Namespace from yang file */,
- Map<String /* Name of module entry from yang file */, ModuleMXBeanEntry>> moduleMXBeanEntryMap;
+ Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
- private final Map<QName, Map<String, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries;
+ for (Module module : schemaContext.getModules()) {
+ String packageName = packageTranslator.getPackageName(module);
+ TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
+ new TypeProviderImpl(schemaContext));
- private final SchemaContext schemaContext;
- private final BindingRuntimeContext bindingContextProvider;
- private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
+ QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
- public YangStoreSnapshot(final SchemaContext resolveSchemaContext,
- final BindingRuntimeContext bindingContextProvider,
- final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
- this.bindingContextProvider = bindingContextProvider;
- this.sourceProvider = sourceProvider;
- LOG.trace("Resolved modules:{}", resolveSchemaContext.getModules());
- this.schemaContext = resolveSchemaContext;
- // JMX generator
-
- Map<String, String> namespaceToPackageMapping = Maps.newHashMap();
- PackageTranslator packageTranslator = new PackageTranslator(namespaceToPackageMapping);
- Map<QName, ServiceInterfaceEntry> qNamesToSIEs = new HashMap<>();
- Map<IdentitySchemaNode, ServiceInterfaceEntry> knownSEITracker = new HashMap<>();
- // create SIE structure qNamesToSIEs
- for (Module module : resolveSchemaContext.getModules()) {
- String packageName = packageTranslator.getPackageName(module);
- Map<QName, ServiceInterfaceEntry> namesToSIEntries = ServiceInterfaceEntry
- .create(module, packageName, knownSEITracker);
- for (Entry<QName, ServiceInterfaceEntry> sieEntry : namesToSIEntries.entrySet()) {
- // merge value into qNamesToSIEs
- if (qNamesToSIEs.containsKey(sieEntry.getKey()) == false) {
- qNamesToSIEs.put(sieEntry.getKey(), sieEntry.getValue());
- } else {
- throw new IllegalStateException("Cannot add two SIE with same qname "
- + sieEntry.getValue());
- }
+ Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
+ Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, schemaContext,
+ typeProviderWrapper, packageName));
+ moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
+
+ qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
}
+ this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
+ this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
}
+ }
- Map<String, Map<String, ModuleMXBeanEntry>> moduleMXBeanEntryMap = Maps.newHashMap();
-
- Map<QName, Map<String /* identity local name */, ModuleMXBeanEntry>> qNamesToIdentitiesToModuleMXBeanEntries = new HashMap<>();
-
+ private static final Logger LOG = LoggerFactory.getLogger(YangStoreSnapshot.class);
+ private final SchemaSourceProvider<YangTextSchemaSource> sourceProvider;
+ private final BindingRuntimeContext bindingContextProvider;
- for (Module module : schemaContext.getModules()) {
- String packageName = packageTranslator.getPackageName(module);
- TypeProviderWrapper typeProviderWrapper = new TypeProviderWrapper(
- new TypeProviderImpl(resolveSchemaContext));
+ /**
+ * We want to lazily compute the context of the MXBean class and have it only softly-attached to this instance,
+ * so it can be garbage collected when the memory gets tight. If the schema context changes as we are computing
+ * things, YangStoreService will detect that and retry, so we do not have to worry about that.
+ */
+ private volatile SoftReference<MXBeans> ref = new SoftReference<>(null);
- QName qName = QName.create(module.getNamespace(), module.getRevision(), module.getName());
+ public YangStoreSnapshot(final BindingRuntimeContext bindingContextProvider,
+ final SchemaSourceProvider<YangTextSchemaSource> sourceProvider) {
+ this.bindingContextProvider = Preconditions.checkNotNull(bindingContextProvider);
+ this.sourceProvider = Preconditions.checkNotNull(sourceProvider);
+ }
- Map<String /* MB identity local name */, ModuleMXBeanEntry> namesToMBEs =
- Collections.unmodifiableMap(ModuleMXBeanEntry.create(module, qNamesToSIEs, resolveSchemaContext,
- typeProviderWrapper, packageName));
- moduleMXBeanEntryMap.put(module.getNamespace().toString(), namesToMBEs);
+ private MXBeans getMXBeans() {
+ MXBeans mxBean = ref.get();
- qNamesToIdentitiesToModuleMXBeanEntries.put(qName, namesToMBEs);
+ if (mxBean == null) {
+ synchronized (this) {
+ mxBean = ref.get();
+ if (mxBean == null) {
+ mxBean = new MXBeans(bindingContextProvider.getSchemaContext());
+ ref = new SoftReference<>(mxBean);
+ }
+ }
}
- this.moduleMXBeanEntryMap = Collections.unmodifiableMap(moduleMXBeanEntryMap);
- this.qNamesToIdentitiesToModuleMXBeanEntries = Collections.unmodifiableMap(qNamesToIdentitiesToModuleMXBeanEntries);
+ return mxBean;
}
@Override
public Map<String, Map<String, ModuleMXBeanEntry>> getModuleMXBeanEntryMap() {
- return moduleMXBeanEntryMap;
+ return getMXBeans().moduleMXBeanEntryMap;
}
@Override
public Map<QName, Map<String, ModuleMXBeanEntry>> getQNamesToIdentitiesToModuleMXBeanEntries() {
- return qNamesToIdentitiesToModuleMXBeanEntries;
+ return getMXBeans().qNamesToIdentitiesToModuleMXBeanEntries;
}
@Override
public Set<Module> getModules() {
- final Set<Module> modules = Sets.newHashSet(schemaContext.getModules());
- for (final Module module : schemaContext.getModules()) {
+ final Set<Module> modules = Sets.newHashSet(bindingContextProvider.getSchemaContext().getModules());
+ for (final Module module : bindingContextProvider.getSchemaContext().getModules()) {
modules.addAll(module.getSubmodules());
}
return modules;
}
@Override
- public boolean equals(final Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
-
- final YangStoreSnapshot that = (YangStoreSnapshot) o;
-
- if (schemaContext != null ? !schemaContext.equals(that.schemaContext) : that.schemaContext != null)
+ public boolean equals(final Object obj) {
+ if (this == obj) {
+ return true;
+ }
+ if (!(obj instanceof YangStoreSnapshot)) {
return false;
+ }
- return true;
+ final YangStoreSnapshot other = (YangStoreSnapshot) obj;
+ return Objects.equals(bindingContextProvider, other.bindingContextProvider);
}
@Override
public int hashCode() {
- return schemaContext != null ? schemaContext.hashCode() : 0;
+ return Objects.hashCode(bindingContextProvider);
}
@Override
Preconditions.checkState(bindingContextProvider != null, "Binding context provider was not set yet");
final BiMap<String, String> enumMapping = bindingContextProvider.getEnumMapping(enumClass);
final String javaName = enumMapping.inverse().get(enumJavaValue);
- return Preconditions.checkNotNull(javaName, "Unable to map enumcd .." +
- "cd value %s for enum class %s with assumed enum mapping: %s", enumJavaValue, enumClass, enumMapping.inverse());
+ return Preconditions.checkNotNull(javaName,
+ "Unable to map enum value %s for enum class %s with assumed enum mapping: %s", enumJavaValue, enumClass,
+ enumMapping.inverse());
}
}