2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024-2025 Nordix Foundation.
4 * Modifications Copyright (C) 2024 TechMahindra Ltd.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.utils;
24 import static org.onap.cps.utils.YangParserHelper.VALIDATE_AND_PARSE;
25 import static org.onap.cps.utils.YangParserHelper.VALIDATE_ONLY;
27 import io.micrometer.core.annotation.Timed;
29 import lombok.RequiredArgsConstructor;
30 import lombok.extern.slf4j.Slf4j;
31 import org.onap.cps.api.exceptions.DataValidationException;
32 import org.onap.cps.api.model.Anchor;
33 import org.onap.cps.impl.YangTextSchemaSourceSetCache;
34 import org.onap.cps.yang.TimedYangTextSchemaSourceSetBuilder;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
37 import org.springframework.stereotype.Service;
41 @RequiredArgsConstructor
42 public class YangParser {
44 private final YangParserHelper yangParserHelper;
45 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
46 private final TimedYangTextSchemaSourceSetBuilder timedYangTextSchemaSourceSetBuilder;
49 * Parses data into (normalized) ContainerNode according to schema context for the given anchor.
51 * @param nodeData data string
52 * @param anchor the anchor for the node data
53 * @return the NormalizedNode object
55 @Timed(value = "cps.utils.yangparser.nodedata.with.parent.parse",
56 description = "Time taken to parse node data with a parent")
57 public ContainerNode parseData(final ContentType contentType,
58 final String nodeData,
60 final String parentNodeXpath) {
61 final SchemaContext schemaContext = getSchemaContext(anchor);
63 return yangParserHelper
64 .parseData(contentType, nodeData, schemaContext, parentNodeXpath, VALIDATE_AND_PARSE);
65 } catch (final DataValidationException e) {
66 invalidateCache(anchor);
68 return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath, VALIDATE_AND_PARSE);
72 * Parses data into (normalized) ContainerNode according to schema context for the given yang resource.
74 * @param nodeData data string
75 * @param yangResourceContentPerName yang resource content per name
76 * @return the NormalizedNode object
78 @Timed(value = "cps.utils.yangparser.nodedata.with.parent.with.yangResourceMap.parse",
79 description = "Time taken to parse node data with a parent")
80 public ContainerNode parseData(final ContentType contentType,
81 final String nodeData,
82 final Map<String, String> yangResourceContentPerName,
83 final String parentNodeXpath) {
84 final SchemaContext schemaContext = getSchemaContext(yangResourceContentPerName);
85 return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath, VALIDATE_AND_PARSE);
89 * Parses data to validate it, using the schema context for given anchor.
91 * @param anchor the anchor used for node data validation
92 * @param parentNodeXpath the xpath of the parent node
93 * @param nodeData JSON or XML data string to validate
94 * @param contentType the content type of the data (e.g., JSON or XML)
95 * @throws DataValidationException if validation fails
97 public void validateData(final ContentType contentType,
98 final String nodeData,
100 final String parentNodeXpath) {
101 final SchemaContext schemaContext = getSchemaContext(anchor);
103 yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath, VALIDATE_ONLY);
104 } catch (final DataValidationException e) {
105 invalidateCache(anchor);
106 log.error("Data validation failed for anchor: {}, xpath: {}, details: {}", anchor, parentNodeXpath,
109 yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath, VALIDATE_ONLY);
112 private SchemaContext getSchemaContext(final Anchor anchor) {
113 return yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(),
114 anchor.getSchemaSetName()).getSchemaContext();
117 private SchemaContext getSchemaContext(final Map<String, String> yangResourceContentPerName) {
118 return timedYangTextSchemaSourceSetBuilder.getYangTextSchemaSourceSet(yangResourceContentPerName)
122 private void invalidateCache(final Anchor anchor) {
123 yangTextSchemaSourceSetCache.removeFromCache(anchor.getDataspaceName(), anchor.getSchemaSetName());