2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation
4 * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * 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 com.google.gson.stream.JsonReader;
24 import java.io.IOException;
25 import java.io.StringReader;
26 import java.util.Collections;
27 import java.util.List;
28 import java.util.stream.Collectors;
29 import lombok.extern.slf4j.Slf4j;
30 import org.onap.cps.spi.exceptions.DataValidationException;
31 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
32 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
33 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
34 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
35 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
36 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
37 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
38 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 public class YangUtils {
46 // Private constructor fo security reasons
50 * Parse a string containing json data for a certain model (schemaContext).
52 * @param jsonData a string containing json data for the given model
53 * @param schemaContext the SchemaContext for the given data
54 * @return the NormalizedNode representing the json data
56 public static NormalizedNode<?, ?> parseJsonData(final String jsonData, final SchemaContext schemaContext) {
57 final JSONCodecFactory jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
58 .getShared(schemaContext);
59 final NormalizedNodeResult normalizedNodeResult = new NormalizedNodeResult();
60 final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
61 .from(normalizedNodeResult);
63 try (final JsonParserStream jsonParserStream = JsonParserStream
64 .create(normalizedNodeStreamWriter, jsonCodecFactory)) {
65 final JsonReader jsonReader = new JsonReader(new StringReader(jsonData));
66 jsonParserStream.parse(jsonReader);
68 } catch (final IOException e) {
69 throw new DataValidationException("Failed to parse json data.", String
70 .format("Exception occurred on parsing string %s.", jsonData), e);
72 return normalizedNodeResult.getResult();
76 * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument).
77 * @param nodeIdentifier the NodeIdentifier
80 public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) {
81 final StringBuilder xpathBuilder = new StringBuilder();
82 xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName());
84 if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
85 xpathBuilder.append(getKeyAttributesStatement(
86 (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier));
88 return xpathBuilder.toString();
91 private static String getKeyAttributesStatement(
92 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
93 final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
95 final String name = entry.getKey().getLocalName();
96 final String value = String.valueOf(entry.getValue()).replace("'", "\\'");
97 return String.format("@%s='%s'", name, value);
99 ).collect(Collectors.toList());
101 if (keyAttributes.isEmpty()) {
104 Collections.sort(keyAttributes);
105 return "[" + String.join(" and ", keyAttributes) + "]";