2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021-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.impl.operations;
23 import java.util.LinkedHashMap;
25 import lombok.AllArgsConstructor;
26 import org.onap.cps.api.CpsDataService;
27 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
28 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
29 import org.onap.cps.spi.FetchDescendantsOption;
30 import org.onap.cps.spi.model.DataNode;
31 import org.onap.cps.utils.CpsValidator;
32 import org.springframework.stereotype.Component;
35 * Retrieves YangModelCmHandles & properties.
39 public class YangModelCmHandleRetriever {
41 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
42 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
44 private CpsDataService cpsDataService;
47 * This method retrieves DMI service name and DMI properties for a given cm handle.
48 * @param cmHandleId the id of the cm handle
49 * @return yang model cm handle
51 public YangModelCmHandle getDmiServiceNamesAndProperties(final String cmHandleId) {
52 CpsValidator.validateNameCharacters(cmHandleId);
53 final DataNode cmHandleDataNode = getCmHandleDataNode(cmHandleId);
54 final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
55 ncmpServiceCmHandle.setCmHandleId(cmHandleId);
56 populateCmHandleProperties(cmHandleDataNode, ncmpServiceCmHandle);
57 return YangModelCmHandle.toYangModelCmHandle(
58 String.valueOf(cmHandleDataNode.getLeaves().get("dmi-service-name")),
59 String.valueOf(cmHandleDataNode.getLeaves().get("dmi-data-service-name")),
60 String.valueOf(cmHandleDataNode.getLeaves().get("dmi-model-service-name")),
65 private DataNode getCmHandleDataNode(final String cmHandle) {
66 final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
67 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
68 NCMP_DMI_REGISTRY_ANCHOR,
69 xpathForDmiRegistryToFetchCmHandle,
70 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
73 private static void populateCmHandleProperties(final DataNode cmHandleDataNode,
74 final NcmpServiceCmHandle ncmpServiceCmHandle) {
75 final Map<String, String> dmiProperties = new LinkedHashMap<>();
76 final Map<String, String> publicProperties = new LinkedHashMap<>();
77 for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
78 if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
79 addProperty(childDataNode, dmiProperties);
80 } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
81 addProperty(childDataNode, publicProperties);
84 ncmpServiceCmHandle.setDmiProperties(dmiProperties);
85 ncmpServiceCmHandle.setPublicProperties(publicProperties);
88 private static void addProperty(final DataNode propertyDataNode, final Map<String, String> propertiesAsMap) {
89 propertiesAsMap.put(String.valueOf(propertyDataNode.getLeaves().get("name")),
90 String.valueOf(propertyDataNode.getLeaves().get("value")));