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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.utils;
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;
33 @RequiredArgsConstructor
34 public class YangParser {
36 private final YangParserHelper yangParserHelper;
37 private final YangTextSchemaSourceSetCache yangTextSchemaSourceSetCache;
40 * Parses data into (normalized) ContainerNode according to schema context for the given anchor.
42 * @param nodeData data string
43 * @param anchor the anchor for the node data
44 * @return the NormalizedNode object
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,
51 final String parentNodeXpath) {
52 final SchemaContext schemaContext = getSchemaContext(anchor);
54 return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
55 } catch (final DataValidationException e) {
56 invalidateCache(anchor);
58 return yangParserHelper.parseData(contentType, nodeData, schemaContext, parentNodeXpath);
61 private SchemaContext getSchemaContext(final Anchor anchor) {
62 return yangTextSchemaSourceSetCache.get(anchor.getDataspaceName(),
63 anchor.getSchemaSetName()).getSchemaContext();
66 private void invalidateCache(final Anchor anchor) {
67 yangTextSchemaSourceSetCache.removeFromCache(anchor.getDataspaceName(), anchor.getSchemaSetName());