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 cm handles from the cps path.
98 * @param cpsPath cps path for which the cmHandle is requested
99 * @return a list of cm handles
101 public List<DataNode> getCmHandlesByCpsPath(final String cpsPath) {
102 return cpsDataPersistenceService.queryDataNodes(
103 NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR, cpsPath, FetchDescendantsOption.OMIT_DESCENDANTS);
107 * This method retrieves DMI service name, DMI properties and the state for a given cm handle.
108 * @param cmHandleId the id of the cm handle
109 * @return yang model cm handle
111 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
112 CpsValidator.validateNameCharacters(cmHandleId);
113 return YangDataConverter.convertCmHandleToYangModel(getCmHandleDataNode(cmHandleId), cmHandleId);
116 private DataNode getCmHandleDataNode(final String cmHandle) {
117 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
118 NCMP_DMI_REGISTRY_ANCHOR,
119 String.format(XPATH_TO_CM_HANDLE, cmHandle),
120 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);