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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory;
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;
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;
51 @RequiredArgsConstructor
53 public class InventoryPersistence {
55 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
57 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
59 private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry";
61 private String xpathCmHandle = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
63 private static final String ANCESTOR_CM_HANDLES = "\"]/ancestor::cm-handles";
65 private final JsonObjectMapper jsonObjectMapper;
67 private final CpsDataService cpsDataService;
69 private final CpsModuleService cpsModuleService;
71 private final CpsDataPersistenceService cpsDataPersistenceService;
73 private final CpsAdminPersistenceService cpsAdminPersistenceService;
76 * Get the Cm Handle Composite State from the data node.
78 * @param cmHandleId cm handle id
79 * @return the cm handle composite state
81 public CompositeState getCmHandleState(final String cmHandleId) {
82 final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
83 String.format(xpathCmHandle, cmHandleId) + "/state",
84 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
85 return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
89 * Save the cm handles state.
91 * @param cmHandleId cm handle id
92 * @param compositeState composite state
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(xpathCmHandle, cmHandleId),
99 cmHandleJsonData, OffsetDateTime.now());
103 * Method which returns cm handles by the cm handles state.
105 * @param cmHandleState cm handle state
106 * @return a list of cm handles
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);
116 * Method to return data nodes representing the cm handles.
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.
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);
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
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);
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
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);
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
158 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
159 CpsValidator.validateNameCharacters(cmHandleId);
160 return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
164 * Method to return module definitions by cmHandleId.
166 * @param cmHandleId cm handle ID
167 * @return a collection of module definitions (moduleName, revision and yang resource content)
169 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
170 return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
174 * Method to return module references by cmHandleId.
176 * @param cmHandleId cm handle ID
177 * @return a collection of module references (moduleName and revision)
179 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
180 CpsValidator.validateNameCharacters(cmHandleId);
181 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
185 * Method to save list elements.
187 * @param cmHandleJsonData cmHandle JSON data
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);
195 * Method to delete a list or a list element.
197 * @param listElementXpath list element xPath
199 public void deleteListOrListElement(final String listElementXpath) {
200 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
201 listElementXpath, NO_TIMESTAMP);
205 * Method to delete a schema set.
207 * @param schemaSetName schema set name
209 public void deleteSchemaSetWithCascade(final String schemaSetName) {
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);
220 * Query data nodes via cps path.
222 * @param cpsPath cps path
223 * @return List of data nodes
225 public List<DataNode> queryDataNodes(final String cpsPath) {
226 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
227 cpsPath, INCLUDE_ALL_DESCENDANTS);
231 * Get data node via xpath.
236 public DataNode getDataNode(final String xpath) {
237 return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
238 xpath, INCLUDE_ALL_DESCENDANTS);
242 * Get data node of given cm handle.
244 * @param cmHandle cm handle
247 private DataNode getCmHandleDataNode(final String cmHandle) {
248 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
249 NCMP_DMI_REGISTRY_ANCHOR,
250 String.format(xpathCmHandle, cmHandle),
251 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
255 * Query anchors via module names.
257 * @param moduleNamesForQuery module names
258 * @return Collection of anchors
260 public Collection<Anchor> queryAnchors(final Collection<String> moduleNamesForQuery) {
261 return cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
265 * Method to get all anchors.
267 * @return Collection of anchors
269 public Collection<Anchor> getAnchors() {
270 return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);