aboutsummaryrefslogtreecommitdiffstats
path: root/cps-service/src/main/java/org/onap/cps/yang/YangTextSchemaSourceSetBuilder.java
blob: ca907148dd3c5f86052cd6c3bee4c027c714f43f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
 *  ============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.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 *  SPDX-License-Identifier: Apache-2.0
 *  ============LICENSE_END=========================================================
 */

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 io.micrometer.core.annotation.Timed;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import lombok.NoArgsConstructor;
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.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;

@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<>();

    /**
     * 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 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();
    }

    /**
     * Validates if SchemaContext can be successfully built from given yang resources.
     *
     * @param yangResourceNameToContent the yang resources as map where key is name and value is content
     * @throws ModelValidationException if validation fails
     */
    public static void validate(final Map<String, String> yangResourceNameToContent) {
        generateSchemaContext(yangResourceNameToContent);
    }

    private static class YangTextSchemaSourceSetImpl implements YangTextSchemaSourceSet {

        private final SchemaContext schemaContext;

        private YangTextSchemaSourceSetImpl(final SchemaContext schemaContext) {
            this.schemaContext = schemaContext;
        }

        @Override
        public List<ModuleReference> getModuleReferences() {
            return schemaContext.getModules().stream()
                .map(YangTextSchemaSourceSetImpl::toModuleReference)
                .collect(Collectors.toList());
        }

        private static ModuleReference toModuleReference(final Module module) {
            return ModuleReference.builder()
                .moduleName(module.getName())
                .namespace(module.getQNameModule().getNamespace().toString())
                .revision(module.getRevision().map(Revision::toString).orElse(null))
                .build();
        }

        @Override
        public SchemaContext getSchemaContext() {
            return schemaContext;
        }
    }

    /**
     * Parse and validate a string representing a yang model to generate a SchemaContext context.
     *
     * @param yangResourceNameToContent is a {@link Map} collection that contains the name of the model represented
     *                                  on yangModelContent as key and the yangModelContent as value.
     * @return the schema context
     */
    private static SchemaContext generateSchemaContext(final Map<String, String> yangResourceNameToContent) {
        final CrossSourceStatementReactor.BuildAction reactor = RFC7950Reactors.defaultReactor().newBuild();
        for (final YangTextSchemaSource yangTextSchemaSource : forResources(yangResourceNameToContent)) {
            final String resourceName = yangTextSchemaSource.getIdentifier().getName();
            try {
                reactor.addSource(YangStatementStreamSource.create(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 reactorException) {
            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),
                reactorException);
        }
    }

    private static List<YangTextSchemaSource> forResources(final Map<String, String> yangResourceNameToContent) {
        return yangResourceNameToContent.entrySet().stream()
            .map(entry -> toYangTextSchemaSource(entry.getKey(), entry.getValue()))
            .collect(Collectors.toList());
    }

    private static YangTextSchemaSource toYangTextSchemaSource(final String sourceName, final String source) {
        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) {
                return toStringHelper;
            }

            @Override
            public InputStream openStream() {
                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);
    }
}