2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022-2024 Nordix Foundation
4 * Modifications Copyright (C) 2022 Bell Canada
5 * Modifications Copyright (C) 2023 TechMahindra Ltd.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.cps.ncmp.api.impl.inventory;
25 import static org.onap.cps.spi.FetchDescendantsOption.OMIT_DESCENDANTS;
27 import com.google.common.collect.Lists;
28 import java.time.OffsetDateTime;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.List;
35 import lombok.extern.slf4j.Slf4j;
36 import org.apache.commons.lang3.StringUtils;
37 import org.onap.cps.api.CpsAnchorService;
38 import org.onap.cps.api.CpsDataService;
39 import org.onap.cps.api.CpsModuleService;
40 import org.onap.cps.ncmp.api.impl.exception.NoAlternateIdParentFoundException;
41 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
42 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
43 import org.onap.cps.spi.FetchDescendantsOption;
44 import org.onap.cps.spi.exceptions.DataNodeNotFoundException;
45 import org.onap.cps.spi.exceptions.DataValidationException;
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.spi.utils.CpsValidator;
50 import org.onap.cps.utils.JsonObjectMapper;
51 import org.springframework.stereotype.Component;
55 public class InventoryPersistenceImpl extends NcmpPersistenceImpl implements InventoryPersistence {
57 private static final int CMHANDLE_BATCH_SIZE = 100;
59 private final CpsModuleService cpsModuleService;
60 private final CpsAnchorService cpsAnchorService;
61 private final CpsValidator cpsValidator;
62 private final CmHandleQueries cmHandleQueries;
65 * initialize an inventory persistence object.
67 * @param jsonObjectMapper json mapper object
68 * @param cpsDataService cps data service instance
69 * @param cpsModuleService cps module service instance
70 * @param cpsValidator cps validation service instance
71 * @param cpsAnchorService cps anchor service instance
73 public InventoryPersistenceImpl(final JsonObjectMapper jsonObjectMapper, final CpsDataService cpsDataService,
74 final CpsModuleService cpsModuleService, final CpsValidator cpsValidator,
75 final CpsAnchorService cpsAnchorService, final CmHandleQueries cmHandleQueries) {
76 super(jsonObjectMapper, cpsDataService, cpsModuleService, cpsValidator);
77 this.cpsModuleService = cpsModuleService;
78 this.cpsAnchorService = cpsAnchorService;
79 this.cpsValidator = cpsValidator;
80 this.cmHandleQueries = cmHandleQueries;
85 public CompositeState getCmHandleState(final String cmHandleId) {
86 final DataNode stateAsDataNode = cpsDataService.getDataNodes(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
87 getXPathForCmHandleById(cmHandleId) + "/state", FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS)
89 cpsValidator.validateNameCharacters(cmHandleId);
90 return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
94 public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
95 final String cmHandleJsonData = createStateJsonData(jsonObjectMapper.asJsonString(compositeState));
96 cpsDataService.updateDataNodeAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
97 getXPathForCmHandleById(cmHandleId), cmHandleJsonData, OffsetDateTime.now());
101 public void saveCmHandleStateBatch(final Map<String, CompositeState> cmHandleStatePerCmHandleId) {
102 final Map<String, String> cmHandlesJsonDataMap = new HashMap<>();
103 cmHandleStatePerCmHandleId.forEach((cmHandleId, compositeState) -> cmHandlesJsonDataMap.put(
104 getXPathForCmHandleById(cmHandleId),
105 createStateJsonData(jsonObjectMapper.asJsonString(compositeState))));
106 cpsDataService.updateDataNodesAndDescendants(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
107 cmHandlesJsonDataMap, OffsetDateTime.now());
111 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
112 cpsValidator.validateNameCharacters(cmHandleId);
113 final DataNode dataNode = getCmHandleDataNodeByCmHandleId(cmHandleId).iterator().next();
114 return YangDataConverter.convertCmHandleToYangModel(dataNode);
118 public Collection<YangModelCmHandle> getYangModelCmHandles(final Collection<String> cmHandleIds) {
119 final Collection<String> validCmHandleIds = new ArrayList<>(cmHandleIds.size());
120 cmHandleIds.forEach(cmHandleId -> {
122 cpsValidator.validateNameCharacters(cmHandleId);
123 validCmHandleIds.add(cmHandleId);
124 } catch (final DataValidationException dataValidationException) {
125 log.error("DataValidationException in CmHandleId {} to be ignored",
126 dataValidationException.getMessage());
129 return YangDataConverter.convertDataNodesToYangModelCmHandles(getCmHandleDataNodes(validCmHandleIds));
133 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
134 return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
138 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleAndModule(final String cmHandleId,
139 final String moduleName,
140 final String moduleRevision) {
141 cpsValidator.validateNameCharacters(cmHandleId, moduleName);
142 return cpsModuleService.getModuleDefinitionsByAnchorAndModule(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME,
143 cmHandleId, moduleName, moduleRevision);
147 public Collection<ModuleReference> getYangResourcesModuleReferences(final String cmHandleId) {
148 cpsValidator.validateNameCharacters(cmHandleId);
149 return cpsModuleService.getYangResourcesModuleReferences(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
153 public void saveCmHandle(final YangModelCmHandle yangModelCmHandle) {
154 saveCmHandleBatch(Collections.singletonList(yangModelCmHandle));
158 public void saveCmHandleBatch(final List<YangModelCmHandle> yangModelCmHandles) {
159 for (final List<YangModelCmHandle> yangModelCmHandleBatch :
160 Lists.partition(yangModelCmHandles, CMHANDLE_BATCH_SIZE)) {
161 final String cmHandlesJsonData = createCmHandlesJsonData(yangModelCmHandleBatch);
162 cpsDataService.saveListElements(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
163 NCMP_DMI_REGISTRY_PARENT, cmHandlesJsonData, NO_TIMESTAMP);
168 public Collection<DataNode> getCmHandleDataNodeByCmHandleId(final String cmHandleId) {
169 return this.getDataNode(getXPathForCmHandleById(cmHandleId));
173 public DataNode getCmHandleDataNodeByAlternateId(final String alternateId) {
174 final String cpsPathForCmHandleByAlternateId = getCpsPathForCmHandleByAlternateId(alternateId);
175 final Collection<DataNode> dataNodes = cmHandleQueries
176 .queryNcmpRegistryByCpsPath(cpsPathForCmHandleByAlternateId, OMIT_DESCENDANTS);
177 if (dataNodes.isEmpty()) {
178 throw new DataNodeNotFoundException(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
179 cpsPathForCmHandleByAlternateId);
181 return dataNodes.iterator().next();
185 public DataNode getCmHandleDataNodeByLongestMatchAlternateId(final String alternateId, final String separator) {
186 String bestMatch = alternateId;
187 while (StringUtils.isNotEmpty(bestMatch)) {
189 return getCmHandleDataNodeByAlternateId(bestMatch);
190 } catch (final DataNodeNotFoundException ignored) {
191 bestMatch = getParentPath(bestMatch, separator);
194 throw new NoAlternateIdParentFoundException(alternateId);
198 public Collection<DataNode> getCmHandleDataNodes(final Collection<String> cmHandleIds) {
199 final Collection<String> xpaths = new ArrayList<>(cmHandleIds.size());
200 cmHandleIds.forEach(cmHandleId -> xpaths.add(getXPathForCmHandleById(cmHandleId)));
201 return this.getDataNodes(xpaths);
205 public Collection<String> getCmHandleIdsWithGivenModules(final Collection<String> moduleNamesForQuery) {
206 return cpsAnchorService.queryAnchorNames(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, moduleNamesForQuery);
209 private static String getXPathForCmHandleById(final String cmHandleId) {
210 return NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@id='" + cmHandleId + "']";
213 private static String getCpsPathForCmHandleByAlternateId(final String alternateId) {
214 return NCMP_DMI_REGISTRY_PARENT + "/cm-handles[@alternate-id='" + alternateId + "']";
217 private static String createStateJsonData(final String state) {
218 return "{\"state\":" + state + "}";
221 private String createCmHandlesJsonData(final List<YangModelCmHandle> yangModelCmHandles) {
222 return "{\"cm-handles\":" + jsonObjectMapper.asJsonString(yangModelCmHandles) + "}";
225 private static String getParentPath(final String path, final String separator) {
226 final int lastSeparatorIndex = path.lastIndexOf(separator);
227 return lastSeparatorIndex < 0 ? "" : path.substring(0, lastSeparatorIndex);