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 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.utils;
25 import com.google.gson.JsonSyntaxException;
26 import com.google.gson.stream.JsonReader;
27 import java.io.IOException;
28 import java.io.StringReader;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Optional;
35 import java.util.stream.Collectors;
36 import lombok.AccessLevel;
37 import lombok.NoArgsConstructor;
38 import lombok.extern.slf4j.Slf4j;
39 import org.onap.cps.spi.exceptions.DataValidationException;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
43 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
44 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
45 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
46 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
47 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
48 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
49 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
50 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
51 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
52 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
53 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
54 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
55 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
58 @NoArgsConstructor(access = AccessLevel.PRIVATE)
59 public class YangUtils {
61 private static final String XPATH_DELIMITER_REGEX = "\\/";
62 private static final String XPATH_NODE_KEY_ATTRIBUTES_REGEX = "\\[.*?\\]";
65 * Parses jsonData into NormalizedNode according to given schema context.
67 * @param jsonData json data as string
68 * @param schemaContext schema context describing associated data model
69 * @return the NormalizedNode object
71 public static NormalizedNode parseJsonData(final String jsonData, final SchemaContext schemaContext) {
72 return parseJsonData(jsonData, schemaContext, Optional.empty());
76 * Parses jsonData into NormalizedNode according to given schema context.
78 * @param jsonData json data fragment as string
79 * @param schemaContext schema context describing associated data model
80 * @param parentNodeXpath the xpath referencing the parent node current data fragment belong to
81 * @return the NormalizedNode object
83 public static NormalizedNode parseJsonData(final String jsonData, final SchemaContext schemaContext,
84 final String parentNodeXpath) {
85 final Collection<QName> dataSchemaNodeIdentifiers =
86 getDataSchemaNodeIdentifiersByXpath(parentNodeXpath, schemaContext);
87 return parseJsonData(jsonData, schemaContext, Optional.of(dataSchemaNodeIdentifiers));
90 private static NormalizedNode parseJsonData(final String jsonData, final SchemaContext schemaContext,
91 final Optional<Collection<QName>> dataSchemaNodeIdentifiers) {
92 final JSONCodecFactory jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
93 .getShared((EffectiveModelContext) schemaContext);
94 final NormalizedNodeResult normalizedNodeResult = new NormalizedNodeResult();
95 final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
96 .from(normalizedNodeResult);
97 final JsonReader jsonReader = new JsonReader(new StringReader(jsonData));
98 final JsonParserStream jsonParserStream;
100 if (dataSchemaNodeIdentifiers.isPresent()) {
101 final EffectiveModelContext effectiveModelContext = ((EffectiveModelContext) schemaContext);
102 final EffectiveStatementInference effectiveStatementInference =
103 SchemaInferenceStack.of(effectiveModelContext,
104 SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers.get())).toInference();
106 JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory, effectiveStatementInference);
108 jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory);
112 jsonParserStream.parse(jsonReader);
113 jsonParserStream.close();
114 } catch (final JsonSyntaxException exception) {
115 throw new DataValidationException(
116 "Failed to parse json data: " + jsonData, exception.getMessage(), exception);
117 } catch (final IOException | IllegalStateException illegalStateException) {
118 throw new DataValidationException(
119 "Failed to parse json data. Unsupported xpath or json data:" + jsonData, illegalStateException
120 .getMessage(), illegalStateException);
122 return normalizedNodeResult.getResult();
126 * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument).
128 * @param nodeIdentifier the NodeIdentifier
131 public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) {
132 final StringBuilder xpathBuilder = new StringBuilder();
133 xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName());
135 if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
136 xpathBuilder.append(getKeyAttributesStatement(
137 (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier));
139 return xpathBuilder.toString();
143 private static String getKeyAttributesStatement(
144 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
145 final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
147 final String name = entry.getKey().getLocalName();
148 final String value = String.valueOf(entry.getValue()).replace("'", "\\'");
149 return String.format("@%s='%s'", name, value);
151 ).collect(Collectors.toList());
153 if (keyAttributes.isEmpty()) {
156 Collections.sort(keyAttributes);
157 return "[" + String.join(" and ", keyAttributes) + "]";
161 private static Collection<QName> getDataSchemaNodeIdentifiersByXpath(final String parentNodeXpath,
162 final SchemaContext schemaContext) {
163 final String[] xpathNodeIdSequence = xpathToNodeIdSequence(parentNodeXpath);
164 return findDataSchemaNodeIdentifiersByXpathNodeIdSequence(xpathNodeIdSequence, schemaContext.getChildNodes(),
168 private static String[] xpathToNodeIdSequence(final String xpath) {
169 final String[] xpathNodeIdSequence = Arrays.stream(xpath
170 .replaceAll(XPATH_NODE_KEY_ATTRIBUTES_REGEX, "")
171 .split(XPATH_DELIMITER_REGEX))
172 .filter(identifier -> !identifier.isEmpty())
173 .toArray(String[]::new);
174 if (xpathNodeIdSequence.length < 1) {
175 throw new DataValidationException("Invalid xpath.", "Xpath contains no node identifiers.");
177 return xpathNodeIdSequence;
180 private static Collection<QName> findDataSchemaNodeIdentifiersByXpathNodeIdSequence(
181 final String[] xpathNodeIdSequence,
182 final Collection<? extends DataSchemaNode> dataSchemaNodes,
183 final Collection<QName> dataSchemaNodeIdentifiers) {
184 final String currentXpathNodeId = xpathNodeIdSequence[0];
185 final DataSchemaNode currentDataSchemaNode = dataSchemaNodes.stream()
186 .filter(dataSchemaNode -> currentXpathNodeId.equals(dataSchemaNode.getQName().getLocalName()))
187 .findFirst().orElseThrow(() -> schemaNodeNotFoundException(currentXpathNodeId));
188 dataSchemaNodeIdentifiers.add(currentDataSchemaNode.getQName());
189 if (xpathNodeIdSequence.length <= 1) {
190 return dataSchemaNodeIdentifiers;
192 if (currentDataSchemaNode instanceof DataNodeContainer) {
193 return findDataSchemaNodeIdentifiersByXpathNodeIdSequence(
194 getNextLevelXpathNodeIdSequence(xpathNodeIdSequence),
195 ((DataNodeContainer) currentDataSchemaNode).getChildNodes(),
196 dataSchemaNodeIdentifiers);
198 throw schemaNodeNotFoundException(xpathNodeIdSequence[1]);
201 private static String[] getNextLevelXpathNodeIdSequence(final String[] xpathNodeIdSequence) {
202 final String[] nextXpathNodeIdSequence = new String[xpathNodeIdSequence.length - 1];
203 System.arraycopy(xpathNodeIdSequence, 1, nextXpathNodeIdSequence, 0, nextXpathNodeIdSequence.length);
204 return nextXpathNodeIdSequence;
207 private static DataValidationException schemaNodeNotFoundException(final String schemaNodeIdentifier) {
208 return new DataValidationException("Invalid xpath.",
209 String.format("No schema node was found for xpath identifier '%s'.", schemaNodeIdentifier));