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 lombok.RequiredArgsConstructor;
32 import lombok.extern.slf4j.Slf4j;
33 import org.onap.cps.api.CpsDataService;
34 import org.onap.cps.api.CpsModuleService;
35 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
36 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
37 import org.onap.cps.spi.CpsAdminPersistenceService;
38 import org.onap.cps.spi.CpsDataPersistenceService;
39 import org.onap.cps.spi.FetchDescendantsOption;
40 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
41 import org.onap.cps.spi.model.Anchor;
42 import org.onap.cps.spi.model.DataNode;
43 import org.onap.cps.spi.model.ModuleDefinition;
44 import org.onap.cps.spi.model.ModuleReference;
45 import org.onap.cps.utils.CpsValidator;
46 import org.onap.cps.utils.JsonObjectMapper;
47 import org.springframework.stereotype.Component;
50 @RequiredArgsConstructor
52 public class InventoryPersistence {
54 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
56 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
58 private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry";
60 private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
62 private final JsonObjectMapper jsonObjectMapper;
64 private final CpsDataService cpsDataService;
66 private final CpsModuleService cpsModuleService;
68 private final CpsDataPersistenceService cpsDataPersistenceService;
70 private final CpsAdminPersistenceService cpsAdminPersistenceService;
73 * Get the Cm Handle Composite State from the data node.
75 * @param cmHandleId cm handle id
76 * @return the cm handle composite state
78 public CompositeState getCmHandleState(final String cmHandleId) {
79 final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
80 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId) + "/state",
81 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
82 return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
86 * Save the cm handles state.
88 * @param cmHandleId cm handle id
89 * @param compositeState composite state
91 public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
92 final String cmHandleJsonData = String.format("{\"state\":%s}",
93 jsonObjectMapper.asJsonString(compositeState));
94 cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
95 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
96 cmHandleJsonData, OffsetDateTime.now());
100 * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
101 * @param cmHandleId the id of the cm handle
102 * @return yang model cm handle
104 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
105 CpsValidator.validateNameCharacters(cmHandleId);
106 return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
110 * Method to return module definitions by cmHandleId.
112 * @param cmHandleId cm handle ID
113 * @return a collection of module definitions (moduleName, revision and yang resource content)
115 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
116 return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
120 * Method to return module references by cmHandleId.
122 * @param cmHandleId cm handle ID
123 * @return a collection of module references (moduleName and revision)
125 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
126 CpsValidator.validateNameCharacters(cmHandleId);
127 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
131 * Method to save cmHandle.
133 * @param yangModelCmHandle cmHandle represented as Yang Model
135 public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) {
136 final String cmHandleJsonData =
137 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle));
138 cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
139 cmHandleJsonData, NO_TIMESTAMP);
143 * Method to delete a list or a list element.
145 * @param listElementXpath list element xPath
147 public void deleteListOrListElement(final String listElementXpath) {
148 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
149 listElementXpath, NO_TIMESTAMP);
153 * Method to delete a schema set.
155 * @param schemaSetName schema set name
157 public void deleteSchemaSetWithCascade(final String schemaSetName) {
159 CpsValidator.validateNameCharacters(schemaSetName);
160 cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
161 CASCADE_DELETE_ALLOWED);
162 } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
163 log.warn("Schema set {} does not exist or already deleted", schemaSetName);
168 * Get data node via xpath.
173 public DataNode getDataNode(final String xpath) {
174 return cpsDataPersistenceService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
175 xpath, INCLUDE_ALL_DESCENDANTS);
179 * Get data node of given cm handle.
181 * @param cmHandleId cmHandle ID
184 public DataNode getCmHandleDataNode(final String cmHandleId) {
185 return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId));
189 * Query anchors via module names.
191 * @param moduleNamesForQuery module names
192 * @return Collection of anchors
194 public Collection<Anchor> queryAnchors(final Collection<String> moduleNamesForQuery) {
195 return cpsAdminPersistenceService.queryAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
199 * Method to get all anchors.
201 * @return Collection of anchors
203 public Collection<Anchor> getAnchors() {
204 return cpsAdminPersistenceService.getAnchors(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME);
208 * Replaces list content by removing all existing elements and inserting the given new elements as data nodes.
210 * @param parentNodeXpath parent node xpath
211 * @param dataNodes datanodes representing the updated data
213 public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) {
214 cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
215 parentNodeXpath, dataNodes, NO_TIMESTAMP);
219 * Deletes data node for given anchor and dataspace.
221 * @param dataNodeXpath data node xpath
223 public void deleteDataNode(final String dataNodeXpath) {
224 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath,