Fix Sonar Qube Violations
[cps.git] / cps-service / src / main / java / org / onap / cps / yang / YangTextSchemaSourceSetBuilder.java
index ae0f2cd..3a65369 100644 (file)
 
 package org.onap.cps.yang;
 
+import static com.google.common.base.Preconditions.checkNotNull;
+
 import com.google.common.base.MoreObjects;
 import com.google.common.collect.ImmutableMap;
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
+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;
@@ -47,6 +50,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<String, String> yangModelMap = new ImmutableMap.Builder<>();
 
     public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
@@ -55,7 +61,7 @@ public final class YangTextSchemaSourceSetBuilder {
     }
 
     public YangTextSchemaSourceSet build() {
-        final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build());
+        final var schemaContext = generateSchemaContext(yangModelMap.build());
         return new YangTextSchemaSourceSetImpl(schemaContext);
     }
 
@@ -91,7 +97,7 @@ public final class YangTextSchemaSourceSetBuilder {
         private static ModuleReference toModuleReference(final Module module) {
             return ModuleReference.builder()
                 .name(module.getName())
-                .namespace(module.getNamespace().toString())
+                .namespace(module.getQNameModule().getNamespace().toString())
                 .revision(module.getRevision().map(Revision::toString).orElse(null))
                 .build();
         }
@@ -120,7 +126,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 {
@@ -141,9 +148,8 @@ public final class YangTextSchemaSourceSetBuilder {
     }
 
     private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
-        final Map.Entry<String, String> sourceNameParsed = YangNames.parseFilename(sourceName);
-        final RevisionSourceIdentifier revisionSourceIdentifier = RevisionSourceIdentifier
-            .create(sourceNameParsed.getKey(), Revision.ofNullable(sourceNameParsed.getValue()));
+        final var revisionSourceIdentifier =
+            createIdentifierFromSourceName(checkNotNull(sourceName));
 
         return new YangTextSchemaSource(revisionSourceIdentifier) {
             @Override
@@ -154,8 +160,16 @@ public final class YangTextSchemaSourceSetBuilder {
 
             @Override
             public InputStream openStream() {
-                return new ByteArrayInputStream(source.getBytes());
+                return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
             }
         };
     }
+
+    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);
+    }
 }