2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2022 Nordix Foundation
4 * Modifications Copyright (C) 2021 Bell Canada.
5 * Modifications Copyright (C) 2021 Pantheon.tech
6 * Modifications Copyright (C) 2022 TechMahindra Ltd.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
20 * SPDX-License-Identifier: Apache-2.0
21 * ============LICENSE_END=========================================================
24 package org.onap.cps.utils;
26 import com.google.gson.JsonSyntaxException;
27 import com.google.gson.stream.JsonReader;
28 import java.io.IOException;
29 import java.io.StringReader;
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Optional;
36 import java.util.stream.Collectors;
37 import lombok.AccessLevel;
38 import lombok.NoArgsConstructor;
39 import lombok.extern.slf4j.Slf4j;
40 import org.onap.cps.spi.exceptions.DataValidationException;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
46 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
47 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
48 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
49 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
50 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
51 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
52 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
54 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
57 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
60 @NoArgsConstructor(access = AccessLevel.PRIVATE)
61 public class YangUtils {
63 private static final String XPATH_DELIMITER_REGEX = "\\/";
64 private static final String XPATH_NODE_KEY_ATTRIBUTES_REGEX = "\\[.*?\\]";
67 * Parses jsonData into Collection of NormalizedNode according to given schema context.
69 * @param jsonData json data as string
70 * @param schemaContext schema context describing associated data model
71 * @return the Collection of NormalizedNode object
73 public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext) {
74 return parseJsonData(jsonData, schemaContext, Optional.empty());
78 * Parses jsonData into Collection of NormalizedNode according to given schema context.
80 * @param jsonData json data fragment as string
81 * @param schemaContext schema context describing associated data model
82 * @param parentNodeXpath the xpath referencing the parent node current data fragment belong to
83 * @return the NormalizedNode object
85 public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext,
86 final String parentNodeXpath) {
87 final Collection<QName> dataSchemaNodeIdentifiers =
88 getDataSchemaNodeIdentifiersByXpath(parentNodeXpath, schemaContext);
89 return parseJsonData(jsonData, schemaContext, Optional.of(dataSchemaNodeIdentifiers));
92 private static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext,
93 final Optional<Collection<QName>> dataSchemaNodeIdentifiers) {
94 final JSONCodecFactory jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
95 .getShared((EffectiveModelContext) schemaContext);
96 final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> dataContainerNodeBuilder =
97 Builders.containerBuilder()
98 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()));
99 final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
100 .from(dataContainerNodeBuilder);
101 final JsonReader jsonReader = new JsonReader(new StringReader(jsonData));
102 final JsonParserStream jsonParserStream;
104 if (dataSchemaNodeIdentifiers.isPresent()) {
105 final EffectiveModelContext effectiveModelContext = ((EffectiveModelContext) schemaContext);
106 final EffectiveStatementInference effectiveStatementInference =
107 SchemaInferenceStack.of(effectiveModelContext,
108 SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers.get())).toInference();
110 JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory, effectiveStatementInference);
112 jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory);
116 jsonParserStream.parse(jsonReader);
117 jsonParserStream.close();
118 } catch (final JsonSyntaxException exception) {
119 throw new DataValidationException(
120 "Failed to parse json data: " + jsonData, exception.getMessage(), exception);
121 } catch (final IOException | IllegalStateException illegalStateException) {
122 throw new DataValidationException(
123 "Failed to parse json data. Unsupported xpath or json data:" + jsonData, illegalStateException
124 .getMessage(), illegalStateException);
126 return dataContainerNodeBuilder.build();
130 * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument).
132 * @param nodeIdentifier the NodeIdentifier
135 public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) {
136 final StringBuilder xpathBuilder = new StringBuilder();
137 xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName());
139 if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
140 xpathBuilder.append(getKeyAttributesStatement(
141 (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier));
143 return xpathBuilder.toString();
147 private static String getKeyAttributesStatement(
148 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
149 final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
151 final String name = entry.getKey().getLocalName();
152 final String value = String.valueOf(entry.getValue()).replace("'", "\\'");
153 return String.format("@%s='%s'", name, value);
155 ).collect(Collectors.toList());
157 if (keyAttributes.isEmpty()) {
160 Collections.sort(keyAttributes);
161 return "[" + String.join(" and ", keyAttributes) + "]";
165 private static Collection<QName> getDataSchemaNodeIdentifiersByXpath(final String parentNodeXpath,
166 final SchemaContext schemaContext) {
167 final String[] xpathNodeIdSequence = xpathToNodeIdSequence(parentNodeXpath);
168 return findDataSchemaNodeIdentifiersByXpathNodeIdSequence(xpathNodeIdSequence, schemaContext.getChildNodes(),
172 private static String[] xpathToNodeIdSequence(final String xpath) {
173 final String[] xpathNodeIdSequence = Arrays.stream(xpath
174 .replaceAll(XPATH_NODE_KEY_ATTRIBUTES_REGEX, "")
175 .split(XPATH_DELIMITER_REGEX))
176 .filter(identifier -> !identifier.isEmpty())
177 .toArray(String[]::new);
178 if (xpathNodeIdSequence.length < 1) {
179 throw new DataValidationException("Invalid xpath.", "Xpath contains no node identifiers.");
181 return xpathNodeIdSequence;
184 private static Collection<QName> findDataSchemaNodeIdentifiersByXpathNodeIdSequence(
185 final String[] xpathNodeIdSequence,
186 final Collection<? extends DataSchemaNode> dataSchemaNodes,
187 final Collection<QName> dataSchemaNodeIdentifiers) {
188 final String currentXpathNodeId = xpathNodeIdSequence[0];
189 final DataSchemaNode currentDataSchemaNode = dataSchemaNodes.stream()
190 .filter(dataSchemaNode -> currentXpathNodeId.equals(dataSchemaNode.getQName().getLocalName()))
191 .findFirst().orElseThrow(() -> schemaNodeNotFoundException(currentXpathNodeId));
192 dataSchemaNodeIdentifiers.add(currentDataSchemaNode.getQName());
193 if (xpathNodeIdSequence.length <= 1) {
194 return dataSchemaNodeIdentifiers;
196 if (currentDataSchemaNode instanceof DataNodeContainer) {
197 return findDataSchemaNodeIdentifiersByXpathNodeIdSequence(
198 getNextLevelXpathNodeIdSequence(xpathNodeIdSequence),
199 ((DataNodeContainer) currentDataSchemaNode).getChildNodes(),
200 dataSchemaNodeIdentifiers);
202 throw schemaNodeNotFoundException(xpathNodeIdSequence[1]);
205 private static String[] getNextLevelXpathNodeIdSequence(final String[] xpathNodeIdSequence) {
206 final String[] nextXpathNodeIdSequence = new String[xpathNodeIdSequence.length - 1];
207 System.arraycopy(xpathNodeIdSequence, 1, nextXpathNodeIdSequence, 0, nextXpathNodeIdSequence.length);
208 return nextXpathNodeIdSequence;
211 private static DataValidationException schemaNodeNotFoundException(final String schemaNodeIdentifier) {
212 return new DataValidationException("Invalid xpath.",
213 String.format("No schema node was found for xpath identifier '%s'.", schemaNodeIdentifier));