X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=cps-service%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Fcps%2Fyang%2FYangTextSchemaSourceSetBuilder.java;h=fd534971a137cf8ab102fc767df81de256304622;hb=3d02e9210625b75419089a3f5612f386c3b997ea;hp=b1462cdc214f1f9ec8c71bbd08d4593b23e1472f;hpb=9afc8d1448a6a913db56304d3bc80cd92c141d0f;p=cps.git diff --git a/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java b/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java index b1462cdc2..fd534971a 100644 --- a/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java +++ b/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java @@ -1,12 +1,14 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2020 Pantheon.tech + * Modifications Copyright (C) 2022 Nordix Foundation. * ================================================================================ * 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. @@ -30,13 +32,13 @@ import java.nio.charset.StandardCharsets; import java.util.Collections; import java.util.List; import java.util.Map; +import java.util.regex.Pattern; import java.util.stream.Collectors; import lombok.NoArgsConstructor; import org.onap.cps.spi.exceptions.CpsException; import org.onap.cps.spi.exceptions.ModelValidationException; import org.onap.cps.spi.model.ModuleReference; import org.opendaylight.yangtools.yang.common.Revision; -import org.opendaylight.yangtools.yang.common.YangNames; import org.opendaylight.yangtools.yang.model.api.Module; import org.opendaylight.yangtools.yang.model.api.SchemaContext; import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException; @@ -50,6 +52,9 @@ import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementR @NoArgsConstructor public final class YangTextSchemaSourceSetBuilder { + private static final Pattern RFC6020_RECOMMENDED_FILENAME_PATTERN = + Pattern.compile("([\\w-]+)@(\\d{4}-\\d{2}-\\d{2})(?:\\.yang)?", Pattern.CASE_INSENSITIVE); + private final ImmutableMap.Builder yangModelMap = new ImmutableMap.Builder<>(); public YangTextSchemaSourceSetBuilder putAll(final Map yangResourceNameToContent) { @@ -58,7 +63,7 @@ public final class YangTextSchemaSourceSetBuilder { } public YangTextSchemaSourceSet build() { - final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build()); + final var schemaContext = generateSchemaContext(yangModelMap.build()); return new YangTextSchemaSourceSetImpl(schemaContext); } @@ -93,8 +98,8 @@ public final class YangTextSchemaSourceSetBuilder { private static ModuleReference toModuleReference(final Module module) { return ModuleReference.builder() - .name(module.getName()) - .namespace(module.getNamespace().toString()) + .moduleName(module.getName()) + .namespace(module.getQNameModule().getNamespace().toString()) .revision(module.getRevision().map(Revision::toString).orElse(null)) .build(); } @@ -123,7 +128,8 @@ public final class YangTextSchemaSourceSetBuilder { String.format("Exception occurred on reading resource %s.", resourceName), e); } catch (final YangSyntaxErrorException e) { throw new ModelValidationException("Yang resource is invalid.", - String.format("Yang syntax validation failed for resource %s.", resourceName), e); + String.format( + "Yang syntax validation failed for resource %s:%n%s", resourceName, e.getMessage()), e); } } try { @@ -143,11 +149,9 @@ public final class YangTextSchemaSourceSetBuilder { .collect(Collectors.toList()); } - private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, - final String source) { - final Map.Entry sourceNameParsed = checkNotNull(YangNames.parseFilename(sourceName)); - final RevisionSourceIdentifier revisionSourceIdentifier = RevisionSourceIdentifier - .create(sourceNameParsed.getKey(), Revision.ofNullable(sourceNameParsed.getValue())); + private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) { + final var revisionSourceIdentifier = + createIdentifierFromSourceName(checkNotNull(sourceName)); return new YangTextSchemaSource(revisionSourceIdentifier) { @Override @@ -162,4 +166,12 @@ public final class YangTextSchemaSourceSetBuilder { } }; } + + private static RevisionSourceIdentifier createIdentifierFromSourceName(final String sourceName) { + final var matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName); + if (matcher.matches()) { + return RevisionSourceIdentifier.create(matcher.group(1), Revision.of(matcher.group(2))); + } + return RevisionSourceIdentifier.create(sourceName); + } }