2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 Nordix Foundation
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.cps.ncmp.api.inventory;
23 import java.time.OffsetDateTime;
24 import java.util.LinkedHashMap;
25 import java.util.List;
27 import lombok.RequiredArgsConstructor;
28 import org.onap.cps.api.CpsDataService;
29 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
30 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
31 import org.onap.cps.spi.CpsDataPersistenceService;
32 import org.onap.cps.spi.FetchDescendantsOption;
33 import org.onap.cps.spi.model.DataNode;
34 import org.onap.cps.utils.CpsValidator;
35 import org.onap.cps.utils.JsonObjectMapper;
36 import org.springframework.stereotype.Component;
38 @RequiredArgsConstructor
40 public class InventoryPersistence {
42 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
44 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
46 private final JsonObjectMapper jsonObjectMapper;
48 private final CpsDataService cpsDataService;
50 private final CpsDataPersistenceService cpsDataPersistenceService;
52 private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
55 * Get the Cm Handle Composite State from the data node.
57 * @param cmHandleId cm handle id
58 * @return the cm handle composite state
60 public CompositeState getCmHandleState(final String cmHandleId) {
61 final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
62 "/dmi-registry/cm-handles[@id='" + cmHandleId + "']/state",
63 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
64 return compositeStateBuilder.fromDataNode(stateAsDataNode).build();
68 * Save the cm handles state.
70 * @param cmHandleId cm handle id
71 * @param compositeState composite state
73 public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
74 final String cmHandleJsonData = String.format("{\"state\":%s}",
75 jsonObjectMapper.asJsonString(compositeState));
76 cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
77 "/dmi-registry/cm-handles[@id='" + cmHandleId + "']",
78 cmHandleJsonData, OffsetDateTime.now());
82 * Method which returns cm handles by the cm handles state.
84 * @param cmHandleState cm handle state
85 * @return a list of cm handles
87 public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
88 return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
89 NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
90 + cmHandleState + "\"]/ancestor::cm-handles",
91 FetchDescendantsOption.OMIT_DESCENDANTS);
95 * This method retrieves DMI service name and DMI properties for a given cm handle.
96 * @param cmHandleId the id of the cm handle
97 * @return yang model cm handle
99 public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
100 CpsValidator.validateNameCharacters(cmHandleId);
101 final DataNode cmHandleDataNode = getCmHandleDataNode(cmHandleId);
102 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
103 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
104 populateCmHandleDetails(cmHandleDataNode, ncmpServiceCmHandle);
105 return YangModelCmHandle.toYangModelCmHandle(
106 (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
107 (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
108 (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
113 private DataNode getCmHandleDataNode(final String cmHandle) {
114 final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
115 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
116 NCMP_DMI_REGISTRY_ANCHOR,
117 xpathForDmiRegistryToFetchCmHandle,
118 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
121 private static void populateCmHandleDetails(final DataNode cmHandleDataNode,
122 final NcmpServiceCmHandle ncmpServiceCmHandle) {
123 final Map<String, String> dmiProperties = new LinkedHashMap<>();
124 final Map<String, String> publicProperties = new LinkedHashMap<>();
125 final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
126 CompositeState compositeState = compositeStateBuilder.build();
127 for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
128 if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
129 addProperty(childDataNode, dmiProperties);
130 } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
131 addProperty(childDataNode, publicProperties);
132 } else if (childDataNode.getXpath().endsWith("/state")) {
133 compositeState = compositeStateBuilder.fromDataNode(childDataNode).build();
136 ncmpServiceCmHandle.setDmiProperties(dmiProperties);
137 ncmpServiceCmHandle.setPublicProperties(publicProperties);
138 ncmpServiceCmHandle.setCompositeState(compositeState);
141 private static void addProperty(final DataNode propertyDataNode, final Map<String, String> propertiesAsMap) {
142 propertiesAsMap.put(String.valueOf(propertyDataNode.getLeaves().get("name")),
143 String.valueOf(propertyDataNode.getLeaves().get("value")));