Fix compile issue after facade introduction
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / YangParser.java
1 /*
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
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 io.micrometer.core.annotation.Timed;
25 import java.util.Map;
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;
35
36 @Slf4j
37 @Service
38 @RequiredArgsConstructor
39 public class YangParser {
40
41     private final YangParserHelper yangParserHelper;
42     private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
43     private final TimedYangTextSchemaSourceSetBuilder timedYangTextSchemaSourceSetBuilder;
44
45     /**
46      * Parses data into (normalized) ContainerNode according to schema context for the given anchor.
47      *
48      * @param nodeData  data string
49      * @param anchor    the anchor for the node data
50      * @return the NormalizedNode object
51      */
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,
56                                    final Anchor anchor,
57                                    final String parentNodeXpath) {
58         final SchemaContext schemaContext = getSchemaContext(anchor);
59         try {
60             return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
61         } catch (final DataValidationException e) {
62             invalidateCache(anchor);
63         }
64         return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
65     }
66
67     /**
68      * Parses data into (normalized) ContainerNode according to schema context for the given yang resource.
69      *
70      * @param nodeData                         data string
71      * @param yangResourcesNameToContentMap    yang resource to content map
72      * @return                                 the NormalizedNode object
73      */
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);
82     }
83
84     private SchemaContext getSchemaContext(final Anchor anchor) {
85         return yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(),
86             anchor.getSchemaSetName()).getSchemaContext();
87     }
88
89     private SchemaContext getSchemaContext(final Map<String, String> yangResourcesNameToContentMap) {
90         return timedYangTextSchemaSourceSetBuilder.getYangTextSchemaSourceSet(yangResourcesNameToContentMap)
91                 .getSchemaContext();
92     }
93
94     private void invalidateCache(final Anchor anchor) {
95         yangTextSchemaSourceSetCache.removeFromCache(anchor.getDataspaceName(), anchor.getSchemaSetName());
96     }
97
98 }