2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2024 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 io.micrometer.core.annotation.Timed;
26 import lombok.RequiredArgsConstructor;
27 import lombok.extern.slf4j.Slf4j;
28 import org.onap.cps.api.impl.YangTextSchemaSourceSetCache;
29 import org.onap.cps.spi.exceptions.DataValidationException;
30 import org.onap.cps.spi.model.Anchor;
31 import org.onap.cps.yang.TimedYangTextSchemaSourceSetBuilder;
32 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
33 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
34 import org.springframework.stereotype.Service;
38 @RequiredArgsConstructor
39 public class YangParser {
41 private final YangParserHelper yangParserHelper;
42 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
43 private final TimedYangTextSchemaSourceSetBuilder timedYangTextSchemaSourceSetBuilder;
46 * Parses data into (normalized) ContainerNode according to schema context for the given anchor.
48 * @param nodeData data string
49 * @param anchor the anchor for the node data
50 * @return the NormalizedNode object
52 @Timed(value = "cps.utils.yangparser.nodedata.with.parent.parse",
53 description = "Time taken to parse node data with a parent")
54 public ContainerNode parseData(final ContentType contentType,
55 final String nodeData,
57 final String parentNodeXpath) {
58 final SchemaContext schemaContext = getSchemaContext(anchor);
60 return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
61 } catch (final DataValidationException e) {
62 invalidateCache(anchor);
64 return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
68 * Parses data into (normalized) ContainerNode according to schema context for the given yang resource.
70 * @param nodeData data string
71 * @param yangResourcesNameToContentMap yang resource to content map
72 * @return the NormalizedNode object
74 @Timed(value = "cps.utils.yangparser.nodedata.with.parent.with.yangResourceMap.parse",
75 description = "Time taken to parse node data with a parent")
76 public ContainerNode parseData(final ContentType contentType,
77 final String nodeData,
78 final Map<String, String> yangResourcesNameToContentMap,
79 final String parentNodeXpath) {
80 final SchemaContext schemaContext = getSchemaContext(yangResourcesNameToContentMap);
81 return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
84 private SchemaContext getSchemaContext(final Anchor anchor) {
85 return yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(),
86 anchor.getSchemaSetName()).getSchemaContext();
89 private SchemaContext getSchemaContext(final Map<String, String> yangResourcesNameToContentMap) {
90 return timedYangTextSchemaSourceSetBuilder.getYangTextSchemaSourceSet(yangResourcesNameToContentMap)
94 private void invalidateCache(final Anchor anchor) {
95 yangTextSchemaSourceSetCache.removeFromCache(anchor.getDataspaceName(), anchor.getSchemaSetName());