Merge "Define Initial Data Sync Enabled Flag and state"
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / InventoryPersistence.java
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory;
23
24 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME;
25 import static org.onap.cps.ncmp.api.impl.constants.DmiRegistryConstants.NO_TIMESTAMP;
26 import static org.onap.cps.spi.CascadeDeleteAllowed.CASCADE_DELETE_ALLOWED;
27 import static org.onap.cps.spi.FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS;
28
29 import java.time.OffsetDateTime;
30 import java.util.Collection;
31 import java.util.List;
32 import lombok.RequiredArgsConstructor;
33 import lombok.extern.slf4j.Slf4j;
34 import org.onap.cps.api.CpsDataService;
35 import org.onap.cps.api.CpsModuleService;
36 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
37 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
38 import org.onap.cps.spi.CpsAdminPersistenceService;
39 import org.onap.cps.spi.CpsDataPersistenceService;
40 import org.onap.cps.spi.FetchDescendantsOption;
41 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
42 import org.onap.cps.spi.model.Anchor;
43 import org.onap.cps.spi.model.DataNode;
44 import org.onap.cps.spi.model.ModuleDefinition;
45 import org.onap.cps.spi.model.ModuleReference;
46 import org.onap.cps.utils.CpsValidator;
47 import org.onap.cps.utils.JsonObjectMapper;
48 import org.springframework.stereotype.Component;
49
50 @Slf4j
51 @RequiredArgsConstructor
52 @Component
53 public class InventoryPersistence {
54
55     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
56
57     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
58
59     private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry";
60
61     private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
62
63     private static final String ANCESTOR_CM_HANDLES = "\"]/ancestor::cm-handles";
64
65     private final JsonObjectMapper jsonObjectMapper;
66
67     private final CpsDataService cpsDataService;
68
69     private final CpsModuleService cpsModuleService;
70
71     private final CpsDataPersistenceService cpsDataPersistenceService;
72
73     private final CpsAdminPersistenceService cpsAdminPersistenceService;
74
75     /**
76      * Get the Cm Handle Composite State from the data node.
77      *
78      * @param cmHandleId cm handle id
79      * @return the cm handle composite state
80      */
81     public CompositeState getCmHandleState(final String cmHandleId) {
82         final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
83             String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state",
84             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
85         return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
86     }
87
88     /**
89      * Save the cm handles state.
90      *
91      * @param cmHandleId    cm handle id
92      * @param compositeState composite state
93      */
94     public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
95         final String cmHandleJsonData = String.format("{\"state\":%s}",
96             jsonObjectMapper.asJsonString(compositeState));
97         cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
98             String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
99             cmHandleJsonData, OffsetDateTime.now());
100     }
101
102     /**
103      * Method which returns cm handles by the cm handles state.
104      *
105      * @param cmHandleState cm handle state
106      * @return a list of cm handles
107      */
108     public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
109         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
110             NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
111                 + cmHandleState + ANCESTOR_CM_HANDLES,
112             FetchDescendantsOption.OMIT_DESCENDANTS);
113     }
114
115     /**
116      * Method to return data nodes representing the cm handles.
117      *
118      * @param cpsPath cps path for which the cmHandle is requested
119      * @param fetchDescendantsOption defines the scope of data to fetch: either single node or all the descendant nodes
120      * @return a list of data nodes representing the cm handles.
121      */
122     public List<DataNode> getCmHandleDataNodesByCpsPath(final String cpsPath,
123                                                         final FetchDescendantsOption fetchDescendantsOption) {
124         return cpsDataPersistenceService.queryDataNodes(
125             NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cpsPath, fetchDescendantsOption);
126     }
127
128     /**
129      * Method which returns cm handles by the cm handle id and state.
130      * @param cmHandleId cm handle id
131      * @param cmHandleState cm handle state
132      * @return a list of cm handles
133      */
134     public List<DataNode> getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) {
135         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
136             NCMP_DMI_REGISTRY_ANCHOR, "//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\""
137                 + cmHandleState + ANCESTOR_CM_HANDLES,
138             FetchDescendantsOption.OMIT_DESCENDANTS);
139     }
140
141     /**
142      * Method which returns cm handles by the operational sync state of cm handle.
143      * @param dataStoreSyncState sync state
144      * @return a list of cm handles
145      */
146     public List<DataNode> getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
147         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
148             NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores"
149                 + "/operational[@sync-state=\"" + dataStoreSyncState + ANCESTOR_CM_HANDLES,
150             FetchDescendantsOption.OMIT_DESCENDANTS);
151     }
152
153     /**
154      * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
155      * @param cmHandleId the id of the cm handle
156      * @return yang model cm handle
157      */
158     public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
159         CpsValidator.validateNameCharacters(cmHandleId);
160         return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
161     }
162
163     /**
164      * Method to return module definitions by cmHandleId.
165      *
166      * @param cmHandleId cm handle ID
167      * @return a collection of module definitions (moduleName, revision and yang resource content)
168      */
169     public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
170         return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
171     }
172
173     /**
174      * Method to return module references by cmHandleId.
175      *
176      * @param cmHandleId cm handle ID
177      * @return a collection of module references (moduleName and revision)
178      */
179     public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
180         CpsValidator.validateNameCharacters(cmHandleId);
181         return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
182     }
183
184     /**
185      * Method to save list elements.
186      *
187      * @param cmHandleJsonData cmHandle JSON data
188      */
189     public void saveListElements(final String cmHandleJsonData) {
190         cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
191                     cmHandleJsonData, NO_TIMESTAMP);
192     }
193
194     /**
195      * Method to delete a list or a list element.
196      *
197      * @param listElementXpath list element xPath
198      */
199     public void deleteListOrListElement(final String listElementXpath) {
200         cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
201                 listElementXpath, NO_TIMESTAMP);
202     }
203
204     /**
205      * Method to delete a schema set.
206      *
207      * @param schemaSetName schema set name
208      */
209     public void deleteSchemaSetWithCascade(final String schemaSetName) {
210         try {
211             CpsValidator.validateNameCharacters(schemaSetName);
212             cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
213                     CASCADE_DELETE_ALLOWED);
214         } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
215             log.warn("Schema set {} does not exist or already deleted", schemaSetName);
216         }
217     }
218
219     /**
220      * Query data nodes via cps path.
221      *
222      * @param cpsPath cps path
223      * @return List of data nodes
224      */
225     public List<DataNode> queryDataNodes(final String cpsPath) {
226         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
227                 cpsPath, INCLUDE_ALL_DESCENDANTS);
228     }
229
230     /**
231      * Get data node via xpath.
232      *
233      * @param xpath xpath
234      * @return data node
235      */
236     public DataNode getDataNode(final String xpath) {
237         return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
238                 xpath, INCLUDE_ALL_DESCENDANTS);
239     }
240
241     /**
242      * Get data node of given cm handle.
243      *
244      * @param cmHandleId cmHandle ID
245      * @return data node
246      */
247     public DataNode getCmHandleDataNode(final String cmHandleId) {
248         return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId));
249     }
250
251     /**
252      * Query anchors via module names.
253      *
254      * @param moduleNamesForQuery module names
255      * @return Collection of anchors
256      */
257     public Collection<Anchor> queryAnchors(final Collection<String> moduleNamesForQuery) {
258         return  cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
259     }
260
261     /**
262      * Method to get all anchors.
263      *
264      * @return Collection of anchors
265      */
266     public Collection<Anchor> getAnchors() {
267         return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
268     }
269
270     /**
271      * Replaces list content by removing all existing elements and inserting the given new elements as data nodes.
272      *
273      * @param parentNodeXpath   parent node xpath
274      * @param dataNodes         datanodes representing the updated data
275      */
276     public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) {
277         cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
278                 parentNodeXpath, dataNodes, NO_TIMESTAMP);
279     }
280
281     /**
282      * Deletes data node for given anchor and dataspace.
283      *
284      * @param dataNodeXpath data node xpath
285      */
286     public void deleteDataNode(final String dataNodeXpath) {
287         cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath,
288                 NO_TIMESTAMP);
289     }
290 }