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 io.micrometer.core.annotation.Timed;
30 import java.time.OffsetDateTime;
31 import java.util.ArrayList;
32 import java.util.Collection;
33 import java.util.HashMap;
34 import java.util.List;
36 import java.util.stream.Collectors;
37 import lombok.RequiredArgsConstructor;
38 import lombok.extern.slf4j.Slf4j;
39 import org.onap.cps.api.CpsAdminService;
40 import org.onap.cps.api.CpsDataService;
41 import org.onap.cps.api.CpsModuleService;
42 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
43 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
44 import org.onap.cps.spi.FetchDescendantsOption;
45 import org.onap.cps.spi.exceptions.DataValidationException;
46 import org.onap.cps.spi.exceptions.SchemaSetNotFoundException;
47 import org.onap.cps.spi.model.DataNode;
48 import org.onap.cps.spi.model.ModuleDefinition;
49 import org.onap.cps.spi.model.ModuleReference;
50 import org.onap.cps.spi.utils.CpsValidator;
51 import org.onap.cps.utils.JsonObjectMapper;
52 import org.springframework.stereotype.Component;
55 @RequiredArgsConstructor
57 public class InventoryPersistenceImpl implements InventoryPersistence {
59 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
61 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
63 private static final String NCMP_DMI_REGISTRY_PARENT = "/dmi-registry";
65 private static final String CM_HANDLE_XPATH_TEMPLATE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
67 private final JsonObjectMapper jsonObjectMapper;
69 private final CpsDataService cpsDataService;
71 private final CpsModuleService cpsModuleService;
73 private final CpsAdminService cpsAdminService;
75 private final CpsValidator cpsValidator;
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 cpsValidator.validateNameCharacters(cmHandleId);
83 return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
87 public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
88 final String cmHandleJsonData = String.format("{\"state\":%s}",
89 jsonObjectMapper.asJsonString(compositeState));
90 cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
91 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
92 cmHandleJsonData, OffsetDateTime.now());
96 public void saveCmHandleStateBatch(final Map<String, CompositeState> cmHandleStatePerCmHandleId) {
97 final Map<String, String> cmHandlesJsonDataMap = new HashMap<>();
98 cmHandleStatePerCmHandleId.forEach((cmHandleId, compositeState) -> cmHandlesJsonDataMap.put(
99 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId),
100 String.format("{\"state\":%s}", jsonObjectMapper.asJsonString(compositeState))));
101 cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
102 cmHandlesJsonDataMap, OffsetDateTime.now());
106 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
107 cpsValidator.validateNameCharacters(cmHandleId);
108 return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
112 public Collection<YangModelCmHandle> getYangModelCmHandles(final Collection<String> cmHandleIds) {
113 final Collection<String> validCmHandleIds = new ArrayList<>();
114 cmHandleIds.forEach(cmHandleId -> {
116 cpsValidator.validateNameCharacters(cmHandleId);
117 validCmHandleIds.add(cmHandleId);
118 } catch (final DataValidationException dataValidationException) {
119 log.error("DataValidationException in CmHandleId {} to be ignored",
120 dataValidationException.getMessage());
123 return YangDataConverter.convertDataNodesToYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds));
127 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
128 return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
132 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
133 cpsValidator.validateNameCharacters(cmHandleId);
134 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
138 public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) {
139 final String cmHandleJsonData =
140 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle));
141 cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, NCMP_DMI_REGISTRY_PARENT,
142 cmHandleJsonData, NO_TIMESTAMP);
146 public void saveCmHandleBatch(final Collection<YangModelCmHandle> yangModelCmHandles) {
147 final List<String> cmHandlesJsonData = new ArrayList<>();
148 yangModelCmHandles.forEach(yangModelCmHandle -> cmHandlesJsonData.add(
149 String.format("{\"cm-handles\":[%s]}", jsonObjectMapper.asJsonString(yangModelCmHandle))));
150 cpsDataService.saveListElementsBatch(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
151 NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP);
155 public void deleteListOrListElement(final String listElementXpath) {
156 cpsDataService.deleteListOrListElement(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
157 listElementXpath, NO_TIMESTAMP);
161 @Timed(value = "cps.ncmp.inventory.persistence.schemaset.delete",
162 description = "Time taken to delete a schemaset")
163 public void deleteSchemaSetWithCascade(final String schemaSetName) {
165 cpsValidator.validateNameCharacters(schemaSetName);
166 cpsModuleService.deleteSchemaSet(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, schemaSetName,
167 CASCADE_DELETE_ALLOWED);
168 } catch (final SchemaSetNotFoundException schemaSetNotFoundException) {
169 log.warn("Schema set {} does not exist or already deleted", schemaSetName);
174 @Timed(value = "cps.ncmp.inventory.persistence.datanode.get",
175 description = "Time taken to get a data node (from ncmp dmi registry)")
176 public DataNode getDataNode(final String xpath) {
177 return getDataNode(xpath, INCLUDE_ALL_DESCENDANTS);
181 @Timed(value = "cps.ncmp.inventory.persistence.datanode.get",
182 description = "Time taken to get a data node (from ncmp dmi registry)")
183 public DataNode getDataNode(final String xpath, final FetchDescendantsOption fetchDescendantsOption) {
184 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
185 xpath, fetchDescendantsOption);
189 public Collection<DataNode> getDataNodes(final Collection<String> xpaths) {
190 return getDataNodes(xpaths, INCLUDE_ALL_DESCENDANTS);
194 public Collection<DataNode> getDataNodes(final Collection<String> xpaths,
195 final FetchDescendantsOption fetchDescendantsOption) {
196 return cpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
197 xpaths, fetchDescendantsOption);
201 public DataNode getCmHandleDataNode(final String cmHandleId) {
202 return this.getDataNode(String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId));
206 public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds) {
207 final Collection<String> xpaths = cmHandleIds.stream().map(cmHandleId ->
208 String.format(CM_HANDLE_XPATH_TEMPLATE, cmHandleId))
209 .collect(Collectors.toList());
210 return this.getDataNodes(xpaths);
214 public Collection<String> getCmHandleIdsWithGivenModules(final Collection<String> moduleNamesForQuery) {
215 return cpsAdminService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
219 public void replaceListContent(final String parentNodeXpath, final Collection<DataNode> dataNodes) {
220 cpsDataService.replaceListContent(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
221 parentNodeXpath, dataNodes, NO_TIMESTAMP);
225 public void deleteDataNode(final String dataNodeXpath) {
226 cpsDataService.deleteDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpath, NO_TIMESTAMP);
230 public void deleteDataNodes(final Collection<String> dataNodeXpaths) {
231 cpsDataService.deleteDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, dataNodeXpaths, NO_TIMESTAMP);