/*
* ============LICENSE_START=======================================================
* Copyright (C) 2020 Pantheon.tech
+ * Modifications Copyright (C) 2022-2023 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.
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
+import io.micrometer.core.annotation.Timed;
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.Matcher;
+import java.util.Optional;
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.model.api.Module;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
import org.opendaylight.yangtools.yang.model.repo.api.RevisionSourceIdentifier;
import org.opendaylight.yangtools.yang.model.repo.api.YangTextSchemaSource;
-import org.opendaylight.yangtools.yang.parser.rfc7950.reactor.RFC7950Reactors;
-import org.opendaylight.yangtools.yang.parser.rfc7950.repo.YangStatementStreamSource;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
-import org.opendaylight.yangtools.yang.parser.stmt.reactor.CrossSourceStatementReactor;
+import org.opendaylight.yangtools.yang.parser.api.YangParser;
+import org.opendaylight.yangtools.yang.parser.api.YangParserException;
+import org.opendaylight.yangtools.yang.parser.api.YangParserFactory;
+import org.opendaylight.yangtools.yang.parser.impl.DefaultYangParserFactory;
+import org.opendaylight.yangtools.yang.xpath.impl.di.DefaultXPathParserFactory;
@NoArgsConstructor
public final class YangTextSchemaSourceSetBuilder {
private final ImmutableMap.Builder<String, String> yangModelMap = new ImmutableMap.Builder<>();
+ private static final YangParserFactory YANG_PARSER_FACTORY =
+ new DefaultYangParserFactory(new DefaultXPathParserFactory());
+
+ /**
+ * Add Yang resource context.
+ *
+ * @param yangResourceNameToContent the resource content
+ * @return this builder
+ */
public YangTextSchemaSourceSetBuilder putAll(final Map<String, String> yangResourceNameToContent) {
this.yangModelMap.putAll(yangResourceNameToContent);
return this;
}
+ /**
+ * Build a YangTextSchemaSourceSet.
+ *
+ * @return the YangTextSchemaSourceSet
+ */
public YangTextSchemaSourceSet build() {
- final SchemaContext schemaContext = generateSchemaContext(yangModelMap.build());
+ final var schemaContext = generateSchemaContext(yangModelMap.build());
return new YangTextSchemaSourceSetImpl(schemaContext);
}
+ /**
+ * Add yangResourceNameToContent and build a YangTextSchemaSourceSet.
+ *
+ * @param yangResourceNameToContent the resource content
+ * @return the YangTextSchemaSourceSet
+ */
+
+ @Timed(value = "cps.yang.schemasourceset.build", description = "Time taken to build a ODL yang Model")
public static YangTextSchemaSourceSet of(final Map<String, String> yangResourceNameToContent) {
return new YangTextSchemaSourceSetBuilder().putAll(yangResourceNameToContent).build();
}
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();
}
* @return the schema context
*/
private static SchemaContext generateSchemaContext(final Map<String, String> yangResourceNameToContent) {
- final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
+ final YangParser yangParser = YANG_PARSER_FACTORY.createParser();
for (final YangTextSchemaSource yangTextSchemaSource : forResources(yangResourceNameToContent)) {
final String resourceName = yangTextSchemaSource.getIdentifier().getName();
try {
- reactor.addSource(YangStatementStreamSource.create(yangTextSchemaSource));
- } catch (final IOException e) {
- throw new CpsException("Failed to read yang resource.",
- 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);
+ yangParser.addSource(yangTextSchemaSource);
+ } catch (final Exception exception) {
+ throw new ModelValidationException("Yang resource processing exception.",
+ String.format("Could not process resource %s:%n%s", resourceName, exception.getMessage()),
+ exception);
}
}
try {
- return reactor.buildEffective();
- } catch (final ReactorException e) {
+ return yangParser.buildEffectiveModel();
+ } catch (final YangParserException yangParserException) {
final List<String> resourceNames = yangResourceNameToContent.keySet().stream().collect(Collectors.toList());
Collections.sort(resourceNames);
throw new ModelValidationException("Invalid schema set.",
- String.format("Effective schema context build failed for resources %s.", resourceNames.toString()),
- e);
+ String.format("Effective schema context build failed for resources %s.", resourceNames),
+ yangParserException);
}
}
}
private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
- final RevisionSourceIdentifier revisionSourceIdentifier =
+ final var revisionSourceIdentifier =
createIdentifierFromSourceName(checkNotNull(sourceName));
return new YangTextSchemaSource(revisionSourceIdentifier) {
+ @Override
+ public Optional<String> getSymbolicName() {
+ return Optional.empty();
+ }
+
@Override
protected MoreObjects.ToStringHelper addToStringAttributes(
final MoreObjects.ToStringHelper toStringHelper) {
}
private static RevisionSourceIdentifier createIdentifierFromSourceName(final String sourceName) {
- final Matcher matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName);
+ final var matcher = RFC6020_RECOMMENDED_FILENAME_PATTERN.matcher(sourceName);
if (matcher.matches()) {
return RevisionSourceIdentifier.create(matcher.group(1), Revision.of(matcher.group(2)));
}