Merge "Use PollingConditions to improve intermittent test failure"
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / YangUtils.java
1 /*
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
12  *
13  *        http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
21  *  SPDX-License-Identifier: Apache-2.0
22  *  ============LICENSE_END=========================================================
23  */
24
25 package org.onap.cps.utils;
26
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;
33
34 @NoArgsConstructor(access = AccessLevel.PRIVATE)
35 public class YangUtils {
36
37     /**
38      * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument).
39      *
40      * @param nodeIdentifier the NodeIdentifier
41      * @return a xpath
42      */
43     public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) {
44         final StringBuilder xpathBuilder = new StringBuilder();
45         xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName());
46
47         if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
48             xpathBuilder.append(getKeyAttributesStatement(
49                 (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier));
50         }
51         return xpathBuilder.toString();
52     }
53
54     private static String getKeyAttributesStatement(
55             final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
56         final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
57                 entry -> {
58                     final String name = entry.getKey().getLocalName();
59                     final String value = String.valueOf(entry.getValue()).replace("'", "''");
60                     return String.format("@%s='%s'", name, value);
61                 }
62         ).collect(Collectors.toList());
63
64         if (keyAttributes.isEmpty()) {
65             return "";
66         } else {
67             Collections.sort(keyAttributes);
68             return "[" + String.join(" and ", keyAttributes) + "]";
69         }
70     }
71
72 }