2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020-2024 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 java.util.Collections;
28 import java.util.List;
29 import java.util.stream.Collectors;
30 import lombok.AccessLevel;
31 import lombok.NoArgsConstructor;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public class YangUtils {
38 * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument).
40 * @param nodeIdentifier the NodeIdentifier
43 public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) {
44 final StringBuilder xpathBuilder = new StringBuilder();
45 xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName());
47 if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
48 xpathBuilder.append(getKeyAttributesStatement(
49 (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier));
51 return xpathBuilder.toString();
54 private static String getKeyAttributesStatement(
55 final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
56 final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
58 final String name = entry.getKey().getLocalName();
59 final String value = String.valueOf(entry.getValue()).replace("'", "''");
60 return String.format("@%s='%s'", name, value);
62 ).collect(Collectors.toList());
64 if (keyAttributes.isEmpty()) {
67 Collections.sort(keyAttributes);
68 return "[" + String.join(" and ", keyAttributes) + "]";