@Override public String toString() { return MoreObjects.toStringHelper(this).add("name", getName()).toString(); } };
private static boolean hasDependency(final YangModuleInfo superiorModel, final YangModuleInfo dependency) { for (YangModuleInfo moduleInfo: superiorModel.getImportedModules()) { if (moduleInfo.getName().equals(dependency.getName())) { return true; } hasDependency(moduleInfo, dependency); } return false; }
/** * Given a {@link YangModuleInfo}, create a QName representing it. The QName * is formed by reusing the module's namespace and revision using the * module's name as the QName's local name. * * @param moduleInfo * module information * @return QName representing the module */ public static QName getModuleQName(final YangModuleInfo moduleInfo) { checkArgument(moduleInfo != null, "moduleInfo must not be null."); return QName.create(moduleInfo.getNamespace(), moduleInfo.getRevision(), moduleInfo.getName()); }
private SourceIdentifier sourceIdentifierFrom(final YangModuleInfo moduleInfo) { return SourceIdentifier.create(moduleInfo.getName(), Optional.of(moduleInfo.getRevision())); }
private static void collectYangModuleInfo(final YangModuleInfo moduleInfo, final Builder<YangModuleInfo> moduleInfoSet) { moduleInfoSet.add(moduleInfo); for (YangModuleInfo dependency : moduleInfo.getImportedModules()) { collectYangModuleInfo(dependency, moduleInfoSet); } }
public static final QNameModule getQNameModule(final YangModuleInfo modInfo) { return QNameModule.create(URI.create(modInfo.getNamespace()), QName.parseRevision(modInfo.getRevision())); }
/** * Return a {@link CharSource} accessing the YANG text of the module. * * @return A CharSource. */ default CharSource getYangTextCharSource() { return getYangTextByteSource().asCharSource(StandardCharsets.UTF_8); } }
public BindingToRestRpc(final Class<?> proxiedInterface,final BindingNormalizedNodeCodecRegistry mappingService2,final RestconfClientImpl client,final SchemaContext schemaContext) throws Exception { this.mappingService = mappingService2; this.client = client; this.schcemaContext = schemaContext; YangModuleInfo moduleInfo = BindingReflections.getModuleInfo(proxiedInterface); this.module = schemaContext.findModuleByName(moduleInfo.getName(),org.opendaylight.yangtools.yang.common.QName.parseRevision(moduleInfo.getRevision())); }
private boolean resolveModuleInfo(final YangModuleInfo moduleInfo) { SourceIdentifier identifier = sourceIdentifierFrom(moduleInfo); YangModuleInfo previous = sourceIdentifierToModuleInfo.putIfAbsent(identifier, moduleInfo); ClassLoader moduleClassLoader = moduleInfo.getClass().getClassLoader(); if (previous == null) { String modulePackageName = moduleInfo.getClass().getPackage().getName(); packageNameToClassLoader.putIfAbsent(modulePackageName, new WeakReference<ClassLoader>(moduleClassLoader)); for (YangModuleInfo importedInfo : moduleInfo.getImportedModules()) { resolveModuleInfo(importedInfo); } } else { return false; } return true; }
private static Optional<DataNodeContainer> findFirstDataNodeContainerInRpc(final SchemaContext ctx, final Class<? extends DataObject> targetType) { final YangModuleInfo moduleInfo; try { moduleInfo = BindingReflections.getModuleInfo(targetType); } catch (Exception e) { throw new IllegalArgumentException( String.format("Failed to load module information for class %s", targetType), e); } for(RpcDefinition rpc : ctx.getOperations()) { String rpcNamespace = rpc.getQName().getNamespace().toString(); String rpcRevision = rpc.getQName().getFormattedRevision(); if(moduleInfo.getNamespace().equals(rpcNamespace) && moduleInfo.getRevision().equals(rpcRevision)) { Optional<DataNodeContainer> potential = findInputOutput(rpc,targetType.getSimpleName()); if(potential.isPresent()) { return potential; } } } return Optional.absent(); }
private static YangTextSchemaSource toYangTextSource(final SourceIdentifier identifier, final YangModuleInfo moduleInfo) { return YangTextSchemaSource.delegateForByteSource(identifier, moduleInfo.getYangTextByteSource()); }
ModelTypeIndex() throws IOException { namespaceToModuleIndex = collectAllModules(this.getClass().getClassLoader()) .stream() .collect(toMap(YangModelKey::new, yangModuleInfo -> yangModuleInfo.getName().toString())); }
private static void printDependencies(final Collection<YangModuleInfo> yangModuleInfos, final int prefixLength) { for (final YangModuleInfo yangModuleInfo : yangModuleInfos) { final QName qname = yangModuleInfo.getName(); LOG.info("{}{} {} {}", Strings.repeat(" ", prefixLength), qname.getNamespace(), qname.getLocalName(), qname.getRevision()); printDependencies(yangModuleInfo.getImportedModules(), prefixLength + PREFIX); } }
private static void addDependencies(final Map<ModuleId, YangModuleInfo> resolvedModules, final Collection<YangModuleInfo> importedModules) { for (YangModuleInfo yangModuleInfo : importedModules) { resolvedModules.put(ModuleId.from(yangModuleInfo), yangModuleInfo); LOG.info("Adding [{}] module into known modules", yangModuleInfo); addDependencies(resolvedModules, yangModuleInfo.getImportedModules()); } }
@Override public ListenableFuture<? extends YangTextSchemaSource> getSource( final SourceIdentifier sourceIdentifier) { final YangModuleInfo yangModuleInfo = sourceIdentifierToModuleInfo.get(sourceIdentifier); if (yangModuleInfo == null) { LOG.debug("Unknown schema source requested: {}, available sources: {}", sourceIdentifier, sourceIdentifierToModuleInfo.keySet()); return Futures.immediateFailedFuture(new SchemaSourceException( "Unknown schema source: " + sourceIdentifier)); } return Futures.immediateFuture(YangTextSchemaSource.delegateForByteSource(sourceIdentifier, yangModuleInfo.getYangTextByteSource())); }
public static void printModelInfo(final Set<YangModuleInfo> allModelsFromClasspath) { final int prefixLength = 0; final Set<YangModuleInfo> topLevelModels = YangModuleUtils.filterTopLevelModels(allModelsFromClasspath); LOG.info("# top-level models tree: {}", topLevelModels.size()); for (final YangModuleInfo yangModuleInfo : topLevelModels) { final QName qname = yangModuleInfo.getName(); LOG.info("{}", qname.getNamespace(), qname.getLocalName(), qname.getRevision()); printDependencies(yangModuleInfo.getImportedModules(), prefixLength + PREFIX); } LOG.info("# top-level models list: {}", topLevelModels.size()); for (final YangModuleInfo yangModuleInfo : topLevelModels) { final QName qname = yangModuleInfo.getName(); LOG.info("{} {} {}", qname.getNamespace(), qname.getLocalName(), qname.getRevision()); } final Set<YangModuleInfo> uniqueModels = YangModuleUtils.filterUniqueModels(allModelsFromClasspath); LOG.info("# unique models list : {}", uniqueModels.size()); for (final YangModuleInfo yangModuleInfo : uniqueModels) { final QName qname = yangModuleInfo.getName(); LOG.info("{} {} {}", qname.getNamespace(), qname.getLocalName(), qname.getRevision()); } }
/** * Get all Yang modules from classpath filtered by collection of top-level modules. * @param filter * The collection of top-level modules represented by name and revision. * @return * Collection top-level modules and all of imported yang module dependencies recursively. * Empty collection is returned if no suitable modules are found. */ public static Set<YangModuleInfo> getModelsFromClasspath(final Set<ModuleId> filter) { Map<ModuleId, YangModuleInfo> resolvedModules = new HashMap<>(); ServiceLoader<YangModelBindingProvider> yangProviderLoader = ServiceLoader.load(YangModelBindingProvider.class); for (ModuleId moduleId: filter) { Set<YangModuleInfo> filteredSet = filterYangModelBindingProviders(moduleId, yangProviderLoader); for (YangModuleInfo yangModuleInfo : filteredSet) { resolvedModules.put(ModuleId.from(yangModuleInfo), yangModuleInfo); LOG.info("Adding [{}] module into known modules", yangModuleInfo); addDependencies(resolvedModules, yangModuleInfo.getImportedModules()); } } return Collections.unmodifiableSet(resolvedModules.values().stream().collect(Collectors.toSet())); }
public static void printConfiguration(final Set<YangModuleInfo> allModelsFromClasspath) { final Set<YangModuleInfo> topLevelModels = YangModuleUtils.filterTopLevelModels(allModelsFromClasspath); LOG.info("# top-level models list: {}", topLevelModels.size()); for (final YangModuleInfo yangModuleInfo : topLevelModels) { final QName qname = yangModuleInfo.getName(); System.out.println("{ \"nameSpace\": \"" + qname.getNamespace() + "\", \"name\": \"" + qname.getLocalName() + "\", \"revision\": \"" + qname.getRevision().orElse(null) + "\" },"); } LOG.info("# top-level models list: {}", topLevelModels.size()); for (final YangModuleInfo yangModuleInfo: topLevelModels) { System.out.println(yangModuleInfo.getClass().getCanonicalName() + ".getInstance(),"); } }
/** * Filter unique models from given entry set. * This filter scans recursively dependencies and returns minimal set of models that are unique. * @param models * Unfiltered entry set of models. * @return * Filtered set of unique models only. */ public static Set<YangModuleInfo> filterUniqueModels(final Collection<YangModuleInfo> models) { Map<ModuleId, YangModuleInfo> result = new HashMap<>(); for (YangModuleInfo yangModuleInfo: models) { result.put(ModuleId.from(yangModuleInfo), yangModuleInfo); for (YangModuleInfo yangModuleInfoDep : filterUniqueModels(yangModuleInfo.getImportedModules())) { result.put(ModuleId.from(yangModuleInfoDep), yangModuleInfoDep); } } return new HashSet<>(result.values()); }