Merge "Fix: Update OpenSSF Scorecard to RelEng reusable"
[cps.git] / cps-service / src / main / java / org / onap / cps / spi / CpsDataPersistenceService.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020-2025 OpenInfra Foundation Europe. All rights reserved.
4  *  Modifications Copyright (C) 2021 Pantheon.tech
5  *  Modifications Copyright (C) 2022 Bell Canada
6  *  Modifications Copyright (C) 2022-2023 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.spi;
25
26 import java.io.Serializable;
27 import java.util.Collection;
28 import java.util.List;
29 import java.util.Map;
30 import java.util.Set;
31 import org.onap.cps.api.model.DataNode;
32 import org.onap.cps.api.parameters.FetchDescendantsOption;
33 import org.onap.cps.api.parameters.PaginationOption;
34
35 /*
36     Data Store interface that is responsible for handling yang data.
37     Please follow guidelines in https://gerrit.nordix.org/#/c/onap/ccsdk/features/+/6698/19/cps/interface-proposal/src/main/java/cps/javadoc/spi/DataStoreService.java
38     when adding methods.
39  */
40 public interface CpsDataPersistenceService {
41
42     /**
43      * Store multiple datanodes at once.
44      * @param dataspaceName dataspace name
45      * @param anchorName    anchor name
46      * @param dataNodes     data nodes
47      */
48     void storeDataNodes(String dataspaceName, String anchorName, Collection<DataNode> dataNodes);
49
50
51     /**
52      * Add multiple children to a Fragment.
53      *
54      * @param dataspaceName dataspace name
55      * @param anchorName    anchor name
56      * @param parentXpath   parent xpath
57      * @param dataNodes     collection of dataNodes
58      */
59     void addChildDataNodes(String dataspaceName, String anchorName, String parentXpath, Collection<DataNode> dataNodes);
60
61     /**
62      * Adds list child elements to a Fragment.
63      *
64      * @param dataspaceName          dataspace name
65      * @param anchorName             anchor name
66      * @param parentNodeXpath        parent node xpath
67      * @param listElementsCollection collection of data nodes representing list elements
68      */
69     void addListElements(String dataspaceName, String anchorName, String parentNodeXpath,
70         Collection<DataNode> listElementsCollection);
71
72     /**
73      * Retrieves multiple datanodes for a single XPath for given dataspace and anchor.
74      * Multiple data nodes are returned when xPath is set to root '/', otherwise single data node
75      * is returned when a specific xpath is used (Example: /bookstore).
76      *
77      * @param dataspaceName          dataspace name
78      * @param anchorName             anchor name
79      * @param xpath                  one xpath
80      * @param fetchDescendantsOption defines the scope of data to fetch: either single node or all the descendant nodes
81      *                               (recursively) as well
82      * @return collection of data node object
83      */
84     Collection<DataNode> getDataNodes(String dataspaceName, String anchorName, String xpath,
85                                       FetchDescendantsOption fetchDescendantsOption);
86
87     /**
88      * Retrieves multiple datanodes for multiple XPaths, given a dataspace and anchor.
89      *
90      * @param dataspaceName           dataspace name
91      * @param anchorName              anchor name
92      * @param xpaths                  collection of xpaths
93      * @param fetchDescendantsOption  defines the scope of data to fetch: either single node or all the descendant nodes
94      *                                (recursively) as well
95      * @return collection of data node object
96      */
97     Collection<DataNode> getDataNodesForMultipleXpaths(String dataspaceName, String anchorName,
98                                                        Collection<String> xpaths,
99                                                        FetchDescendantsOption fetchDescendantsOption);
100
101     /**
102      * Updates data leaves for multiple data nodes.
103      *
104      * @param dataspaceName              dataspace name
105      * @param anchorName                 anchor name
106      * @param updatedLeavesPerXPath      Map of xPaths to updated leaf nodes
107      */
108     void batchUpdateDataLeaves(String dataspaceName, String anchorName,
109                                Map<String, Map<String, Serializable>> updatedLeavesPerXPath);
110
111     /**
112      * Replaces multiple existing data nodes' content including descendants in a batch operation.
113      *
114      * @param dataspaceName dataspace name
115      * @param anchorName    anchor name
116      * @param dataNodes     data nodes
117      */
118     void updateDataNodesAndDescendants(String dataspaceName, String anchorName, final Collection<DataNode> dataNodes);
119
120     /**
121      * Replaces list content by removing all existing elements and inserting the given new elements
122      * under given parent, anchor and dataspace.
123      *
124      * @param dataspaceName   dataspace name
125      * @param anchorName      anchor name
126      * @param parentNodeXpath parent node xpath
127      * @param newListElements collection of data nodes representing the new list content
128      */
129     void replaceListContent(String dataspaceName, String anchorName,
130                             String parentNodeXpath, Collection<DataNode> newListElements);
131
132     /**
133      * Deletes any dataNode, yang container or yang list or yang list element.
134      *
135      * @param dataspaceName   dataspace name
136      * @param anchorName      anchor name
137      * @param targetXpath     xpath to list or list element (include [@key=value] to delete a single list element)
138      */
139     void deleteDataNode(String dataspaceName, String anchorName, String targetXpath);
140
141     /**
142      * Deletes multiple dataNode, yang container or yang list or yang list element.
143      *
144      * @param dataspaceName   dataspace name
145      * @param anchorName      anchor name
146      * @param targetXpaths    xpaths of nodes to delete
147      */
148     void deleteDataNodes(String dataspaceName, String anchorName, Collection<String> targetXpaths);
149
150     /**
151      * Deletes all dataNodes in a given anchor.
152      *
153      * @param dataspaceName   dataspace name
154      * @param anchorName      anchor name
155      */
156     void deleteDataNodes(String dataspaceName, String anchorName);
157
158     /**
159      * Deletes all dataNodes in multiple anchors.
160      *
161      * @param dataspaceName   dataspace name
162      * @param anchorNames     anchor names
163      */
164     void deleteDataNodes(String dataspaceName, Collection<String> anchorNames);
165
166     /**
167      * Deletes a single existing list element or the whole list.
168      *
169      * @param dataspaceName   dataspace name
170      * @param anchorName      anchor name
171      * @param targetXpath     xpath to list or list element (include [@key=value] to delete a single list element)
172      */
173     void deleteListDataNode(String dataspaceName, String anchorName, String targetXpath);
174
175     /**
176      * Get a datanode by cps path.
177      *
178      * @param dataspaceName          dataspace name
179      * @param anchorName             anchor name
180      * @param cpsPath                cps path
181      * @param fetchDescendantsOption defines whether the descendants of the node(s) found by the query should be
182      *                               included in the output
183      * @return the data nodes found i.e. 0 or more data nodes
184      */
185     List<DataNode> queryDataNodes(String dataspaceName, String anchorName,
186                                   String cpsPath, FetchDescendantsOption fetchDescendantsOption);
187
188     /**
189      * Get a datanode by cps path.
190      *
191      * @param dataspaceName          dataspace name
192      * @param anchorName             anchor name
193      * @param cpsPath                cps path
194      * @param fetchDescendantsOption defines whether the descendants of the node(s) found by the query should be
195      *                               included in the output
196      * @param queryResultLimit       limits the number of returned entities (if less than 1 returns all)
197      *
198      * @return the data nodes found i.e. 0 or more data nodes
199      */
200     List<DataNode> queryDataNodes(String dataspaceName,
201                                   String anchorName,
202                                   String cpsPath, FetchDescendantsOption fetchDescendantsOption,
203                                   int queryResultLimit);
204
205     /**
206      * Get data leaf for the given dataspace and anchor by cps path.
207      *
208      * @param dataspaceName          dataspace name
209      * @param anchorName             anchor name
210      * @param cpsPath                cps path
211      * @param queryResultLimit       limits the number of returned entities (if less than 1 returns all)
212      * @param targetClass            class of the expected data type
213      * @return a collection of data objects of expected type
214      */
215     <T> Set<T> queryDataLeaf(String dataspaceName, String anchorName, String cpsPath, int queryResultLimit,
216                              Class<T> targetClass);
217
218     /**
219      * Get a datanode by dataspace name and cps path across all anchors.
220      *
221      * @param dataspaceName          dataspace name
222      * @param cpsPath                cps path
223      * @param fetchDescendantsOption defines whether the descendants of the node(s) found by the query should be
224      *                               included in the output
225      * @param paginationOption pagination option
226      * @return the data nodes found i.e. 0 or more data nodes
227      */
228     List<DataNode> queryDataNodesAcrossAnchors(String dataspaceName,
229                                   String cpsPath, FetchDescendantsOption fetchDescendantsOption,
230                                   PaginationOption paginationOption);
231
232     /**
233      * Starts a session which allows use of locks and batch interaction with the persistence service.
234      *
235      * @return Session ID string
236      */
237     String startSession();
238
239     /**
240      * Close session.
241      *
242      * @param sessionId session ID
243      */
244     void closeSession(String sessionId);
245
246     /**
247      * Lock anchor.
248      * To release locks(s), the session holding the lock(s) must be closed.
249      *
250      * @param sessionID session ID
251      * @param dataspaceName dataspace name
252      * @param anchorName anchor name
253      * @param timeoutInMilliseconds lock attempt timeout in milliseconds
254      */
255     void lockAnchor(String sessionID, String dataspaceName, String anchorName, Long timeoutInMilliseconds);
256
257     /**
258      * Query total anchors for dataspace name and cps path.
259      * @param dataspaceName datasoace name
260      * @param cpsPath cps path
261      * @return total anchors for dataspace name and cps path
262      */
263     Integer countAnchorsForDataspaceAndCpsPath(String dataspaceName, String cpsPath);
264 }