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;
26 import java.time.OffsetDateTime;
27 import java.util.Collection;
28 import java.util.List;
29 import lombok.RequiredArgsConstructor;
30 import org.onap.cps.api.CpsDataService;
31 import org.onap.cps.api.CpsModuleService;
32 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
33 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
34 import org.onap.cps.spi.CpsDataPersistenceService;
35 import org.onap.cps.spi.FetchDescendantsOption;
36 import org.onap.cps.spi.model.DataNode;
37 import org.onap.cps.spi.model.ModuleDefinition;
38 import org.onap.cps.utils.CpsValidator;
39 import org.onap.cps.utils.JsonObjectMapper;
40 import org.springframework.stereotype.Component;
42 @RequiredArgsConstructor
44 public class InventoryPersistence {
46 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
48 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
50 private String xpathCmHandle = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
52 private static final String ANCESTOR_CM_HANDLES = "\"]/ancestor::cm-handles";
54 private final JsonObjectMapper jsonObjectMapper;
56 private final CpsDataService cpsDataService;
58 private final CpsModuleService cpsModuleService;
60 private final CpsDataPersistenceService cpsDataPersistenceService;
63 * Get the Cm Handle Composite State from the data node.
65 * @param cmHandleId cm handle id
66 * @return the cm handle composite state
68 public CompositeState getCmHandleState(final String cmHandleId) {
69 final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
70 String.format(xpathCmHandle, cmHandleId) + "/state",
71 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
72 return new CompositeStateBuilder().fromDataNode(stateAsDataNode).build();
76 * Save the cm handles state.
78 * @param cmHandleId cm handle id
79 * @param compositeState composite state
81 public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
82 final String cmHandleJsonData = String.format("{\"state\":%s}",
83 jsonObjectMapper.asJsonString(compositeState));
84 cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
85 String.format(xpathCmHandle, cmHandleId),
86 cmHandleJsonData, OffsetDateTime.now());
90 * Method which returns cm handles by the cm handles state.
92 * @param cmHandleState cm handle state
93 * @return a list of cm handles
95 public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
96 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
97 NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
98 + cmHandleState + ANCESTOR_CM_HANDLES,
99 FetchDescendantsOption.OMIT_DESCENDANTS);
103 * Method to return data nodes representing the cm handles.
105 * @param cpsPath cps path for which the cmHandle is requested
106 * @param fetchDescendantsOption defines the scope of data to fetch: either single node or all the descendant nodes
107 * @return a list of data nodes representing the cm handles.
109 public List<DataNode> getCmHandleDataNodesByCpsPath(final String cpsPath,
110 final FetchDescendantsOption fetchDescendantsOption) {
111 return cpsDataPersistenceService.queryDataNodes(
112 NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cpsPath, fetchDescendantsOption);
116 * Method which returns cm handles by the cm handle id and state.
117 * @param cmHandleId cm handle id
118 * @param cmHandleState cm handle state
119 * @return a list of cm handles
121 public List<DataNode> getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) {
122 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
123 NCMP_DMI_REGISTRY_ANCHOR, "//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\""
124 + cmHandleState + ANCESTOR_CM_HANDLES,
125 FetchDescendantsOption.OMIT_DESCENDANTS);
129 * Method which returns cm handles by the operational sync state of cm handle.
130 * @param dataStoreSyncState sync state
131 * @return a list of cm handles
133 public List<DataNode> getCmHandlesByOperationalSyncState(final DataStoreSyncState dataStoreSyncState) {
134 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
135 NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores"
136 + "/operational[@sync-state=\"" + dataStoreSyncState + ANCESTOR_CM_HANDLES,
137 FetchDescendantsOption.OMIT_DESCENDANTS);
141 * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
142 * @param cmHandleId the id of the cm handle
143 * @return yang model cm handle
145 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
146 CpsValidator.validateNameCharacters(cmHandleId);
147 return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
151 * Method to return module definitions by cmHandleId.
153 * @param cmHandleId cm handle ID
154 * @return a collection of module definitions (moduleName, revision and yang resource content)
156 public Collection<ModuleDefinition> getModuleDefinitionsByCmHandleId(final String cmHandleId) {
157 return cpsModuleService.getModuleDefinitionsByAnchorName(NFP_OPERATIONAL_DATASTORE_DATASPACE_NAME, cmHandleId);
160 private DataNode getCmHandleDataNode(final String cmHandle) {
161 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
162 NCMP_DMI_REGISTRY_ANCHOR,
163 String.format(xpathCmHandle, cmHandle),
164 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);