Fix sonar code smells
[cps.git] / cps-ncmp-service / src / main / java / org / onap / cps / ncmp / api / inventory / InventoryPersistence.java
1 /*
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
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.inventory;
22
23 import java.time.OffsetDateTime;
24 import java.util.LinkedHashMap;
25 import java.util.List;
26 import java.util.Map;
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;
37
38 @RequiredArgsConstructor
39 @Component
40 public class InventoryPersistence {
41
42     private static final String NCMP_DATASPACE_NAME = "NCMP-Admin";
43
44     private static final String NCMP_DMI_REGISTRY_ANCHOR = "ncmp-dmi-registry";
45
46     private static final String XPATH_TO_CM_HANDLE = "/dmi-registry/cm-handles[@id='" + "%s" + "']";
47
48     private final JsonObjectMapper jsonObjectMapper;
49
50     private final CpsDataService cpsDataService;
51
52     private final CpsDataPersistenceService cpsDataPersistenceService;
53
54     private static final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
55
56     /**
57      * Get the Cm Handle Composite State from the data node.
58      *
59      * @param cmHandleId cm handle id
60      * @return the cm handle composite state
61      */
62     public CompositeState getCmHandleState(final String cmHandleId) {
63         final DataNode stateAsDataNode = cpsDataService.getDataNode(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
64                 String.format(XPATH_TO_CM_HANDLE, cmHandleId) + "/state",
65             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
66         return compositeStateBuilder.fromDataNode(stateAsDataNode).build();
67     }
68
69     /**
70      * Save the cm handles state.
71      *
72      * @param cmHandleId    cm handle id
73      * @param compositeState composite state
74      */
75     public void saveCmHandleState(final String cmHandleId, final CompositeState compositeState) {
76         final String cmHandleJsonData = String.format("{\"state\":%s}",
77             jsonObjectMapper.asJsonString(compositeState));
78         cpsDataService.replaceNodeTree(NCMP_DATASPACE_NAME, NCMP_DMI_REGISTRY_ANCHOR,
79             String.format(XPATH_TO_CM_HANDLE, cmHandleId),
80             cmHandleJsonData, OffsetDateTime.now());
81     }
82
83     /**
84      * Method which returns cm handles by the cm handles state.
85      *
86      * @param cmHandleState cm handle state
87      * @return a list of cm handles
88      */
89     public List<DataNode> getCmHandlesByState(final CmHandleState cmHandleState) {
90         return cpsDataPersistenceService.queryDataNodes(NCMP_DATASPACE_NAME,
91             NCMP_DMI_REGISTRY_ANCHOR, "//state[@cm-handle-state=\""
92                 + cmHandleState + "\"]/ancestor::cm-handles",
93             FetchDescendantsOption.OMIT_DESCENDANTS);
94     }
95
96     /**
97      * This method retrieves DMI service name and DMI properties for a given cm handle.
98      * @param cmHandleId the id of the cm handle
99      * @return yang model cm handle
100      */
101     public YangModelCmHandle getYangModelCmHandle(final String cmHandleId) {
102         CpsValidator.validateNameCharacters(cmHandleId);
103         final DataNode cmHandleDataNode = getCmHandleDataNode(cmHandleId);
104         final NcmpServiceCmHandle ncmpServiceCmHandle = new NcmpServiceCmHandle();
105         ncmpServiceCmHandle.setCmHandleId(cmHandleId);
106         populateCmHandleDetails(cmHandleDataNode, ncmpServiceCmHandle);
107         return YangModelCmHandle.toYangModelCmHandle(
108             (String) cmHandleDataNode.getLeaves().get("dmi-service-name"),
109             (String) cmHandleDataNode.getLeaves().get("dmi-data-service-name"),
110             (String) cmHandleDataNode.getLeaves().get("dmi-model-service-name"),
111             ncmpServiceCmHandle
112         );
113     }
114
115     private DataNode getCmHandleDataNode(final String cmHandle) {
116         return cpsDataService.getDataNode(NCMP_DATASPACE_NAME,
117             NCMP_DMI_REGISTRY_ANCHOR,
118             String.format(XPATH_TO_CM_HANDLE, cmHandle),
119             FetchDescendantsOption.INCLUDE_ALL_DESCENDANTS);
120     }
121
122     private static void populateCmHandleDetails(final DataNode cmHandleDataNode,
123                                                 final NcmpServiceCmHandle ncmpServiceCmHandle) {
124         final Map<String, String> dmiProperties = new LinkedHashMap<>();
125         final Map<String, String> publicProperties = new LinkedHashMap<>();
126         final CompositeStateBuilder compositeStateBuilder = new CompositeStateBuilder();
127         CompositeState compositeState = compositeStateBuilder.build();
128         for (final DataNode childDataNode: cmHandleDataNode.getChildDataNodes()) {
129             if (childDataNode.getXpath().contains("/additional-properties[@name=")) {
130                 addProperty(childDataNode, dmiProperties);
131             } else if (childDataNode.getXpath().contains("/public-properties[@name=")) {
132                 addProperty(childDataNode, publicProperties);
133             } else if (childDataNode.getXpath().endsWith("/state")) {
134                 compositeState = compositeStateBuilder.fromDataNode(childDataNode).build();
135             }
136         }
137         ncmpServiceCmHandle.setDmiProperties(dmiProperties);
138         ncmpServiceCmHandle.setPublicProperties(publicProperties);
139         ncmpServiceCmHandle.setCompositeState(compositeState);
140     }
141
142     private static void addProperty(final DataNode propertyDataNode, final Map<String, String> propertiesAsMap) {
143         propertiesAsMap.put(String.valueOf(propertyDataNode.getLeaves().get("name")),
144             String.valueOf(propertyDataNode.getLeaves().get("value")));
145     }
146
147 }