Fixing CompositeState mapping
[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  *  Modifications Copyright (C) 2021 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
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.impl.operations;
23
24 import java.util.LinkedHashMap;
25 import java.util.Map;
26 import lombok.AllArgsConstructor;
27 import org.onap.cps.api.CpsDataService;
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle;
29 import org.onap.cps.ncmp.api.inventory.CompositeState;
30 import org.onap.cps.ncmp.api.inventory.CompositeStateBuilder;
31 import org.onap.cps.ncmp.api.models.NcmpServiceCmHandle;
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.springframework.stereotype.Component;
36
37 /**
38  * Retrieves YangModelCmHandles & properties.
39  */
40 @Component
41 @AllArgsConstructor
42 public class YangModelCmHandleRetriever {
43
44     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
45     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
46
47     private CpsDataService cpsDataService;
48
49     /**
50      * This method retrieves DMI service name and DMI properties for a given cm handle.
51      * @param cmHandleId the id of the cm handle
52      * @return yang model cm handle
53      */
54     public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
55         CpsValidator.validateNameCharacters(cmHandleId);
56         final DataNode cmHandleDataNode = getCmHandleDataNode(cmHandleId);
57         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
58         ncmpServiceCmHandle.setCmHandleId(cmHandleId);
59         populateCmHandleDetails(cmHandleDataNode, ncmpServiceCmHandle);
60         return YangModelCmHandle.toYangModelCmHandle(
61             (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
62             (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
63             (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
64             ncmpServiceCmHandle
65         );
66     }
67
68     private DataNode getCmHandleDataNode(final String cmHandle) {
69         final String xpathForDmiRegistryToFetchCmHandle = "/dmi-registry/cm-handles[@id='" + cmHandle + "']";
70         return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
71             NCMP_DMI_REGISTRY_ANCHOR,
72             xpathForDmiRegistryToFetchCmHandle,
73             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
74     }
75
76     private static void populateCmHandleDetails(final DataNode cmHandleDataNode,
77                                                    final NcmpServiceCmHandle ncmpServiceCmHandle) {
78         final Map<String, String> dmiProperties = new LinkedHashMap<>();
79         final Map<String, String> publicProperties = new LinkedHashMap<>();
80         final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
81         CompositeState compositeState = compositeStateBuilder.build();
82         for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
83             if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
84                 addProperty(childDataNode, dmiProperties);
85             } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
86                 addProperty(childDataNode, publicProperties);
87             } else if (childDataNode.getXpath().endsWith("/state")) {
88                 compositeState = compositeStateBuilder.fromDataNode(childDataNode).build();
89             }
90         }
91         ncmpServiceCmHandle.setDmiProperties(dmiProperties);
92         ncmpServiceCmHandle.setPublicProperties(publicProperties);
93         ncmpServiceCmHandle.setCompositeState(compositeState);
94     }
95
96     private static void addProperty(final DataNode propertyDataNode, final Map<String, String> propertiesAsMap) {
97         propertiesAsMap.put(String.valueOf(propertyDataNode.getLeaves().get("name")),
98             String.valueOf(propertyDataNode.getLeaves().get("value")));
99     }
100
101 }