Merge "Refactor WriteSubJobSpec to make use of test REST endpoint of write job"
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / YangParser.java
1 /*
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.utils;
23
24 import static org.onap.cps.utils.YangParserHelper.VALIDATE_AND_PARSE;
25 import static org.onap.cps.utils.YangParserHelper.VALIDATE_ONLY;
26
27 import io.micrometer.core.annotation.Timed;
28 import java.util.Map;
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;
38
39 @Slf4j
40 @Service
41 @RequiredArgsConstructor
42 public class YangParser {
43
44     private final YangParserHelper yangParserHelper;
45     private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
46     private final TimedYangTextSchemaSourceSetBuilder timedYangTextSchemaSourceSetBuilder;
47
48     /**
49      * Parses data into (normalized) ContainerNode according to schema context for the given anchor.
50      *
51      * @param nodeData  data string
52      * @param anchor    the anchor for the node data
53      * @return the NormalizedNode object
54      */
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,
59                                    final Anchor anchor,
60                                    final String parentNodeXpath) {
61         final SchemaContext schemaContext = getSchemaContext(anchor);
62         try {
63             return yangParserHelper
64                     .parseData(contentType, nodeData, schemaContext, parentNodeXpath, VALIDATE_AND_PARSE);
65         } catch (final DataValidationException e) {
66             invalidateCache(anchor);
67         }
68         return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath, VALIDATE_AND_PARSE);
69     }
70
71     /**
72      * Parses data into (normalized) ContainerNode according to schema context for the given yang resource.
73      *
74      * @param nodeData                    data string
75      * @param yangResourceContentPerName  yang resource content per name
76      * @return                            the NormalizedNode object
77      */
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);
86     }
87
88     /**
89      * Parses data to validate it, using the schema context for given anchor.
90      *
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
96      */
97     public void validateData(final ContentType contentType,
98                              final String nodeData,
99                              final Anchor anchor,
100                              final String parentNodeXpath) {
101         final SchemaContext schemaContext = getSchemaContext(anchor);
102         try {
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,
107                     e.getMessage());
108         }
109         yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath, VALIDATE_ONLY);
110     }
111
112     private SchemaContext getSchemaContext(final Anchor anchor) {
113         return yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(),
114             anchor.getSchemaSetName()).getSchemaContext();
115     }
116
117     private SchemaContext getSchemaContext(final Map<String, String> yangResourceContentPerName) {
118         return timedYangTextSchemaSourceSetBuilder.getYangTextSchemaSourceSet(yangResourceContentPerName)
119                 .getSchemaContext();
120     }
121
122     private void invalidateCache(final Anchor anchor) {
123         yangTextSchemaSourceSetCache.removeFromCache(anchor.getDataspaceName(), anchor.getSchemaSetName());
124     }
125
126 }