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 * Modifications Copyright (C) 2022 Deutsche Telekom AG
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * SPDX-License-Identifier: Apache-2.0
22 * ============LICENSE_END=========================================================
25 package org.onap.cps.utils;
27 import com.google.gson.JsonSyntaxException;
28 import com.google.gson.stream.JsonReader;
29 import java.io.IOException;
30 import java.io.StringReader;
31 import java.net.URISyntaxException;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.HashMap;
36 import java.util.Iterator;
37 import java.util.List;
39 import java.util.Optional;
40 import java.util.stream.Collectors;
41 import javax.xml.stream.XMLInputFactory;
42 import javax.xml.stream.XMLStreamException;
43 import javax.xml.stream.XMLStreamReader;
44 import lombok.AccessLevel;
45 import lombok.NoArgsConstructor;
46 import lombok.extern.slf4j.Slf4j;
47 import org.onap.cps.cpspath.parser.CpsPathUtil;
48 import org.onap.cps.cpspath.parser.PathParsingException;
49 import org.onap.cps.spi.exceptions.DataValidationException;
50 import org.opendaylight.yangtools.yang.common.QName;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
52 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
53 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
54 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
55 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
56 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
57 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
58 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
59 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
60 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
61 import org.opendaylight.yangtools.yang.data.codec.xml.XmlParserStream;
62 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
63 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
64 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
65 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
66 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
67 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
68 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
69 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
70 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
71 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
72 import org.xml.sax.SAXException;
75 @NoArgsConstructor(access = AccessLevel.PRIVATE)
76 public class YangUtils {
79 * Parses data into Collection of NormalizedNode according to given schema context.
81 * @param nodeData data string
82 * @param schemaContext schema context describing associated data model
83 * @return the NormalizedNode object
85 public static ContainerNode parseData(final ContentType contentType, final String nodeData,
86 final SchemaContext schemaContext) {
87 if (contentType == ContentType.JSON) {
88 return parseJsonData(nodeData, schemaContext, Optional.empty());
90 return parseXmlData(XmlFileUtils.prepareXmlContent(nodeData, schemaContext), schemaContext,
95 * Parses data into NormalizedNode according to given schema context.
97 * @param nodeData data string
98 * @param schemaContext schema context describing associated data model
99 * @return the NormalizedNode object
101 public static ContainerNode parseData(final ContentType contentType, final String nodeData,
102 final SchemaContext schemaContext, final String parentNodeXpath) {
103 final DataSchemaNode parentSchemaNode =
104 (DataSchemaNode) getDataSchemaNodeAndIdentifiersByXpath(parentNodeXpath, schemaContext)
105 .get("dataSchemaNode");
106 final Collection<QName> dataSchemaNodeIdentifiers =
107 (Collection<QName>) getDataSchemaNodeAndIdentifiersByXpath(parentNodeXpath, schemaContext)
108 .get("dataSchemaNodeIdentifiers");
109 if (contentType == ContentType.JSON) {
110 return parseJsonData(nodeData, schemaContext, Optional.of(dataSchemaNodeIdentifiers));
112 return parseXmlData(XmlFileUtils.prepareXmlContent(nodeData, parentSchemaNode, parentNodeXpath), schemaContext,
113 Optional.of(dataSchemaNodeIdentifiers));
117 * Parses data into Collection of NormalizedNode according to given schema context.
119 * @param jsonData json data as string
120 * @param schemaContext schema context describing associated data model
121 * @return the Collection of NormalizedNode object
123 public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext) {
124 return parseJsonData(jsonData, schemaContext, Optional.empty());
128 * Parses jsonData into Collection of NormalizedNode according to given schema context.
130 * @param jsonData json data fragment as string
131 * @param schemaContext schema context describing associated data model
132 * @param parentNodeXpath the xpath referencing the parent node current data fragment belong to
133 * @return the NormalizedNode object
135 public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext,
136 final String parentNodeXpath) {
137 final Collection<QName> dataSchemaNodeIdentifiers =
138 (Collection<QName>) getDataSchemaNodeAndIdentifiersByXpath(parentNodeXpath, schemaContext)
139 .get("dataSchemaNodeIdentifiers");
140 return parseJsonData(jsonData, schemaContext, Optional.of(dataSchemaNodeIdentifiers));
143 private static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext,
144 final Optional<Collection<QName>> dataSchemaNodeIdentifiers) {
145 final JSONCodecFactory jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
146 .getShared((EffectiveModelContext) schemaContext);
147 final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> dataContainerNodeBuilder =
148 Builders.containerBuilder()
149 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()));
150 final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
151 .from(dataContainerNodeBuilder);
152 final JsonReader jsonReader = new JsonReader(new StringReader(jsonData));
153 final JsonParserStream jsonParserStream;
155 if (dataSchemaNodeIdentifiers.isPresent()) {
156 final EffectiveModelContext effectiveModelContext = ((EffectiveModelContext) schemaContext);
157 final EffectiveStatementInference effectiveStatementInference =
158 SchemaInferenceStack.of(effectiveModelContext,
159 SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers.get())).toInference();
161 JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory, effectiveStatementInference);
163 jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory);
167 jsonParserStream.parse(jsonReader);
168 jsonParserStream.close();
169 } catch (final IOException | JsonSyntaxException exception) {
170 throw new DataValidationException(
171 "Failed to parse json data: " + jsonData, exception.getMessage(), exception);
172 } catch (final IllegalStateException | IllegalArgumentException exception) {
173 throw new DataValidationException(
174 "Failed to parse json data. Unsupported xpath or json data:" + jsonData, exception
175 .getMessage(), exception);
177 return dataContainerNodeBuilder.build();
180 private static ContainerNode parseXmlData(final String xmlData, final SchemaContext schemaContext,
181 final Optional<Collection<QName>> dataSchemaNodeIdentifiers) {
182 final XMLInputFactory factory = XMLInputFactory.newInstance();
183 factory.setProperty(XMLInputFactory.SUPPORT_DTD, false);
184 final NormalizedNodeResult normalizedNodeResult = new NormalizedNodeResult();
185 final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
186 .from(normalizedNodeResult);
188 final XmlParserStream xmlParser;
189 final EffectiveModelContext effectiveModelContext = ((EffectiveModelContext) schemaContext);
191 if (dataSchemaNodeIdentifiers.isPresent()) {
192 final EffectiveStatementInference effectiveStatementInference =
193 SchemaInferenceStack.of(effectiveModelContext,
194 SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers.get())).toInference();
195 xmlParser = XmlParserStream.create(normalizedNodeStreamWriter, effectiveStatementInference);
197 xmlParser = XmlParserStream.create(normalizedNodeStreamWriter, effectiveModelContext);
201 final XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(xmlData));
202 xmlParser.parse(reader);
204 } catch (final XMLStreamException | URISyntaxException | IOException
205 | SAXException | NullPointerException exception) {
206 throw new DataValidationException(
207 "Failed to parse xml data: " + xmlData, exception.getMessage(), exception);
209 final NormalizedNode normalizedNode = getFirstChildXmlRoot(normalizedNodeResult.getResult());
210 return Builders.containerBuilder().withChild((DataContainerChild) normalizedNode)
211 .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName())).build();
215 * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument).
217 * @param nodeIdentifier the NodeIdentifier
220 public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) {
221 final StringBuilder xpathBuilder = new StringBuilder();
222 xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName());
224 if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
225 xpathBuilder.append(getKeyAttributesStatement(
226 (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier));
228 return xpathBuilder.toString();
232 private static String getKeyAttributesStatement(
233 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
234 final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
236 final String name = entry.getKey().getLocalName();
237 final String value = String.valueOf(entry.getValue()).replace("'", "\\'");
238 return String.format("@%s='%s'", name, value);
240 ).collect(Collectors.toList());
242 if (keyAttributes.isEmpty()) {
245 Collections.sort(keyAttributes);
246 return "[" + String.join(" and ", keyAttributes) + "]";
250 private static Map<String, Object> getDataSchemaNodeAndIdentifiersByXpath(final String parentNodeXpath,
251 final SchemaContext schemaContext) {
252 final String[] xpathNodeIdSequence = xpathToNodeIdSequence(parentNodeXpath);
253 return findDataSchemaNodeAndIdentifiersByXpathNodeIdSequence(xpathNodeIdSequence, schemaContext.getChildNodes(),
257 private static String[] xpathToNodeIdSequence(final String xpath) {
259 return CpsPathUtil.getXpathNodeIdSequence(xpath);
260 } catch (final PathParsingException pathParsingException) {
261 throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
262 pathParsingException);
266 private static Map<String, Object> findDataSchemaNodeAndIdentifiersByXpathNodeIdSequence(
267 final String[] xpathNodeIdSequence,
268 final Collection<? extends DataSchemaNode> dataSchemaNodes,
269 final Collection<QName> dataSchemaNodeIdentifiers) {
270 final String currentXpathNodeId = xpathNodeIdSequence[0];
271 final DataSchemaNode currentDataSchemaNode = dataSchemaNodes.stream()
272 .filter(dataSchemaNode -> currentXpathNodeId.equals(dataSchemaNode.getQName().getLocalName()))
273 .findFirst().orElseThrow(() -> schemaNodeNotFoundException(currentXpathNodeId));
274 dataSchemaNodeIdentifiers.add(currentDataSchemaNode.getQName());
275 if (xpathNodeIdSequence.length <= 1) {
276 final Map<String, Object> dataSchemaNodeAndIdentifiers =
278 dataSchemaNodeAndIdentifiers.put("dataSchemaNode", currentDataSchemaNode);
279 dataSchemaNodeAndIdentifiers.put("dataSchemaNodeIdentifiers", dataSchemaNodeIdentifiers);
280 return dataSchemaNodeAndIdentifiers;
282 if (currentDataSchemaNode instanceof DataNodeContainer) {
283 return findDataSchemaNodeAndIdentifiersByXpathNodeIdSequence(
284 getNextLevelXpathNodeIdSequence(xpathNodeIdSequence),
285 ((DataNodeContainer) currentDataSchemaNode).getChildNodes(),
286 dataSchemaNodeIdentifiers);
288 throw schemaNodeNotFoundException(xpathNodeIdSequence[1]);
291 private static String[] getNextLevelXpathNodeIdSequence(final String[] xpathNodeIdSequence) {
292 final String[] nextXpathNodeIdSequence = new String[xpathNodeIdSequence.length - 1];
293 System.arraycopy(xpathNodeIdSequence, 1, nextXpathNodeIdSequence, 0, nextXpathNodeIdSequence.length);
294 return nextXpathNodeIdSequence;
297 private static DataValidationException schemaNodeNotFoundException(final String schemaNodeIdentifier) {
298 return new DataValidationException("Invalid xpath.",
299 String.format("No schema node was found for xpath identifier '%s'.", schemaNodeIdentifier));
302 private static NormalizedNode getFirstChildXmlRoot(final NormalizedNode parent) {
303 final String rootNodeType = parent.getIdentifier().getNodeType().getLocalName();
304 final Collection<DataContainerChild> children = (Collection<DataContainerChild>) parent.body();
305 final Iterator<DataContainerChild> iterator = children.iterator();
306 NormalizedNode child = null;
307 while (iterator.hasNext()) {
308 child = iterator.next();
309 if (!child.getIdentifier().getNodeType().getLocalName().equals(rootNodeType)
310 && !(child instanceof LeafNode)) {
314 return getFirstChildXmlRoot(child);