Add fix for posting nodes with xPath with '/'
[cps.git] / cps-service / src / main / java / org / onap / cps / utils / YangUtils.java
1 /*
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  *  ================================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  *
12  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *
20  *  SPDX-License-Identifier: Apache-2.0
21  *  ============LICENSE_END=========================================================
22  */
23
24 package org.onap.cps.utils;
25
26 import com.google.gson.JsonSyntaxException;
27 import com.google.gson.stream.JsonReader;
28 import java.io.IOException;
29 import java.io.StringReader;
30 import java.util.ArrayList;
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.cpspath.parser.CpsPathUtil;
40 import org.onap.cps.cpspath.parser.PathParsingException;
41 import org.onap.cps.spi.exceptions.DataValidationException;
42 import org.opendaylight.yangtools.yang.common.QName;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
44 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
45 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
46 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
47 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
48 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
49 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
50 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
51 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
52 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
53 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
54 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
55 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
56 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
57 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
58 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
59
60 @Slf4j
61 @NoArgsConstructor(access = AccessLevel.PRIVATE)
62 public class YangUtils {
63
64     /**
65      * Parses jsonData into Collection of NormalizedNode according to given schema context.
66      *
67      * @param jsonData      json data as string
68      * @param schemaContext schema context describing associated data model
69      * @return the Collection of NormalizedNode object
70      */
71     public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext) {
72         return parseJsonData(jsonData, schemaContext, Optional.empty());
73     }
74
75     /**
76      * Parses jsonData into Collection of NormalizedNode according to given schema context.
77      *
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
82      */
83     public static ContainerNode 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));
88     }
89
90     private static ContainerNode 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 DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> dataContainerNodeBuilder =
95                 Builders.containerBuilder()
96                         .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()));
97         final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
98                 .from(dataContainerNodeBuilder);
99         final JsonReader jsonReader = new JsonReader(new StringReader(jsonData));
100         final JsonParserStream jsonParserStream;
101
102         if (dataSchemaNodeIdentifiers.isPresent()) {
103             final EffectiveModelContext effectiveModelContext = ((EffectiveModelContext) schemaContext);
104             final EffectiveStatementInference effectiveStatementInference =
105                     SchemaInferenceStack.of(effectiveModelContext,
106                     SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers.get())).toInference();
107             jsonParserStream =
108                     JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory, effectiveStatementInference);
109         } else {
110             jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory);
111         }
112
113         try {
114             jsonParserStream.parse(jsonReader);
115             jsonParserStream.close();
116         } catch (final JsonSyntaxException exception) {
117             throw new DataValidationException(
118                 "Failed to parse json data: " + jsonData, exception.getMessage(), exception);
119         } catch (final IOException | IllegalStateException illegalStateException) {
120             throw new DataValidationException(
121                 "Failed to parse json data. Unsupported xpath or json data:" + jsonData, illegalStateException
122                 .getMessage(), illegalStateException);
123         }
124         return dataContainerNodeBuilder.build();
125     }
126
127     /**
128      * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument).
129      *
130      * @param nodeIdentifier the NodeIdentifier
131      * @return an xpath
132      */
133     public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) {
134         final StringBuilder xpathBuilder = new StringBuilder();
135         xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName());
136
137         if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
138             xpathBuilder.append(getKeyAttributesStatement(
139                 (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier));
140         }
141         return xpathBuilder.toString();
142     }
143
144
145     private static String getKeyAttributesStatement(
146         final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
147         final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
148             entry -> {
149                 final String name = entry.getKey().getLocalName();
150                 final String value = String.valueOf(entry.getValue()).replace("'", "\\'");
151                 return String.format("@%s='%s'", name, value);
152             }
153         ).collect(Collectors.toList());
154
155         if (keyAttributes.isEmpty()) {
156             return "";
157         } else {
158             Collections.sort(keyAttributes);
159             return "[" + String.join(" and ", keyAttributes) + "]";
160         }
161     }
162
163     private static Collection<QName> getDataSchemaNodeIdentifiersByXpath(final String parentNodeXpath,
164                                                                       final SchemaContext schemaContext) {
165         final String[] xpathNodeIdSequence = xpathToNodeIdSequence(parentNodeXpath);
166         return findDataSchemaNodeIdentifiersByXpathNodeIdSequence(xpathNodeIdSequence, schemaContext.getChildNodes(),
167                 new ArrayList<>());
168     }
169
170     private static String[] xpathToNodeIdSequence(final String xpath) {
171         try {
172             return CpsPathUtil.getXpathNodeIdSequence(xpath);
173         } catch (final PathParsingException pathParsingException) {
174             throw new DataValidationException(pathParsingException.getMessage(), pathParsingException.getDetails(),
175                     pathParsingException);
176         }
177     }
178
179     private static Collection<QName> findDataSchemaNodeIdentifiersByXpathNodeIdSequence(
180             final String[] xpathNodeIdSequence,
181             final Collection<? extends DataSchemaNode> dataSchemaNodes,
182             final Collection<QName> dataSchemaNodeIdentifiers) {
183         final String currentXpathNodeId = xpathNodeIdSequence[0];
184         final DataSchemaNode currentDataSchemaNode = dataSchemaNodes.stream()
185             .filter(dataSchemaNode -> currentXpathNodeId.equals(dataSchemaNode.getQName().getLocalName()))
186             .findFirst().orElseThrow(() -> schemaNodeNotFoundException(currentXpathNodeId));
187         dataSchemaNodeIdentifiers.add(currentDataSchemaNode.getQName());
188         if (xpathNodeIdSequence.length <= 1) {
189             return dataSchemaNodeIdentifiers;
190         }
191         if (currentDataSchemaNode instanceof DataNodeContainer) {
192             return findDataSchemaNodeIdentifiersByXpathNodeIdSequence(
193                 getNextLevelXpathNodeIdSequence(xpathNodeIdSequence),
194                     ((DataNodeContainer) currentDataSchemaNode).getChildNodes(),
195                     dataSchemaNodeIdentifiers);
196         }
197         throw schemaNodeNotFoundException(xpathNodeIdSequence[1]);
198     }
199
200     private static String[] getNextLevelXpathNodeIdSequence(final String[] xpathNodeIdSequence) {
201         final String[] nextXpathNodeIdSequence = new String[xpathNodeIdSequence.length - 1];
202         System.arraycopy(xpathNodeIdSequence, 1, nextXpathNodeIdSequence, 0, nextXpathNodeIdSequence.length);
203         return nextXpathNodeIdSequence;
204     }
205
206     private static DataValidationException schemaNodeNotFoundException(final String schemaNodeIdentifier) {
207         return new DataValidationException("Invalid xpath.",
208             String.format("No schema node was found for xpath identifier '%s'.", schemaNodeIdentifier));
209     }
210 }