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.ArrayList;
31 import java.util.Collection;
32 import java.util.HashMap;
33 import java.util.List;
35 import lombok.RequiredArgsConstructor;
36 import lombok.extern.slf4j.Slf4j;
37 import org.onap.cps.api.CpsDataService;
38 import org.onap.cps.api.CpsModuleService;
39 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
40 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
41 import org.onap.cps.spi.CpsAdminPersistenceService;
42 import org.onap.cps.spi.CpsDataPersistenceService;
43 import org.onap.cps.spi.FetchDescendantsOption;
44 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
45 import org.onap.cps.spi.model.Anchor;
46 import org.onap.cps.spi.model.DataNode;
47 import org.onap.cps.spi.model.ModuleDefinition;
48 import org.onap.cps.spi.model.ModuleReference;
49 import org.onap.cps.utils.CpsValidator;
50 import org.onap.cps.utils.JsonObjectMapper;
51 import org.springframework.stereotype.Component;
54 @RequiredArgsConstructor
56 public class InventoryPersistence {
58 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
60 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
62 private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry";
64 private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
66 private final JsonObjectMapper jsonObjectMapper;
68 private final CpsDataService cpsDataService;
70 private final CpsModuleService cpsModuleService;
72 private final CpsDataPersistenceService cpsDataPersistenceService;
74 private final CpsAdminPersistenceService cpsAdminPersistenceService;
77 * Get the Cm Handle Composite State from the data node.
79 * @param cmHandleId cm handle id
80 * @return the cm handle composite state
82 public CompositeState getCmHandleState(final String cmHandleId) {
83 final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
84 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state",
85 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
86 return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
90 * Save the cm handles state.
92 * @param cmHandleId cm handle id
93 * @param compositeState composite state
95 public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
96 final String cmHandleJsonData = String.format("{\"state\":%s}",
97 jsonObjectMapper.asJsonString(compositeState));
98 cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
99 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
100 cmHandleJsonData, OffsetDateTime.now());
104 * Save all cm handles states in batch.
106 * @param cmHandleStatePerCmHandleId contains cm handle id and updated state
108 public void saveCmHandleStateBatch(final Map<String, CompositeState> cmHandleStatePerCmHandleId) {
109 final Map<String, String> cmHandlesJsonDataMap = new HashMap<>();
110 cmHandleStatePerCmHandleId.forEach((cmHandleId, compositeState) -> cmHandlesJsonDataMap.put(
111 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
112 String.format("{\"state\":%s}", jsonObjectMapper.asJsonString(compositeState))));
113 cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
114 cmHandlesJsonDataMap, OffsetDateTime.now());
118 * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
119 * @param cmHandleId the id of the cm handle
120 * @return yang model cm handle
122 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
123 CpsValidator.validateNameCharacters(cmHandleId);
124 return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
128 * Method to return module definitions by cmHandleId.
130 * @param cmHandleId cm handle ID
131 * @return a collection of module definitions (moduleName, revision and yang resource content)
133 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
134 return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
138 * Method to return module references by cmHandleId.
140 * @param cmHandleId cm handle ID
141 * @return a collection of module references (moduleName and revision)
143 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
144 CpsValidator.validateNameCharacters(cmHandleId);
145 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
149 * Method to save cmHandle.
151 * @param yangModelCmHandle cmHandle represented as Yang Model
153 public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) {
154 final String cmHandleJsonData =
155 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle));
156 cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
157 cmHandleJsonData, NO_TIMESTAMP);
161 * Method to save batch of cm handles.
163 * @param yangModelCmHandles cm handle represented as Yang Models
165 public void saveCmHandleBatch(final Collection<YangModelCmHandle> yangModelCmHandles) {
166 final List<String> cmHandlesJsonData = new ArrayList<>();
167 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandlesJsonData.add(
168 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle))));
169 cpsDataService.saveListElementsBatch(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
170 NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP);
174 * Method to delete a list or a list element.
176 * @param listElementXpath list element xPath
178 public void deleteListOrListElement(final String listElementXpath) {
179 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
180 listElementXpath, NO_TIMESTAMP);
184 * Method to delete a schema set.
186 * @param schemaSetName schema set name
188 public void deleteSchemaSetWithCascade(final String schemaSetName) {
190 CpsValidator.validateNameCharacters(schemaSetName);
191 cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
192 CASCADE_DELETE_ALLOWED);
193 } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
194 log.warn("Schema set {} does not exist or already deleted", schemaSetName);
199 * Get data node via xpath.
204 public DataNode getDataNode(final String xpath) {
205 return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
206 xpath, INCLUDE_ALL_DESCENDANTS);
210 * Get data node of given cm handle.
212 * @param cmHandleId cmHandle ID
215 public DataNode getCmHandleDataNode(final String cmHandleId) {
216 return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId));
220 * Query anchors via module names.
222 * @param moduleNamesForQuery module names
223 * @return Collection of anchors
225 public Collection<Anchor> queryAnchors(final Collection<String> moduleNamesForQuery) {
226 return cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
230 * Method to get all anchors.
232 * @return Collection of anchors
234 public Collection<Anchor> getAnchors() {
235 return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
239 * Replaces list content by removing all existing elements and inserting the given new elements as data nodes.
241 * @param parentNodeXpath parent node xpath
242 * @param dataNodes datanodes representing the updated data
244 public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) {
245 cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
246 parentNodeXpath, dataNodes, NO_TIMESTAMP);
250 * Deletes data node for given anchor and dataspace.
252 * @param dataNodeXpath data node xpath
254 public void deleteDataNode(final String dataNodeXpath) {
255 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath,