Merge "CPS Delta API: Update action for delta service"
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / YangParser.java
1 /*
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2024 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.utils;
22
23 import io.micrometer.core.annotation.Timed;
24 import lombok.RequiredArgsConstructor;
25 import org.onap.cps.api.impl.YangTextSchemaSourceSetCache;
26 import org.onap.cps.spi.exceptions.DataValidationException;
27 import org.onap.cps.spi.model.Anchor;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
30 import org.springframework.stereotype.Service;
31
32 @Service
33 @RequiredArgsConstructor
34 public class YangParser {
35
36     private final YangParserHelper yangParserHelper;
37     private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
38
39     /**
40      * Parses data into (normalized) ContainerNode according to schema context for the given anchor.
41      *
42      * @param nodeData  data string
43      * @param anchor    the anchor for the node data
44      * @return the NormalizedNode object
45      */
46     @Timed(value = "cps.utils.yangparser.nodedata.with.parent.parse",
47         description = "Time taken to parse node data with a parent")
48     public ContainerNode parseData(final ContentType contentType,
49                                    final String nodeData,
50                                    final Anchor anchor,
51                                    final String parentNodeXpath) {
52         final SchemaContext schemaContext = getSchemaContext(anchor);
53         try {
54             return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
55         } catch (final DataValidationException e) {
56             invalidateCache(anchor);
57         }
58         return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
59     }
60
61     private SchemaContext getSchemaContext(final Anchor anchor) {
62         return yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(),
63             anchor.getSchemaSetName()).getSchemaContext();
64     }
65
66     private void invalidateCache(final Anchor anchor) {
67         yangTextSchemaSourceSetCache.removeFromCache(anchor.getDataspaceName(), anchor.getSchemaSetName());
68     }
69
70 }