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.HashMap;
33 import lombok.RequiredArgsConstructor;
34 import lombok.extern.slf4j.Slf4j;
35 import org.onap.cps.api.CpsDataService;
36 import org.onap.cps.api.CpsModuleService;
37 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
38 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
39 import org.onap.cps.spi.CpsAdminPersistenceService;
40 import org.onap.cps.spi.CpsDataPersistenceService;
41 import org.onap.cps.spi.FetchDescendantsOption;
42 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
43 import org.onap.cps.spi.model.Anchor;
44 import org.onap.cps.spi.model.DataNode;
45 import org.onap.cps.spi.model.ModuleDefinition;
46 import org.onap.cps.spi.model.ModuleReference;
47 import org.onap.cps.utils.CpsValidator;
48 import org.onap.cps.utils.JsonObjectMapper;
49 import org.springframework.stereotype.Component;
52 @RequiredArgsConstructor
54 public class InventoryPersistence {
56 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
58 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
60 private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry";
62 private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
64 private final JsonObjectMapper jsonObjectMapper;
66 private final CpsDataService cpsDataService;
68 private final CpsModuleService cpsModuleService;
70 private final CpsDataPersistenceService cpsDataPersistenceService;
72 private final CpsAdminPersistenceService cpsAdminPersistenceService;
75 * Get the Cm Handle Composite State from the data node.
77 * @param cmHandleId cm handle id
78 * @return the cm handle composite state
80 public CompositeState getCmHandleState(final String cmHandleId) {
81 final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
82 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state",
83 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
84 return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
88 * Save the cm handles state.
90 * @param cmHandleId cm handle id
91 * @param compositeState composite state
93 public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
94 final String cmHandleJsonData = String.format("{\"state\":%s}",
95 jsonObjectMapper.asJsonString(compositeState));
96 cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
97 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
98 cmHandleJsonData, OffsetDateTime.now());
102 * Save all cm handles states in batch.
104 * @param cmHandleStates contains cm handle id and updated state
106 public void saveCmHandleStates(final Map<String, CompositeState> cmHandleStates) {
107 final Map<String, String> cmHandlesJsonDataMap = new HashMap<>();
108 cmHandleStates.entrySet().stream().forEach(cmHandleEntry ->
109 cmHandlesJsonDataMap.put(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleEntry.getKey()),
110 String.format("{\"state\":%s}",
111 jsonObjectMapper.asJsonString(cmHandleEntry.getValue()))));
112 cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
113 cmHandlesJsonDataMap, OffsetDateTime.now());
117 * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
118 * @param cmHandleId the id of the cm handle
119 * @return yang model cm handle
121 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
122 CpsValidator.validateNameCharacters(cmHandleId);
123 return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
127 * Method to return module definitions by cmHandleId.
129 * @param cmHandleId cm handle ID
130 * @return a collection of module definitions (moduleName, revision and yang resource content)
132 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
133 return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
137 * Method to return module references by cmHandleId.
139 * @param cmHandleId cm handle ID
140 * @return a collection of module references (moduleName and revision)
142 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
143 CpsValidator.validateNameCharacters(cmHandleId);
144 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
148 * Method to save cmHandle.
150 * @param yangModelCmHandle cmHandle represented as Yang Model
152 public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) {
153 final String cmHandleJsonData =
154 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle));
155 cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
156 cmHandleJsonData, NO_TIMESTAMP);
160 * Method to delete a list or a list element.
162 * @param listElementXpath list element xPath
164 public void deleteListOrListElement(final String listElementXpath) {
165 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
166 listElementXpath, NO_TIMESTAMP);
170 * Method to delete a schema set.
172 * @param schemaSetName schema set name
174 public void deleteSchemaSetWithCascade(final String schemaSetName) {
176 CpsValidator.validateNameCharacters(schemaSetName);
177 cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
178 CASCADE_DELETE_ALLOWED);
179 } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
180 log.warn("Schema set {} does not exist or already deleted", schemaSetName);
185 * Get data node via xpath.
190 public DataNode getDataNode(final String xpath) {
191 return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
192 xpath, INCLUDE_ALL_DESCENDANTS);
196 * Get data node of given cm handle.
198 * @param cmHandleId cmHandle ID
201 public DataNode getCmHandleDataNode(final String cmHandleId) {
202 return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId));
206 * Query anchors via module names.
208 * @param moduleNamesForQuery module names
209 * @return Collection of anchors
211 public Collection<Anchor> queryAnchors(final Collection<String> moduleNamesForQuery) {
212 return cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
216 * Method to get all anchors.
218 * @return Collection of anchors
220 public Collection<Anchor> getAnchors() {
221 return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
225 * Replaces list content by removing all existing elements and inserting the given new elements as data nodes.
227 * @param parentNodeXpath parent node xpath
228 * @param dataNodes datanodes representing the updated data
230 public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) {
231 cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
232 parentNodeXpath, dataNodes, NO_TIMESTAMP);
236 * Deletes data node for given anchor and dataspace.
238 * @param dataNodeXpath data node xpath
240 public void deleteDataNode(final String dataNodeXpath) {
241 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath,