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 java.time.OffsetDateTime;
25 import java.util.List;
26 import lombok.RequiredArgsConstructor;
27 import org.onap.cps.api.CpsDataService;
28 import org.onap.cps.ncmp.api.impl.utils.YangDataConverter;
29 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
30 import org.onap.cps.spi.CpsDataPersistenceService;
31 import org.onap.cps.spi.FetchDescendantsOption;
32 import org.onap.cps.spi.model.DataNode;
33 import org.onap.cps.utils.CpsValidator;
34 import org.onap.cps.utils.JsonObjectMapper;
35 import org.springframework.stereotype.Component;
37 @RequiredArgsConstructor
39 public class InventoryPersistence {
41 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
43 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
45 private static final String XPATH_TO_CM_HANDLE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
47 private final JsonObjectMapper jsonObjectMapper;
49 private final CpsDataService cpsDataService;
51 private final CpsDataPersistenceService cpsDataPersistenceService;
53 private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
56 * Get the Cm Handle Composite State from the data node.
58 * @param cmHandleId cm handle id
59 * @return the cm handle composite state
61 public CompositeState getCmHandleState(final String cmHandleId) {
62 final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
63 String.format(XPATH_TO_CM_HANDLE, cmHandleId) + "/state",
64 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
65 return compositeStateBuilder.fromDataNode(stateAsDataNode).build();
69 * Save the cm handles state.
71 * @param cmHandleId cm handle id
72 * @param compositeState composite state
74 public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
75 final String cmHandleJsonData = String.format("{\"state\":%s}",
76 jsonObjectMapper.asJsonString(compositeState));
77 cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
78 String.format(XPATH_TO_CM_HANDLE, cmHandleId),
79 cmHandleJsonData, OffsetDateTime.now());
83 * Method which returns cm handles by the cm handles state.
85 * @param cmHandleState cm handle state
86 * @return a list of cm handles
88 public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
89 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
90 NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
91 + cmHandleState + "\"]/ancestor::cm-handles",
92 FetchDescendantsOption.OMIT_DESCENDANTS);
96 * Method to return data nodes representing the cm handles.
98 * @param cpsPath cps path for which the cmHandle is requested
99 * @return a list of data nodes representing the cm handles.
101 public List<DataNode> getCmHandleDataNodesByCpsPath(final String cpsPath) {
102 return cpsDataPersistenceService.queryDataNodes(
103 NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS);
107 * Method which returns cm handles by the cm handle id and state.
108 * @param cmHandleId cm handle id
109 * @param cmHandleState cm handle state
110 * @return a list of cm handles
112 public List<DataNode> getCmHandlesByIdAndState(final String cmHandleId, final CmHandleState cmHandleState) {
113 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
114 NCMP_DMI_REGISTRY_ANCHOR, "//cm-handles[@id='" + cmHandleId + "']/state[@cm-handle-state=\""
115 + cmHandleState + "\"]/ancestor::cm-handles",
116 FetchDescendantsOption.OMIT_DESCENDANTS);
120 * Method which returns cm handles by the operational sync state of cm handle.
121 * @param syncState sync state
122 * @return a list of cm handles
124 public List<DataNode> getCmHandlesByOperationalSyncState(final SyncState syncState) {
125 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
126 NCMP_DMI_REGISTRY_ANCHOR, "//state/datastores"
127 + "/operational[@sync-state=\"" + syncState + "\"]/ancestor::cm-handles",
128 FetchDescendantsOption.OMIT_DESCENDANTS);
132 * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
133 * @param cmHandleId the id of the cm handle
134 * @return yang model cm handle
136 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
137 CpsValidator.validateNameCharacters(cmHandleId);
138 return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
141 private DataNode getCmHandleDataNode(final String cmHandle) {
142 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
143 NCMP_DMI_REGISTRY_ANCHOR,
144 String.format(XPATH_TO_CM_HANDLE, cmHandle),
145 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);