Create Endpoint For Get Cm Handles By Name
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / impl / operations / YangModelCmHandleRetriever.java
1 /*
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
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.ncmp.api.impl.operations;
22
23 import java.util.LinkedHashMap;
24 import java.util.Map;
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.springframework.stereotype.Component;
32
33 /**
34  * Retrieves YangModelCmHandles & properties.
35  */
36 @Component
37 @AllArgsConstructor
38 public class YangModelCmHandleRetriever {
39
40     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
41     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
42
43     private CpsDataService cpsDataService;
44
45     /**
46      * This method retrieves DMI service name and DMI properties for a given cm handle.
47      * @param cmHandleId the id of the cm handle
48      * @return yang model cm handle
49      */
50     public YangModelCmHandle getDmiServiceNamesAndProperties(final String cmHandleId) {
51         final DataNode cmHandleDataNode = getCmHandleDataNode(cmHandleId);
52         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
53         ncmpServiceCmHandle.setCmHandleID(cmHandleId);
54         populateCmHandleProperties(cmHandleDataNode, ncmpServiceCmHandle);
55         return YangModelCmHandle.toYangModelCmHandle(
56             String.valueOf(cmHandleDataNode.getLeaves().get("dmi-service-name")),
57             String.valueOf(cmHandleDataNode.getLeaves().get("dmi-data-service-name")),
58             String.valueOf(cmHandleDataNode.getLeaves().get("dmi-model-service-name")),
59             ncmpServiceCmHandle
60         );
61     }
62
63     private DataNode getCmHandleDataNode(final String cmHandle) {
64         final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
65         return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
66             NCMP_DMI_REGISTRY_ANCHOR,
67             xpathForDmiRegistryToFetchCmHandle,
68             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
69     }
70
71     private static void populateCmHandleProperties(final DataNode cmHandleDataNode,
72                                                    final NcmpServiceCmHandle ncmpServiceCmHandle) {
73         final Map<String, String> dmiProperties = new LinkedHashMap<>();
74         final Map<String, String> publicProperties = new LinkedHashMap<>();
75         for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
76             if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
77                 addProperty(childDataNode, dmiProperties);
78             } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
79                 addProperty(childDataNode, publicProperties);
80             }
81         }
82         ncmpServiceCmHandle.setDmiProperties(dmiProperties);
83         ncmpServiceCmHandle.setPublicProperties(publicProperties);
84     }
85
86     private static void addProperty(final DataNode propertyDataNode, final Map<String, String> propertiesAsMap) {
87         propertiesAsMap.put(String.valueOf(propertyDataNode.getLeaves().get("name")),
88             String.valueOf(propertyDataNode.getLeaves().get("value")));
89     }
90
91 }