CPS-341 Support for multiple data tree instances under 1 anchor
[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.Arrays;
32 import java.util.Collection;
33 import java.util.Collections;
34 import java.util.List;
35 import java.util.Optional;
36 import java.util.stream.Collectors;
37 import lombok.AccessLevel;
38 import lombok.NoArgsConstructor;
39 import lombok.extern.slf4j.Slf4j;
40 import org.onap.cps.spi.exceptions.DataValidationException;
41 import org.opendaylight.yangtools.yang.common.QName;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
44 import org.opendaylight.yangtools.yang.data.api.schema.builder.DataContainerNodeBuilder;
45 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
46 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactory;
47 import org.opendaylight.yangtools.yang.data.codec.gson.JSONCodecFactorySupplier;
48 import org.opendaylight.yangtools.yang.data.codec.gson.JsonParserStream;
49 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
50 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
51 import org.opendaylight.yangtools.yang.model.api.DataNodeContainer;
52 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
53 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
54 import org.opendaylight.yangtools.yang.model.api.EffectiveStatementInference;
55 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
56 import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier;
57 import org.opendaylight.yangtools.yang.model.util.SchemaInferenceStack;
58
59 @Slf4j
60 @NoArgsConstructor(access = AccessLevel.PRIVATE)
61 public class YangUtils {
62
63     private static final String XPATH_DELIMITER_REGEX = "\\/";
64     private static final String XPATH_NODE_KEY_ATTRIBUTES_REGEX = "\\[.*?\\]";
65
66     /**
67      * Parses jsonData into Collection of NormalizedNode according to given schema context.
68      *
69      * @param jsonData      json data as string
70      * @param schemaContext schema context describing associated data model
71      * @return the Collection of NormalizedNode object
72      */
73     public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext) {
74         return parseJsonData(jsonData, schemaContext, Optional.empty());
75     }
76
77     /**
78      * Parses jsonData into Collection of NormalizedNode according to given schema context.
79      *
80      * @param jsonData        json data fragment as string
81      * @param schemaContext   schema context describing associated data model
82      * @param parentNodeXpath the xpath referencing the parent node current data fragment belong to
83      * @return the NormalizedNode object
84      */
85     public static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext,
86         final String parentNodeXpath) {
87         final Collection<QName> dataSchemaNodeIdentifiers =
88                 getDataSchemaNodeIdentifiersByXpath(parentNodeXpath, schemaContext);
89         return parseJsonData(jsonData, schemaContext, Optional.of(dataSchemaNodeIdentifiers));
90     }
91
92     private static ContainerNode parseJsonData(final String jsonData, final SchemaContext schemaContext,
93         final Optional<Collection<QName>> dataSchemaNodeIdentifiers) {
94         final JSONCodecFactory jsonCodecFactory = JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02
95             .getShared((EffectiveModelContext) schemaContext);
96         final DataContainerNodeBuilder<YangInstanceIdentifier.NodeIdentifier, ContainerNode> dataContainerNodeBuilder =
97                 Builders.containerBuilder()
98                         .withNodeIdentifier(new YangInstanceIdentifier.NodeIdentifier(schemaContext.getQName()));
99         final NormalizedNodeStreamWriter normalizedNodeStreamWriter = ImmutableNormalizedNodeStreamWriter
100                 .from(dataContainerNodeBuilder);
101         final JsonReader jsonReader = new JsonReader(new StringReader(jsonData));
102         final JsonParserStream jsonParserStream;
103
104         if (dataSchemaNodeIdentifiers.isPresent()) {
105             final EffectiveModelContext effectiveModelContext = ((EffectiveModelContext) schemaContext);
106             final EffectiveStatementInference effectiveStatementInference =
107                     SchemaInferenceStack.of(effectiveModelContext,
108                     SchemaNodeIdentifier.Absolute.of(dataSchemaNodeIdentifiers.get())).toInference();
109             jsonParserStream =
110                     JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory, effectiveStatementInference);
111         } else {
112             jsonParserStream = JsonParserStream.create(normalizedNodeStreamWriter, jsonCodecFactory);
113         }
114
115         try {
116             jsonParserStream.parse(jsonReader);
117             jsonParserStream.close();
118         } catch (final JsonSyntaxException exception) {
119             throw new DataValidationException(
120                 "Failed to parse json data: " + jsonData, exception.getMessage(), exception);
121         } catch (final IOException | IllegalStateException illegalStateException) {
122             throw new DataValidationException(
123                 "Failed to parse json data. Unsupported xpath or json data:" + jsonData, illegalStateException
124                 .getMessage(), illegalStateException);
125         }
126         return dataContainerNodeBuilder.build();
127     }
128
129     /**
130      * Create an xpath form a Yang Tools NodeIdentifier (i.e. PathArgument).
131      *
132      * @param nodeIdentifier the NodeIdentifier
133      * @return an xpath
134      */
135     public static String buildXpath(final YangInstanceIdentifier.PathArgument nodeIdentifier) {
136         final StringBuilder xpathBuilder = new StringBuilder();
137         xpathBuilder.append("/").append(nodeIdentifier.getNodeType().getLocalName());
138
139         if (nodeIdentifier instanceof YangInstanceIdentifier.NodeIdentifierWithPredicates) {
140             xpathBuilder.append(getKeyAttributesStatement(
141                 (YangInstanceIdentifier.NodeIdentifierWithPredicates) nodeIdentifier));
142         }
143         return xpathBuilder.toString();
144     }
145
146
147     private static String getKeyAttributesStatement(
148         final YangInstanceIdentifier.NodeIdentifierWithPredicates nodeIdentifier) {
149         final List<String> keyAttributes = nodeIdentifier.entrySet().stream().map(
150             entry -> {
151                 final String name = entry.getKey().getLocalName();
152                 final String value = String.valueOf(entry.getValue()).replace("'", "\\'");
153                 return String.format("@%s='%s'", name, value);
154             }
155         ).collect(Collectors.toList());
156
157         if (keyAttributes.isEmpty()) {
158             return "";
159         } else {
160             Collections.sort(keyAttributes);
161             return "[" + String.join(" and ", keyAttributes) + "]";
162         }
163     }
164
165     private static Collection<QName> getDataSchemaNodeIdentifiersByXpath(final String parentNodeXpath,
166                                                                       final SchemaContext schemaContext) {
167         final String[] xpathNodeIdSequence = xpathToNodeIdSequence(parentNodeXpath);
168         return findDataSchemaNodeIdentifiersByXpathNodeIdSequence(xpathNodeIdSequence, schemaContext.getChildNodes(),
169                 new ArrayList<>());
170     }
171
172     private static String[] xpathToNodeIdSequence(final String xpath) {
173         final String[] xpathNodeIdSequence = Arrays.stream(xpath
174                         .replaceAll(XPATH_NODE_KEY_ATTRIBUTES_REGEX, "")
175                         .split(XPATH_DELIMITER_REGEX))
176                 .filter(identifier -> !identifier.isEmpty())
177                 .toArray(String[]::new);
178         if (xpathNodeIdSequence.length < 1) {
179             throw new DataValidationException("Invalid xpath.", "Xpath contains no node identifiers.");
180         }
181         return xpathNodeIdSequence;
182     }
183
184     private static Collection<QName> findDataSchemaNodeIdentifiersByXpathNodeIdSequence(
185             final String[] xpathNodeIdSequence,
186             final Collection<? extends DataSchemaNode> dataSchemaNodes,
187             final Collection<QName> dataSchemaNodeIdentifiers) {
188         final String currentXpathNodeId = xpathNodeIdSequence[0];
189         final DataSchemaNode currentDataSchemaNode = dataSchemaNodes.stream()
190             .filter(dataSchemaNode -> currentXpathNodeId.equals(dataSchemaNode.getQName().getLocalName()))
191             .findFirst().orElseThrow(() -> schemaNodeNotFoundException(currentXpathNodeId));
192         dataSchemaNodeIdentifiers.add(currentDataSchemaNode.getQName());
193         if (xpathNodeIdSequence.length <= 1) {
194             return dataSchemaNodeIdentifiers;
195         }
196         if (currentDataSchemaNode instanceof DataNodeContainer) {
197             return findDataSchemaNodeIdentifiersByXpathNodeIdSequence(
198                 getNextLevelXpathNodeIdSequence(xpathNodeIdSequence),
199                     ((DataNodeContainer) currentDataSchemaNode).getChildNodes(),
200                     dataSchemaNodeIdentifiers);
201         }
202         throw schemaNodeNotFoundException(xpathNodeIdSequence[1]);
203     }
204
205     private static String[] getNextLevelXpathNodeIdSequence(final String[] xpathNodeIdSequence) {
206         final String[] nextXpathNodeIdSequence = new String[xpathNodeIdSequence.length - 1];
207         System.arraycopy(xpathNodeIdSequence, 1, nextXpathNodeIdSequence, 0, nextXpathNodeIdSequence.length);
208         return nextXpathNodeIdSequence;
209     }
210
211     private static DataValidationException schemaNodeNotFoundException(final String schemaNodeIdentifier) {
212         return new DataValidationException("Invalid xpath.",
213             String.format("No schema node was found for xpath identifier '%s'.", schemaNodeIdentifier));
214     }
215 }