import com.google.common.cache.LoadingCache;
import com.google.common.collect.ImmutableBiMap;
import java.lang.reflect.Method;
+import java.net.URI;
import java.util.AbstractMap.SimpleEntry;
+import java.util.Collection;
+import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
+import java.util.concurrent.TimeUnit;
import javax.annotation.Nonnull;
import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationException;
import org.opendaylight.controller.md.sal.common.impl.util.compat.DataNormalizationOperation;
import org.opendaylight.yangtools.binding.data.codec.api.BindingCodecTreeNode;
import org.opendaylight.yangtools.binding.data.codec.api.BindingNormalizedNodeSerializer;
import org.opendaylight.yangtools.binding.data.codec.impl.BindingNormalizedNodeCodecRegistry;
+import org.opendaylight.yangtools.binding.data.codec.impl.MissingSchemaException;
import org.opendaylight.yangtools.sal.binding.generator.impl.GeneratedClassLoadingStrategy;
import org.opendaylight.yangtools.sal.binding.generator.util.BindingRuntimeContext;
import org.opendaylight.yangtools.yang.binding.BindingMapping;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
import org.opendaylight.yangtools.yang.model.api.SchemaContextListener;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
public final class BindingToNormalizedNodeCodec implements BindingCodecTreeFactory, BindingNormalizedNodeSerializer, SchemaContextListener, AutoCloseable {
+ private static final long WAIT_DURATION_SEC = 5;
+ private static final Logger LOG = LoggerFactory.getLogger(BindingToNormalizedNodeCodec.class);
+
private final BindingNormalizedNodeCodecRegistry codecRegistry;
- private DataNormalizer legacyToNormalized;
+
private final GeneratedClassLoadingStrategy classLoadingStrategy;
- private BindingRuntimeContext runtimeContext;
+ private final FutureSchema futureSchema;
private final LoadingCache<InstanceIdentifier<?>, YangInstanceIdentifier> iiCache = CacheBuilder.newBuilder()
.softValues().build(new CacheLoader<InstanceIdentifier<?>, YangInstanceIdentifier>() {
@Override
public YangInstanceIdentifier load(final InstanceIdentifier<?> key) throws Exception {
- return toYangInstanceIdentifier(key);
+ return toYangInstanceIdentifierBlocking(key);
}
});
+ private BindingRuntimeContext runtimeContext;
+ private DataNormalizer legacyToNormalized;
+
public BindingToNormalizedNodeCodec(final GeneratedClassLoadingStrategy classLoadingStrategy,
final BindingNormalizedNodeCodecRegistry codecRegistry) {
+ this(classLoadingStrategy,codecRegistry,false);
+
+ }
+
+ public BindingToNormalizedNodeCodec(final GeneratedClassLoadingStrategy classLoadingStrategy,
+ final BindingNormalizedNodeCodecRegistry codecRegistry,final boolean waitForSchema) {
this.classLoadingStrategy = Preconditions.checkNotNull(classLoadingStrategy,"classLoadingStrategy");
this.codecRegistry = Preconditions.checkNotNull(codecRegistry,"codecRegistry");
+ this.futureSchema = waitForSchema ? new FutureSchema(WAIT_DURATION_SEC, TimeUnit.SECONDS) : null;
+ }
+ final YangInstanceIdentifier toYangInstanceIdentifierBlocking(final InstanceIdentifier<? extends DataObject> binding) {
+ try {
+ return codecRegistry.toYangInstanceIdentifier(binding);
+ } catch (final MissingSchemaException e) {
+ waitForSchema(decompose(binding),e);
+ return codecRegistry.toYangInstanceIdentifier(binding);
+ }
}
/**
legacyToNormalized = new DataNormalizer (arg0);
runtimeContext = BindingRuntimeContext.create(classLoadingStrategy, arg0);
codecRegistry.onBindingRuntimeContextUpdated(runtimeContext);
+ if(futureSchema != null) {
+ futureSchema.onRuntimeContextUpdated(runtimeContext);
+ }
}
public <T extends DataObject> Function<Optional<NormalizedNode<?, ?>>, Optional<T>> deserializeFunction(final InstanceIdentifier<T> path) {
// FIXME: This should be probably part of Binding Runtime context
public ImmutableBiMap<Method, SchemaPath> getRpcMethodToSchemaPath(final Class<? extends RpcService> key) {
- final QNameModule moduleName = BindingReflections.getQNameModule(key);
- final Module module = runtimeContext.getSchemaContext().findModuleByNamespaceAndRevision(moduleName.getNamespace(), moduleName.getRevision());
+ final Module module = getModuleBlocking(key);
final ImmutableBiMap.Builder<Method, SchemaPath> ret = ImmutableBiMap.<Method, SchemaPath>builder();
try {
for (final RpcDefinition rpcDef : module.getRpcs()) {
}
protected ImmutableBiMap<Method, RpcDefinition> getRpcMethodToSchema(final Class<? extends RpcService> key) {
- final QNameModule moduleName = BindingReflections.getQNameModule(key);
- final Module module = runtimeContext.getSchemaContext().findModuleByNamespaceAndRevision(moduleName.getNamespace(), moduleName.getRevision());
+ final Module module = getModuleBlocking(key);
final ImmutableBiMap.Builder<Method, RpcDefinition> ret = ImmutableBiMap.builder();
try {
for (final RpcDefinition rpcDef : module.getRpcs()) {
return ret.build();
}
+ private Module getModuleBlocking(final Class<?> modeledClass) {
+ final QNameModule moduleName = BindingReflections.getQNameModule(modeledClass);
+ final URI namespace = moduleName.getNamespace();
+ final Date revision = moduleName.getRevision();
+ Module module = runtimeContext.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision);
+ if(module == null && futureSchema != null && futureSchema.waitForSchema(namespace,revision)) {
+ module = runtimeContext.getSchemaContext().findModuleByNamespaceAndRevision(namespace, revision);
+ }
+ Preconditions.checkState(module != null, "Schema for %s is not available.", modeledClass);
+ return module;
+ }
+
+ private void waitForSchema(final Collection<Class<?>> binding, final MissingSchemaException e) {
+ if(futureSchema != null) {
+ LOG.warn("Blocking thread to wait for schema convergence updates for {} {}",futureSchema.getDuration(), futureSchema.getUnit());
+ if(!futureSchema.waitForSchema(binding)) {
+ return;
+ }
+ }
+ throw e;
+ }
+
private Method findRpcMethod(final Class<? extends RpcService> key, final RpcDefinition rpcDef) throws NoSuchMethodException {
final String methodName = BindingMapping.getMethodName(rpcDef.getQName());
if(rpcDef.getInput() != null) {
return new SimpleEntry<InstanceIdentifier<?>, BindingCodecTreeNode<?>>(bindingPath, codecContext);
}
+ @SuppressWarnings("unchecked")
public Set<Class<? extends Notification>> getNotificationClasses(final Set<SchemaPath> interested) {
final Set<Class<? extends Notification>> result = new HashSet<>();
final Set<NotificationDefinition> knownNotifications = runtimeContext.getSchemaContext().getNotifications();
result.add((Class<? extends Notification>) runtimeContext.getClassForSchema(notification));
} catch (final IllegalStateException e) {
// Ignore
+ LOG.warn("Class for {} is currently not known.",notification.getPath(),e);
}
}
}
return result;
}
+ private static Collection<Class<?>> decompose(final InstanceIdentifier<?> path) {
+ final Set<Class<?>> clazzes = new HashSet<>();
+ for(final InstanceIdentifier.PathArgument arg : path.getPathArguments()) {
+ clazzes.add(arg.getType());
+ }
+ return clazzes;
+ }
+
}