2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 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.Collections;
24 import java.util.LinkedHashMap;
26 import org.onap.cps.api.CpsDataService;
27 import org.onap.cps.ncmp.api.models.CmHandle;
28 import org.onap.cps.ncmp.api.models.PersistenceCmHandle;
29 import org.onap.cps.spi.FetchDescendantsOption;
30 import org.onap.cps.spi.model.DataNode;
31 import org.springframework.stereotype.Component;
34 * Retrieves PersistenceCmHandles & properties.
37 public class PersistenceCmHandleRetriever {
39 private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
40 private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
42 private CpsDataService cpsDataService;
45 * Constructor for PersistenceCmHandleRetriever.
47 * @param cpsDataService the cps data service.
49 public PersistenceCmHandleRetriever(final CpsDataService cpsDataService) {
50 this.cpsDataService = cpsDataService;
54 * This method retieves dmi service name and properties for a given cm handle.
55 * @param cmHandleId the id of the cm handle
56 * @return persistence cm handle
58 public PersistenceCmHandle retrieveCmHandleDmiServiceNameAndProperties(final String cmHandleId) {
59 final DataNode cmHandleDataNode = getCmHandleDataNode(cmHandleId);
60 final CmHandle cmHandle = new CmHandle(cmHandleId, getCmHandleProperties(cmHandleDataNode));
61 return PersistenceCmHandle.toPersistenceCmHandle(
62 String.valueOf(cmHandleDataNode.getLeaves().get("dmi-service-name")),
63 String.valueOf(cmHandleDataNode.getLeaves().get("dmi-data-service-name")),
64 String.valueOf(cmHandleDataNode.getLeaves().get("dmi-model-service-name")),
69 private DataNode getCmHandleDataNode(final String cmHandle) {
70 final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
71 return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
72 NCMP_DMI_REGISTRY_ANCHOR,
73 xpathForDmiRegistryToFetchCmHandle,
74 FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
77 private static Map<String, String> getCmHandleProperties(final DataNode cmHandleDataNode) {
78 if (cmHandleDataNode.getChildDataNodes().isEmpty()) {
79 return Collections.emptyMap();
81 final Map<String, String> cmHandlePropertiesAsMap = new LinkedHashMap<>();
82 for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
83 cmHandlePropertiesAsMap.put(String.valueOf(childDataNode.getLeaves().get("name")),
84 String.valueOf(childDataNode.getLeaves().get("value")));
86 return cmHandlePropertiesAsMap;