diff options
Diffstat (limited to 'yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/ModuleSource.java')
-rw-r--r-- | yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/ModuleSource.java | 153 |
1 files changed, 153 insertions, 0 deletions
diff --git a/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/ModuleSource.java b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/ModuleSource.java new file mode 100644 index 0000000..96977da --- /dev/null +++ b/yang-compiler/src/main/java/org/onap/modeling/yangkit/compiler/ModuleSource.java @@ -0,0 +1,153 @@ +/* +Copyright 2023 Huawei Technologies + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package org.onap.modeling.yangkit.compiler; + +import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.net.URI; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import org.onap.modeling.yangkit.catalog.ModuleInfo; +import org.onap.modeling.yangkit.compiler.util.YangCompilerUtil; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.yangcentral.yangkit.model.api.schema.ModuleId; +import org.yangcentral.yangkit.model.api.schema.YangSchemaContext; +import org.yangcentral.yangkit.model.api.stmt.Module; +import org.yangcentral.yangkit.parser.YangYinParser; +import org.yangcentral.yangkit.utils.file.FileUtil; + + +public class ModuleSource implements Source { + private List<ModuleInfo> modules; + + private boolean importOnly; + private static final Logger logger = LoggerFactory.getLogger(ModuleSource.class); + + public ModuleSource(List<ModuleInfo> modules) { + this.modules = modules; + } + + public ModuleSource(List<ModuleInfo> modules, boolean importOnly) { + this.modules = modules; + this.importOnly = importOnly; + } + + public boolean isImportOnly() { + return importOnly; + } + + public void setImportOnly(boolean importOnly) { + this.importOnly = importOnly; + } + + public List<ModuleInfo> getModules() { + return modules; + } + + @Override + public YangSchemaContext buildSource(Settings settings, YangSchemaContext yangSchemaContext) + throws YangCompilerException { + return buildSource(settings, yangSchemaContext, false); + } + + @Override + public YangSchemaContext buildSource(Settings settings, YangSchemaContext schemaContext, boolean withDependencies) + throws YangCompilerException { + ModuleInfo targetModuleInfo = null; + for (ModuleInfo moduleInfo : modules) { + if (schemaContext != null) { + Optional<Module> oldModule = schemaContext.getModule( + new ModuleId(moduleInfo.getName(), moduleInfo.getRevision())); + if (oldModule.isPresent()) { + if (!importOnly && schemaContext.isImportOnly(oldModule.get())) { + schemaContext.removeModule(oldModule.get().getModuleId()); + schemaContext.addModule(oldModule.get()); + } + continue; + } + } + URI schema = moduleInfo.getSchema(); + if (schema == null) { + try { + targetModuleInfo = YangCompilerUtil.getSchema(moduleInfo, settings); + } catch (IOException e) { + throw new YangCompilerException(e.getMessage()); + } + if (targetModuleInfo == null) { + throw new YangCompilerException("module=" + + moduleInfo.getModuleInfo() + + " is not found."); + } + schema = targetModuleInfo.getSchema(); + } + if (targetModuleInfo == null) { + targetModuleInfo = moduleInfo; + } + + + try { + logger.info("download yang from " + schema.toURL()); + String yangString = YangCompilerUtil.urlInvoke2String(schema.toURL().toString(), settings); + + InputStream inputStream = new ByteArrayInputStream(yangString.getBytes()); + String parseModuleInfo = schema.toURL().toString(); + schemaContext = YangYinParser.parse(inputStream, + parseModuleInfo, true, importOnly, schemaContext); + //judge whether this module exists in local repository + if (YangCompilerUtil.getSchemaFromLocal(targetModuleInfo, settings) == null) { + // if not found, install to local repository + String fileName = settings.getLocalRepository() + File.separator + targetModuleInfo.getModuleInfo() + + ".yang"; + FileUtil.writeUtf8File(fileName, yangString); + logger.info("install " + targetModuleInfo.getModuleInfo() + ".yang to " + + settings.getLocalRepository()); + } + if (withDependencies) { + logger.info("get dependencies for " + targetModuleInfo.getModuleInfo()); + Module sourceModule = + schemaContext.getModule(targetModuleInfo.getName(), targetModuleInfo.getRevision()) + .get(); + + List<ModuleInfo> dependencies = YangCompilerUtil.getDependencies(sourceModule); + if (!dependencies.isEmpty()) { + List<ModuleInfo> extraDependencies = new ArrayList<>(); + for (ModuleInfo dependency : dependencies) { + if (schemaContext.getModule(dependency.getName(), dependency.getRevision()).isPresent()) { + continue; + } + extraDependencies.add(dependency); + } + ModuleSource extraDependenciesSource = new ModuleSource(extraDependencies, true); + logger.info("start to build dependencies for " + targetModuleInfo.getModuleInfo()); + schemaContext = extraDependenciesSource.buildSource(settings, schemaContext, true); + logger.info("end to build dependencies for " + targetModuleInfo.getModuleInfo()); + } + } + + } catch (Exception e) { + throw new YangCompilerException(e.getMessage()); + } + } + return schemaContext; + } +} |